Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
pg: use RETURNING for insertHistoryQuery because postgres doesn't support LastInsertId().
spinningbot5f507520affb
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.goindex 928a21f287..1639aae724 100644--- a/plugin/db/pg/pg.go+++ b/plugin/db/pg/pg.go@@ -572,6 +572,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,payload)VALUES ($1, EXTRACT(epoch from NOW()), $2, EXTRACT(epoch from NOW()), $3, $4, $5, $6, 'PENDING', $7, $8, $9, $10, 0, $11, $12)+ RETURNING id`updateHistoryQuery := `UPDATE ` +diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.goindex 54a30d69c3..4eed4c3e1a 100644--- a/plugin/db/util/driverutil.go+++ b/plugin/db/util/driverutil.go@@ -103,28 +103,46 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *// MySQL runs DDL in its own transaction, so we can't commit migration history together with DDL in a single transaction.// Thus we sort of doing a 2-phase commit, where we first write a PENDING migration record, and after migration completes, we then// update the record to DONE together with the updated schema.- res, err := tx.ExecContext(ctx, args.InsertHistoryQuery,- m.Creator,- m.Creator,- m.Namespace,- sequence,- m.Engine,- m.Type,- m.Version,- m.Description,- statement,- schemaBuf.String(),- m.IssueId,- m.Payload,- )+ insertedId := int64(-1)+ if dbType == db.Postgres {+ tx.QueryRowContext(ctx, args.InsertHistoryQuery,+ m.Creator,+ m.Creator,+ m.Namespace,+ sequence,+ m.Engine,+ m.Type,+ m.Version,+ m.Description,+ statement,+ schemaBuf.String(),+ m.IssueId,+ m.Payload,+ ).Scan(&insertedId)+ } else {+ res, err := tx.ExecContext(ctx, args.InsertHistoryQuery,+ m.Creator,+ m.Creator,+ m.Namespace,+ sequence,+ m.Engine,+ m.Type,+ m.Version,+ m.Description,+ statement,+ schemaBuf.String(),+ m.IssueId,+ m.Payload,+ )- if err != nil {- return "", FormatErrorWithQuery(err, args.InsertHistoryQuery)- }+ if err != nil {+ return "", FormatErrorWithQuery(err, args.InsertHistoryQuery)+ }- insertedId, err := res.LastInsertId()- if err != nil {- return "", FormatErrorWithQuery(err, args.InsertHistoryQuery)+ insertedId, err = res.LastInsertId()+ if err != nil {+ return "", FormatErrorWithQuery(err, args.InsertHistoryQuery)+ }}if err := tx.Commit(); err != nil {