mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-10 15:43:09 -06:00
Add function to insert migration
This commit is contained in:
parent
2668050e53
commit
e87183e9f4
|
|
@ -21,8 +21,9 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
const createDBMigrationsSQL = "" +
|
const createDBMigrationsSQL = "" +
|
||||||
|
|
@ -95,11 +96,11 @@ func (m *Migrator) Up(ctx context.Context) error {
|
||||||
for i := range m.migrations {
|
for i := range m.migrations {
|
||||||
now := time.Now().UTC().Format(time.RFC3339)
|
now := time.Now().UTC().Format(time.RFC3339)
|
||||||
migration := m.migrations[i]
|
migration := m.migrations[i]
|
||||||
logrus.Debugf("Executing database migration '%s'", migration.Version)
|
|
||||||
// Skip migration if it was already executed
|
// Skip migration if it was already executed
|
||||||
if _, ok := executedMigrations[migration.Version]; ok {
|
if _, ok := executedMigrations[migration.Version]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
logrus.Debugf("Executing database migration '%s'", migration.Version)
|
||||||
err = migration.Up(ctx, txn)
|
err = migration.Up(ctx, txn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to execute migration '%s': %w", migration.Version, err)
|
return fmt.Errorf("unable to execute migration '%s': %w", migration.Version, err)
|
||||||
|
|
@ -140,3 +141,14 @@ func (m *Migrator) ExecutedMigrations(ctx context.Context) (map[string]struct{},
|
||||||
|
|
||||||
return result, rows.Err()
|
return result, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InsertMigration inserts a migration given there name to the database.
|
||||||
|
// This should only be used when manually inserting migrations.
|
||||||
|
func InsertMigration(ctx context.Context, db *sql.DB, migrationName string) error {
|
||||||
|
_, err := db.ExecContext(ctx, insertVersionSQL,
|
||||||
|
migrationName,
|
||||||
|
time.Now().Format(time.RFC3339),
|
||||||
|
internal.VersionString(),
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue