Class: Rake::Tasks Private

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_ar/rake/tasks.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Utility that defines Rake tasks of MultiAR

Defined Under Namespace

Modules: DSL

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.common_migrationsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
# File 'lib/multi_ar/rake/tasks.rb', line 45

def common_migrations
  @common_migrations
end

.databasesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
# File 'lib/multi_ar/rake/tasks.rb', line 43

def databases
  @databases
end

.environmentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
# File 'lib/multi_ar/rake/tasks.rb', line 44

def environment
  @environment
end

Class Method Details

.defineObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

When called, this declares Rake tasks of MultiAR, most notably custom active record migration tasks.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/multi_ar/rake/tasks.rb', line 50

def self.define
  load "active_record/railties/databases.rake"

  # dunno if this is any use
    #task environment: 'db:load_config' do
    #  ActiveRecord::Base.establish_connection ActiveRecord::Tasks::DatabaseTasks.current_config
    #end

  DSL.namespace :db do

    DSL.desc "Creates a new migration file with the specified name"
    DSL.task :new_migration, :name, :options do |t, args|
      name = args[:name] || ENV["name"]
      options = args[:options] || ENV["options"]

      raise "You need to specify exactly one database for migration generation. See --databases. Given databases: #{databases.inspect}" if databases.size != 1

      unless name
        generator = Rails::Generators.find_by_namespace "migration"
        desc = generator.desc.gsub(/`rails (?:g|generate) migration (\w+)`/, '`rake "db:new_migration[\\1]"`' ).
          gsub(/`rails (?:g|generate) migration (\w+) (.*)`/, '`rake "db:new_migration[\\1, \\2]"`' )
        puts [
          %Q{Usage: rake "#{t.name}[AddFieldToForm[, field[:type][:index]] field[:type][:index]]"},
          desc,
        ].join "\n\n"
        abort
      end
      params = [name]
      params.concat options.split(' ') if options
      Rails::Generators.invoke "active_record_migrations:migration", params,
        behavior: :invoke, destination_root: Rails.root
    end
  end

  multiple_databases_task "migrate", "db" do |database_name|
    establish_connection database_name

    MultiAR::MultiAR.migration_dirs.each do |dir|
      path = "#{dir}/#{database_name}/"
      # The database should be present only on one migration dir, so this will fail if there is more than one migration dir
      ActiveRecord::Migrator.migrate path if Dir.exist? path
    end
  end

  multiple_databases_task "create", "db" do |database_name|
    establish_connection database_name
    ActiveRecord::Tasks::DatabaseTasks.create_current(connection_name(database_name))
  end

  multiple_databases_task "rollback", "db" do |database_name|
    establish_connection database_name
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1
    MultiAR::MultiAR.migration_dirs.each do |dir|
      path = "#{dir}/#{database_name}/"
      # The database should be present only on one migration dir, so this will fail if there is more than one migration dir
      ActiveRecord::Migrator.rollback(path, step) if Dir.exist? path
    end
  end

  multiple_databases_task "drop", "db" do |database_name|
    establish_connection database_name
    ActiveRecord::Tasks::DatabaseTasks.drop_current(connection_name(database_name))
  end
end