mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 10:33:11 -06:00
Use postgres table to determine if the migration was already executed
This commit is contained in:
parent
caf7ebb1a1
commit
d2b3eac3af
|
|
@ -17,8 +17,8 @@ package postgres
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/lib/pq"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal"
|
"github.com/matrix-org/dendrite/internal"
|
||||||
|
|
@ -85,9 +85,17 @@ func executeMigration(ctx context.Context, db *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var partition int
|
err = db.QueryRowContext(ctx, "select column_name from information_schema.columns where table_name = 'keyserver_key_changes' AND column_name = 'partition'").Err()
|
||||||
err = db.QueryRowContext(ctx, "SELECT partition FROM keyserver_key_changes LIMIT 1;").Scan(&partition)
|
if err != nil {
|
||||||
if err == nil {
|
if errors.Is(err, sql.ErrNoRows) { // migration was already executed, as the column was removed
|
||||||
|
if err = sqlutil.InsertMigration(ctx, db, migrationName); err != nil {
|
||||||
|
// not a fatal error, log and continue
|
||||||
|
logrus.WithError(err).Warnf("unable to manually insert migration '%s'", migrationName)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
m := sqlutil.NewMigrator(db)
|
m := sqlutil.NewMigrator(db)
|
||||||
m.AddMigrations(sqlutil.Migration{
|
m.AddMigrations(sqlutil.Migration{
|
||||||
Version: migrationName,
|
Version: migrationName,
|
||||||
|
|
@ -96,21 +104,7 @@ func executeMigration(ctx context.Context, db *sql.DB) error {
|
||||||
if err = m.Up(ctx); err != nil {
|
if err = m.Up(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
switch e := err.(type) {
|
|
||||||
case *pq.Error:
|
|
||||||
// ignore undefined_column (42703) errors, as this is expected at this point
|
|
||||||
if e.Code != "42703" {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = sqlutil.InsertMigration(ctx, db, migrationName); err != nil {
|
|
||||||
// not a fatal error, log and continue
|
|
||||||
logrus.WithError(err).Warnf("unable to manually insert migration '%s'", migrationName)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,9 @@ package postgres
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/lib/pq"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
// Import the postgres database driver.
|
// Import the postgres database driver.
|
||||||
|
|
@ -83,9 +83,17 @@ func executeMigration(ctx context.Context, db *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var eventNID int
|
err = db.QueryRowContext(ctx, "select column_name from information_schema.columns where table_name = 'roomserver_state_block' AND column_name = 'event_nid'").Err()
|
||||||
err = db.QueryRowContext(ctx, "SELECT event_nid FROM roomserver_state_block LIMIT 1;").Scan(&eventNID)
|
if err != nil {
|
||||||
if err == nil {
|
if errors.Is(err, sql.ErrNoRows) { // migration was already executed, as the column was removed
|
||||||
|
if err = sqlutil.InsertMigration(ctx, db, migrationName); err != nil {
|
||||||
|
// not a fatal error, log and continue
|
||||||
|
logrus.WithError(err).Warnf("unable to manually insert migration '%s'", migrationName)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
m := sqlutil.NewMigrator(db)
|
m := sqlutil.NewMigrator(db)
|
||||||
m.AddMigrations(sqlutil.Migration{
|
m.AddMigrations(sqlutil.Migration{
|
||||||
Version: migrationName,
|
Version: migrationName,
|
||||||
|
|
@ -94,21 +102,6 @@ func executeMigration(ctx context.Context, db *sql.DB) error {
|
||||||
if err = m.Up(ctx); err != nil {
|
if err = m.Up(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
switch e := err.(type) {
|
|
||||||
case *pq.Error:
|
|
||||||
// ignore undefined_column (42703) errors, as this is expected at this point
|
|
||||||
if e.Code != "42703" {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = sqlutil.InsertMigration(ctx, db, migrationName); err != nil {
|
|
||||||
// not a fatal error, log and continue
|
|
||||||
logrus.WithError(err).Warnf("unable to manually insert migration '%s'", migrationName)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue