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: re-introspection now preserves 'referentialIntegrity' policy #3337

Merged
merged 6 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ impl<'a> Datasource<'a> {
let shadow_database_url = psl_ds.shadow_database_url.as_ref().map(|(url, _)| Env::from(url));
let namespaces: Vec<Text<_>> = psl_ds.namespaces.iter().map(|(ns, _)| Text(ns.as_str())).collect();

let relation_mode = psl_ds.relation_mode.or(psl_ds.referential_integrity);

Self {
name: &psl_ds.name,
provider: Text(&psl_ds.provider),
url: Env::from(&psl_ds.url),
shadow_database_url,
relation_mode: psl_ds.relation_mode,
relation_mode,
documentation: psl_ds.documentation.as_deref().map(Documentation),
custom_properties: Default::default(),
namespaces: Array::from(namespaces),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod mssql;
mod mysql;
mod relation_mode;
mod sqlite;
mod vitess;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod mssql;
mod mysql;
mod postgres;
mod sqlite;
Original file line number Diff line number Diff line change
@@ -0,0 +1,361 @@
use indoc::indoc;
use introspection_engine_tests::test_api::*;

// referentialIntegrity="prisma" is renamed as relationMode="prisma", and @relations are preserved.
#[test_connector(tags(Mssql))]
async fn referential_integrity_prisma(api: &TestApi) -> TestResult {
let init = formatdoc! {r#"
CREATE TABLE [dbo].[Foo] (
[id] INT NOT NULL,
[bar_id] INT NOT NULL,
CONSTRAINT [Foo_pkey] PRIMARY KEY CLUSTERED ([id]),
CONSTRAINT [Foo_bar_id_key] UNIQUE NONCLUSTERED ([bar_id])
);

CREATE TABLE [dbo].[Bar] (
[id] INT NOT NULL,
CONSTRAINT [Bar_pkey] PRIMARY KEY CLUSTERED ([id])
);
"#};

api.raw_cmd(&init).await;

let input = indoc! {r#"
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
referentialIntegrity = "prisma"
}

model Foo {
id Int @id
bar Bar @relation(fields: [bar_id], references: [id])
bar_id Int @unique
}

model Bar {
id Int @id
foo Foo?
}
"#};

let expected = expect![[r#"
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
relationMode = "prisma"
}

model Foo {
id Int @id
bar Bar @relation(fields: [bar_id], references: [id])
bar_id Int @unique
}

model Bar {
id Int @id
foo Foo?
}
"#]];

let result = api.re_introspect_config(input).await?;
expected.assert_eq(&result);

Ok(())
}

// referentialIntegrity="foreignKeys" is renamed as relationMode="foreignKeys", and @relations are preserved but moved to the bottom.
#[test_connector(tags(Mssql))]
async fn referential_integrity_foreign_keys(api: &TestApi) -> TestResult {
let init = formatdoc! {r#"
CREATE TABLE [dbo].[Foo] (
[id] INT NOT NULL,
[bar_id] INT NOT NULL,
CONSTRAINT [Foo_pkey] PRIMARY KEY CLUSTERED ([id]),
CONSTRAINT [Foo_bar_id_key] UNIQUE NONCLUSTERED ([bar_id])
);

CREATE TABLE [dbo].[Bar] (
[id] INT NOT NULL,
CONSTRAINT [Bar_pkey] PRIMARY KEY CLUSTERED ([id])
);

ALTER TABLE [dbo].[Foo] ADD CONSTRAINT [Foo_bar_id_fkey] FOREIGN KEY ([bar_id]) REFERENCES [dbo].[Bar]([id]) ON DELETE NO ACTION ON UPDATE CASCADE;
"#};

api.raw_cmd(&init).await;

let input = indoc! {r#"
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
referentialIntegrity = "foreignKeys"
}

model Foo {
id Int @id
bar Bar @relation(fields: [bar_id], references: [id])
bar_id Int @unique
}

model Bar {
id Int @id
foo Foo?
}
"#};

let expected = expect![[r#"
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
relationMode = "foreignKeys"
}

model Foo {
id Int @id
bar_id Int @unique
bar Bar @relation(fields: [bar_id], references: [id])
}

model Bar {
id Int @id
foo Foo?
}
"#]];

let result = api.re_introspect_config(input).await?;
expected.assert_eq(&result);

Ok(())
}

// relationMode="prisma" preserves the relation policy ("prisma") as well as @relations.
#[test_connector(tags(Mssql))]
async fn relation_mode_prisma(api: &TestApi) -> TestResult {
let init = formatdoc! {r#"
CREATE TABLE [dbo].[Foo] (
[id] INT NOT NULL,
[bar_id] INT NOT NULL,
CONSTRAINT [Foo_pkey] PRIMARY KEY CLUSTERED ([id]),
CONSTRAINT [Foo_bar_id_key] UNIQUE NONCLUSTERED ([bar_id])
);

CREATE TABLE [dbo].[Bar] (
[id] INT NOT NULL,
CONSTRAINT [Bar_pkey] PRIMARY KEY CLUSTERED ([id])
);
"#};

api.raw_cmd(&init).await;

let input = indoc! {r#"
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
relationMode = "prisma"
}

model Foo {
id Int @id
bar Bar @relation(fields: [bar_id], references: [id])
bar_id Int @unique
}

model Bar {
id Int @id
foo Foo?
}
"#};

let expected = expect![[r#"
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
relationMode = "prisma"
}

model Foo {
id Int @id
bar Bar @relation(fields: [bar_id], references: [id])
bar_id Int @unique
}

model Bar {
id Int @id
foo Foo?
}
"#]];

let result = api.re_introspect_config(input).await?;
expected.assert_eq(&result);

Ok(())
}

// relationMode="foreignKeys" preserves the relation policy ("foreignKeys") as well as @relations, which are moved to the bottom.
#[test_connector(tags(Mssql))]
async fn relation_mode_foreign_keys(api: &TestApi) -> TestResult {
let init = formatdoc! {r#"
CREATE TABLE [dbo].[Foo] (
[id] INT NOT NULL,
[bar_id] INT NOT NULL,
CONSTRAINT [Foo_pkey] PRIMARY KEY CLUSTERED ([id]),
CONSTRAINT [Foo_bar_id_key] UNIQUE NONCLUSTERED ([bar_id])
);

CREATE TABLE [dbo].[Bar] (
[id] INT NOT NULL,
CONSTRAINT [Bar_pkey] PRIMARY KEY CLUSTERED ([id])
);

ALTER TABLE [dbo].[Foo] ADD CONSTRAINT [Foo_bar_id_fkey] FOREIGN KEY ([bar_id]) REFERENCES [dbo].[Bar]([id]) ON DELETE NO ACTION ON UPDATE CASCADE;
"#};

api.raw_cmd(&init).await;

let input = indoc! {r#"
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
relationMode = "foreignKeys"
}

model Foo {
id Int @id
bar Bar @relation(fields: [bar_id], references: [id])
bar_id Int @unique
}

model Bar {
id Int @id
foo Foo?
}
"#};

let expected = expect![[r#"
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
relationMode = "foreignKeys"
}

model Foo {
id Int @id
bar_id Int @unique
bar Bar @relation(fields: [bar_id], references: [id])
}

model Bar {
id Int @id
foo Foo?
}
"#]];

let result = api.re_introspect_config(input).await?;
expected.assert_eq(&result);

Ok(())
}

// @relations are moved to the bottom of the model even when no referentialIntegrity/relationMode is used.
#[test_connector(tags(Mssql))]
async fn no_relation_mode(api: &TestApi) -> TestResult {
jkomyno marked this conversation as resolved.
Show resolved Hide resolved
let init = formatdoc! {r#"
CREATE TABLE [dbo].[Foo] (
[id] INT NOT NULL,
[bar_id] INT NOT NULL,
CONSTRAINT [Foo_pkey] PRIMARY KEY CLUSTERED ([id]),
CONSTRAINT [Foo_bar_id_key] UNIQUE NONCLUSTERED ([bar_id])
);

CREATE TABLE [dbo].[Bar] (
[id] INT NOT NULL,
CONSTRAINT [Bar_pkey] PRIMARY KEY CLUSTERED ([id])
);

ALTER TABLE [dbo].[Foo] ADD CONSTRAINT [Foo_bar_id_fkey] FOREIGN KEY ([bar_id]) REFERENCES [dbo].[Bar]([id]) ON DELETE NO ACTION ON UPDATE CASCADE;
"#};

api.raw_cmd(&init).await;

let input = indoc! {r#"
datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
}

model Foo {
id Int @id
bar Bar @relation(fields: [bar_id], references: [id])
bar_id Int @unique
}

model Bar {
id Int @id
foo Foo?
}
"#};

let expected = expect![[r#"
datasource db {
provider = "sqlserver"
url = env("TEST_DATABASE_URL")
}

model Foo {
id Int @id
bar_id Int @unique
bar Bar @relation(fields: [bar_id], references: [id])
}

model Bar {
id Int @id
foo Foo?
}
"#]];

let result = api.re_introspect_config(input).await?;
expected.assert_eq(&result);

Ok(())
}