-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_migration.go
57 lines (47 loc) · 1.7 KB
/
schema_migration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package dbmigration
import (
"go-graphql-mongo-server/config"
"go-graphql-mongo-server/logger"
"go-graphql-mongo-server/models"
"strings"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mongodb"
_ "github.com/golang-migrate/migrate/v4/source/file" // Import the file driver
)
// RunDbSchemaMigration runs the database schema migration
// Give the relative path prefix to the main package as input
// Eg: "." or "./" or "", "./.." or "./../" or "../" or "..", "./../.." etc.
func RunDbSchemaMigration(relativePathPrefixToMainDir string) error {
dbName := config.Store.Database.Name
mongoDriver, err := mongodb.WithInstance(models.GetDbSession(), &mongodb.Config{
DatabaseName: dbName,
MigrationsCollection: models.SchemaMigrationCollection,
TransactionMode: false,
})
if err != nil {
logger.Log.Error("Error creating mongo driver: " + err.Error())
return err
}
if len(relativePathPrefixToMainDir) > 0 && !strings.HasSuffix(relativePathPrefixToMainDir, "/") {
relativePathPrefixToMainDir = relativePathPrefixToMainDir + "/"
}
migrationPath := "file://" + relativePathPrefixToMainDir + "resources/schema_migrations"
// Read migrations from resources/schema_migrations and connect to database
migrateInstance, err := migrate.NewWithDatabaseInstance(
migrationPath,
dbName,
mongoDriver,
)
if err != nil {
logger.Log.Error("Error reading migration files: " + err.Error())
return err
}
// Run migrations all the way up
err = migrateInstance.Up()
if err != nil && err != migrate.ErrNoChange {
logger.Log.Error("Error running migrations: " + err.Error())
return err
}
logger.Log.Info("Successfully ran schema migration")
return nil
}