From c2b4f01f6cded5cdf3456a8d8e0dc3aa365aedc0 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Mon, 27 Nov 2017 14:06:17 +0000 Subject: [PATCH] Add helper function to run migrations --- .../matrix-org/dendrite/common/sql.go | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/github.com/matrix-org/dendrite/common/sql.go b/src/github.com/matrix-org/dendrite/common/sql.go index f3de66f11..e82f46173 100644 --- a/src/github.com/matrix-org/dendrite/common/sql.go +++ b/src/github.com/matrix-org/dendrite/common/sql.go @@ -16,6 +16,15 @@ package common import ( "database/sql" + "fmt" + + // Import the postgres database driver for migrate + _ "github.com/lib/pq" + + "github.com/mattes/migrate" + "github.com/mattes/migrate/database/postgres" + // Import the file source driver for migrate + _ "github.com/mattes/migrate/source/file" ) // A Transaction is something that can be committed or rolledback. @@ -66,3 +75,38 @@ func TxStmt(transaction *sql.Tx, statement *sql.Stmt) *sql.Stmt { } return statement } + +// DoMigrations runs any new schema migrations. +// The db param is just a connection to the postgres database, while `prefix` +// is the prefix of the tables. The prefix is used to 1) namespace the +// internal schema migrations table and 2) as the directory in `db-schema` +// where the migration files can be found. +func DoMigrations(db *sql.DB, prefix string) (err error) { + tableName := fmt.Sprintf("%s_schema_migrations", prefix) + + driver, err := postgres.WithInstance(db, &postgres.Config{ + MigrationsTable: tableName, + }) + if err != nil { + return err + } + + // We explicitly don't use path.Join here as it doesn't like the `file://` + // part of it + source := fmt.Sprintf("file://db-schema/%s", prefix) + migrater, err := migrate.NewWithDatabaseInstance( + source, "postgres", driver, + ) + if err != nil { + return err + } + + // TODO: Handle stops gracefully via migrater.GracefulStop <- true + + err = migrater.Up() + if err != nil && err != migrate.ErrNoChange { + return err + } + + return nil +}