Skip to content

Commit c38fe7b

Browse files
FGoesslerjacek-prisma
authored andcommitted
[TML-1610] adjust behavior for default namespace to avoid differences during migrate diff (#5699)
This PR addresses prisma/prisma#28240. When creating the db schema for a schema file we do NOT include the `CREATE SCHEMA public ...`  statement. But when creating it from the migrations (aka shadow db) it is included. Due to this when creating a diff between the two as in migrate diff an actual diff in the db schema is observed. This bug only shows up when using `migrate diff` with the `--exit-only` or `--script` options because of a missing implementation for schemas in the drift summary. This PR fixes the issue by changing the way how we determine the `explicit_namespace` for rendering SQL statements (see #5614) and ensuring that the default namespace is always part of the namespace list - no matter how the db schema is derived.
1 parent 2ba551f commit c38fe7b

16 files changed

Lines changed: 113 additions & 10 deletions

File tree

schema-engine/connectors/sql-schema-connector/src/sql_migration.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl SqlMigration {
4646
AlteredExtension,
4747
DroppedExtension,
4848
CreatedExtension,
49+
AddedSchema,
4950
AddedEnum,
5051
AddedTable,
5152
RemovedEnum,
@@ -64,7 +65,9 @@ impl SqlMigration {
6465
let idx = idx as u32;
6566
match step {
6667
SqlMigrationStep::AlterSequence(_, _) => (),
67-
SqlMigrationStep::CreateSchema(_) => (), // todo
68+
SqlMigrationStep::CreateSchema(_) => {
69+
drift_items.insert((DriftType::AddedSchema, "", idx));
70+
}
6871
SqlMigrationStep::DropView(drop_view) => {
6972
drift_items.insert((
7073
DriftType::RemovedView,
@@ -185,6 +188,9 @@ impl SqlMigration {
185188
for (line_idx, (new_state, item_name, step_idx)) in drift_items.iter().enumerate() {
186189
if render_state != (*new_state, item_name) || line_idx == 0 {
187190
match new_state {
191+
DriftType::AddedSchema => {
192+
out.push_str("\n[+] Added Schemas\n");
193+
}
188194
DriftType::AddedEnum => {
189195
out.push_str("\n[+] Added enums\n");
190196
}
@@ -235,7 +241,11 @@ impl SqlMigration {
235241
out.push_str(self.schemas().next.walk(*enum_id).name());
236242
out.push('\n');
237243
}
238-
SqlMigrationStep::CreateSchema(_) => {} // todo
244+
SqlMigrationStep::CreateSchema(namespace) => {
245+
out.push_str(" - ");
246+
out.push_str(self.schemas().next.walk(*namespace).name());
247+
out.push('\n');
248+
}
239249
SqlMigrationStep::AlterEnum(alter_enum) => {
240250
for added in &alter_enum.created_variants {
241251
out.push_str(" [+] Added variant `");

schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ fn push_namespaces<'a>(ctx: &mut Context<'a>, default_namespace: Option<&'a str>
6161
}
6262

6363
if let Some(default_namespace) = default_namespace {
64+
if ctx.schemas.is_empty() {
65+
ctx.schemas.insert(
66+
default_namespace,
67+
ctx.schema
68+
.describer_schema
69+
.push_namespace(default_namespace.to_string()),
70+
);
71+
}
72+
6473
ctx.schema
6574
.describer_schema
6675
.set_default_namespace(default_namespace.to_owned());

schema-engine/sql-migration-tests/tests/migrations/drift_summary.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,46 @@ fn empty_schemas() {
6262
)
6363
}
6464

65+
#[test]
66+
fn additions_schemas() {
67+
check(
68+
"postgres",
69+
r#"
70+
datasource db {
71+
provider = "postgres"
72+
}
73+
"#,
74+
r#"
75+
datasource db {
76+
provider = "postgres"
77+
schemas = ["one", "two"]
78+
}
79+
80+
model Cat {
81+
id Int @id
82+
83+
@@schema("one")
84+
}
85+
86+
model Dog {
87+
id Int @id
88+
89+
@@schema("two")
90+
}
91+
"#,
92+
expect![[r#"
93+
94+
[+] Added Schemas
95+
- one
96+
- two
97+
98+
[+] Added tables
99+
- Cat
100+
- Dog
101+
"#]],
102+
);
103+
}
104+
65105
#[test]
66106
fn additions_table() {
67107
check(

schema-engine/sql-migration-tests/tests/migrations/enums.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ fn mapped_enum_defaults_must_work(api: TestApi) {
597597
"#;
598598

599599
let expect = expect![[r#"
600+
-- CreateSchema
601+
CREATE SCHEMA IF NOT EXISTS "public";
602+
600603
-- CreateEnum
601604
CREATE TYPE "Color" AS ENUM ('0', 'Grün', 'Blu', 'pfuh 🙄...');
602605

schema-engine/sql-migration-tests/tests/migrations/mssql.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ fn bigint_defaults_work(api: TestApi) {
316316
317317
BEGIN TRAN;
318318
319+
-- CreateSchema
320+
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'dbo') EXEC sp_executesql N'CREATE SCHEMA [dbo];';
321+
319322
-- CreateTable
320323
CREATE TABLE [dbo].[foo] (
321324
[id] NVARCHAR(1000) NOT NULL,
@@ -362,6 +365,9 @@ fn float_columns(api: TestApi) {
362365
363366
BEGIN TRAN;
364367
368+
-- CreateSchema
369+
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'dbo') EXEC sp_executesql N'CREATE SCHEMA [dbo];';
370+
365371
-- CreateTable
366372
CREATE TABLE [dbo].[foo] (
367373
[id] NVARCHAR(1000) NOT NULL,

schema-engine/sql-migration-tests/tests/migrations/postgres.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ fn scalar_list_defaults_work(api: TestApi) {
537537
api.schema_push(schema).send().assert_green().assert_no_steps();
538538

539539
let expected_sql = expect![[r#"
540+
-- CreateSchema
541+
CREATE SCHEMA IF NOT EXISTS "public";
542+
540543
-- CreateEnum
541544
CREATE TYPE "Color" AS ENUM ('RED', 'GREEN', 'BLUE');
542545
@@ -677,6 +680,9 @@ fn json_defaults_with_escaped_quotes_work(api: TestApi) {
677680
api.schema_push(schema).send().assert_green().assert_no_steps();
678681

679682
let sql = expect![[r#"
683+
-- CreateSchema
684+
CREATE SCHEMA IF NOT EXISTS "public";
685+
680686
-- CreateTable
681687
CREATE TABLE "Foo" (
682688
"id" INTEGER NOT NULL,
@@ -703,6 +709,9 @@ fn bigint_defaults_work(api: TestApi) {
703709
}
704710
"#;
705711
let sql = expect![[r#"
712+
-- CreateSchema
713+
CREATE SCHEMA IF NOT EXISTS "public";
714+
706715
-- CreateTable
707716
CREATE TABLE "foo" (
708717
"id" TEXT NOT NULL,

schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_dbgenerated.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ model table {
1212
}
1313

1414
// Expected Migration:
15+
// -- CreateSchema
16+
// CREATE SCHEMA IF NOT EXISTS "public";
17+
//
1518
// -- CreateTable
1619
// CREATE TABLE "table" (
1720
// "id" TEXT NOT NULL,

schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_unsupported_dbgenerated.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ model table {
1212
}
1313

1414
// Expected Migration:
15+
// -- CreateSchema
16+
// CREATE SCHEMA IF NOT EXISTS "public";
17+
//
1518
// -- CreateTable
1619
// CREATE TABLE "table" (
1720
// "id" TEXT NOT NULL,

schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/enums/enum_basic.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ enum MyEnum {
1717
}
1818

1919
// Expected Migration:
20+
// -- CreateSchema
21+
// CREATE SCHEMA IF NOT EXISTS "public";
22+
//
2023
// -- CreateEnum
2124
// CREATE TYPE "MyEnum" AS ENUM ('A', 'B');
2225
//

schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/enums/enum_fields.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ enum Kind {
3131
}
3232

3333
// Expected Migration:
34+
// -- CreateSchema
35+
// CREATE SCHEMA IF NOT EXISTS "public";
36+
//
3437
// -- CreateEnum
3538
// CREATE TYPE "Result" AS ENUM ('succeeded', 'failed');
3639
//

0 commit comments

Comments
 (0)