mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/pgtesting"
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/uptrace/bun"
|
|
"github.com/uptrace/bun/dialect/pgdialect"
|
|
"github.com/uptrace/bun/extra/bundebug"
|
|
)
|
|
|
|
func TestMigrations(t *testing.T) {
|
|
require.NoError(t, pgtesting.CreatePostgresServer())
|
|
t.Cleanup(func() {
|
|
require.NoError(t, pgtesting.DestroyPostgresServer())
|
|
})
|
|
|
|
migrator := NewMigrator()
|
|
migrator.RegisterMigrations(
|
|
Migration{
|
|
Up: func(tx bun.Tx) error {
|
|
_, err := tx.Exec(`CREATE TABLE "foo" (id varchar)`)
|
|
return err
|
|
},
|
|
},
|
|
)
|
|
|
|
db := pgtesting.NewPostgresDatabase(t)
|
|
sqlDB, err := sql.Open("pgx", db.ConnString())
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
_ = sqlDB.Close()
|
|
})
|
|
|
|
bunDB := bun.NewDB(sqlDB, pgdialect.New())
|
|
if testing.Verbose() {
|
|
bunDB.AddQueryHook(bundebug.NewQueryHook(
|
|
bundebug.WithVerbose(true),
|
|
bundebug.FromEnv("BUNDEBUG"),
|
|
))
|
|
}
|
|
|
|
require.NoError(t, migrator.Up(context.Background(), bunDB))
|
|
version, err := migrator.GetDBVersion(context.Background(), bunDB)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, 1, version)
|
|
}
|