upgrade from go-pg to bun

This commit is contained in:
dhax 2024-08-31 18:57:54 +02:00
parent f59f129354
commit 1886be62bc
23 changed files with 415 additions and 385 deletions

View file

@ -0,0 +1,49 @@
package migrations
import (
"context"
"embed"
"fmt"
"log"
"github.com/dhax/go-base/database"
"github.com/uptrace/bun/migrate"
)
//go:embed *.sql
var sqlMigrations embed.FS
var Migrations = migrate.NewMigrations()
func init() {
if err := Migrations.Discover(sqlMigrations); err != nil {
panic(err)
}
}
// Migrate runs all migrations
func Migrate() {
db, err := database.DBConn()
if err != nil {
log.Fatal(err)
}
defer db.Close()
migrator := migrate.NewMigrator(db, Migrations)
err = migrator.Init(context.Background())
if err != nil {
log.Fatal(err)
}
group, err := migrator.Migrate(context.Background())
if err != nil {
log.Fatal(err)
}
if group.ID == 0 {
fmt.Printf("there are no new migrations to run\n")
} else {
fmt.Printf("migrated to %s\n", group)
}
}