Class: Combustion::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/combustion/database.rb

Class Method Summary collapse

Class Method Details

.load_schemaObject



44
45
46
# File 'lib/combustion/database.rb', line 44

def self.load_schema
  load "#{Rails.root}/db/schema.rb"
end

.migrateObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/combustion/database.rb', line 48

def self.migrate
  migrator = ActiveRecord::Migrator
  paths    = 'db/migrate/'

  if migrator.respond_to?(:migrations_paths)
    paths    = migrator.migrations_paths
  end

  migrator.migrate paths, nil
end

.reset_databaseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/combustion/database.rb', line 10

def self.reset_database
  abcs = ActiveRecord::Base.configurations
  case abcs['test']['adapter']
  when /mysql/
    ActiveRecord::Base.establish_connection(:test)
    ActiveRecord::Base.connection.recreate_database(abcs['test']['database'],
      mysql_creation_options(abcs['test']))
    ActiveRecord::Base.establish_connection(:test)
  when /postgresql/
    ActiveRecord::Base.clear_active_connections!
    drop_database(abcs['test'])
    create_database(abcs['test'])
  when /sqlite/
    dbfile = abcs['test']['database'] || abcs['test']['dbfile']
    File.delete(dbfile) if File.exist?(dbfile)
  when 'sqlserver'
    test = abcs.deep_dup['test']
    test_database = test['database']
    test['database'] = 'master'
    ActiveRecord::Base.establish_connection(test)
    ActiveRecord::Base.connection.recreate_database!(test_database)
  when "oci", "oracle"
    ActiveRecord::Base.establish_connection(:test)
    ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
      ActiveRecord::Base.connection.execute(ddl)
    end
  when 'firebird'
    ActiveRecord::Base.establish_connection(:test)
    ActiveRecord::Base.connection.recreate_database!
  else
    raise "Cannot reset databases for '#{abcs['test']['adapter']}'"
  end
end

.setupObject



2
3
4
5
6
7
8
# File 'lib/combustion/database.rb', line 2

def self.setup
  silence_stream(STDOUT) do
    reset_database
    load_schema
    migrate
  end
end