@@ -844,12 +844,12 @@ func (a *setReplicaIdentityAction) Execute(ctx context.Context) error {
844844
845845type alterReferencesAction struct {
846846 conn db.DB
847- referencedBy schema.ReferencedBy
847+ referencedBy map [ string ][] * schema.ReferencedBy
848848 table string
849849 column string
850850}
851851
852- func NewAlterReferencesAction (conn db.DB , referencedBy schema.ReferencedBy , table , column string ) * alterReferencesAction {
852+ func NewAlterReferencesAction (conn db.DB , referencedBy map [ string ][] * 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 schema.ReferencedBy, tabl
859859}
860860
861861func (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}
0 commit comments