-
Notifications
You must be signed in to change notification settings - Fork 212
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
Changes from 3 commits
26d84fa
4ea215b
a2c40b8
8526f49
61ebdbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,6 +321,171 @@ fn creating_a_migration_with_a_non_existent_migrations_directory_should_work(api | |
.assert_migration_directories_count(1); | ||
} | ||
|
||
#[test_connector(tags(Postgres, CockroachDb))] | ||
fn alter_enum_name_remove_and_change_default(mut api: TestApi) { | ||
let plain_dm = api.datamodel_with_provider( | ||
r#" | ||
model Cat { | ||
id Int @id | ||
mood Mood @default(HUNGRY) | ||
} | ||
|
||
enum Mood { | ||
HUNGRY | ||
SLEEPY | ||
} | ||
"#, | ||
); | ||
|
||
let dir = api.create_migrations_directory(); | ||
api.create_migration("plain", &plain_dm, &dir).send_sync(); | ||
|
||
let custom_dm = api.datamodel_with_provider( | ||
r#" | ||
model Cat { | ||
id Int @id | ||
mood Mood @default(MOODY) | ||
} | ||
|
||
enum Mood { | ||
SLEEPY | ||
MOODY | ||
} | ||
"#, | ||
); | ||
|
||
let is_postgres = api.is_postgres(); | ||
let is_cockroach = api.is_cockroach(); | ||
|
||
api.create_migration("custom", &custom_dm, &dir) | ||
.send_sync() | ||
.assert_migration_directories_count(2) | ||
.assert_migration("custom", move |migration| { | ||
let expected_script = if is_cockroach { | ||
expect![[r#" | ||
/* | ||
Warnings: | ||
|
||
- The values [HUNGRY] on the enum `Mood` will be removed. If these variants are still used in the database, this will fail. | ||
|
||
*/ | ||
-- AlterEnum | ||
ALTER TABLE "Cat" ALTER COLUMN "mood" DROP DEFAULT; | ||
ALTER TYPE "Mood" ADD VALUE 'MOODY'; | ||
ALTER TYPE "Mood" DROP VALUE 'HUNGRY'; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Cat" ALTER COLUMN "mood" SET DEFAULT 'MOODY'; | ||
"#]] | ||
} else if is_postgres { | ||
expect![[r#" | ||
/* | ||
Warnings: | ||
|
||
- The values [HUNGRY] on the enum `Mood` will be removed. If these variants are still used in the database, this will fail. | ||
|
||
*/ | ||
-- AlterEnum | ||
BEGIN; | ||
CREATE TYPE "Mood_new" AS ENUM ('SLEEPY', 'MOODY'); | ||
ALTER TABLE "Cat" ALTER COLUMN "mood" DROP DEFAULT; | ||
ALTER TABLE "Cat" ALTER COLUMN "mood" TYPE "Mood_new" USING ("mood"::text::"Mood_new"); | ||
ALTER TYPE "Mood" RENAME TO "Mood_old"; | ||
ALTER TYPE "Mood_new" RENAME TO "Mood"; | ||
DROP TYPE "Mood_old"; | ||
ALTER TABLE "Cat" ALTER COLUMN "mood" SET DEFAULT 'MOODY'; | ||
COMMIT; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Cat" ALTER COLUMN "mood" SET DEFAULT 'MOODY'; | ||
"#]] | ||
} else { | ||
panic!() | ||
}; | ||
|
||
migration.expect_contents(expected_script) | ||
}); | ||
} | ||
|
||
#[test_connector(tags(Postgres, CockroachDb))] | ||
fn alter_enum_name_with_default_empty_list_shouldnt_set_new_default(mut api: TestApi) { | ||
jkomyno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let plain_dm = api.datamodel_with_provider( | ||
r#" | ||
model Cat { | ||
id Int @id | ||
moods Mood[] @default([]) | ||
} | ||
|
||
enum Mood { | ||
HUNGRY | ||
SLEEPY | ||
} | ||
"#, | ||
); | ||
|
||
let dir = api.create_migrations_directory(); | ||
api.create_migration("plain", &plain_dm, &dir).send_sync(); | ||
|
||
let custom_dm = api.datamodel_with_provider( | ||
r#" | ||
model Cat { | ||
id Int @id | ||
moods Mood[] @default([]) | ||
} | ||
|
||
enum Mood { | ||
SLEEPY | ||
MOODY | ||
} | ||
"#, | ||
); | ||
|
||
let is_postgres = api.is_postgres(); | ||
let is_cockroach = api.is_cockroach(); | ||
|
||
api.create_migration("custom", &custom_dm, &dir) | ||
.send_sync() | ||
.assert_migration_directories_count(2) | ||
.assert_migration("custom", move |migration| { | ||
let expected_script = if is_cockroach { | ||
expect![[r#" | ||
/* | ||
Warnings: | ||
|
||
- The values [HUNGRY] on the enum `Mood` will be removed. If these variants are still used in the database, this will fail. | ||
|
||
*/ | ||
-- AlterEnum | ||
ALTER TYPE "Mood" ADD VALUE 'MOODY'; | ||
ALTER TYPE "Mood" DROP VALUE 'HUNGRY'; | ||
"#]] | ||
} else if is_postgres { | ||
expect![[r#" | ||
/* | ||
Warnings: | ||
|
||
- The values [HUNGRY] on the enum `Mood` will be removed. If these variants are still used in the database, this will fail. | ||
|
||
*/ | ||
-- AlterEnum | ||
BEGIN; | ||
CREATE TYPE "Mood_new" AS ENUM ('SLEEPY', 'MOODY'); | ||
ALTER TABLE "Cat" ALTER COLUMN "moods" DROP DEFAULT; | ||
ALTER TABLE "Cat" ALTER COLUMN "moods" TYPE "Mood_new"[] USING ("moods"::text::"Mood_new"[]); | ||
ALTER TYPE "Mood" RENAME TO "Mood_old"; | ||
ALTER TYPE "Mood_new" RENAME TO "Mood"; | ||
DROP TYPE "Mood_old"; | ||
ALTER TABLE "Cat" ALTER COLUMN "moods"; | ||
COMMIT; | ||
"#]] | ||
} else { | ||
panic!() | ||
}; | ||
|
||
migration.expect_contents(expected_script) | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we test the following scenario:
I think it's a more minimal reproduction of the conditions for the crash. That test can live in tests/migrations/enums.rs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this scenario already tested by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep we could move it there. It's a different test however, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have moved the tests to |
||
|
||
#[test_connector(tags(Mysql, Postgres))] | ||
fn create_enum_step_only_rendered_when_needed(api: TestApi) { | ||
let dm = format!( | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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:
Is it idiomatic in Rust to have mutable operations inside functional methods like
.filter()
and.map()
?There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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))))
.There was a problem hiding this comment.
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.