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

detect when adding a reference that lock writes #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

milmazz
Copy link

@milmazz milmazz commented Nov 17, 2022

What I'm doing

Adding a new check to avoid locks when an Ecto Migration includes a foreign key.

Why?

As the README suggests, adding a migration like this:

def change do
  alter table("posts") do
    add :group_id, references("groups")
  end
end

It would block writes on both tables. This PR includes a new check to detect these cases and report them as dangerous.

Approach

Add a new function in the AST Parser to detect when a column is added and includes references to another table; then, we check if the validate: false option is given.

How to review this PR / I’d like feedback on

This is my first PR here, so any constructive feedback is welcome.

@milmazz milmazz force-pushed the feat/detect-blocking-foreign-keys branch 2 times, most recently from 468b4b9 to 04cd368 Compare November 17, 2022 03:07
@Artur-Sulej
Copy link
Owner

Hi @milmazz! Thank you for your PR.
You have a valid point about validate: false. This option indeed makes the operation non blocking and such case should not be marked as unsafe. Good catch.

Please notice, that there is already a check column_reference_added. Can you please extending its implementation to react on validate: false (instead of adding a new danger)?
Code: https://github.com/Artur-Sulej/excellent_migrations/blob/master/lib/ast_parser.ex#L132-L140
Test: https://github.com/Artur-Sulej/excellent_migrations/blob/master/test/ast_parser_test.exs#L43-L56

As the README suggests, adding a migration like this:

```elixir
def change do
  alter table("posts") do
    add :group_id, references("groups")
  end
end
```

Would block writes on both tables. This PR includes a new check to
detect these cases and report them as dangerous.
@milmazz milmazz force-pushed the feat/detect-blocking-foreign-keys branch from 04cd368 to 36ba668 Compare September 18, 2023 19:44
@milmazz
Copy link
Author

milmazz commented Sep 18, 2023

@Artur-Sulej Hello. Sorry that I completely forgot about this.

I followed your suggestion, and please let me know if that's what you want.

@milmazz
Copy link
Author

milmazz commented Sep 18, 2023

Also, please let me know if you like the use of defguard to improve the readability of the AST parser code. Here is an example, I can prepare another PR as a followup.

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.

2 participants