mirror of
https://github.com/matrix-org/dendrite.git
synced 2024-11-26 16:21:55 -06:00
Drop constraint if exists
This commit is contained in:
parent
792b4142df
commit
8cac948d7e
|
@ -45,19 +45,25 @@ func UpServerNames(ctx context.Context, tx *sql.Tx, serverName gomatrixserverlib
|
||||||
return fmt.Errorf("add server name to %q error: %w", table, err)
|
return fmt.Errorf("add server name to %q error: %w", table, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Commit at this point, because it's possible that altering indices will
|
||||||
|
// fail and force us to roll back.
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
return fmt.Errorf("can't commit after column addition: %w", err)
|
||||||
|
}
|
||||||
for newTable, oldTable := range serverNamesDropPK {
|
for newTable, oldTable := range serverNamesDropPK {
|
||||||
q := fmt.Sprintf(
|
q := fmt.Sprintf(
|
||||||
"ALTER TABLE IF EXISTS %s DROP CONSTRAINT %s;",
|
"ALTER TABLE IF EXISTS %s DROP CONSTRAINT IF EXISTS %s;",
|
||||||
pq.QuoteIdentifier(newTable), pq.QuoteIdentifier(newTable+"_pkey"),
|
pq.QuoteIdentifier(newTable), pq.QuoteIdentifier(newTable+"_pkey"),
|
||||||
)
|
)
|
||||||
if _, err := tx.ExecContext(ctx, q); err != nil {
|
if _, err := tx.ExecContext(ctx, q); err != nil {
|
||||||
q = fmt.Sprintf(
|
return fmt.Errorf("drop PK from %q error: %w", newTable, err)
|
||||||
"ALTER TABLE IF EXISTS %s DROP CONSTRAINT %s;",
|
}
|
||||||
pq.QuoteIdentifier(oldTable), pq.QuoteIdentifier(oldTable+"_pkey"),
|
q = fmt.Sprintf(
|
||||||
)
|
"ALTER TABLE IF EXISTS %s DROP CONSTRAINT IF EXISTS %s;",
|
||||||
if _, err := tx.ExecContext(ctx, q); err != nil {
|
pq.QuoteIdentifier(oldTable), pq.QuoteIdentifier(oldTable+"_pkey"),
|
||||||
return fmt.Errorf("drop PK from %q / %q error: %w", newTable, oldTable, err)
|
)
|
||||||
}
|
if _, err := tx.ExecContext(ctx, q); err != nil {
|
||||||
|
return fmt.Errorf("drop PK from %q error: %w", oldTable, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue