Skip to content

Commit

Permalink
fix(introspection, sqlserver): Accept (and ignore) multi-line shared …
Browse files Browse the repository at this point in the history
…defaults (#4884)

* fix(introspection, sqlserver): Accept (and ignore) multi-line shared defaults
  • Loading branch information
janpio authored May 24, 2024
1 parent f742678 commit 417c14e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- tags=mssql2017

CREATE TABLE a (
id INT IDENTITY,
savings INT,
CONSTRAINT [A_pkey] PRIMARY KEY (id)
);

EXEC('/* This is a comment */' +
'CREATE DEFAULT NEARLY_NOTHING AS 0');
EXEC('sp_bindefault ''NEARLY_NOTHING'', ''a.savings''');
/*
generator js {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
model a {
id Int @id(map: "A_pkey") @default(autoincrement())
savings Int?
}
*/
37 changes: 37 additions & 0 deletions schema-engine/sql-migration-tests/tests/migrations/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@ fn shared_default_constraints_are_ignored_issue_5423(api: TestApi) {
.assert_no_steps();
}

#[test_connector(tags(Mssql))]
fn shared_default_constraints_with_multilines_are_ignored_issue_24275(api: TestApi) {
let schema = api.schema_name();

api.raw_cmd(&format!(
r#"
/* This is a comment */
CREATE DEFAULT [{schema}].dogdog AS 'mugi'
"#
));

api.raw_cmd(&format!(
r#"
CREATE TABLE [{schema}].dogs (
id INT IDENTITY,
name NVARCHAR(255) NOT NULL,
CONSTRAINT [dogs_pkey] PRIMARY KEY CLUSTERED ([id] ASC)
)
"#
));

api.raw_cmd(&format!("sp_bindefault '{schema}.dogdog', '{schema}.dogs.name'"));

let dm = r#"
model dogs {
id Int @id @default(autoincrement())
name String @db.NVarChar(255)
}
"#;

api.schema_push_w_datasource(dm)
.migration_id(Some("first"))
.send()
.assert_green()
.assert_no_steps();
}

#[test_connector(tags(Mssql))]
fn mssql_apply_migrations_error_output(api: TestApi) {
let dm = "";
Expand Down
2 changes: 1 addition & 1 deletion schema-engine/sql-schema-describer/src/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static DEFAULT_DB_GEN: Lazy<Regex> = Lazy::new(|| Regex::new(r"\((.*)\)").unwrap
/// ```ignore
/// CREATE DEFAULT catcat AS 'musti';
/// ```
static DEFAULT_SHARED_CONSTRAINT: Lazy<Regex> = Lazy::new(|| Regex::new(r"^CREATE DEFAULT (.*)").unwrap());
static DEFAULT_SHARED_CONSTRAINT: Lazy<Regex> = Lazy::new(|| Regex::new(r"CREATE DEFAULT (.*)").unwrap());

pub struct SqlSchemaDescriber<'a> {
conn: &'a dyn Queryable,
Expand Down

0 comments on commit 417c14e

Please sign in to comment.