Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no lock parameter for Postgres #1030

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions database/postgres/postgres.go
Expand Up @@ -53,6 +53,7 @@ type Config struct {
migrationsTableName string
StatementTimeout time.Duration
MultiStatementMaxSize int
NoLock bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document this new parameter in the README

}

type Postgres struct {
Expand Down Expand Up @@ -202,13 +203,22 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
}
}

noLock := false
if s := purl.Query().Get("x-no-lock"); len(s) > 0 {
noLock, err = strconv.ParseBool(s)
if err != nil {
return nil, fmt.Errorf("Unable to parse option no-lock: %w", err)
}
}

px, err := WithInstance(db, &Config{
DatabaseName: purl.Path,
MigrationsTable: migrationsTable,
MigrationsTableQuoted: migrationsTableQuoted,
StatementTimeout: time.Duration(statementTimeout) * time.Millisecond,
MultiStatementEnabled: multiStatementEnabled,
MultiStatementMaxSize: multiStatementMaxSize,
NoLock: noLock,
})

if err != nil {
Expand All @@ -234,6 +244,9 @@ func (p *Postgres) Close() error {
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS
func (p *Postgres) Lock() error {
return database.CasRestoreOnErr(&p.isLocked, false, true, database.ErrLocked, func() error {
if p.config.NoLock {
return nil
}
aid, err := database.GenerateAdvisoryLockId(p.config.DatabaseName, p.config.migrationsSchemaName, p.config.migrationsTableName)
if err != nil {
return err
Expand All @@ -251,6 +264,9 @@ func (p *Postgres) Lock() error {

func (p *Postgres) Unlock() error {
return database.CasRestoreOnErr(&p.isLocked, true, false, database.ErrNotLocked, func() error {
if p.config.NoLock {
return nil
}
aid, err := database.GenerateAdvisoryLockId(p.config.DatabaseName, p.config.migrationsSchemaName, p.config.migrationsTableName)
if err != nil {
return err
Expand Down Expand Up @@ -453,6 +469,7 @@ func (p *Postgres) Drop() (err error) {
// Note that this function locks the database, which deviates from the usual
// convention of "caller locks" in the Postgres type.
func (p *Postgres) ensureVersionTable() (err error) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove this unnecessary change

if err = p.Lock(); err != nil {
return err
}
Expand Down
51 changes: 51 additions & 0 deletions database/postgres/postgres_test.go
Expand Up @@ -642,6 +642,57 @@ func TestPostgres_Lock(t *testing.T) {
})
}

func TestNoLockParamValidation(t *testing.T) {
ip := "127.0.0.1"
port := 5432
addr := fmt.Sprintf("postgres://root:root@%v:%v/public", ip, port)
p := &Postgres{}
_, err := p.Open(addr + "?x-no-lock=not-a-bool")
if !errors.Is(err, strconv.ErrSyntax) {
t.Fatal("Expected syntax error when passing a non-bool as x-no-lock parameter")
}
}

func TestNoLockWorks(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)
}

addr := pgConnectionString(ip, port)
p := &Postgres{}
d, err := p.Open(addr)
if err != nil {
t.Fatal(err)
}

lock := d.(*Postgres)

p = &Postgres{}
d, err = p.Open(addr + "&x-no-lock=true")
if err != nil {
t.Fatal(err)
}

noLock := d.(*Postgres)

// Should be possible to take real lock and no-lock at the same time
if err = lock.Lock(); err != nil {
t.Fatal(err)
}
if err = noLock.Lock(); err != nil {
t.Fatal(err)
}
if err = lock.Unlock(); err != nil {
t.Fatal(err)
}
if err = noLock.Unlock(); err != nil {
t.Fatal(err)
}
})
}

func TestWithInstance_Concurrent(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
Expand Down