Skip to content

Commit

Permalink
postgres: Atomically create migrations table
Browse files Browse the repository at this point in the history
This commit makes `ensureVersionTable` atomic.

Fixes golang-migrate#55
  • Loading branch information
Tomás Senart committed Jan 11, 2019
1 parent 2feaaad commit f6caf3e
Showing 1 changed file with 1 addition and 12 deletions.
13 changes: 1 addition & 12 deletions database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,7 @@ func (p *Postgres) Drop() error {
}

func (p *Postgres) ensureVersionTable() error {
// check if migration table exists
var count int
query := `SELECT COUNT(1) FROM information_schema.tables WHERE table_name = $1 AND table_schema = (SELECT current_schema()) LIMIT 1`
if err := p.conn.QueryRowContext(context.Background(), query, p.config.MigrationsTable).Scan(&count); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
if count == 1 {
return nil
}

// if not, create the empty migration table
query = `CREATE TABLE "` + p.config.MigrationsTable + `" (version bigint not null primary key, dirty boolean not null)`
query := `CREATE TABLE IF NOT EXISTS"` + p.config.MigrationsTable + `" (version bigint not null primary key, dirty boolean not null)`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
Expand Down

0 comments on commit f6caf3e

Please sign in to comment.