Add checks for specific migrations

This commit is contained in:
Till Faelligen 2022-03-09 15:37:23 +01:00
parent 8dc71378d1
commit 673e0f601b
5 changed files with 55 additions and 36 deletions

View file

@ -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 );")

View file

@ -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) {

View file

@ -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) {

View file

@ -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{
{

View file

@ -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{
{