Skip to content

Commit

Permalink
fix(me): make bigint default migrations idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule authored and yume-chan committed Jul 8, 2022
1 parent 5b132e7 commit 0a3a2b6
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ fn defaults_match(cols: Pair<ColumnWalker<'_>>, flavour: &dyn SqlFlavour) -> boo
(Some(DefaultKind::Value(PrismaValue::DateTime(_))), Some(_))
| (Some(_), Some(DefaultKind::Value(PrismaValue::DateTime(_)))) => true, // can't diff these in at present

(Some(DefaultKind::Value(PrismaValue::Int(i))), Some(DefaultKind::Value(PrismaValue::BigInt(j))))
| (Some(DefaultKind::Value(PrismaValue::BigInt(i))), Some(DefaultKind::Value(PrismaValue::Int(j)))) => i == j,
(Some(DefaultKind::Value(prev)), Some(DefaultKind::Value(next))) => (prev == next) && names_match,
(Some(DefaultKind::Value(_)), Some(DefaultKind::Now)) => false,
(Some(DefaultKind::Value(_)), None) => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,3 +1230,31 @@ fn sequence_with_multiple_models_works(api: TestApi) {
api.schema_push(schema).send().assert_green();
api.schema_push(schema).send().assert_green().assert_no_steps();
}

#[test_connector(tags(CockroachDb))]
fn bigint_defaults_work(api: TestApi) {
let schema = r#"
datasource mypg {
provider = "cockroachdb"
url = env("TEST_DATABASE_URL")
}
model foo {
id String @id
bar BigInt @default(0)
}
"#;
let sql = expect![[r#"
-- CreateTable
CREATE TABLE "foo" (
"id" STRING NOT NULL,
"bar" INT8 NOT NULL DEFAULT 0,
CONSTRAINT "foo_pkey" PRIMARY KEY ("id")
);
"#]];
api.expect_sql_for_schema(schema, &sql);

api.schema_push(schema).send().assert_green();
api.schema_push(schema).send().assert_green().assert_no_steps();
}
44 changes: 44 additions & 0 deletions migration-engine/migration-engine-tests/tests/migrations/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,47 @@ fn prisma_9537(api: TestApi) {
.send()
.assert_green();
}

#[test_connector(tags(Mssql))]
fn bigint_defaults_work(api: TestApi) {
let schema = r#"
datasource mypg {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
}
model foo {
id String @id
bar BigInt @default(0)
}
"#;
let sql = expect![[r#"
BEGIN TRY
BEGIN TRAN;
-- CreateTable
CREATE TABLE [bigint_defaults_work].[foo] (
[id] NVARCHAR(1000) NOT NULL,
[bar] BIGINT NOT NULL CONSTRAINT [foo_bar_df] DEFAULT 0,
CONSTRAINT [foo_pkey] PRIMARY KEY CLUSTERED ([id])
);
COMMIT TRAN;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRAN;
END;
THROW
END CATCH
"#]];
api.expect_sql_for_schema(schema, &sql);

api.schema_push(schema).send().assert_green();
api.schema_push(schema).send().assert_green().assert_no_steps();
}
28 changes: 28 additions & 0 deletions migration-engine/migration-engine-tests/tests/migrations/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,31 @@ fn alter_constraint_name(mut api: TestApi) {
migration.expect_contents(expected_script)
});
}

#[test_connector(tags(Mysql))]
fn bigint_defaults_work(api: TestApi) {
let schema = r#"
datasource mypg {
provider = "mysql"
url = env("TEST_DATABASE_URL")
}
model foo {
id String @id
bar BigInt @default(0)
}
"#;
let sql = expect![[r#"
-- CreateTable
CREATE TABLE `foo` (
`id` VARCHAR(191) NOT NULL,
`bar` BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
"#]];
api.expect_sql_for_schema(schema, &sql);

api.schema_push(schema).send().assert_green();
api.schema_push(schema).send().assert_green().assert_no_steps();
}
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,31 @@ fn json_defaults_with_escaped_quotes_work(api: TestApi) {

api.expect_sql_for_schema(schema, &sql);
}

#[test_connector(tags(Postgres), exclude(CockroachDb))]
fn bigint_defaults_work(api: TestApi) {
let schema = r#"
datasource mypg {
provider = "postgresql"
url = env("TEST_DATABASE_URL")
}
model foo {
id String @id
bar BigInt @default(0)
}
"#;
let sql = expect![[r#"
-- CreateTable
CREATE TABLE "foo" (
"id" TEXT NOT NULL,
"bar" BIGINT NOT NULL DEFAULT 0,
CONSTRAINT "foo_pkey" PRIMARY KEY ("id")
);
"#]];
api.expect_sql_for_schema(schema, &sql);

api.schema_push(schema).send().assert_green();
api.schema_push(schema).send().assert_green().assert_no_steps();
}
26 changes: 26 additions & 0 deletions migration-engine/migration-engine-tests/tests/migrations/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,29 @@ fn treat_nullable_integer_primary_key_as_required(api: TestApi) {

api.schema_push_w_datasource(dm).send().assert_green().assert_no_steps();
}

#[test_connector(tags(Sqlite))]
fn bigint_defaults_work(api: TestApi) {
let schema = r#"
datasource mypg {
provider = "sqlite"
url = env("TEST_DATABASE_URL")
}
model foo {
id String @id
bar BigInt @default(0)
}
"#;
let sql = expect![[r#"
-- CreateTable
CREATE TABLE "foo" (
"id" TEXT NOT NULL PRIMARY KEY,
"bar" BIGINT NOT NULL DEFAULT 0
);
"#]];
api.expect_sql_for_schema(schema, &sql);

api.schema_push(schema).send().assert_green();
api.schema_push(schema).send().assert_green().assert_no_steps();
}

0 comments on commit 0a3a2b6

Please sign in to comment.