Add migration to rename dupe index

This commit is contained in:
Till Faelligen 2023-02-01 15:43:02 +01:00
parent 1af5c7e69a
commit fc74946246
No known key found for this signature in database
GPG key ID: ACCDC9606D472758
2 changed files with 48 additions and 0 deletions

View file

@ -0,0 +1,29 @@
// Copyright 2023 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deltas
import (
"context"
"database/sql"
"fmt"
)
func UpRenameOutputRoomEventsIndex(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `ALTER TABLE syncapi_output_room_events RENAME CONSTRAINT syncapi_event_id_idx TO syncapi_output_room_event_id_idx;`)
if err != nil {
return fmt.Errorf("failed to execute upgrade: %w", err)
}
return nil
}

View file

@ -19,6 +19,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"sort" "sort"
"github.com/lib/pq" "github.com/lib/pq"
@ -223,12 +224,30 @@ func NewPostgresEventsTable(db *sql.DB) (tables.Events, error) {
return nil, err return nil, err
} }
migrationName := "syncapi: rename dupe index (output_room_events)"
var cName string
err = db.QueryRowContext(context.Background(), "select constraint_name from information_schema.table_constraints where table_name = 'syncapi_output_room_events' AND constraint_name = 'syncapi_event_id_idx'").Scan(&cName)
switch err {
case sql.ErrNoRows: // migration was already executed, as the index was renamed
if err = sqlutil.InsertMigration(context.Background(), db, migrationName); err != nil {
return nil, fmt.Errorf("unable to manually insert migration '%s': %w", migrationName, err)
}
case nil:
default:
return nil, err
}
m := sqlutil.NewMigrator(db) m := sqlutil.NewMigrator(db)
m.AddMigrations( m.AddMigrations(
sqlutil.Migration{ sqlutil.Migration{
Version: "syncapi: add history visibility column (output_room_events)", Version: "syncapi: add history visibility column (output_room_events)",
Up: deltas.UpAddHistoryVisibilityColumnOutputRoomEvents, Up: deltas.UpAddHistoryVisibilityColumnOutputRoomEvents,
}, },
sqlutil.Migration{
Version: migrationName,
Up: deltas.UpRenameOutputRoomEventsIndex,
},
) )
err = m.Up(context.Background()) err = m.Up(context.Background())
if err != nil { if err != nil {