summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrail Finder (aider) <wohilas@gmail.com>2025-04-05 13:54:17 +0300
committerGrail Finder (aider) <wohilas@gmail.com>2025-04-05 13:54:17 +0300
commit9039360835e38a6de809332eb785ede124d71dea (patch)
tree6ef95b24753f3db14581f20d05022825a14dc530
parentc6c6b7983ab188fecce855b32eaf4e4849bfae33 (diff)
fix: properly initialize and run database migrations
-rw-r--r--internal/database/migrations/init.go25
-rw-r--r--internal/database/sql/main.go10
2 files changed, 26 insertions, 9 deletions
diff --git a/internal/database/migrations/init.go b/internal/database/migrations/init.go
index 2b5a212..754a097 100644
--- a/internal/database/migrations/init.go
+++ b/internal/database/migrations/init.go
@@ -1,3 +1,28 @@
package migrations
+import (
+ "log/slog"
+
+ migrate "github.com/rubenv/sql-migrate"
+)
+
//go:generate go-bindata -o ./migrations.bindata.go -pkg migrations -ignore=\\*.go ./...
+
+func GetMigrationSource() *migrate.AssetMigrationSource {
+ return &migrate.AssetMigrationSource{
+ Asset: Asset,
+ AssetDir: AssetDir,
+ Dir: ".",
+ }
+}
+
+func RunMigrations(db migrate.Execer, driver string) (int, error) {
+ source := GetMigrationSource()
+ n, err := migrate.Exec(db, driver, source, migrate.Up)
+ if err != nil {
+ slog.Error("failed to run migrations", "error", err)
+ return 0, err
+ }
+ slog.Info("applied database migrations", "count", n)
+ return n, nil
+}
diff --git a/internal/database/sql/main.go b/internal/database/sql/main.go
index 1a29157..9534e58 100644
--- a/internal/database/sql/main.go
+++ b/internal/database/sql/main.go
@@ -59,17 +59,9 @@ func Init(dbPath string) (*DB, error) {
}
// Run migrations
- migrations := &migrate.AssetMigrationSource{
- Asset: migrations.Asset,
- AssetDir: migrations.AssetDir,
- Dir: "migrations",
- }
-
- n, err := migrate.Exec(result.Conn.DB, dbDriver, migrations, migrate.Up)
- if err != nil {
+ if _, err := migrations.RunMigrations(result.Conn.DB, dbDriver); err != nil {
return nil, errors.Wrap(err, "failed to run migrations")
}
- log.Info("applied database migrations", "count", n)
return &result, nil
}