This refactors the schema package and subpackages to support editions.
An Edition is a collection of sql migrations. A given boundary binary
can have multiple migration editions. The `schema.Manager` is updated to
apply the necessary migrations in the correct order, leveraging an
Edition's priority to determine the order in which the separate Editions
are applied.
The sql migration files are now nested under a directory for the
edition.
This also switches from the generated code to using embed, which
eliminates the need for the `make migrations` target and the generated
code. As such this commit removes the target and generated code.
See:
https://pkg.go.dev/embed
95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package oplog
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/internal/db/common"
|
|
"github.com/hashicorp/boundary/internal/db/schema"
|
|
"github.com/hashicorp/boundary/internal/oplog/oplog_test"
|
|
"github.com/hashicorp/boundary/testing/dbtest"
|
|
wrapping "github.com/hashicorp/go-kms-wrapping"
|
|
"github.com/hashicorp/go-kms-wrapping/wrappers/aead"
|
|
"github.com/hashicorp/go-uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func testCleanup(t *testing.T, cleanupFunc func() error, db *gorm.DB) {
|
|
t.Helper()
|
|
err := cleanupFunc()
|
|
assert.NoError(t, err)
|
|
sqlDB, err := db.DB()
|
|
assert.NoError(t, err)
|
|
err = sqlDB.Close()
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func testUser(t *testing.T, db *gorm.DB, name, phoneNumber, email string) *oplog_test.TestUser {
|
|
t.Helper()
|
|
u := &oplog_test.TestUser{
|
|
Name: name,
|
|
PhoneNumber: phoneNumber,
|
|
Email: email,
|
|
}
|
|
w := GormWriter{db}
|
|
|
|
err := w.Create(&u)
|
|
require.NoError(t, err)
|
|
return u
|
|
}
|
|
|
|
func testFindUser(t *testing.T, db *gorm.DB, userId uint32) *oplog_test.TestUser {
|
|
t.Helper()
|
|
var foundUser oplog_test.TestUser
|
|
err := db.Where("id = ?", userId).First(&foundUser).Error
|
|
require.NoError(t, err)
|
|
return &foundUser
|
|
}
|
|
|
|
func testId(t *testing.T) string {
|
|
t.Helper()
|
|
id, err := uuid.GenerateUUID()
|
|
require.NoError(t, err)
|
|
return id
|
|
}
|
|
|
|
func testInitDbInDocker(t *testing.T) (cleanup func() error, retURL string, err error) {
|
|
t.Helper()
|
|
|
|
cleanup, retURL, _, err = dbtest.StartUsingTemplate(dbtest.Postgres)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testInitStore(t, cleanup, retURL)
|
|
return
|
|
}
|
|
|
|
// testWrapper initializes an AEAD wrapping.Wrapper for testing the oplog
|
|
func testWrapper(t *testing.T) wrapping.Wrapper {
|
|
t.Helper()
|
|
rootKey := make([]byte, 32)
|
|
n, err := rand.Read(rootKey)
|
|
require.NoError(t, err)
|
|
require.Equal(t, n, 32)
|
|
root := aead.NewWrapper(nil)
|
|
err = root.SetAESGCMKeyBytes(rootKey)
|
|
require.NoError(t, err)
|
|
return root
|
|
}
|
|
|
|
// testInitStore will execute the migrations needed to initialize the store for tests
|
|
func testInitStore(t *testing.T, cleanup func() error, url string) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
dialect := "postgres"
|
|
|
|
d, err := common.SqlOpen(dialect, url)
|
|
require.NoError(t, err)
|
|
sm, err := schema.NewManager(ctx, schema.Dialect(dialect), d)
|
|
require.NoError(t, err)
|
|
require.NoError(t, sm.ApplyMigrations(ctx))
|
|
}
|