Skip to content

Commit

Permalink
fix(me): make @db.Float migrations idempotent on MSSQL
Browse files Browse the repository at this point in the history
We weren't properly handling the implicit precision: we sent FLOAT, got
back FLOAT(53), and interpreted that as different types, but they're the
same.

closes prisma/prisma#14052
  • Loading branch information
tomhoule committed Jul 14, 2022
1 parent 861e2df commit 4c78e13
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
Expand Up @@ -1138,9 +1138,13 @@ fn native_type_change_riskyness(previous: MsSqlType, next: MsSqlType) -> Option<
},
};

if previous == next {
None
} else {
Some(cast())
match (previous, next) {
(p, n) if p == n => None,
// https://docs.microsoft.com/en-us/sql/t-sql/data-types/float-and-real-transact-sql?view=sql-server-ver16#syntax
(MsSqlType::Float(Some(53)), MsSqlType::Float(None))
| (MsSqlType::Float(None), MsSqlType::Float(Some(53)))
| (MsSqlType::Float(Some(24)), MsSqlType::Real)
| (MsSqlType::Real, MsSqlType::Float(Some(24))) => None,
_ => Some(cast()),
}
}
48 changes: 48 additions & 0 deletions migration-engine/migration-engine-tests/tests/migrations/mssql.rs
Expand Up @@ -302,3 +302,51 @@ fn bigint_defaults_work(api: TestApi) {
api.schema_push(schema).send().assert_green();
api.schema_push(schema).send().assert_green().assert_no_steps();
}

#[test_connector(tags(Mssql))]
fn float_columns(api: TestApi) {
let schema = r#"
datasource mypg {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
}
model foo {
id String @id
bar Float @mypg.Float @default(0.90001)
baz Float? @mypg.Float
qux Float? @mypg.Real
}
"#;
let sql = expect![[r#"
BEGIN TRY
BEGIN TRAN;
-- CreateTable
CREATE TABLE [float_columns].[foo] (
[id] NVARCHAR(1000) NOT NULL,
[bar] FLOAT NOT NULL CONSTRAINT [foo_bar_df] DEFAULT 0.90001,
[baz] FLOAT,
[qux] REAL,
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();
}

0 comments on commit 4c78e13

Please sign in to comment.