Try to find old indices if necessary

This commit is contained in:
Neil Alexander 2022-11-10 10:49:25 +00:00
parent de21b48b06
commit 88dbe945f6
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -22,10 +22,12 @@ var serverNamesTables = []string{
// These tables have a PRIMARY KEY constraint which we need to drop so // These tables have a PRIMARY KEY constraint which we need to drop so
// that we can recreate a new unique index that contains the server name. // that we can recreate a new unique index that contains the server name.
var serverNamesDropPK = []string{ // If the new key doesn't exist (i.e. the database was created before the
"userapi_accounts", // table rename migration) we'll try to drop the old one instead.
"userapi_account_datas", var serverNamesDropPK = map[string]string{
"userapi_profiles", "userapi_accounts": "account_accounts",
"userapi_account_datas": "account_data",
"userapi_profiles": "account_profiles",
} }
// I know what you're thinking: you're wondering "why doesn't this use $1 // I know what you're thinking: you're wondering "why doesn't this use $1
@ -43,13 +45,19 @@ 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)
} }
} }
for _, table := 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 %s;",
pq.QuoteIdentifier(table), pq.QuoteIdentifier(table+"_pkey"), pq.QuoteIdentifier(newTable), pq.QuoteIdentifier(newTable+"_pkey"),
) )
if _, err := tx.ExecContext(ctx, q); err != nil { if _, err := tx.ExecContext(ctx, q); err != nil {
return fmt.Errorf("drop PK from %q error: %w", table, err) q = fmt.Sprintf(
"ALTER TABLE IF EXISTS %s DROP CONSTRAINT %s;",
pq.QuoteIdentifier(oldTable), pq.QuoteIdentifier(oldTable+"_pkey"),
)
if _, err := tx.ExecContext(ctx, q); err != nil {
return fmt.Errorf("drop PK from %q / %q error: %w", newTable, oldTable, err)
}
} }
} }
return nil return nil