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

.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.



54
55
56
# File 'lib/multi_ar/rake/tasks.rb', line 54

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.



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/multi_ar/rake/tasks.rb', line 59

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|
      # Only migration generator requires Rails generators
      require "rails"
      require "rails/generators"
      require_relative "migration_generator"

      name = args[:name] || ENV["name"]
      options = args[:options] || ENV["options"]

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

      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

    migration_path = MultiAR::MultiAR::solve_migration_path database_name
    context = ActiveRecord::MigrationContext.new(migration_path, ::ActiveRecord::Base.connection.schema_migration)
    context.migrate

    #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

    context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
    context.rollback step
  end

  multiple_databases_task "forward", "db" do |database_name|
    establish_connection database_name
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1

    context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
    context.forward step
  end

  multiple_databases_task "down", "db:migrate" do |database_name|
    establish_connection database_name
    raise "db:down is used to go back to certain version. Use db:rollback if you want to go back n migrations." unless ENV["VERSION"]
    version = ENV["VERSION"]

    context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
    context.down version
  end

  multiple_databases_task "up", "db:migrate" do |database_name|
    establish_connection database_name
    raise "db:up is used to go to certain version. Use db:forward if you want to go up n migrations." unless ENV["VERSION"]
    version = ENV["VERSION"]

    context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
    context.up version
  end

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

    unless ActiveRecord::SchemaMigration.table_exists?
      abort "Schema migrations table does not exist yet."
    end

    #raise "db:up is used to go to certain version. Use db:forward if you want to go up n migrations." unless ENV["VERSION"]
    #version = ENV["VERSION"]

    #context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
    #context.up version

    # output
    puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
    puts "#{'Status'.center(8)}  #{'Migration ID'.ljust(14)}  Migration Name"
    puts "-" * 50
    ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
      puts "#{status.center(8)}  #{version.ljust(14)}  #{name}"
    end
    puts

  end

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