Is there a way to run rake commands for db:migrate and db:rollback on the console?
It sucks to wait for the rails environment to load!
Is there a way to run rake commands for db:migrate and db:rollback on the console?
It sucks to wait for the rails environment to load!
This will allow you to migrate without reloading the whole rails environment:
ActiveRecord::Migrator.migrate "db/migrate"
and rollback:
# 3 is the number of migrations to rollback, optional, defaults to 1
ActiveRecord::Migrator.rollback "db/migrate", 3
Migrate :
ActiveRecord::MigrationContext.new("db/migrate").migrate
And rollback :
# 3 is the number of migrations to rollback, optional, defaults to 1
ActiveRecord::MigrationContext.new("db/migrate").rollback 3
'db/migrate'
, use ActiveRecord::Tasks::DatabaseTasks.migrations_paths
in its place –
Tapir ActiveRecord::MigrationContext.new("db/migrate").rollback
- not mentioned but also works –
Sisera schema_migration
: ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths, ActiveRecord::Base.connection.schema_migration)
–
Alopecia In the console:
ActiveRecord::Migration.remove_column :table_name, :column_name
To update your schema.rb
file after running migrations from the console, you must run rails db:migrate
reload!
to get the effect in the console, and having finished with the console, as long I exit the console, I do rails db:migrate, and it migrates them, and updates db/schema.rb . Really good to look at db/schema.rb . it shows all your tables. rails db:migrate
updates schema.rb
for you, at least I think that's what i've found. –
Helsell rails db:migrate
can automatically update the schema. However this is an extra step that is not in the answer. –
Ginter This will allow you to migrate without reloading the whole rails environment:
ActiveRecord::Migrator.migrate "db/migrate"
and rollback:
# 3 is the number of migrations to rollback, optional, defaults to 1
ActiveRecord::Migrator.rollback "db/migrate", 3
Migrate :
ActiveRecord::MigrationContext.new("db/migrate").migrate
And rollback :
# 3 is the number of migrations to rollback, optional, defaults to 1
ActiveRecord::MigrationContext.new("db/migrate").rollback 3
Mongoid::Migrator.migrate "db/migrate"
–
Niles 'db/migrate'
, use ActiveRecord::Tasks::DatabaseTasks.migrations_paths
in its place –
Tapir ActiveRecord::MigrationContext.new("db/migrate").rollback
- not mentioned but also works –
Sisera schema_migration
: ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths, ActiveRecord::Base.connection.schema_migration)
–
Alopecia Another way that I find neater to just run some migration command from console is this:
ActiveRecord::Schema.define do
create_table :foo do |t|
t.string :bar
t.timestamps
end
end
This has the advantage that the contents inside the block is compatible with just copy and pasting random contents from a real migration file / schema.rb
.
For rails 5.2 the accepted answer has been removed and replaced with
ActiveRecord::MigrationContext.new("db/migrate").migrate
Please be aware as this may also change for future versions of rails as they work to add multiple database connections
For Rails 5 and Rails 6:
ActiveRecord::Base.connection.migration_context.migrate
For Rails 3 and Rails 4:
ActiveRecord::Migrator.migrate 'db/migrate'
I needed to pretend a migration was run to unblock a deploy, this can be done with:
class Mig < ActiveRecord::Base; self.table_name = 'schema_migrations';end
Mig.create! version: '20180611172637'
You can use the %x[command]
%x[rake db:migrate]
ActiveRecord::Migration.add_column(:table_name, :column_name, :data_type)
ActiveRecord::Migrator.migrate('db/migrate')
ActiveRecord::Migrator.rollback('db/migrate', n)
I created a method in my .irbrc file that runs migrations then reloads the console:
def migrate
if defined? Rails::Console # turn off info logging for Rails 3
old_log_level = ActiveRecord::Base.logger.try(:sev_threshold)
ActiveRecord::Base.logger.sev_threshold = Logger::WARN
end
reload! && migations_ran = true if ActiveRecord::Migrator.migrate(Rails.root.join("db/migrate")).any?
ActiveRecord::Base.logger.sev_threshold = old_log_level if defined? old_log_level
migations_ran ||= nil # useful exit status
end
See the entire file here: https://gist.github.com/imme5150/6548368
© 2022 - 2024 — McMap. All rights reserved.
Mongoid::Migrator.migrate "db/migrate"
– Niles