MCPcopy Create free account
hub / github.com/Permify/permify / Migrate

Function Migrate

internal/storage/migration.go:28–80  ·  view source on GitHub ↗

Migrate performs database migrations depending on the given configuration.

(conf config.Database)

Source from the content-addressed store, hash-verified

26
27// Migrate performs database migrations depending on the given configuration.
28func Migrate(conf config.Database) (err error) {
29 switch conf.Engine {
30 case database.POSTGRES.String():
31 // Create a new Postgres database connection
32 var db *PQDatabase.Postgres
33
34 if conf.URI == "" {
35 db, err = PQDatabase.NewWithSeparateURIs(conf.Writer.URI, conf.Reader.URI)
36 if err != nil {
37 return err
38 }
39 } else {
40 db, err = PQDatabase.New(conf.URI)
41 if err != nil {
42 return err
43 }
44 }
45
46 // Ensure database connection is closed when function returns
47 defer closeDB(db)
48 // Verify postgres version compatibility
49 // Check postgres version compatibility with the database
50 _, err = utils.EnsureDBVersion(db.ReadPool)
51 if err != nil { // Version check failed
52 return err // Return version error
53 }
54 // Set table name for migrations
55 goose.SetTableName(migrationsTable)
56
57 // Set dialect to be used for migration
58 if err = goose.SetDialect(postgresDialect); err != nil {
59 return err
60 }
61
62 // Set file system for migration scripts
63 goose.SetBaseFS(postgresMigrations)
64
65 pool := stdlib.OpenDBFromPool(db.WritePool)
66
67 // Perform migration
68 if err = goose.Up(pool, postgresMigrationDir); err != nil {
69 return err
70 }
71
72 return nil
73 case database.MEMORY.String():
74 // No migrations needed for in-memory database
75 return nil
76 default:
77 // Unsupported database engine
78 return fmt.Errorf("%s connection is unsupported", conf.Engine)
79 }
80}
81
82// MigrateUp performs all available database migrations to update the schema to the latest version.
83func MigrateUp(engine, uri string) (err error) {

Callers 2

serveFunction · 0.92
PostgresDBFunction · 0.92

Calls 3

EnsureDBVersionFunction · 0.92
closeDBFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected