Skip to content

Commit

Permalink
Merge pull request rails#6855 from kennyj/refactor_database_tasks
Browse files Browse the repository at this point in the history
Refactor and improve database tasks.
  • Loading branch information
rafaelfranca committed Jun 25, 2012
2 parents 51b055b + 04002db commit 1981159
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
49 changes: 28 additions & 21 deletions activerecord/lib/active_record/railties/databases.rake
Expand Up @@ -35,7 +35,7 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.drop_current
end

desc "Migrate the database (options: VERSION=x, VERBOSE=false)."
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
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) do |migration|
Expand Down Expand Up @@ -270,6 +270,15 @@ db_namespace = namespace :db do
end

namespace :structure do
def set_firebird_env(config)
ENV['ISC_USER'] = config['username'].to_s if config['username']
ENV['ISC_PASSWORD'] = config['password'].to_s if config['password']
end

def firebird_db_string(config)
FireRuby::Database.db_string_for(config.symbolize_keys)
end

desc 'Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql'
task :dump => [:environment, :load_config] do
abcs = ActiveRecord::Base.configurations
Expand Down Expand Up @@ -338,6 +347,13 @@ db_namespace = namespace :db do
end
end

# desc "Recreate the test database from an existent schema.rb file"
task :load_schema => 'db:test:purge' do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
ActiveRecord::Schema.verbose = false
db_namespace["schema:load"].invoke
end

# desc "Recreate the test database from an existent structure.sql file"
task :load_structure => 'db:test:purge' do
begin
Expand All @@ -348,15 +364,18 @@ db_namespace = namespace :db do
end
end

# desc "Recreate the test database from an existent schema.rb file"
task :load_schema => 'db:test:purge' do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
ActiveRecord::Schema.verbose = false
db_namespace["schema:load"].invoke
# desc "Recreate the test database from a fresh schema"
task :clone do
case ActiveRecord::Base.schema_format
when :ruby
db_namespace["test:clone_schema"].invoke
when :sql
db_namespace["test:clone_structure"].invoke
end
end

# desc "Recreate the test database from a fresh schema.rb file"
task :clone => %w(db:schema:dump db:test:load_schema)
task :clone_schema => ["db:schema:dump", "db:test:load_schema"]

# desc "Recreate the test database from a fresh structure.sql file"
task :clone_structure => [ "db:structure:dump", "db:test:load_structure" ]
Expand Down Expand Up @@ -389,7 +408,7 @@ db_namespace = namespace :db do
# desc 'Check for pending migrations and load the test schema'
task :prepare => 'db:abort_if_pending_migrations' do
unless ActiveRecord::Base.configurations.blank?
db_namespace[{ :sql => 'test:clone_structure', :ruby => 'test:load' }[ActiveRecord::Base.schema_format]].invoke
db_namespace['test:load'].invoke
end
end
end
Expand All @@ -405,7 +424,7 @@ db_namespace = namespace :db do

# desc "Clear the sessions table"
task :clear => [:environment, :load_config] do
ActiveRecord::Base.connection.execute "DELETE FROM #{session_table_name}"
ActiveRecord::Base.connection.execute "DELETE FROM #{ActiveRecord::SessionStore::Session.table_name}"
end
end
end
Expand Down Expand Up @@ -440,15 +459,3 @@ end

task 'test:prepare' => 'db:test:prepare'

def session_table_name
ActiveRecord::SessionStore::Session.table_name
end

def set_firebird_env(config)
ENV['ISC_USER'] = config['username'].to_s if config['username']
ENV['ISC_PASSWORD'] = config['password'].to_s if config['password']
end

def firebird_db_string(config)
FireRuby::Database.db_string_for(config.symbolize_keys)
end
18 changes: 18 additions & 0 deletions railties/test/application/rake_test.rb
Expand Up @@ -144,6 +144,24 @@ def test_scaffold_tests_pass_by_default
assert_no_match(/Errors running/, output)
end

def test_db_test_clone_when_using_sql_format
add_to_config "config.active_record.schema_format = :sql"
output = Dir.chdir(app_path) do
`rails generate scaffold user username:string;
bundle exec rake db:migrate db:test:clone 2>&1 --trace`
end
assert_match(/Execute db:test:clone_structure/, output)
end

def test_db_test_prepare_when_using_sql_format
add_to_config "config.active_record.schema_format = :sql"
output = Dir.chdir(app_path) do
`rails generate scaffold user username:string;
bundle exec rake db:migrate db:test:clone 2>&1 --trace`
end
assert_match(/Execute db:test:load_structure/, output)
end

def test_rake_dump_structure_should_respect_db_structure_env_variable
Dir.chdir(app_path) do
# ensure we have a schema_migrations table to dump
Expand Down

0 comments on commit 1981159

Please sign in to comment.