Module: Buhos::SchemaCreation

Defined in:
lib/buhos/create_schema.rb

Overview

Module to create a blank, but usable schema for Buhos Used a on installer and to create clean schemas for testing

Example:

db=Sequel.connect('sqlite::memory:', :encoding => 'utf8',:reconnect=>false,:keep_reference=>false)
Buhos::SchemaCreation.create_db_from_scratch(db)

db should have a correct and usable version of Buhos.

Constant Summary collapse

VIEWS_BN =
["sr_###_references_between_cd_rtr_n",
          "sr_###_resolutions_full_text",
          "sr_###_resolutions_references",
          "sr_###_resolutions_sta",
          "sr_###_references_between_cd_n",
          "sr_###_references_between_cd",
          "sr_###_bib_references",
          "sr_###_cd_id",
          "sr_###_record_id_table",
          "sr_###_reference_id_table",
          "sr_###_all_id_table"
]
BIBLIOGRAPHICAL_DATABASES =
["scopus", "sage", "wos", "scielo", "ebscohost",
"refworks", "ieee", "generic", "pubmed", "lilacs",
"proquest", "bvs" ]

Insert bootstrap data collapse

Class Method Summary collapse

Class Method Details

.allocate_authorizations_to_roles(db) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/buhos/create_schema.rb', line 436

def self.allocate_authorizations_to_roles(db)
  analyst_permits = ['review_view', 'review_analyze', 'message_view', 'message_edit', 'search_view', 'search_edit',
                     'record_view', 'record_edit', 'reference_view', 'reference_edit', 'file_view',
                     'canonical_document_view', 'group_view']
  analyst_permits.each do |auth|
    db[:authorizations_roles].replace(:authorization_id => auth, :role_id => 'analyst') if db[:authorizations_roles].where(:authorization_id => auth, :role_id => 'analyst').count==0
   end

  guest_permits = ['review_view', 'message_view', 'message_edit', 'search_view', 'record_view',
                   'reference_view', 'file_view', 'canonical_document_view', 'group_view']
  guest_permits.each do |auth|
    db[:authorizations_roles].replace(:authorization_id => auth, :role_id => 'guest') if db[:authorizations_roles].where(:authorization_id => auth, :role_id => 'guest').count==0
  end
end

.allocate_users_to_groups(db, id_admin, id_analyst, id_guest) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/buhos/create_schema.rb', line 496

def self.allocate_users_to_groups(db, id_admin, id_analyst, id_guest)
  if db[:groups][id:1]
    group_id = 1
  else
    group_id = db[:groups].insert(:id=>1,:group_administrator => id_admin, :description => "First group, just for demostration", :name => "demo group")
  end
  insert_group_user_if_not_exists(db,group_id, id_admin)
  insert_group_user_if_not_exists(db,group_id, id_analyst)
  if db[:groups][id:2]
    group_guest = 2
  else
    group_guest = db[:groups].insert(:id=>2,:group_administrator => id_admin, :description => "Guest group", :name => "guest group")
  end

  insert_group_user_if_not_exists(db,group_guest, id_admin)
  insert_group_user_if_not_exists(db,group_guest, id_guest)

end

.create_authorizations(db) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/buhos/create_schema.rb', line 451

def self.create_authorizations(db)
  authorizations = [
      'canonical_document_admin',
      'canonical_document_view',
      'crossref_query',
      'file_admin',
      'file_view',
      'group_admin',
      'group_view',
      'message_edit',
      'message_view',
      'pubmed_query',
      'record_edit',
      'record_view',
      'reference_edit',
      'reference_view',
      'reflection',
      'review_admin',
      'review_admin_view',
      'semantic_scholar_query',
      'review_analyze',
      'review_edit',
      'review_view',
      'role_admin',
      'role_view',
      'scale_admin',
      'institution_admin',
      'scopus_query',
      'search_edit',
      'search_view',
      'tag_edit',
      'user_admin'
  ]
  authorizations.each do |auth|
    db[:authorizations].insert(:id => auth) unless db[:authorizations][id:auth]
    #puts(db[:authorizations_roles].where(:authorization_id => auth, :role_id => 'administrator').count)
    db[:authorizations_roles].insert(:authorization_id => auth, :role_id => 'administrator') unless db[:authorizations_roles].where(:authorization_id => auth, :role_id => 'administrator').count>0
  end
end

.create_bootstrap_data(db, language = 'en') ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/buhos/create_schema.rb', line 345

def self.create_bootstrap_data(db,language='en')
  db.transaction do
    create_roles(db)
    id_admin, id_analyst, id_guest = create_users(db, language)
    create_authorizations(db)
    allocate_authorizations_to_roles(db)
    allocate_users_to_groups(db, id_admin, id_analyst, id_guest)
    insert_bib_db_data(db)
    insert_taxonomies_data(db)
    insert_basic_scales(db)
  end
