Class: Combustion::Database

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

Class Method Summary collapse

Class Method Details

.load_schemaObject



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

def self.load_schema
  case Combustion.schema_format
  when :ruby
    load Rails.root.join('db', 'schema.rb')
  when :sql
    ActiveRecord::Base.connection.execute(
      File.read(Rails.root.join('db', 'structure.sql'))
    )
  else
    raise "Unknown schema format: #{Combustion.schema_format}"
  end
end

.migrateObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/combustion/database.rb', line 58

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

  if migrator.respond_to?(:migrations_paths)
    paths = migrator.migrations_paths
  end
  # Append the migrations inside the internal app's db/migrate directory
  paths << File.join(Rails.root, 'db/migrate')
  migrator.migrate paths, nil
end

.reset_databaseObject



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
43
# File 'lib/combustion/database.rb', line 11

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/
    drop_database(abcs['test'])
    create_database(abcs['test'])
  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



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

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