Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
hold migration lock on migration table creation
  • Loading branch information
coadler committed Dec 1, 2023
commit 0cb53fd621bc68b3725b98b929747cc7c53d1de2
12 changes: 11 additions & 1 deletion coderd/database/migrations/txnmigrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,20 @@ func (*pgTxnDriver) Drop() error {
}

func (d *pgTxnDriver) ensureVersionTable() error {
err := d.Lock()
if err != nil {
return xerrors.Errorf("acquire migration lock: %w", err)
}

const query = `CREATE TABLE IF NOT EXISTS ` + migrationsTableName + ` (version bigint not null primary key, dirty boolean not null)`
if _, err := d.db.ExecContext(context.Background(), query); err != nil {
if _, err := d.tx.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}

err = d.Unlock()
if err != nil {
return xerrors.Errorf("release migration lock: %w", err)
}

return nil
}