Class: Dumbo::DbTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/dumbo/db_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'db') ⇒ DbTask

Returns a new instance of DbTask.



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
42
43
44
45
46
47
48
49
50
# File 'lib/dumbo/db_task.rb', line 9

def initialize(name = 'db')
  @name = name

  namespace name do
    task environment: ['db:configure_connection']

    task :configuration do
      @config = YAML.load_file('config/database.yml')[ENV['DUMBO_ENV']]
    end

    task configure_connection: :configuration do
      ActiveRecord::Base.establish_connection @config
      ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
    end

    desc 'Create the database from config/database.yml for the current ENV'
    task create: :environment do
      create_database @config
    end

    desc 'Drops the database for the current ENV'
    task drop: :environment do
      ActiveRecord::Base.establish_connection @config.merge('database' => nil)
      ActiveRecord::Base.connection.drop_database @config['database']
    end

    namespace :test do
      task :environment do
        ENV['DUMBO_ENV'] = 'test'
        ActiveRecord::Schema.verbose = false
      end

      task load_structure: :environment do
        filename = ENV['DB_STRUCTURE'] || File.join('db', 'structure.sql')
        ActiveRecord::Tasks::DatabaseTasks.structure_load(@config, filename)
      end

      desc 'Re-create and prepare test database'
      task prepare: [:environment, :drop, :create]
    end
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/dumbo/db_task.rb', line 8

def name
  @name
end

Instance Method Details

#create_database(config) ⇒ Object



52
53
54
55
56
# File 'lib/dumbo/db_task.rb', line 52

def create_database(config)
  ActiveRecord::Base.establish_connection config.merge('database' => nil)
  ActiveRecord::Base.connection.create_database config['database'], config
  ActiveRecord::Base.establish_connection config
end