Adding Migrate Command (#872)
This commit is contained in:
1 parent
5de4befb58
commit
1f9fcad1cd
25 files changed
+590
-650
No files matched your search
@@ -52,6 +52,7 @@ const (
|
||||
var defaultMigrationsTable = "boundary_schema_version"
|
||||
|
||||
// Postgres is a driver usable by a boundary schema manager.
|
||||
// This struct is not thread safe.
|
||||
type Postgres struct {
|
||||
// Locking and unlocking need to use the same connection
|
||||
conn *sql.Conn
|
||||
@@ -86,7 +87,8 @@ func New(ctx context.Context, instance *sql.DB) (*Postgres, error) {
|
||||
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS
|
||||
func (p *Postgres) TrySharedLock(ctx context.Context) error {
|
||||
const op = "postgres.(Postgres).TrySharedLock"
|
||||
r := p.conn.QueryRowContext(ctx, "SELECT pg_try_advisory_lock_shared($1)", schemaAccessLockId)
|
||||
const query = "SELECT pg_try_advisory_lock_shared($1)"
|
||||
r := p.conn.QueryRowContext(ctx, query, schemaAccessLockId)
|
||||
if r.Err() != nil {
|
||||
return errors.Wrap(r.Err(), op)
|
||||
}
|
||||
@@ -104,8 +106,8 @@ func (p *Postgres) TrySharedLock(ctx context.Context) error {
|
||||
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS
|
||||
func (p *Postgres) TryLock(ctx context.Context) error {
|
||||
const op = "postgres.(Postgres).TryLock"
|
||||
|
||||
r := p.conn.QueryRowContext(ctx, "SELECT pg_try_advisory_lock($1)", schemaAccessLockId)
|
||||
const query = "SELECT pg_try_advisory_lock($1)"
|
||||
r := p.conn.QueryRowContext(ctx, query, schemaAccessLockId)
|
||||
if r.Err() != nil {
|
||||
return errors.Wrap(r.Err(), op)
|
||||
}
|
||||
@@ -123,13 +125,10 @@ func (p *Postgres) TryLock(ctx context.Context) error {
|
||||
// if we were unable to get the lock before the context cancels.
|
||||
func (p *Postgres) Lock(ctx context.Context) error {
|
||||
const op = "postgres.(Postgres).Lock"
|
||||
|
||||
// This will wait indefinitely until the Lock can be acquired.
|
||||
query := `SELECT pg_advisory_lock($1)`
|
||||
const query = "SELECT pg_advisory_lock($1)"
|
||||
if _, err := p.conn.ExecContext(ctx, query, schemaAccessLockId); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -137,8 +136,7 @@ func (p *Postgres) Lock(ctx context.Context) error {
|
||||
// release the lock before the context cancels.
|
||||
func (p *Postgres) Unlock(ctx context.Context) error {
|
||||
const op = "postgres.(Postgres).Unlock"
|
||||
|
||||
query := `SELECT pg_advisory_unlock($1)`
|
||||
const query = `SELECT pg_advisory_unlock($1)`
|
||||
if _, err := p.conn.ExecContext(ctx, query, schemaAccessLockId); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
}
|
||||
@@ -266,9 +264,9 @@ func (p *Postgres) SetVersion(ctx context.Context, version int, dirty bool) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// Version returns the version, if the database is currently in a dirty state, and any error.
|
||||
// CurrentState returns the version, if the database is currently in a dirty state, and any error.
|
||||
// A version value of -1 indicates no version is set.
|
||||
func (p *Postgres) Version(ctx context.Context) (version int, dirty bool, err error) {
|
||||
func (p *Postgres) CurrentState(ctx context.Context) (version int, dirty bool, err error) {
|
||||
const op = "postgres.(Postgres).Version"
|
||||
query := `SELECT Version, dirty FROM ` + pq.QuoteIdentifier(defaultMigrationsTable) + ` LIMIT 1`
|
||||
err = p.conn.QueryRowContext(ctx, query).Scan(&version, &dirty)
|
||||
@@ -337,7 +335,7 @@ func (p *Postgres) drop(ctx context.Context) (err error) {
|
||||
// convention of "caller locks" in the postgres type.
|
||||
func (p *Postgres) ensureVersionTable(ctx context.Context) (err error) {
|
||||
const op = "postgres.(Postgres).ensureVersionTable"
|
||||
if err = p.Lock(ctx); err != nil {
|
||||
if err = p.TryLock(ctx); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,6 @@ import (
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/dhui/dktest"
|
||||
@@ -136,10 +135,10 @@ func TestVersion_NoVersionTable(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
// Drop the version table so calls to Version don't rely on that
|
||||
// Drop the version table so calls to CurrentState don't rely on that
|
||||
d.drop(ctx)
|
||||
|
||||
v, dirt, err := d.Version(ctx)
|
||||
v, dirt, err := d.CurrentState(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, v, nilVersion)
|
||||
assert.False(t, dirt)
|
||||
@@ -218,7 +217,7 @@ func TestWithSchema(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
version, _, err := d2.Version(ctx)
|
||||
version, _, err := d2.CurrentState(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -226,11 +225,11 @@ func TestWithSchema(t *testing.T) {
|
||||
t.Fatal("expected NilVersion")
|
||||
}
|
||||
|
||||
// now update Version and compare
|
||||
// now update CurrentState and compare
|
||||
if err := d2.SetVersion(ctx, 2, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
version, _, err = d2.Version(ctx)
|
||||
version, _, err = d2.CurrentState(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -238,8 +237,8 @@ func TestWithSchema(t *testing.T) {
|
||||
t.Fatal("expected Version 2")
|
||||
}
|
||||
|
||||
// meanwhile, the public schema still has the other Version
|
||||
version, _, err = d.Version(ctx)
|
||||
// meanwhile, the public schema still has the other CurrentState
|
||||
version, _, err = d.CurrentState(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -287,49 +286,6 @@ func TestPostgres_Lock(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestWithInstance_Concurrent(t *testing.T) {
|
||||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
|
||||
ip, port, err := c.FirstPort()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The number of concurrent processes running New
|
||||
const concurrency = 30
|
||||
|
||||
// We can instantiate a single database handle because it is
|
||||
// actually a connection pool, and so, each of the below go
|
||||
// routines will have a high probability of using a separate
|
||||
// connection, which is something we want to exercise.
|
||||
db, err := sql.Open("postgres", pgConnectionString(ip, port))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := db.Close(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
db.SetMaxIdleConns(concurrency)
|
||||
db.SetMaxOpenConns(concurrency)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
defer wg.Wait()
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
_, err := New(context.Background(), db)
|
||||
if err != nil {
|
||||
t.Errorf("process %d error: %s", i, err)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRun_Error(t *testing.T) {
|
||||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -49,14 +49,14 @@ func test(t *testing.T, d *Postgres, migration []byte) {
|
||||
testNilVersion(t, d) // test first
|
||||
testLockAndUnlock(t, d)
|
||||
testRun(t, d, bytes.NewReader(migration))
|
||||
testSetVersion(t, d) // also tests Version()
|
||||
testSetVersion(t, d) // also tests CurrentState()
|
||||
// drop breaks the driver, so test it last.
|
||||
testDrop(t, d)
|
||||
}
|
||||
|
||||
func testNilVersion(t *testing.T, d *Postgres) {
|
||||
ctx := context.Background()
|
||||
v, _, err := d.Version(ctx)
|
||||
v, _, err := d.CurrentState(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -136,7 +136,7 @@ func testSetVersion(t *testing.T, d *Postgres) {
|
||||
if err != tc.expectedErr {
|
||||
t.Fatal("Got unexpected error:", err, "!=", tc.expectedErr)
|
||||
}
|
||||
v, dirty, readErr := d.Version(ctx)
|
||||
v, dirty, readErr := d.CurrentState(ctx)
|
||||
if readErr != tc.expectedReadErr {
|
||||
t.Fatal("Got unexpected error:", readErr, "!=", tc.expectedReadErr)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user