Skip to content

Commit

Permalink
Avoid DDL when checking for versions table golang-migrate#134
Browse files Browse the repository at this point in the history
  • Loading branch information
Russ Egan committed Jan 6, 2017
1 parent 966ba84 commit da6c505
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions driver/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func (driver *Driver) Close() error {
}

func (driver *Driver) ensureVersionTableExists() error {
r := driver.db.QueryRow("SELECT count(*) FROM information_schema.tables WHERE table_name = $1;", tableName)
c := 0
if err := r.Scan(&c); err != nil {
return err
}
if c > 0 {
return nil
}
if _, err := driver.db.Exec("CREATE TABLE IF NOT EXISTS " + tableName + " (version int not null primary key);"); err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions driver/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func TestMigrate(t *testing.T) {
t.Fatal(err)
}

// testing idempotency: second call should be a no-op, since table already exists
if err := d.Initialize(driverUrl); err != nil {
t.Fatal(err)
}

files := []file.File{
{
Path: "/foobar",
Expand Down

0 comments on commit da6c505

Please sign in to comment.