Skip to content
This repository
Browse code

Rake tasks for managing database while development and testing of eng…

…ines

Some of the engines needs database. As engine needs to be run inside
Rails application, migrations and other database operations are run
from dummy application. To make that process simple I changed db
tasks to look for migrations in both engine's and application's
db/migrate directory. You can run all of the database tasks from
test/dummy or directly from engine with prefix app, like:

  rake app:db:migrate
  rake app:db:migrate:redo
  • Loading branch information...
commit 5df72a238e9fcb18daf6ab6e6dc9051c9106d7bb 1 parent 2dc3342
Piotr Sarnacki authored December 04, 2010
28  activerecord/lib/active_record/railties/databases.rake
... ...
@@ -1,4 +1,4 @@
1  
-namespace :db do
  1
+db_namespace = namespace :db do
2 2
   task :load_config => :rails_env do
3 3
     require 'active_record'
4 4
     ActiveRecord::Base.configurations = Rails.application.config.database_configuration
@@ -141,18 +141,18 @@ namespace :db do
141 141
   task :migrate => [:environment, :load_config] do
142 142
     ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
143 143
     ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
144  
-    Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
  144
+    db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
145 145
   end
146 146
 
147 147
   namespace :migrate do
148 148
     # desc  'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
149 149
     task :redo => [:environment, :load_config] do
150 150
       if ENV["VERSION"]
151  
-        Rake::Task["db:migrate:down"].invoke
152  
-        Rake::Task["db:migrate:up"].invoke
  151
+        db_namespace["migrate:down"].invoke
  152
+        db_namespace["migrate:up"].invoke
153 153
       else
154  
-        Rake::Task["db:rollback"].invoke
155  
-        Rake::Task["db:migrate"].invoke
  154
+        db_namespace["rollback"].invoke
  155
+        db_namespace["migrate"].invoke
156 156
       end
157 157
     end
158 158
 
@@ -164,7 +164,7 @@ namespace :db do
164 164
       version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
165 165
       raise "VERSION is required" unless version
166 166
       ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_paths, version)
167  
-      Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
  167
+      db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
168 168
     end
169 169
 
170 170
     # desc 'Runs the "down" for a given migration VERSION.'
@@ -172,7 +172,7 @@ namespace :db do
172 172
       version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
173 173
       raise "VERSION is required" unless version
174 174
       ActiveRecord::Migrator.run(:down, ActiveRecord::Migrator.migrations_paths, version)
175  
-      Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
  175
+      db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
176 176
     end
177 177
 
178 178
     desc "Display status of migrations"
@@ -210,14 +210,14 @@ namespace :db do
210 210
   task :rollback => [:environment, :load_config] do
211 211
     step = ENV['STEP'] ? ENV['STEP'].to_i : 1
212 212
     ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_paths, step)
213  
-    Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
  213
+    db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
214 214
   end
215 215
 
216 216
   # desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
217 217
   task :forward => [:environment, :load_config] do
218 218
     step = ENV['STEP'] ? ENV['STEP'].to_i : 1
219 219
     ActiveRecord::Migrator.forward(ActiveRecord::Migrator.migrations_paths, step)
220  
-    Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
  220
+    db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
221 221
   end
222 222
 
223 223
   # desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
@@ -321,12 +321,12 @@ namespace :db do
321 321
 
322 322
   namespace :schema do
323 323
     desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
324  
-    task :dump => :environment do
  324
+    task :dump => :load_config do
325 325
       require 'active_record/schema_dumper'
326 326
       File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
327 327
         ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
328 328
       end
329  
-      Rake::Task["db:schema:dump"].reenable
  329
+      db_namespace["schema:dump"].reenable
330 330
     end
331 331
 
332 332
     desc "Load a schema.rb file into the database"
@@ -383,7 +383,7 @@ namespace :db do
383 383
     task :load => 'db:test:purge' do
384 384
       ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
385 385
       ActiveRecord::Schema.verbose = false
