Skip to content

Commit 40cbff0

Browse files
committed
Add Manually Run A Migration From Rails Console as a rails til
1 parent 0fffba8 commit 40cbff0

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
99

1010
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1111

12-
_902 TILs and counting..._
12+
_903 TILs and counting..._
1313

1414
---
1515

@@ -551,6 +551,7 @@ _902 TILs and counting..._
551551
- [Log SQL Queries Executed By ActiveRecord](rails/log-sql-queries-executed-by-activerecord.md)
552552
- [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md)
553553
- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md)
554+
- [Manually Run A Migration From Rails Console](rails/manually-run-a-migration-from-rails-console.md)
554555
- [Mark For Destruction](rails/mark-for-destruction.md)
555556
- [Merge A Scope Into An ActiveRecord Query](rails/merge-a-scope-into-an-activerecord-query.md)
556557
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Manually Run A Migrations From Rails Console
2+
3+
A migration can be manually run from the rails console. In 99% of cases you are
4+
going to be better off using the migration CLI that Rails provides (e.g. `rails
5+
db:migrate`, `rails db:rollback`, etc.).
6+
7+
If you are in a hyper-specific scenario where you need to run the `up` or the
8+
`down` of a migration without the migration-table check, then you'll want to
9+
consider this approach.
10+
11+
First, connect to the rails console: `rails c`. Then require your migration
12+
file.
13+
14+
```ruby
15+
> require "./db/migration/20200220181733_some_migration.rb"
16+
#=> true
17+
```
18+
19+
You'll now have access to the `SomeMigration` constant. Create an instance of this and then run either the `up`-side of the migration:
20+
21+
```ruby
22+
> SomeMigration.new.up
23+
#=> ... # a bunch of migration output
24+
```
25+
26+
or the `down`-side of it:
27+
28+
```ruby
29+
> SomeMigration.new.down
30+
#=> ... # a bunch of migration output
31+
```
32+
33+
[source](https://stackoverflow.com/a/754316/535590)

0 commit comments

Comments
 (0)