Class: Sped2DB::DbTools

Inherits:
Object
  • Object
show all
Defined in:
lib/sped2db/db_tools.rb

Constant Summary collapse

DEFAULT_DATABASES =
{
  ado: 'master',
  mysql2: 'mysql',
  postgres: 'template1'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(db, layout) ⇒ DbTools

Returns a new instance of DbTools.



11
12
13
14
# File 'lib/sped2db/db_tools.rb', line 11

def initialize(db, layout)
  @db = db
  @layout = layout
end

Instance Method Details

#create_databaseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sped2db/db_tools.rb', line 22

def create_database
  db_opts = @db.opts.dup
  adapter = db_opts[:adapter].to_sym

  return unless DEFAULT_DATABASES.keys.include? adapter

  db_name = db_opts[:database]
  db_opts[:database] = DEFAULT_DATABASES[adapter]

  Sequel.connect(db_opts) do |db|
    db.run "create database #{db_name}"
  end
end

#create_tablesObject



36
37
38
39
40
41
# File 'lib/sped2db/db_tools.rb', line 36

def create_tables
  directory = "../../../migrations/#{@layout.type}/v#{@layout.version}"
  Sequel::Migrator.run @db, File.expand_path(directory, __FILE__)

  # @db.drop_table? :schema_info

end

#exists?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/sped2db/db_tools.rb', line 16

def exists?
  @db.test_connection
rescue Sequel::DatabaseConnectionError
  false
end