passing lock factory to every migrate command
@@ -257,6 +257,8 @@ type RunCommand struct {
}
type Migration struct {
+ lockFactory lock.LockFactory
+
Postgres flag.PostgresConfig `group:"PostgreSQL Configuration" namespace:"postgres"`
EncryptionKey flag.Cipher `long:"encryption-key" description:"A 16 or 32 length key used to encrypt sensitive information before storing it in the database."`
OldEncryptionKey flag.Cipher `long:"old-encryption-key" description:"Encryption key previously used for encrypting sensitive information. If provided without a new key, data is decrypted. If provided with a new key, data is re-encrypted."`
@@ -267,6 +269,14 @@ type Migration struct {
}
func (m *Migration) Execute(args []string) error {
+ lockConn, err := constructLockConn(defaultDriverName, m.Postgres.ConnectionString())
+ if err != nil {
+ return err
+ }
+ defer lockConn.Close()
+
+ m.lockFactory = lock.NewLockFactory(lockConn, metric.LogLockAcquired, metric.LogLockReleased)
+
if m.MigrateToLatestVersion {
return m.migrateToLatestVersion()
}
@@ -289,7 +299,7 @@ func (cmd *Migration) currentDBVersion() error {
helper := migration.NewOpenHelper(
defaultDriverName,
cmd.Postgres.ConnectionString(),
- nil,
+ cmd.lockFactory,
nil,
nil,
)
@@ -307,7 +317,7 @@ func (cmd *Migration) supportedDBVersion() error {
helper := migration.NewOpenHelper(
defaultDriverName,
cmd.Postgres.ConnectionString(),
- nil,
+ cmd.lockFactory,
nil,
nil,
)
@@ -337,7 +347,7 @@ func (cmd *Migration) migrateDBToVersion() error {
helper := migration.NewOpenHelper(
defaultDriverName,
cmd.Postgres.ConnectionString(),
- nil,
+ cmd.lockFactory,
newKey,
oldKey,
)
@@ -365,7 +375,7 @@ func (cmd *Migration) rotateEncryptionKey() error {
helper := migration.NewOpenHelper(
defaultDriverName,
cmd.Postgres.ConnectionString(),
- nil,
+ cmd.lockFactory,
newKey,
oldKey,
)
@@ -379,18 +389,10 @@ func (cmd *Migration) rotateEncryptionKey() error {
}
func (cmd *Migration) migrateToLatestVersion() error {
- lockConn, err := constructLockConn(defaultDriverName, cmd.Postgres.ConnectionString())
- if err != nil {
- return err
- }
- defer lockConn.Close()
-
- lockFactory := lock.NewLockFactory(lockConn, metric.LogLockAcquired, metric.LogLockReleased)
-
helper := migration.NewOpenHelper(
defaultDriverName,
cmd.Postgres.ConnectionString(),
- lockFactory,
+ cmd.lockFactory,
nil,
nil,
)
