Skip to content

Commit 5067e17

Browse files
committed
tmp
1 parent 7643f97 commit 5067e17

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

pkg/migrations/dbactions.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,12 @@ func (a *setReplicaIdentityAction) Execute(ctx context.Context) error {
844844

845845
type alterReferencesAction struct {
846846
conn db.DB
847-
referencedBy map[string][]*schema.ReferencedBy
847+
referencedBy schema.ReferencedBy
848848
table string
849849
column string
850850
}
851851

852-
func NewAlterReferencesAction(conn db.DB, referencedBy map[string][]*schema.ReferencedBy, table, column string) *alterReferencesAction {
852+
func NewAlterReferencesAction(conn db.DB, referencedBy schema.ReferencedBy, table, column string) *alterReferencesAction {
853853
return &alterReferencesAction{
854854
conn: conn,
855855
referencedBy: referencedBy,
@@ -859,30 +859,30 @@ func NewAlterReferencesAction(conn db.DB, referencedBy map[string][]*schema.Refe
859859
}
860860

861861
func (a *alterReferencesAction) Execute(ctx context.Context) error {
862-
for table, constraints := range a.referencedBy {
863-
for _, constraint := range constraints {
864-
// Drop the existing constraint
865-
_, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s DROP CONSTRAINT %s",
866-
pq.QuoteIdentifier(table),
867-
pq.QuoteIdentifier(constraint.Name),
868-
))
869-
if err != nil {
870-
return fmt.Errorf("dropping constraint %s on %s: %w", constraint.Name, table, err)
871-
}
872-
873-
// Recreate the constraint with the table and new column
874-
newDef := strings.ReplaceAll(constraint.Definition, a.column, pq.QuoteIdentifier(TemporaryName(a.column)))
875-
newDef = strings.ReplaceAll(newDef, a.table, pq.QuoteIdentifier(a.table))
876-
_, err = a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s %s",
877-
pq.QuoteIdentifier(table),
878-
pq.QuoteIdentifier(constraint.Name),
879-
newDef,
880-
))
881-
if err != nil {
882-
return fmt.Errorf("altering references for %s.%s: %w", a.table, a.column, err)
883-
}
884-
885-
}
886-
}
862+
//for table, constraints := range a.referencedBy {
863+
// for _, constraint := range constraints {
864+
// // Drop the existing constraint
865+
// _, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s DROP CONSTRAINT %s",
866+
// pq.QuoteIdentifier(table),
867+
// pq.QuoteIdentifier(constraint.Name),
868+
// ))
869+
// if err != nil {
870+
// return fmt.Errorf("dropping constraint %s on %s: %w", constraint.Name, table, err)
871+
// }
872+
873+
// // Recreate the constraint with the table and new column
874+
// newDef := strings.ReplaceAll(constraint.Definition, a.column, pq.QuoteIdentifier(TemporaryName(a.column)))
875+
// newDef = strings.ReplaceAll(newDef, a.table, pq.QuoteIdentifier(a.table))
876+
// _, err = a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s %s",
877+
// pq.QuoteIdentifier(table),
878+
// pq.QuoteIdentifier(constraint.Name),
879+
// newDef,
880+
// ))
881+
// if err != nil {
882+
// return fmt.Errorf("altering references for %s.%s: %w", a.table, a.column, err)
883+
// }
884+
885+
// }
886+
//}
887887
return nil
888888
}

pkg/schema/schema.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ type Table struct {
6565
// ReferencedBy is a map of table names that reference this table by foreign key
6666
// The key is the name of the referencing table, and the value is the name and
6767
// the definition of the foreign key constraint
68-
ReferencedBy map[string][]*ReferencedBy `json:"referencedBy"`
68+
//ReferencedBy map[string][]*ReferencedBy `json:"referencedBy"`
69+
ReferencedBy ReferencedBy `json:"referencedBy"`
6970

7071
// Whether or not the table has been deleted in the virtual schema
7172
Deleted bool `json:"-"`

pkg/state/init.sql

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,20 @@ BEGIN
346346
AND fk_constraint.contype = 'f' GROUP BY fk_constraint.conrelid, fk_constraint.conname, fk_constraint.confrelid, fk_cl.relname, fk_constraint.confkey, fk_constraint.confmatchtype, fk_constraint.confdeltype, fk_constraint.confupdtype) AS fk_info
347347
INNER JOIN pg_attribute ref_attr ON ref_attr.attrelid = fk_info.confrelid
348348
AND ref_attr.attnum = ANY (fk_info.confkey) -- join the columns of the referenced table
349-
GROUP BY fk_info.conname, fk_info.conrelid, fk_info.columns, fk_info.confrelid, fk_info.confmatchtype, fk_info.confdeltype, fk_info.confupdtype, fk_info.relname) AS fk_details)))), '{}'::json)
349+
GROUP BY fk_info.conname, fk_info.conrelid, fk_info.columns, fk_info.confrelid, fk_info.confmatchtype, fk_info.confdeltype, fk_info.confupdtype, fk_info.relname) AS fk_details), 'referencedBy', (
350+
SELECT
351+
json_object_agg(ref_fk.conname, json_build_object('name', ref_fk.conname, 'table', ref_fk.relname, 'definition', ref_fk.definition))
352+
FROM (
353+
SELECT
354+
ref_constraint.conname,
355+
ref_cl.relname,
356+
pg_get_constraintdef(ref_constraint.oid) AS definition
357+
FROM pg_constraint AS ref_constraint
358+
INNER JOIN pg_class ref_cl ON ref_constraint.conrelid = ref_cl.oid
359+
WHERE
360+
ref_constraint.confrelid = t.oid
361+
AND ref_constraint.contype = 'f'
362+
) AS ref_fk)))), '{}'::json)
350363
FROM pg_class AS t
351364
INNER JOIN pg_namespace AS ns ON t.relnamespace = ns.oid
352365
LEFT JOIN pg_description AS descr ON t.oid = descr.objoid

0 commit comments

Comments
 (0)