feat: add auto migration
Docker Build and Push / Build and Push Docker Image (push) Successful in 17m56s

This commit is contained in:
2026-02-23 21:08:33 +07:00
parent de821b2762
commit 39f71633ec
3 changed files with 97 additions and 51 deletions
+27 -1
View File
@@ -2,6 +2,8 @@ package main
import (
"context"
"embed"
"errors"
"log"
"os"
"os/signal"
@@ -11,9 +13,15 @@ import (
"ristek-task-be/internal/server"
"syscall"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jackc/pgx/v5/pgxpool"
)
//go:embed internal/db/sqlc/migrations/*.sql
var migrationFiles embed.FS
// @title Ristek Task API
// @version 1.0
// @description REST API for Ristek Task Backend
@@ -43,10 +51,28 @@ func main() {
connect, err := pgxpool.New(ctx, conf.DatabaseURL())
if err != nil {
panic(err)
log.Fatalf("failed to connect to database: %s", err)
return
}
defer connect.Close()
repo := repository.New(connect)
d, err := iofs.New(migrationFiles, "internal/db/sqlc/migrations")
if err != nil {
log.Fatalf("failed to create migration source: %s", err)
return
}
m, err := migrate.NewWithSourceInstance("iofs", d, conf.DatabaseURL())
if err != nil {
log.Fatalf("failed to initialize migrations: %s", err)
return
}
err = m.Up()
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
log.Fatal(err)
return
}
j := jwt.New("yomama")
go func() {
s := server.New(conf.Addr(), conf.Port(), repo, j)