mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 20:05:17 +03:00
feat: create migrator
This commit is contained in:
+23
-1
@@ -1,10 +1,13 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
standartlog "log"
|
||||
"net"
|
||||
|
||||
hubdir "github.com/lorsanstand/HomeOps-Hub/internal/hub"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/hub/migrator"
|
||||
grpcserv "github.com/lorsanstand/HomeOps-Hub/internal/hub/rpc"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/hub/service/hub_service"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/shared/config"
|
||||
@@ -29,9 +32,28 @@ func NewApp() *App {
|
||||
}
|
||||
|
||||
func (a *App) Run() {
|
||||
err := a.hubServe()
|
||||
migratePGConn, err := sql.Open("pgx", a.cfg.GetURLPostgres())
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msg("failed to connect to the database")
|
||||
return
|
||||
}
|
||||
defer migratePGConn.Close()
|
||||
|
||||
mgrt, err := migrator.NewMigrator(hubdir.MigrationsFS, "migrations")
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msg("failed create migrator")
|
||||
return
|
||||
}
|
||||
|
||||
if err = mgrt.ApplyMigrations(migratePGConn); err != nil {
|
||||
a.log.Error().Err(err).Msg("migrations were not applied")
|
||||
}
|
||||
migratePGConn.Close()
|
||||
|
||||
err = a.hubServe()
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msg("failed to start the server")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user