end

.create_db_from_scratch(db_url, language = 'en', logger = nil) ⇒ Object

Create a usable db to work on Buhos

Parameters:

  • db_url

    a [String] with a connection that Sequel.connect understand, or a Sequel::Database

  • language (defaults to: 'en')

    used to assign default language for users

  • logger (defaults to: nil)

    A Logger object could be attached to database.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/buhos/create_schema.rb', line 64

def self.create_db_from_scratch(db_url, language='en',logger=nil)
  require 'sequel'
  if db_url.is_a? Sequel::Database
    db=db_url
  else
    db=Sequel.connect(db_url, :encoding => 'utf8',:reconnect=>true)
  end
  db.logger=logger if logger
  Buhos::SchemaCreation.create_schema(db)
  Sequel.extension :migration
  Sequel::Migrator.run(db, "db/migrations")
  Buhos::SchemaCreation.create_bootstrap_data(db,language)
  Buhos::SchemaCreation.delete_views(db,language)
  db
end

.create_roles(db) ⇒ Object



387
388
389
390
391
392
# File 'lib/buhos/create_schema.rb', line 387

def self.create_roles(db)

  db[:roles].insert(:id => 'administrator', :description => 'App administrator') unless db[:roles][id:'administrator']
  db[:roles].insert(:id => 'analyst', :description => 'App analyst') unless db[:roles][id:'analyst']
  db[:roles].insert(:id => 'guest', :description => 'Guest') unless db[:roles][id:'guest']
end

.create_schema(db) ⇒ Object

Create basis schema to run Buhos. Updates to database are stored on /db/migrations, so don’t forget later to run migrations.

