Module: Artbase::Database

Defined in:
lib/artbase-importers/database.rb

Defined Under Namespace

Classes: Metadata

Class Method Summary collapse

Class Method Details

.auto_migrate!(spec) ⇒ Object



57
58
59
60
61
62
# File 'lib/artbase-importers/database.rb', line 57

def self.auto_migrate!( spec )
  # first time? - auto-run db migratation, that is, create db tables
  unless Metadata.table_exists?
    create( spec )
  end
end

.connect(database = './artbase.db') ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/artbase-importers/database.rb', line 34

def self.connect( database='./artbase.db' )

      config =   { adapter:  'sqlite3',
                   database: database,
                    }

    puts "Connecting to db using settings: "
    pp config
    ActiveRecord::Base.establish_connection( config )
    # ActiveRecord::Base.logger = Logger.new( STDOUT )

    ## if sqlite3 add (use) some pragmas for speedups
      ## note: if in memory database e.g. ':memory:' no pragma needed!!
      ## try to speed up sqlite
      ##   see http://www.sqlite.org/pragma.html
      con = ActiveRecord::Base.connection
      con.execute( 'PRAGMA synchronous=OFF;' )
      con.execute( 'PRAGMA journal_mode=OFF;' )
      con.execute( 'PRAGMA temp_store=MEMORY;' )

end

.create(spec) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/artbase-importers/database.rb', line 14

def self.create( spec )
  ActiveRecord::Schema.define do
    create_table :metadata do |t|

      spec.each do |column|
         column_name = column[0].to_sym
         column_opts = column[1] || {}
         column_type = (column[2] || 'string').to_sym

         t.__send__( column_type, column_name, **column_opts )
      end

      t.string  :image, null: false

      t.timestamps
    end
  end  # Schema.define
end