386  
-      Rake::Task["db:schema:load"].invoke
  386
+      db_namespace["schema:load"].invoke
387 387
     end
388 388
 
389 389
     # desc "Recreate the test database from the current environment's database schema"
@@ -457,7 +457,7 @@ namespace :db do
457 457
     # desc 'Check for pending migrations and load the test schema'
458 458
     task :prepare => 'db:abort_if_pending_migrations' do
459 459
       if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
460  
-        Rake::Task[{ :sql  => "db:test:clone_structure", :ruby => "db:test:load" }[ActiveRecord::Base.schema_format]].invoke
  460
+        db_namespace[{ :sql  => "test:clone_structure", :ruby => "test:load" }[ActiveRecord::Base.schema_format]].invoke
461 461
       end
462 462
     end
463 463
   end
10  railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
@@ -70,6 +70,16 @@ def test_dummy_config
70 70
       if mountable?
71 71
         template "rails/routes.rb", "#{dummy_path}/config/routes.rb", :force => true
72 72
       end
  73
+
  74
+      if full? && !options[:skip_active_record]
  75
+        append_file "#{dummy_path}/Rakefile", <<-EOF
  76
+
  77
+task :"db:load_config" do
  78
+  ActiveRecord::Migrator.migrations_paths = Rails.application.config.paths["db/migrate"].to_a +
  79
+                                            <%= camelized %>::Engine.config.paths["db/migrate"].to_a
  80
+end
  81
+        EOF
  82
+      end
73 83
     end
74 84
 
75 85
     def test_dummy_clean
12  railties/lib/rails/generators/rails/plugin_new/templates/Rakefile
@@ -14,3 +14,15 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
14 14
   rdoc.rdoc_files.include('README.rdoc')
15 15
   rdoc.rdoc_files.include('lib/**/*.rb')
16 16
 end
  17
+
  18
+<% if full? && !options[:skip_active_record] -%>
  19
+namespace :app do
  20
+  load File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__)
  21
+
  22
+  task :"db:load_config" do
  23
+    ActiveRecord::Migrator.migrations_paths = Rails.application.config.paths["db/migrate"].to_a +
  24
+                                              <%= camelized %>::Engine.config.paths["db/migrate"].to_a
  25
+  end
  26
+end
  27
+<% end -%>
  28
+
5  railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb
@@ -6,10 +6,5 @@
6 6
 
7 7
 Rails.backtrace_cleaner.remove_silencers!
8 8
 
9  
-<% if full? && !options[:skip_active_record] -%>
10  
-# Run any available migration from application
11  
-ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
12  
-<% end -%>
13  
-
14 9
 # Load support files
15 10
 Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
2  railties/test/generators/plugin_new_generator_test.rb
@@ -114,7 +114,7 @@ def test_ensure_that_tests_works
114 114
   end
115 115
 
116 116
   def test_ensure_that_tests_works_in_full_mode
117  
-    run_generator [destination_root, "--full"]
  117
+    run_generator [destination_root, "--full", "--skip_active_record"]
118 118
     FileUtils.cd destination_root
119 119
     `bundle install`
120 120
     assert_match /2 tests, 2 assertions, 0 failures, 0 errors/, `bundle exec rake test`

0 notes on commit 5df72a2

...Paul

Why was this line changed? This change ends up not triggering the ActiveSupport on_load callbacks that pull various gems in. Or should it just be a chain, like the other tasks [:environment, :load_config] ?

Ben Woosley

Agreed, this breaks schema dumper extensions such as those used in foreigner and for postgis support. I'll open an issue.

Matthew Higgins

The problem stems from these gems using private methods to hook into the schema dumping process, and the Rails maintainers cannot be responsible for knowing about all the gems. The postgis does some especially ugly things.

I realize that the on_load is not a private API. However, it's behavior is not documented with respect to the migration system.

Please sign in to comment.
Something went wrong with that request. Please try again.