2021-02-23 14:53:00 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-18 16:34:02 +00:00
|
|
|
|
|
|
|
"github.com/c9s/rockhopper/v2"
|
2021-02-23 14:53:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func prepareDB(t *testing.T) (*rockhopper.DB, error) {
|
2024-01-18 16:34:02 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-02-23 14:53:00 +00:00
|
|
|
dialect, err := rockhopper.LoadDialect("sqlite3")
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotNil(t, dialect)
|
|
|
|
|
2024-01-18 16:34:02 +00:00
|
|
|
db, err := rockhopper.Open("sqlite3", dialect, ":memory:", rockhopper.TableName)
|
2021-02-23 14:53:00 +00:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotNil(t, db)
|
|
|
|
|
2024-01-18 16:34:02 +00:00
|
|
|
err = db.Touch(ctx)
|
2021-02-23 14:53:00 +00:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-18 16:34:02 +00:00
|
|
|
var loader = &rockhopper.SqlMigrationLoader{}
|
|
|
|
|
2021-02-23 14:53:00 +00:00
|
|
|
migrations, err := loader.Load("../../migrations/sqlite3")
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-18 16:34:02 +00:00
|
|
|
migrations = migrations.Sort().Connect()
|
2021-02-23 14:53:00 +00:00
|
|
|
assert.NotEmpty(t, migrations)
|
|
|
|
|
2024-01-18 16:34:02 +00:00
|
|
|
err = rockhopper.Up(ctx, db, migrations.Head(), 0)
|
2022-06-01 07:51:32 +00:00
|
|
|
assert.NoError(t, err, "should migrate successfully")
|
2021-02-23 14:53:00 +00:00
|
|
|
|
|
|
|
return db, err
|
|
|
|
}
|