diff options
author | Grail Finder (aider) <wohilas@gmail.com> | 2025-04-05 12:51:25 +0300 |
---|---|---|
committer | Grail Finder (aider) <wohilas@gmail.com> | 2025-04-05 12:51:25 +0300 |
commit | 3e3349ff6a75185a353960955db032d5b16c71cc (patch) | |
tree | f37f93b7e14a80f1d493bdbbac1ced323a0a6e7b | |
parent | 55ed7d6bb605680a21e406a4084edb5df9f5eaec (diff) |
feat: add automatic database migrations on init
-rw-r--r-- | internal/database/sql/main.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/internal/database/sql/main.go b/internal/database/sql/main.go index 5a523f6..d149e35 100644 --- a/internal/database/sql/main.go +++ b/internal/database/sql/main.go @@ -9,6 +9,7 @@ import ( "github.com/jmoiron/sqlx" _ "github.com/mattn/go-sqlite3" "github.com/pkg/errors" + "github.com/rubenv/sql-migrate" ) var ( @@ -45,6 +46,20 @@ func Init(DBURI string) (*DB, error) { if err := testConnection(result.Conn); err != nil { return nil, err } + + // 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 { + return nil, errors.Wrap(err, "failed to run migrations") + } + log.Info("applied database migrations", "count", n) + return &result, nil } |