@@ -23,6 +23,7 @@ import (
2323 "github.com/jackc/pgconn"
2424 "github.com/jackc/pgerrcode"
2525 _ "github.com/jackc/pgx/v4/stdlib"
26+ "github.com/lib/pq"
2627)
2728
2829const (
4243 DefaultMigrationsTable = "schema_migrations"
4344 DefaultMultiStatementMaxSize = 10 * 1 << 20 // 10 MB
4445 DefaultLockTable = "schema_lock"
45- DefaultLockStrategy = "advisory"
46+ DefaultLockStrategy = LockStrategyAdvisory
4647)
4748
4849var (
@@ -293,13 +294,19 @@ func (p *Postgres) applyTableLock() error {
293294 if err != nil {
294295 return & database.Error {OrigErr : err , Err : "transaction start failed" }
295296 }
297+ defer func () {
298+ errRollback := tx .Rollback ()
299+ if errRollback != nil {
300+ err = multierror .Append (err , errRollback )
301+ }
302+ }()
296303
297304 aid , err := database .GenerateAdvisoryLockId (p .config .DatabaseName )
298305 if err != nil {
299306 return err
300307 }
301308
302- query := "SELECT * FROM " + p .config .LockTable + " WHERE lock_id = $1"
309+ query := "SELECT * FROM " + pq . QuoteIdentifier ( p .config .LockTable ) + " WHERE lock_id = $1"
303310 rows , err := tx .Query (query , aid )
304311 if err != nil {
305312 return database.Error {OrigErr : err , Err : "failed to fetch migration lock" , Query : []byte (query )}
@@ -317,7 +324,7 @@ func (p *Postgres) applyTableLock() error {
317324 return database .ErrLocked
318325 }
319326
320- query = "INSERT INTO " + p .config .LockTable + " (lock_id) VALUES ($1)"
327+ query = "INSERT INTO " + pq . QuoteIdentifier ( p .config .LockTable ) + " (lock_id) VALUES ($1)"
321328 if _ , err := tx .Exec (query , aid ); err != nil {
322329 return database.Error {OrigErr : err , Err : "failed to set migration lock" , Query : []byte (query )}
323330 }
@@ -345,7 +352,7 @@ func (p *Postgres) releaseTableLock() error {
345352 return err
346353 }
347354
348- query := "DELETE FROM " + p .config .LockTable + " WHERE lock_id = $1"
355+ query := "DELETE FROM " + pq . QuoteIdentifier ( p .config .LockTable ) + " WHERE lock_id = $1"
349356 if _ , err := p .db .Exec (query , aid ); err != nil {
350357 return database.Error {OrigErr : err , Err : "failed to release migration lock" , Query : []byte (query )}
351358 }
@@ -598,7 +605,7 @@ func (p *Postgres) ensureLockTable() error {
598605 return nil
599606 }
600607
601- query = `CREATE TABLE " ` + p .config .LockTable + `" (lock_id BIGINT NOT NULL PRIMARY KEY)`
608+ query = `CREATE TABLE ` + pq . QuoteIdentifier ( p .config .LockTable ) + ` (lock_id BIGINT NOT NULL PRIMARY KEY)`
602609 if _ , err := p .db .Exec (query ); err != nil {
603610 return & database.Error {OrigErr : err , Query : []byte (query )}
604611 }
0 commit comments