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

fix(me): fields whose type is a list of enums with @default([]) are now migrated without crashes on Postgres #3347

Closed
wants to merge 5 commits into from

Conversation

jkomyno
Copy link
Contributor

@jkomyno jkomyno commented Oct 28, 2022

@jkomyno jkomyno added this to the 4.6.0 milestone Oct 28, 2022
alter_sql,
default = escape_string_literal(default_str),
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the fix can be better expressed as a single line (a .filter() after line 1003). I'd rather we never produce ALTER TABLE ... ALTER COLUMN ... without the last part of the clause. It's most likely a no-op.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean something like:

    default_str_option.filter(|default_str| {
        let set_default = format!(
            "ALTER TABLE {table_name} ALTER COLUMN {column_name} SET DEFAULT '{default}'",
            table_name = Quoted::postgres_ident(&table_name),
            column_name = Quoted::postgres_ident(&column_name),
            default = escape_string_literal(default_str),
        );
        stmts.push(set_default)
    });

Is it idiomatic in Rust to have mutable operations inside functional methods like .filter() and .map()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can also express the same logic with:

    if let Some(default_str) = default_str_option {
        let set_default = format!(
            "ALTER TABLE {table_name} ALTER COLUMN {column_name} SET DEFAULT '{default}'",
            table_name = Quoted::postgres_ident(&table_name),
            column_name = Quoted::postgres_ident(&column_name),
            default = escape_string_literal(default_str),
        );
        stmts.push(set_default)
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

No usually we avoid mutation inside iterator methods.

I meant at line 1003, insert one more call .filter(|(_, next)| next.default.is_some()), and that would be the only change needed in this file. If you want to also remove the unwrap in the loop body, you can .filter_map(|(previous, next)| next.default.map(|d| ((previous, (next, d)))).

Copy link
Contributor

Choose a reason for hiding this comment

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

The if let would also work, but it's a larger diff and it adds more noise.


migration.expect_contents(expected_script)
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we test the following scenario:

  • Single model with a field with mood Mood @default(HUNGRY)
  • Migration to drop one variant from the enum and the default on the field at the same time.

I think it's a more minimal reproduction of the conditions for the crash.

That test can live in tests/migrations/enums.rs

Copy link
Contributor Author

@jkomyno jkomyno Nov 3, 2022

Choose a reason for hiding this comment

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

Isn't this scenario already tested by alter_enum_name_remove_and_change_default? I could move it to tests/migrations/enums.rs.

Copy link
Contributor

Choose a reason for hiding this comment

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

yep we could move it there. It's a different test however, alter_enum_name_remove_and_change_default still has a default, just a different one. We should test what happens when the whole @default() is removed in the second schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have moved the tests to tests/migrations/enums.rs and left a few TODOs.

@jkomyno
Copy link
Contributor Author

jkomyno commented Nov 9, 2022

Closing this PR in favor of #3379.

@jkomyno jkomyno closed this Nov 9, 2022
@janpio janpio deleted the fix/migration-enums-15705 branch November 15, 2023 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants