fix: move implementation to "internal" package
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
This commit is contained in:
48
internal/db/migration.go
Normal file
48
internal/db/migration.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"spend-sparrow/internal/log"
|
||||
"spend-sparrow/internal/types"
|
||||
|
||||
"errors"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type migrationLogger struct{}
|
||||
|
||||
func (l migrationLogger) Printf(format string, v ...interface{}) {
|
||||
log.Info(format, v...)
|
||||
}
|
||||
func (l migrationLogger) Verbose() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func RunMigrations(db *sqlx.DB, pathPrefix string) error {
|
||||
driver, err := sqlite3.WithInstance(db.DB, &sqlite3.Config{})
|
||||
if err != nil {
|
||||
log.Error("Could not create Migration instance: %v", err)
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
m, err := migrate.NewWithDatabaseInstance(
|
||||
"file://"+pathPrefix+"migration/",
|
||||
"",
|
||||
driver)
|
||||
if err != nil {
|
||||
log.Error("Could not create migrations instance: %v", err)
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
m.Log = migrationLogger{}
|
||||
|
||||
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
log.Error("Could not run migrations: %v", err)
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user