Class: Combustion::Database

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

Class Method Summary collapse

Class Method Details

.load_schemaObject



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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/combustion/database.rb', line 56

def self.migrate
  migrator = ActiveRecord::Migrator
  engine_path = Rails.application.root.sub(::Combustion.path, '')
  engine_migration_paths = Rails.application.paths['db/migrate'].to_a

  if engine_migration_paths.include?(engine_path.join('db/migrate').to_s)
    paths = []
  else
    paths = base_migration_paths
  end

  paths += engine_migration_paths
  paths.uniq!

  # Append the migrations inside the internal app's db/migrate directory
  paths << File.join(Rails.root, 'db/migrate')

  if ActiveRecord::VERSION::STRING >= '3.1.0'
    migrator.migrate paths, nil
  else
    paths.each { |path| migrator.migrate path, nil }
  end
end

.reset_databaseObject



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

def self.reset_database
  ActiveRecord::Base.configurations = YAML.load(ERB.new(File.read("#{Rails.root}/config/database.yml")).result)
  abcs = ActiveRecord::Base.configurations
  case abcs['test']['adapter']
  when /mysql/
    drop_database(abcs['test']['database'])
    create_database(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
# File 'lib/combustion/database.rb', line 3

def self.setup
  reset_database
  load_schema
  migrate
end