Skip to content

Commit bead4a9

Browse files
committed
Added documentation and test for lock strategy
1 parent 129922a commit bead4a9

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

database/pgx/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This package is for [pgx/v4](https://pkg.go.dev/github.com/jackc/pgx/v4). A back
1111
| `x-statement-timeout` | `StatementTimeout` | Abort any statement that takes more than the specified number of milliseconds |
1212
| `x-multi-statement` | `MultiStatementEnabled` | Enable multi-statement execution (default: false) |
1313
| `x-multi-statement-max-size` | `MultiStatementMaxSize` | Maximum size of single statement in bytes (default: 10MB) |
14+
| `x-lock-strategy` | `LockStrategy` | Strategy used for locking during migration (default: advisory) |
15+
| `x-lock-table` | `LockTable` | Name of the table which maintains the migration lock (default: schema_lock) |
1416
| `dbname` | `DatabaseName` | The name of the database to connect to |
1517
| `search_path` | | This variable specifies the order in which schemas are searched when an object is referenced by a simple name with no schema specified. |
1618
| `user` | | The user to sign in as |

database/pgx/pgx.go

+8
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
218218
}
219219

220220
lockStrategy := purl.Query().Get("x-lock-strategy")
221+
lockTable := purl.Query().Get("x-lock-table")
221222

222223
px, err := WithInstance(db, &Config{
223224
DatabaseName: purl.Path,
@@ -227,6 +228,7 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
227228
MultiStatementEnabled: multiStatementEnabled,
228229
MultiStatementMaxSize: multiStatementMaxSize,
229230
LockStrategy: lockStrategy,
231+
LockTable: lockTable,
230232
})
231233

232234
if err != nil {
@@ -512,6 +514,12 @@ func (p *Postgres) Drop() (err error) {
512514
if err := tables.Scan(&tableName); err != nil {
513515
return err
514516
}
517+
518+
// do not drop lock table
519+
if tableName == p.config.LockTable && p.config.LockStrategy == LockStrategyTable {
520+
continue
521+
}
522+
515523
if len(tableName) > 0 {
516524
tableNames = append(tableNames, tableName)
517525
}

database/pgx/pgx_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,32 @@ func TestMigrate(t *testing.T) {
134134
})
135135
}
136136

137+
func TestMigrateLockTable(t *testing.T) {
138+
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
139+
ip, port, err := c.FirstPort()
140+
if err != nil {
141+
t.Fatal(err)
142+
}
143+
144+
addr := pgConnectionString(ip, port, "x-lock-strategy=table", "x-lock-table=lock_table")
145+
p := &Postgres{}
146+
d, err := p.Open(addr)
147+
if err != nil {
148+
t.Fatal(err)
149+
}
150+
defer func() {
151+
if err := d.Close(); err != nil {
152+
t.Error(err)
153+
}
154+
}()
155+
m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", "pgx", d)
156+
if err != nil {
157+
t.Fatal(err)
158+
}
159+
dt.TestMigrate(t, m)
160+
})
161+
}
162+
137163
func TestMultipleStatements(t *testing.T) {
138164
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
139165
ip, port, err := c.FirstPort()

0 commit comments

Comments
 (0)