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
[Idea] Stop migrations with disable_ddl_transaction!
and add_reference
#202
Comments
Hey @rui-sebastiao, thanks for the suggestion and all the context. In either case, someone will need to manually resolve if creating the index fails, so I (personally) don't think it's worth the hassle of splitting into separate migrations (plus it's safe in either case). However, seems like a good use for custom checks for those who want it. |
Wouldn't there be less total work to solve in this situation? If the migrations are split, the column will create successfully and upon rerunning migrations, the user doesn't need to worry about a "the column already exists" error. That migration is done. The only concern is with fixing the index. When the migrations are one, the user is unable to rerun the migration without modifying the migration itself or first blowing away the new column. |
Yes, from our experience with the issue that raised this suggestion 😄
Exactly, hence why I think it would be valuable to integrate this. Single line instructions with more than one outcome and disabled transactions can yield inconsistent results. |
I think there's more total work with the proposed change. With the change, users need to spend more time writing Without the change, users don't need to change how they write |
Kinda in the same vibe: (not sure if I should open a new ticket or surf on this one)
Now all of a sudden everything is broken:
What if we "strongly suggest" (pun intended) to create indexes with a |
If creating an index concurrently fails, you'll always need to manually fix. I don't think it's much more work to handle two indexes rather than one, but teams who don't feel the same can use custom checks. See #205 (comment) for an example.
|
Wait... the index could be created and not checked?
Not super practical. Thanks for the fast reply!!! and valuable advice 🙏 PS about the 205#comment:
|
At our company we also patched |
@rui-sebastiao would be curious to check how it looks like?
|
In my "fork" of strong_migrations I made such operations idempotent, for example - https://github.com/fatkodima/online_migrations/blob/bcc4881d2cadb24443ed2d11495f492ab44e882c/lib/online_migrations/schema_statements.rb#L683-L720 |
Yes, exactly. |
@rui-sebastiao I'd be interested in see the cop if you don't mind sharing |
sure @jjb, here you go: class EnsureProperAddReference < Base
MSG = <<~TXT
When using add_reference, don't use disable_ddl_transaction! and define the index on a separate migration.
Migration 1: `add_reference :table, :column, index: false`
Migration 2: `disable_ddl_transaction! add_index :table, :column, algorithm: :concurrently`.
TXT
RESTRICT_ON_SEND = %i[disable_ddl_transaction!].freeze
def_node_matcher :wrong_usage?, <<~PATTERN
(begin
...
(def _
(args)
(send nil? :add_reference _ _ (hash $...))
)
)
PATTERN
def on_send(node)
add_offense(node, message: MSG) if wrong_usage?(node.parent)
end
end |
Closing the issue as I see we're not going to add it to source, but already had a healthy discussion about it. |
@rui-sebastiao is |
|
Thanks! 🤦 and now I realize that Rui's code is a cop and not a strong_migrations custom check. |
Hi,
Recently we had an issue at my company. Our stack: Rails 7 and PG13, deployed on Heroku.
Scenario
disable_ddl_transaction!
andadd_reference ... index: { algorithm: :concurrently}
on some PR.Root causes
disable_ddl_transaction!
, which meant it allowed part of it to go through instead of reverting it all in case of failure.What we did to prevent further chaos
disable_ddl_transaction!
andadd_reference
in the same migrationadd_reference
with index creation.Given that this is a scenario that may happen to more people, do you think it would it be valuable to include this out of the box on the gem? Do you want me to submit a PR?
For reference, our custom check source code:
The text was updated successfully, but these errors were encountered: