Skip to content

Commit 4aba4f4

Browse files
committed
Record whether the constraint is a foreign key or not
1 parent ce6f00f commit 4aba4f4

9 files changed

+122
-82
lines changed

crates/syn2mas/.sqlx/query-12112011318abc0bdd7f722ed8c5d4a86bf5758f8c32d9d41a22999b2f0698ca.json

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/syn2mas/.sqlx/query-575807a26f5caba0a71894f2e3fe4646e23dda898f563fa8cc91d26f8a9d6676.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/syn2mas/.sqlx/query-5b4840f42ae00c5dc9f59f2745d664b16ebd813dfa0aa32a6d39dd5c393af299.json

Lines changed: 0 additions & 34 deletions
This file was deleted.

crates/syn2mas/.sqlx/query-612a5a08d67f9beda39237680fa61c67d4df1936ac5afe6445cdc377a56dd628.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/syn2mas/src/mas_writer/constraint_pausing.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct ConstraintDescription {
1515
pub name: String,
1616
pub table_name: String,
1717
pub definition: String,
18+
pub is_fk: bool,
1819
}
1920

2021
pub struct IndexDescription {
@@ -31,14 +32,21 @@ pub async fn describe_constraints_on_table(
3132
sqlx::query_as!(
3233
ConstraintDescription,
3334
r#"
34-
SELECT conrelid::regclass::text AS "table_name!", conname AS "name!", pg_get_constraintdef(c.oid) AS "definition!"
35+
SELECT
36+
conrelid::regclass::text AS "table_name!",
37+
conname AS "name!",
38+
pg_get_constraintdef(c.oid) AS "definition!",
39+
CASE WHEN contype = 'f' THEN true ELSE false END AS "is_fk!"
3540
FROM pg_constraint c
3641
JOIN pg_namespace n ON n.oid = c.connamespace
3742
WHERE contype IN ('f', 'p', 'u') AND conrelid::regclass::text = $1
3843
AND n.nspname = current_schema;
3944
"#,
4045
table_name
41-
).fetch_all(&mut *conn).await.into_database_with(|| format!("could not read constraint definitions of {table_name}"))
46+
)
47+
.fetch_all(&mut *conn)
48+
.await
49+
.into_database_with(|| format!("could not read constraint definitions of {table_name}"))
4250
}
4351

4452
/// Look up and return the definitions of foreign-key constraints whose
@@ -50,14 +58,23 @@ pub async fn describe_foreign_key_constraints_to_table(
5058
sqlx::query_as!(
5159
ConstraintDescription,
5260
r#"
53-
SELECT conrelid::regclass::text AS "table_name!", conname AS "name!", pg_get_constraintdef(c.oid) AS "definition!"
61+
SELECT
62+
conrelid::regclass::text AS "table_name!",
63+
conname AS "name!",
64+
pg_get_constraintdef(c.oid) AS "definition!",
65+
true AS "is_fk!"
5466
FROM pg_constraint c
5567
JOIN pg_namespace n ON n.oid = c.connamespace
5668
WHERE contype = 'f' AND confrelid::regclass::text = $1
5769
AND n.nspname = current_schema;
5870
"#,
5971
target_table_name
60-
).fetch_all(&mut *conn).await.into_database_with(|| format!("could not read FK constraint definitions targetting {target_table_name}"))
72+
)
73+
.fetch_all(&mut *conn)
74+
.await
75+
.into_database_with(|| {
76+
format!("could not read FK constraint definitions targetting {target_table_name}")
77+
})
6178
}
6279

6380
/// Look up and return the definitions of all indices on a given table.
@@ -122,6 +139,7 @@ pub async fn restore_constraint(
122139
name,
123140
table_name,
124141
definition,
142+
is_fk: _,
125143
} = &constraint;
126144
info!("rebuilding constraint {name}");
127145

crates/syn2mas/src/mas_writer/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ impl<'conn> MasWriter<'conn> {
424424
.into_database("failed to get syn2mas restore data (index descriptions)")?;
425425
constraints_to_restore = query_as!(
426426
ConstraintDescription,
427-
"SELECT table_name, name, definition FROM syn2mas_restore_constraints ORDER BY order_key"
427+
"SELECT table_name, name, definition, is_fk FROM syn2mas_restore_constraints ORDER BY order_key"
428428
)
429429
.fetch_all(conn.as_mut())
430430
.await
@@ -468,16 +468,18 @@ impl<'conn> MasWriter<'conn> {
468468
name,
469469
table_name,
470470
definition,
471+
is_fk,
471472
} in &constraints_to_restore
472473
{
473474
query!(
474475
r#"
475-
INSERT INTO syn2mas_restore_constraints (name, table_name, definition)
476-
VALUES ($1, $2, $3)
476+
INSERT INTO syn2mas_restore_constraints (name, table_name, definition, is_fk)
477+
VALUES ($1, $2, $3, $4)
477478
"#,
478479
name,
479480
table_name,
480-
definition
481+
definition,
482+
is_fk
481483
)
482484
.execute(conn.as_mut())
483485
.await

crates/syn2mas/src/mas_writer/syn2mas_temporary_tables.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ CREATE TABLE syn2mas_restore_constraints (
2323

2424
table_name TEXT NOT NULL,
2525
name TEXT NOT NULL,
26-
definition TEXT NOT NULL
26+
definition TEXT NOT NULL,
27+
is_fk BOOLEAN NOT NULL
2728
);
2829

2930
-- corresponds to `IndexDescription`

0 commit comments

Comments
 (0)