See Also:

  • {self{self.create_db_from_scratch}


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/buhos/create_schema.rb', line 83

def self.create_schema(db)
  # Sequel try to create an already created table even in table exists

  begin
    db.create_table? :roles do
      String :id, :size => 50, :primary_key => true
      String :description
    end

    db.create_table? :users do
      primary_key :id
      String :login, :size => 255, :null => false
      String :name, :size => 255
      String :password, :size => 255, :null => false
      foreign_key :role_id, :roles, :type => String, :size => 50, :null => false, :key => [:id]
      TrueClass :active, :default=>true, :null=>false
    end


    db.create_table? :groups do
      primary_key :id
      foreign_key :group_administrator, :users, :null => false, :key => [:id]
      String :description, :text => true
      String :name, :size => 255, :null => false
    end

    db.create_join_table?(:user_id => :users, :group_id=> :groups)


    db.create_table? :systematic_reviews do
      primary_key :id
      String :name, :size => 255
      Date :date_creation
      String :description, :text => true
      String :objectives, :text => true
      Integer :year_start
      Integer :year_end
      String :keywords, :text =>true
      foreign_key :group_id, :groups, :null => false, :key => [:id]
      foreign_key :sr_administrator, :users, :key => [:id]
      TrueClass :active, :default => true, :null => false
      String :stage, :default => "search", :size => 32, :null => false

      index [:sr_administrator], :name => :sr_administrator_index
      index [:group_id], :name => :group_id_i
    end

    # Systematic review taxonomies

    db.create_table? :sr_taxonomies do
      primary_key :id
      String :name # Should be included on locale later
      String :description, :text=>true # Should be included on locale later
    end

    db.create_table? :sr_taxonomy_categories do
      primary_key :id
      foreign_key :srt_id, :sr_taxonomies, :null => false, :key => [:id]
      String :name, :size=>50 # Should be included on locale later
      String :description # Should be included on locale later
      index [:name]
    end


    db.create_table? :systematic_review_srtcs do
      foreign_key :srtc_id, :sr_taxonomy_categories, :null => false, :key => [:id]
      foreign_key :sr_id, :systematic_reviews, :null => false, :key => [:id]
      primary_key [:srtc_id, :sr_id]
    end



    db.create_table? :bibliographic_databases do
      primary_key :id
      String :name, :size => 255
      String :description, :text => true
    end


    db.create_table? :canonical_documents do
      primary_key :id
      String :type, :size => 255
      String :title, :text => true
      String :author, :text => true
      String :date, :size => 255
      String :journal, :text => true
      String :volume, :size => 255
      String :number, :size => 255
      String :pages, :size => 255
      String :book_name, :text => true
      String :editors, :text => true
      String :proceedings, :text => true
      String :place, :size => 255
      String :editorial, :size => 255
      String :doi, :size => 255
      String :pubmed, :size => 255
      Integer :year, :null => false
      String :journal_abbr, :size => 255
      longtext :abstract
      Integer :duplicated
      String :url, :text => true
      String :wos_id, :size => 32
      String :scopus_id, :size => 255
      String :ebscohost_id, :size => 255
      String :scielo_id, :size => 255
      String :refworks_id, :size => 255
    end

    db.create_table? :canonical_authors do
      primary_key :id
      String :surname, :size => 255
      String :family_name, :size => 255
      String :email, :size => 255
      String :scopus_id, :size => 255
      String :wos_id, :size => 255
    end

    db.create_table? :canonical_document_authors do
      foreign_key :canonical_document_id, :canonical_documents, :null => false, :key => [:id]
      foreign_key :canonical_author_id, :canonical_authors, :null => false, :key => [:id]
      String :affiliation, :size => 255
      String :email, :size => 255
      index [:canonical_author_id], :name => :canonical_author_id_index
      primary_key [:canonical_document_id, :canonical_author_id]
    end

    db.create_table? :searches do
      primary_key :id
      foreign_key :systematic_review_id, :systematic_reviews, :null => false, :key => [:id]
      foreign_key :bibliographic_database_id, :bibliographic_databases, :null => false, :key => [:id]
      Date :date_creation
      String :search_criteria, :text => true
      String :description, :text => true
      File   :file_body, size: :long
      String :filetype, :size => 50
      String :filename, :size => 128

      index [:bibliographic_database_id], :name => :bibliographic_database_id_index
      index [:date_creation]
      index [:systematic_review_id]
    end

    db.create_table? :records do
      primary_key :id
      foreign_key :bibliographic_database_id , :bibliographic_databases, :null => false, :key => [:id]
      String :uid, :text => true
      String :type, :size => 255
      String :title, :text => true
      String :author, :text => true
      String :date, :size => 255
      String :journal, :text => true
      String :volume, :size => 255
      String :number, :size => 255
      String :pages, :size => 255
      String :book_name, :text => true
      String :editors, :text => true
      String :proceedings, :text => true
      String :place, :size => 255
      String :publisher, :size => 255
      String :doi, :size => 255
      String :pmid, :size => 255
      String :arxiv_id, :size => 255
      foreign_key :canonical_document_id, :canonical_documents, :key => [:id]
      String :journal_abbr, :size => 128
      Integer :year
      longtext :abstract
      String :url, :text => true

      index [:bibliographic_database_id]
      index [:canonical_document_id]
    end

    db.create_table? :bib_references do
      String :id, :primary_key => true
      String :text, :text => true
      String :doi, :size => 255
      foreign_key :canonical_document_id, :canonical_documents, :key => [:id]
      index [:canonical_document_id]
    end

    db.create_join_table?(:search_id => :searches, :record_id => :records)


    db.create_join_table?({:reference_id => {:table => :bib_references, :type => String}, :record_id => :records}, {:name=>:records_references})

    db.create_table? :authorizations do
      String :id, :size => 50, :primary_key => true
      String :description, :size => 255
    end


    db.create_table? :configurations do
      String :id, :primary_key => true
      String :value , :text => true
    end

    db.create_table? :authorizations_roles do
      foreign_key :authorization_id, :authorizations, :type => String, :size => 50, :null => false, :key => [:id]
      foreign_key :role_id, :roles, :type => String, :size => 50, :null => false, :key => [:id]

      primary_key [:authorization_id, :role_id]

      index [:role_id]
    end


    db.create_table? :crossref_queries do
      String :id, :size => 100, :primary_key => true
      String :query, :text => true
      longtext  :json
    end


    db.create_table? :crossref_dois do
      String :doi, :size => 100, :primary_key => true
      String :bibtex, :text => true
      longtext :json
    end

    db.create_table? :scopus_abstracts do
      String :id, :primary_key => true
      longtext :xml
      String :doi, :size => 255
    end

    db.create_table? :decisions do
      foreign_key :systematic_review_id, :systematic_reviews, :null => false, :key => [:id]
      foreign_key :canonical_document_id, :canonical_documents, :null => false, :key => [:id]
      foreign_key :user_id, :users, :null => false, :key => [:id]
      String :stage, :size => 32, :null => false
      String :decision, :size => 255
      String :commentary, :text => true

      primary_key [:systematic_review_id, :user_id, :canonical_document_id, :stage]

      index [:canonical_document_id]
      index [:systematic_review_id]
      index [:systematic_review_id, :user_id, :stage]
      index [:user_id]
    end
    db.create_table? :resolutions do
      foreign_key :systematic_review_id, :systematic_reviews, :null => false, :key => [:id]
      foreign_key :canonical_document_id, :canonical_documents, :null => false, :key => [:id]
      foreign_key :user_id, :users, :null => false, :key => [:id]
      String :stage, :size => 32, :null => false
      String :resolution, :size => 255
      String :commentary, :text => true

      primary_key [:systematic_review_id, :canonical_document_id, :stage]

      index [:canonical_document_id]
      index [:systematic_review_id]
      index [:systematic_review_id, :canonical_document_id]
    end
  end

end

.create_users(db, language) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/buhos/create_schema.rb', line 375

def self.create_users(db, language)
  id_admin = (db, 'admin')
  id_admin ||= db[:users].insert(:login => 'admin', :name => 'Administrator', :password => ::Digest::SHA1.hexdigest('admin'), :role_id => 'administrator', :active => 1, :language => language)

  id_analyst = (db, 'analyst')
  id_analyst ||= db[:users].insert(:login => 'analyst', :name => 'Analyst', :password => ::Digest::SHA1.hexdigest('analyst'), :role_id => 'analyst', :active => 1, :language => language)

  id_guest = (db, 'guest')
  id_guest ||= db[:users].insert(:login => 'guest', :name => 'Guest', :password => ::Digest::SHA1.hexdigest('guest'), :role_id => 'guest', :active => 1, :language => language)
  return id_admin, id_analyst, id_guest
end

.delete_views(db, language = 'en') ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/buhos/create_schema.rb', line 358

def self.delete_views(db,language='en')
#require 'logger'
#db.loggers << Logger.new($stdout)
  db[:systematic_reviews].each do |rev|
    rev_id=rev[:id]
      VIEWS_BN.each do  |view_n|
        table_name=view_n.gsub("###", rev_id.to_s)
        if db.table_exists?(table_name.to_sym)
          db.transaction do
            db.drop_view(table_name.to_sym)
          end
        end
      end
  end
end

.get_id_user_by_login(db, login) ⇒ Object



340
341
342
343
# File 'lib/buhos/create_schema.rb', line 340

def self.(db,)
  user=db[:users][:login=>]
  user ? user[:id] :nil
end

.insert_basic_scales(db) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/buhos/create_schema.rb', line 421

def self.insert_basic_scales(db)
  scales = {
      1=>{-99=>'not_applicable', -98=>'cant_say', 0=>'No', 1=>'Yes'},
      2=>{-99=>'not_applicable', -98=>'cant_say', 0=>'No', 1=>'Partial', 2=>'Complete'}
  }

    db[:scales].insert(:id=>1, :name=>::I18n::t("scales.dichomotic"), :description=>::I18n::t('scales.dichotomic_description')) unless db[:scales][id:1]
    db[:scales].insert(:id=>2, :name=>::I18n::t("scales.three_values"), :description=>::I18n::t('scales.three_values_description'))  unless db[:scales][id:2]

  scales.each_pair do |scale_id, values|
    values.each_pair do |value, name|
      db[:scales_items].insert(scale_id:scale_id, value:value, name: ::I18n::t("scales.#{name}")) if db[:scales_items].where(scale_id:scale_id, value:value).count==0
    end
  end
end

.insert_bib_db_data(db) ⇒ Object

Insert data for bibliographic databases



415
416
417
418
419
420
# File 'lib/buhos/create_schema.rb', line 415

def self.insert_bib_db_data(db)
  BIBLIOGRAPHICAL_DATABASES.each do |bib_db|
    bib_db_o=db[:bibliographic_databases][:name=>bib_db]
    db[:bibliographic_databases].insert(:name => bib_db) unless bib_db_o
  end
end

.insert_group_user_if_not_exists(db, group_id, user_id) ⇒ Object



491
492
493
494
495
# File 'lib/buhos/create_schema.rb', line 491

def self.insert_group_user_if_not_exists(db, group_id, user_id)
  if db[:groups_users].where(:group_id => group_id, :user_id => user_id).count==0
    db[:groups_users].insert(:group_id => group_id, :user_id => user_id)
  end
end

.insert_taxonomies_data(db) ⇒ Object

Insert data for taxonomies



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/buhos/create_schema.rb', line 395

def self.insert_taxonomies_data(db)
  taxonomies = {
      "focus" => ["practice_or_application", "theory", "research_methods", "research_results"],
      "objectives" => ["critical", "main_themes", "integration"],
      "perspective" => ["neutral", "adoption_of_posture"],
      "coverage" => ["exhaustive", "exhaustive_with_selection", "representative", "essential"],
      "organization" => ["methodology", "conceptual", "historical"],
      "receiver" => ["academics_specialist", "academics_general", "practicians_politics", "general_public"]

  }

  taxonomies.each_pair do |category, values|
    f_id = db[:sr_taxonomies][:name=>category] && db[:sr_taxonomies][:name=>category][:id]
    f_id = db[:sr_taxonomies].insert(:name => category) unless f_id
    values.each do |name|
      db[:sr_taxonomy_categories].insert(:srt_id => f_id, :name => name) if  db[:sr_taxonomy_categories].where(:srt_id => f_id, :name => name).empty?
    end
  end
end