Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@drogus drogus authored
View
28 activerecord/lib/active_record/railties/databases.rake
@@ -1,4 +1,4 @@
-namespace :db do
+db_namespace = namespace :db do
task :load_config => :rails_env do
require 'active_record'
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
@@ -141,18 +141,18 @@ namespace :db do
task :migrate => [:environment, :load_config] do
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
+ db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
namespace :migrate do
# desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
task :redo => [:environment, :load_config] do
if ENV["VERSION"]
- Rake::Task["db:migrate:down"].invoke
- Rake::Task["db:migrate:up"].invoke
+ db_namespace["migrate:down"].invoke
+ db_namespace["migrate:up"].invoke
else
- Rake::Task["db:rollback"].invoke
- Rake::Task["db:migrate"].invoke
+ db_namespace["rollback"].invoke
+ db_namespace["migrate"].invoke
end
end
@@ -164,7 +164,7 @@ namespace :db do
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
raise "VERSION is required" unless version
ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_paths, version)
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
+ db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
# desc 'Runs the "down" for a given migration VERSION.'
@@ -172,7 +172,7 @@ namespace :db do
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
raise "VERSION is required" unless version
ActiveRecord::Migrator.run(:down, ActiveRecord::Migrator.migrations_paths, version)
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
+ db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
desc "Display status of migrations"
@@ -210,14 +210,14 @@ namespace :db do
task :rollback => [:environment, :load_config] do
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_paths, step)
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
+ db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
# desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
task :forward => [:environment, :load_config] do
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
ActiveRecord::Migrator.forward(ActiveRecord::Migrator.migrations_paths, step)
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
+ db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
# 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
namespace :schema do
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
- task :dump => :environment do

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] ?

@Empact
Empact added a note

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ task :dump => :load_config do
require 'active_record/schema_dumper'
File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
- Rake::Task["db:schema:dump"].reenable
+ db_namespace["schema:dump"].reenable
end
desc "Load a schema.rb file into the database"
@@ -383,7 +383,7 @@ namespace :db do
task :load => 'db:test:purge' do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
ActiveRecord::Schema.verbose = false
- Rake::Task["db:schema:load"].invoke
+ db_namespace["schema:load"].invoke
end
# desc "Recreate the test database from the current environment's database schema"
@@ -457,7 +457,7 @@ namespace :db do
# desc 'Check for pending migrations and load the test schema'
task :prepare => 'db:abort_if_pending_migrations' do
if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
- Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:load" }[ActiveRecord::Base.schema_format]].invoke
+ db_namespace[{ :sql => "test:clone_structure", :ruby => "test:load" }[ActiveRecord::Base.schema_format]].invoke
end
end
end
View
10 railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
@@ -70,6 +70,16 @@ def test_dummy_config
if mountable?
template "rails/routes.rb", "#{dummy_path}/config/routes.rb", :force => true
end
+
+ if full? && !options[:skip_active_record]
+ append_file "#{dummy_path}/Rakefile", <<-EOF
+
+task :"db:load_config" do
+ ActiveRecord::Migrator.migrations_paths = Rails.application.config.paths["db/migrate"].to_a +
+ <%= camelized %>::Engine.config.paths["db/migrate"].to_a
+end
+ EOF
+ end
end
def test_dummy_clean
View
12 railties/lib/rails/generators/rails/plugin_new/templates/Rakefile
@@ -14,3 +14,15 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end
+
+<% if full? && !options[:skip_active_record] -%>
+namespace :app do
+ load File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__)
+
+ task :"db:load_config" do
+ ActiveRecord::Migrator.migrations_paths = Rails.application.config.paths["db/migrate"].to_a +
+ <%= camelized %>::Engine.config.paths["db/migrate"].to_a
+ end
+end
+<% end -%>
+
View
5 railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb
@@ -6,10 +6,5 @@
Rails.backtrace_cleaner.remove_silencers!
-<% if full? && !options[:skip_active_record] -%>
-# Run any available migration from application
-ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
-<% end -%>
-
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
View
2  railties/test/generators/plugin_new_generator_test.rb
@@ -114,7 +114,7 @@ def test_ensure_that_tests_works
end
def test_ensure_that_tests_works_in_full_mode
- run_generator [destination_root, "--full"]
+ run_generator [destination_root, "--full", "--skip_active_record"]
FileUtils.cd destination_root
`bundle install`
assert_match /2 tests, 2 assertions, 0 failures, 0 errors/, `bundle exec rake test`
@dotdotdotpaul

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] ?

@Empact

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

@matthuhiggins

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.