From 673e0f601b6b814e3bb7a9a489a6ca2c2fd39c05 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Wed, 9 Mar 2022 15:37:23 +0100 Subject: [PATCH] Add checks for specific migrations --- internal/sqlutil/migrate.go | 16 +++++++++++++- .../storage/postgres/key_changes_table.go | 22 +++++++++++++------ .../storage/sqlite3/key_changes_table.go | 22 +++++++++++++------ roomserver/storage/postgres/storage.go | 15 +++++-------- roomserver/storage/sqlite3/storage.go | 16 +++++--------- 5 files changed, 55 insertions(+), 36 deletions(-) diff --git a/internal/sqlutil/migrate.go b/internal/sqlutil/migrate.go index 732543a66..62d0e4272 100644 --- a/internal/sqlutil/migrate.go +++ b/internal/sqlutil/migrate.go @@ -1,3 +1,17 @@ +// Copyright 2022 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 sqlutil import ( @@ -92,7 +106,7 @@ func (m *Migrator) Up(ctx context.Context) error { return nil } -// executedMigrations returns a map with already executed migrations +// ExecutedMigrations returns a map with already executed migrations func (m *Migrator) ExecutedMigrations(ctx context.Context) (map[string]bool, error) { result := make(map[string]bool) _, err := m.db.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS db_migrations ( version TEXT, time TEXT );") diff --git a/keyserver/storage/postgres/key_changes_table.go b/keyserver/storage/postgres/key_changes_table.go index ed46205c6..9a69657b7 100644 --- a/keyserver/storage/postgres/key_changes_table.go +++ b/keyserver/storage/postgres/key_changes_table.go @@ -60,13 +60,21 @@ func NewPostgresKeyChangesTable(db *sql.DB) (tables.KeyChanges, error) { if err != nil { return s, err } - m := sqlutil.NewMigrator(db) - m.AddMigration(sqlutil.Migration{ - Version: "refactor key changes", - Up: deltas.UpRefactorKeyChanges, - }) - err = m.Up(context.Background()) - return s, err + + // TODO: Remove when we are sure we are not having goose artifacts in the db + // This forces an error, which indicates the migration is already applied, since the + // column partition was removed from the table + var count int + err = db.QueryRow("SELECT partition FROM keyserver_key_changes LIMIT 1;").Scan(&count) + if err == nil { + m := sqlutil.NewMigrator(db) + m.AddMigration(sqlutil.Migration{ + Version: "refactor key changes", + Up: deltas.UpRefactorKeyChanges, + }) + return s, m.Up(context.Background()) + } + return s, nil } func (s *keyChangesStatements) Prepare() (err error) { diff --git a/keyserver/storage/sqlite3/key_changes_table.go b/keyserver/storage/sqlite3/key_changes_table.go index c77938cc8..3a0e87b17 100644 --- a/keyserver/storage/sqlite3/key_changes_table.go +++ b/keyserver/storage/sqlite3/key_changes_table.go @@ -58,13 +58,21 @@ func NewSqliteKeyChangesTable(db *sql.DB) (tables.KeyChanges, error) { if err != nil { return s, err } - m := sqlutil.NewMigrator(db) - m.AddMigration(sqlutil.Migration{ - Version: "refactor key changes", - Up: deltas.UpRefactorKeyChanges, - }) - err = m.Up(context.Background()) - return s, err + // TODO: Remove when we are sure we are not having goose artifacts in the db + // This forces an error, which indicates the migration is already applied, since the + // column partition was removed from the table + var count int + err = db.QueryRow("SELECT partition FROM keyserver_key_changes LIMIT 1;").Scan(&count) + if err == nil { + m := sqlutil.NewMigrator(db) + m.AddMigration(sqlutil.Migration{ + Version: "refactor key changes", + Up: deltas.UpRefactorKeyChanges, + }) + return s, m.Up(context.Background()) + } + + return s, nil } func (s *keyChangesStatements) Prepare() (err error) { diff --git a/roomserver/storage/postgres/storage.go b/roomserver/storage/postgres/storage.go index a98088b6c..235886c9d 100644 --- a/roomserver/storage/postgres/storage.go +++ b/roomserver/storage/postgres/storage.go @@ -19,7 +19,6 @@ import ( "context" "database/sql" "fmt" - "strings" // Import the postgres database driver. _ "github.com/lib/pq" @@ -52,15 +51,11 @@ func Open(dbProperties *config.DatabaseOptions, cache caching.RoomServerCaches) // Special case, since this migration uses several tables, so it needs to // be sure that all tables are created first. // TODO: Remove when we are sure we are not having goose artifacts in the db - row := db.QueryRow("SELECT COUNT(*) FROM goose_db_version WHERE version_id = '2021041615092700';") - var gooseCount int - if err := row.Scan(&gooseCount); err != nil { - if !strings.Contains(err.Error(), "does not exist") { - return nil, fmt.Errorf("unable to get goose_db_version: %w", err) - } - } - // Migration not yet applied - if gooseCount == 0 { + // This forces an error, which indicates the migration is already applied, since the + // column event_nid was removed from the table + var count int + err = db.QueryRow("SELECT event_nid FROM roomserver_state_block LIMIT 1;").Scan(&count) + if err == nil { m := sqlutil.NewMigrator(db) m.AddMigrations([]sqlutil.Migration{ { diff --git a/roomserver/storage/sqlite3/storage.go b/roomserver/storage/sqlite3/storage.go index 5196804dd..fb5a34c92 100644 --- a/roomserver/storage/sqlite3/storage.go +++ b/roomserver/storage/sqlite3/storage.go @@ -18,8 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "fmt" - "strings" "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -61,15 +59,11 @@ func Open(dbProperties *config.DatabaseOptions, cache caching.RoomServerCaches) // Special case, since this migration uses several tables, so it needs to // be sure that all tables are created first. // TODO: Remove when we are sure we are not having goose artifacts in the db - row := db.QueryRow("SELECT COUNT(*) FROM goose_db_version WHERE version_id = '2021041615092700';") - var gooseCount int - if err := row.Scan(&gooseCount); err != nil { - if !strings.Contains(err.Error(), "no such table") { - return nil, fmt.Errorf("unable to get goose_db_version: %w", err) - } - } - // Migration not yet applied - if gooseCount == 0 { + // This forces an error, which indicates the migration is already applied, since the + // column event_nid was removed from the table + var count int + err = db.QueryRow("SELECT event_nid FROM roomserver_state_block LIMIT 1;").Scan(&count) + if err == nil { m := sqlutil.NewMigrator(db) m.AddMigrations([]sqlutil.Migration{ {