0
@@ -52,38 +52,93 @@ namespace :db do
0
puts "This task only creates local databases. #{config[:database]} is on a remote host."
0
- desc 'Drops the database for the current environment'
0
- task :drop => :merb_start do
0
- config = ActiveRecord::Base.configurations[Merb.environment.to_sym || :development]
0
+ def drop_database(config)
0
- ActiveRecord::Base.connection.drop_database config[:database]
0
- puts "#{config[:database]} seems to have been dropped already"
0
+ ActiveRecord::Base.connection.drop_database config[:database]
0
- FileUtils.rm
_f File.join(Merb.root, config[:database])
0
+ FileUtils.rm
(File.join(RAILS_ROOT, config[:database]))
0
+ ActiveRecord::Base.clear_active_connections!
0
`dropdb "#{config[:database]}"`
0
+ def local_database?(config, &block)
0
+ if %w( 127.0.0.1 localhost ).include?(config[:host]) || config[:host].blank?
0
+ puts "This task only modifies local databases. #{config[:database]} is on a remote host."
0
+ desc 'Drops all the local databases defined in config/database.yml'
0
+ task :all => :merb_start do
0
+ ActiveRecord::Base.configurations.each_value do |config|
0
+ # Skip entries that don't have a database key
0
+ next unless config[:database]
0
+ # Only connect to local databases
0
+ local_database?(config) { drop_database(config) }
0
+ desc 'Drops the database for the current environment (set MERB_ENV to target another environment)'
0
+ task :drop => :merb_start do
0
+ config = ActiveRecord::Base.configurations[Merb.environment.to_sym]
0
+ puts "#{e.inspect} - #{config['database']} might have been already dropped"
0
desc "Migrate the database through scripts in schema/migrations. Target specific version with VERSION=x"
0
task :migrate => :merb_start do
0
- config = ActiveRecord::Base.configurations[Merb.environment.to_sym
|| :development]
0
+ config = ActiveRecord::Base.configurations[Merb.environment.to_sym
]
0
ActiveRecord::Base.establish_connection(config)
0
ActiveRecord::Migrator.migrate("schema/migrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
0
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
0
- desc 'Drops, creates and then migrates the database for the current environment. Target specific version with VERSION=x'
0
- task :reset => ['db:drop', 'db:create', 'db:migrate']
0
+ desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x'
0
+ task :redo => [ 'db:rollback', 'db:migrate' ]
0
+ desc 'Resets your database using your migrations for the current environment'
0
+ task :reset => ["db:drop", "db:create", "db:migrate"]
0
+ desc 'Drops and recreates the database from db/schema.rb for the current environment.'
0
+ task :reset => ['db:drop', 'db:create', 'db:schema:load']
0
+ desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
0
+ task :rollback => :merb_start do
0
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
0
+ version = ActiveRecord::Migrator.current_version - step
0
+ ActiveRecord::Migrator.migrate('schema/migrations/', version)
0
+ desc "Raises an error if there are pending migrations"
0
+ task :abort_if_pending_migrations => :merb_start do
0
+ if defined? ActiveRecord
0
+ pending_migrations = ActiveRecord::Migrator.new(:up, 'schema/migrations').pending_migrations
0
+ if pending_migrations.any?
0
+ puts "You have #{pending_migrations.size} pending migrations:"
0
+ pending_migrations.each do |pending_migration|
0
+ puts ' %4d %s' % [pending_migration.version, pending_migration.name]
0
+ abort "Run `rake db:migrate` to update your database then try again."
0
desc "Retrieves the charset for the current environment's database"
0
task :charset => :merb_start do
0
- config = ActiveRecord::Base.configurations[Merb.environment.to_sym
|| :development]
0
+ config = ActiveRecord::Base.configurations[Merb.environment.to_sym
]
0
ActiveRecord::Base.establish_connection(config)
0
@@ -95,7 +150,7 @@ namespace :db do
0
desc "Retrieves the collation for the current environment's database"
0
task :collation => :merb_start do
0
- config = ActiveRecord::Base.configurations[Merb.environment.to_sym
|| :development]
0
+ config = ActiveRecord::Base.configurations[Merb.environment.to_sym
]
0
ActiveRecord::Base.establish_connection(config)
Comments
No one has commented yet.