Skip to content

Commit ef10477

Browse files
committed
feat: support cross schema references
1 parent e12ca11 commit ef10477

2 files changed

Lines changed: 114 additions & 112 deletions

File tree

  • libs/test-setup/src
  • schema-engine/sql-introspection-tests/tests/multi_schema

libs/test-setup/src/mysql.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ pub async fn drop_mysql_namespaces(database_url: &str, namespaces: &[&str]) -> R
123123
for namespace in namespaces {
124124
let db_name = mysql_safe_identifier(namespace);
125125
let drop = format!(
126-
r#"
127-
DROP DATABASE IF EXISTS `{db_name}`;
126+
r#"
127+
SET FOREIGN_KEY_CHECKS=0;
128+
DROP DATABASE IF EXISTS `{db_name}`;
129+
SET FOREIGN_KEY_CHECKS=1;
128130
"#,
129131
);
130132
conn.raw_cmd(&drop).await?;

schema-engine/sql-introspection-tests/tests/multi_schema/mysql.rs

Lines changed: 110 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -258,146 +258,146 @@ async fn multiple_schemas_w_duplicate_sanitized_table_names_are_introspected(api
258258
Ok(())
259259
}
260260

261-
// #[test_connector(tags(Mysql), preview_features("multiSchema"), namespaces("first", "second"))]
262-
// async fn multiple_schemas_w_cross_schema_are_introspected(api: &mut TestApi) -> TestResult {
263-
// let schema_name = "first";
264-
// let other_name = "second";
265-
// let create_schema = format!("CREATE Schema `{schema_name}`",);
266-
// let create_table = format!("CREATE TABLE `{schema_name}`.`A` (id Text PRIMARY KEY)",);
267-
// //Todo
268-
// api.database().raw_cmd(&create_schema).await?;
269-
// api.database().raw_cmd(&create_table).await?;
261+
#[test_connector(tags(Mysql), preview_features("multiSchema"), namespaces("first", "second"))]
262+
async fn multiple_schemas_w_cross_schema_are_introspected(api: &mut TestApi) -> TestResult {
263+
let schema_name = "first";
264+
let other_name = "second";
265+
let create_schema = format!("CREATE Schema `{schema_name}`",);
266+
let create_table = format!("CREATE TABLE `{schema_name}`.`A` (id INT PRIMARY KEY)",);
267+
//Todo
268+
api.database().raw_cmd(&create_schema).await?;
269+
api.database().raw_cmd(&create_table).await?;
270270

271-
// let create_schema = format!("CREATE Schema `{other_name}`",);
272-
// let create_table = format!(
273-
// "CREATE TABLE `{other_name}`.`B` (id Text PRIMARY KEY, fk Text References `{schema_name}`.`A`(`id`))",
274-
// );
271+
let create_schema = format!("CREATE Schema `{other_name}`",);
272+
let create_table =
273+
format!("CREATE TABLE `{other_name}`.`B` (id INT PRIMARY KEY, fk INT, FOREIGN KEY (fk) REFERENCES `{schema_name}`.`A`(`id`))",);
275274

276-
// api.database().raw_cmd(&create_schema).await?;
277-
// api.database().raw_cmd(&create_table).await?;
275+
api.database().raw_cmd(&create_schema).await?;
276+
api.database().raw_cmd(&create_table).await?;
278277

279-
// let expected = expect![[r#"
280-
// model A {
281-
// id String @id
282-
// B B[]
278+
let expected = expect![[r#"
279+
model A {
280+
id Int @id
281+
B B[]
283282
284-
// @@schema("first")
285-
// }
283+
@@schema("first")
284+
}
286285
287-
// model B {
288-
// id String @id
289-
// fk String?
290-
// A A? @relation(fields: [fk], references: [id], onDelete: NoAction, onUpdate: NoAction)
286+
model B {
287+
id Int @id
288+
fk Int?
289+
A A? @relation(fields: [fk], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "B_ibfk_1")
291290
292-
// @@schema("second")
293-
// }
294-
// "#]];
291+
@@index([fk], map: "fk")
292+
@@schema("second")
293+
}
294+
"#]];
295295

296-
// let result = api.introspect_dml().await?;
297-
// expected.assert_eq(&result);
296+
let result = api.introspect_dml().await?;
297+
expected.assert_eq(&result);
298298

299-
// Ok(())
300-
// }
299+
Ok(())
300+
}
301301

302-
// #[test_connector(tags(Mysql), preview_features("multiSchema"), namespaces("first", "second"))]
303-
// async fn multiple_schemas_w_cross_schema_are_reintrospected(api: &mut TestApi) -> TestResult {
304-
// let schema_name = "first";
305-
// let other_name = "second";
306-
// let create_schema = format!("CREATE Schema `{schema_name}`",);
307-
// let create_table = format!("CREATE TABLE `{schema_name}`.`A` (id Text PRIMARY KEY)",);
308-
// //Todo
309-
// api.database().raw_cmd(&create_schema).await?;
310-
// api.database().raw_cmd(&create_table).await?;
302+
#[test_connector(tags(Mysql), preview_features("multiSchema"), namespaces("first", "second"))]
303+
async fn multiple_schemas_w_cross_schema_are_reintrospected(api: &mut TestApi) -> TestResult {
304+
let schema_name = "first";
305+
let other_name = "second";
306+
let create_schema = format!("CREATE Schema `{schema_name}`",);
307+
let create_table = format!("CREATE TABLE `{schema_name}`.`A` (id Int PRIMARY KEY)",);
308+
//Todo
309+
api.database().raw_cmd(&create_schema).await?;
310+
api.database().raw_cmd(&create_table).await?;
311311

312-
// let create_schema = format!("CREATE Schema `{other_name}`",);
313-
// let create_table = format!(
314-
// "CREATE TABLE `{other_name}`.`B` (id Text PRIMARY KEY, fk Text References `{schema_name}`.`A`(`id`))",
315-
// );
312+
let create_schema = format!("CREATE Schema `{other_name}`",);
313+
let create_table =
314+
format!("CREATE TABLE `{other_name}`.`B` (id Int PRIMARY KEY, fk Int, FOREIGN KEY (fk) REFERENCES `{schema_name}`.`A`(`id`))",);
316315

317-
// api.database().raw_cmd(&create_schema).await?;
318-
// api.database().raw_cmd(&create_table).await?;
316+
api.database().raw_cmd(&create_schema).await?;
317+
api.database().raw_cmd(&create_table).await?;
319318

320-
// let input = indoc! {r#"
321-
// model A {
322-
// id String @id
323-
// B B[]
319+
let input = indoc! {r#"
320+
model A {
321+
id Int @id
322+
B B[]
324323
325-
// @@schema("first")
326-
// }
324+
@@schema("first")
325+
}
327326
328-
// model B {
329-
// id String @id
330-
// fk String?
331-
// A A? @relation(fields: [fk], references: [id], onDelete: NoAction, onUpdate: NoAction)
327+
model B {
328+
id Int @id
329+
fk Int?
330+
A A? @relation(fields: [fk], references: [id], onDelete: NoAction, onUpdate: NoAction)
332331
333-
// @@schema("first")
334-
// }
335-
// "#};
332+
@@schema("first")
333+
}
334+
"#};
336335

337-
// let expected = expect![[r#"
338-
// model A {
339-
// id String @id
340-
// B B[]
336+
let expected = expect![[r#"
337+
model A {
338+
id Int @id
339+
B B[]
341340
342-
// @@schema("first")
343-
// }
341+
@@schema("first")
342+
}
344343
345-
// model B {
346-
// id String @id
347-
// fk String?
348-
// A A? @relation(fields: [fk], references: [id], onDelete: NoAction, onUpdate: NoAction)
344+
model B {
345+
id Int @id
346+
fk Int?
347+
A A? @relation(fields: [fk], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "B_ibfk_1")
349348
350-
// @@schema("second")
351-
// }
352-
// "#]];
349+
@@index([fk], map: "fk")
350+
@@schema("second")
351+
}
352+
"#]];
353353

354-
// api.expect_re_introspected_datamodel(input, expected).await;
354+
api.expect_re_introspected_datamodel(input, expected).await;
355355

356-
// Ok(())
357-
// }
356+
Ok(())
357+
}
358358

359-
// #[test_connector(tags(Mysql), preview_features("multiSchema"), namespaces("first", "second"))]
360-
// async fn multiple_schemas_w_cross_schema_fks_w_duplicate_names_are_introspected(api: &mut TestApi) -> TestResult {
361-
// let schema_name = "first";
362-
// let other_name = "second";
363-
// let create_schema = format!("CREATE SCHEMA `{schema_name}`",);
364-
// let create_table = format!("CREATE TABLE `{schema_name}`.`A` (id Text PRIMARY KEY)",);
365-
// //Todo
366-
// api.database().raw_cmd(&create_schema).await?;
367-
// api.database().raw_cmd(&create_table).await?;
359+
#[test_connector(tags(Mysql), preview_features("multiSchema"), namespaces("first", "second"))]
360+
async fn multiple_schemas_w_cross_schema_fks_w_duplicate_names_are_introspected(api: &mut TestApi) -> TestResult {
361+
let schema_name = "first";
362+
let other_name = "second";
363+
let create_schema = format!("CREATE SCHEMA `{schema_name}`",);
364+
let create_table = format!("CREATE TABLE `{schema_name}`.`A` (id INT PRIMARY KEY)",);
365+
//Todo
366+
api.database().raw_cmd(&create_schema).await?;
367+
api.database().raw_cmd(&create_table).await?;
368368

369-
// let create_schema = format!("CREATE SCHEMA `{other_name}`",);
370-
// let create_table = format!(
371-
// "CREATE TABLE `{other_name}`.`A` (id TEXT PRIMARY KEY, fk TEXT REFERENCES `{schema_name}`.`A`(`id`))",
372-
// );
369+
let create_schema = format!("CREATE SCHEMA `{other_name}`",);
370+
let create_table =
371+
format!("CREATE TABLE `{other_name}`.`A` (id INT PRIMARY KEY, fk INT, FOREIGN KEY (fk) REFERENCES `{schema_name}`.`A`(`id`))",);
373372

374-
// api.database().raw_cmd(&create_schema).await?;
375-
// api.database().raw_cmd(&create_table).await?;
373+
api.database().raw_cmd(&create_schema).await?;
374+
api.database().raw_cmd(&create_table).await?;
376375

377-
// let expected = expect![[r#"
378-
// model first_A {
379-
// id String @id
380-
// A second_A[]
376+
let expected = expect![[r#"
377+
model first_A {
378+
id Int @id
379+
A second_A[]
381380
382-
// @@map("A")
383-
// @@schema("first")
384-
// }
381+
@@map("A")
382+
@@schema("first")
383+
}
385384
386-
// model second_A {
387-
// id String @id
388-
// fk String?
389-
// A first_A? @relation(fields: [fk], references: [id], onDelete: NoAction, onUpdate: NoAction)
385+
model second_A {
386+
id Int @id
387+
fk Int?
388+
A first_A? @relation(fields: [fk], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "A_ibfk_1")
390389
391-
// @@map("A")
392-
// @@schema("second")
393-
// }
394-
// "#]];
390+
@@index([fk], map: "fk")
391+
@@map("A")
392+
@@schema("second")
393+
}
394+
"#]];
395395

396-
// let result = api.introspect_dml().await?;
397-
// expected.assert_eq(&result);
396+
let result = api.introspect_dml().await?;
397+
expected.assert_eq(&result);
398398

399-
// Ok(())
400-
// }
399+
Ok(())
400+
}
401401

402402
// #[test_connector(tags(Mysql), preview_features("multiSchema"), namespaces("first", "second_schema"))]
403403
// async fn multiple_schemas_w_enums_are_introspected(api: &mut TestApi) -> TestResult {

0 commit comments

Comments
 (0)