Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce suspenders:db:migrate task #1161

Merged
merged 2 commits into from
Feb 20, 2024

Conversation

stevepolitodesign
Copy link
Contributor

@stevepolitodesign stevepolitodesign commented Feb 20, 2024

Lifted from our dotfiles, this task runs the latest migration, rolls
it back, and runs it again in an effort to ensure it's reversible. More
context can be found in these comments.

Unfortunately, the following implementation did not work because Rake recognized
that it db:migrate was already invoked, so it wasn't invoked a second
time.

task migrate: ["db:migrate", "db:rollback", "db:migrate", "db:test:prepare"]

Instead, we needed to reenable the task manually.

@stevepolitodesign stevepolitodesign changed the base branch from main to suspenders-3-0-0 February 20, 2024 12:44
@stevepolitodesign
Copy link
Contributor Author

I tested this locally and it worked.

❯ bin/rails suspenders:migrate
== 20240220120804 CreateUsers: reverting ======================================
-- drop_table(:users)
   -> 0.0011s
== 20240220120804 CreateUsers: reverted (0.0034s) =============================

== 20240220120804 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0015s
== 20240220120804 CreateUsers: migrated (0.0015s) =============================

@stevepolitodesign stevepolitodesign marked this pull request as ready for review February 20, 2024 12:45
@stevepolitodesign stevepolitodesign mentioned this pull request Feb 20, 2024
17 tasks
Lifted from our [dotfiles][], this task runs the latest migration, rolls
it back, and runs it again in an effort to ensure it's reversible. More
context can be found [in these comments][].

Unfortunately, this implementation did not work because Rake recognized
that it `db:migrate` was already invoked, so it wasn't invoked a second
time. Instead, we needed to `reenable` the task manually.

```ruby
task migrate: ["db:migrate", "db:rollback", "db:migrate", "db:test:prepare"]
```

[dotfiles]: https://github.com/thoughtbot/dotfiles/blob/f149484269ef98e2bc80b7daa1988e214ddf8e8b/aliases#L12
[in these comments]: thoughtbot/dotfiles@4882c41#r1933964
@crackofdusk
Copy link
Contributor

What is the advantage of this approach vs bin/rails db:migrate:redo?

@stevepolitodesign
Copy link
Contributor Author

What is the advantage of this approach vs bin/rails db:migrate:redo?

@crackofdusk this is a great question.

When there's a pending migration, db:migrate:redo simply runs the migration. It does not revert it, and run it again, which is what suspenders:migrate aims to do.

bin/rails db:migrate:status

 Status   Migration ID    Migration Name
--------------------------------------------------
  down    20240220120804  Create users

bin/rails db:migrate:redo
== 20240220120804 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0121s
== 20240220120804 CreateUsers: migrated (0.0122s) =============================

However, if the migration has been run, then db:migrate:redo will revert it, and migrate it again.

bin/rails db:migrate:status

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20240220120804  Create users

bin/rails db:migrate:redo
== 20240220120804 CreateUsers: reverting ======================================
-- drop_table(:users)
   -> 0.0009s
== 20240220120804 CreateUsers: reverted (0.0031s) =============================

== 20240220120804 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0008s
== 20240220120804 CreateUsers: migrated (0.0008s) =============================

This is in contrast to suspenders:migrate in that it will always run the latest migration, revert it, and run it again.

Copy link
Contributor

@mike-burns mike-burns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. LGTM, pushing this into Suspenders makes some sense (though I still need the shell alias for most Boost projects).
  2. Should it be called like suspenders:db:migrate? I worry that we might one day want a way to migrate part of Suspenders itself.

@stevepolitodesign stevepolitodesign changed the title Introduce suspenders:migrate task Introduce suspenders:db:migrate task Feb 20, 2024
@stevepolitodesign stevepolitodesign merged commit 647bb4c into suspenders-3-0-0 Feb 20, 2024
2 checks passed
@stevepolitodesign stevepolitodesign deleted the suspenders-3-0-0-migrate branch February 20, 2024 21:19
stevepolitodesign added a commit that referenced this pull request May 10, 2024
Lifted from our [dotfiles][], this task runs the latest migration, rolls
it back, and runs it again in an effort to ensure it's reversible. More
context can be found [in these comments][].

Unfortunately, the following implementation did not work because Rake recognized
that it `db:migrate` was already invoked, so it wasn't invoked a second time.

```ruby
task migrate: ["db:migrate", "db:rollback", "db:migrate", "db:test:prepare"]
```

Instead, we needed to `reenable` the task manually.

[dotfiles]: https://github.com/thoughtbot/dotfiles/blob/f149484269ef98e2bc80b7daa1988e214ddf8e8b/aliases#L12
[in these comments]: thoughtbot/dotfiles@4882c41#r1933964
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants