mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-10 15:43:09 -06:00
Use sqlite_master to determine if migration was already executed
This commit is contained in:
parent
d2b3eac3af
commit
637119be92
|
|
@ -17,9 +17,8 @@ package sqlite3
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"strings"
|
"errors"
|
||||||
|
|
||||||
"github.com/mattn/go-sqlite3"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal"
|
"github.com/matrix-org/dendrite/internal"
|
||||||
|
|
@ -85,35 +84,23 @@ func executeMigration(ctx context.Context, db *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var partition int
|
err = db.QueryRowContext(ctx, `SELECT p.name FROM sqlite_master AS m JOIN pragma_table_info(m.name) AS p WHERE m.name = 'keyserver_key_changes' AND p.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
|
||||||
m := sqlutil.NewMigrator(db)
|
|
||||||
m.AddMigrations(sqlutil.Migration{
|
|
||||||
Version: migrationName,
|
|
||||||
Up: deltas.UpRefactorKeyChanges,
|
|
||||||
})
|
|
||||||
if err = m.Up(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
switch e := err.(type) {
|
|
||||||
case *sqlite3.Error:
|
|
||||||
// ignore "no such column" errors, as this is expected at this point
|
|
||||||
if !strings.Contains(e.Error(), "no such column") {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset the error to nil, so the deferred function will insert the migration
|
|
||||||
if err = sqlutil.InsertMigration(ctx, db, migrationName); err != nil {
|
if err = sqlutil.InsertMigration(ctx, db, migrationName); err != nil {
|
||||||
// not a fatal error, log and continue
|
// not a fatal error, log and continue
|
||||||
logrus.WithError(err).Warnf("unable to manually insert migration '%s'", migrationName)
|
logrus.WithError(err).Warnf("unable to manually insert migration '%s'", migrationName)
|
||||||
}
|
}
|
||||||
default:
|
return nil
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
m := sqlutil.NewMigrator(db)
|
||||||
|
m.AddMigrations(sqlutil.Migration{
|
||||||
|
Version: migrationName,
|
||||||
|
Up: deltas.UpRefactorKeyChanges,
|
||||||
|
})
|
||||||
|
return m.Up(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *keyChangesStatements) Prepare() (err error) {
|
func (s *keyChangesStatements) Prepare() (err error) {
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,10 @@ package sqlite3
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/mattn/go-sqlite3"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/caching"
|
"github.com/matrix-org/dendrite/internal/caching"
|
||||||
|
|
@ -92,35 +91,23 @@ func executeMigration(ctx context.Context, db *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var eventNID int
|
err = db.QueryRowContext(ctx, `SELECT p.name FROM sqlite_master AS m JOIN pragma_table_info(m.name) AS p WHERE m.name = 'roomserver_state_block' AND p.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
|
||||||
m := sqlutil.NewMigrator(db)
|
|
||||||
m.AddMigrations(sqlutil.Migration{
|
|
||||||
Version: migrationName,
|
|
||||||
Up: deltas.UpStateBlocksRefactor,
|
|
||||||
})
|
|
||||||
if err = m.Up(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
switch e := err.(type) {
|
|
||||||
case *sqlite3.Error:
|
|
||||||
// ignore "no such column" errors, as this is expected at this point
|
|
||||||
if !strings.Contains(e.Error(), "no such column") {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset the error to nil, so the deferred function will insert the migration
|
|
||||||
if err = sqlutil.InsertMigration(ctx, db, migrationName); err != nil {
|
if err = sqlutil.InsertMigration(ctx, db, migrationName); err != nil {
|
||||||
// not a fatal error, log and continue
|
// not a fatal error, log and continue
|
||||||
logrus.WithError(err).Warnf("unable to manually insert migration '%s'", migrationName)
|
logrus.WithError(err).Warnf("unable to manually insert migration '%s'", migrationName)
|
||||||
}
|
}
|
||||||
default:
|
return nil
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
m := sqlutil.NewMigrator(db)
|
||||||
|
m.AddMigrations(sqlutil.Migration{
|
||||||
|
Version: migrationName,
|
||||||
|
Up: deltas.UpStateBlocksRefactor,
|
||||||
|
})
|
||||||
|
return m.Up(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) create(db *sql.DB) error {
|
func (d *Database) create(db *sql.DB) error {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue