diff --git a/README.md b/README.md index cf4f3c9..a643033 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_902 TILs and counting..._ +_903 TILs and counting..._ --- @@ -551,6 +551,7 @@ _902 TILs and counting..._ - [Log SQL Queries Executed By ActiveRecord](rails/log-sql-queries-executed-by-activerecord.md) - [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md) - [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md) +- [Manually Run A Migration From Rails Console](rails/manually-run-a-migration-from-rails-console.md) - [Mark For Destruction](rails/mark-for-destruction.md) - [Merge A Scope Into An ActiveRecord Query](rails/merge-a-scope-into-an-activerecord-query.md) - [Migrating Up Down Up](rails/migrating-up-down-up.md) diff --git a/rails/manually-run-a-migration-from-rails-console.md b/rails/manually-run-a-migration-from-rails-console.md new file mode 100644 index 0000000..73f3437 --- /dev/null +++ b/rails/manually-run-a-migration-from-rails-console.md @@ -0,0 +1,33 @@ +# Manually Run A Migrations From Rails Console + +A migration can be manually run from the rails console. In 99% of cases you are +going to be better off using the migration CLI that Rails provides (e.g. `rails +db:migrate`, `rails db:rollback`, etc.). + +If you are in a hyper-specific scenario where you need to run the `up` or the +`down` of a migration without the migration-table check, then you'll want to +consider this approach. + +First, connect to the rails console: `rails c`. Then require your migration +file. + +```ruby +> require "./db/migration/20200220181733_some_migration.rb" +#=> true +``` + +You'll now have access to the `SomeMigration` constant. Create an instance of this and then run either the `up`-side of the migration: + +```ruby +> SomeMigration.new.up +#=> ... # a bunch of migration output +``` + +or the `down`-side of it: + +```ruby +> SomeMigration.new.down +#=> ... # a bunch of migration output +``` + +[source](https://stackoverflow.com/a/754316/535590)