Skip to content

Commit

Permalink
fix(postgres): duplicate snapshot name
Browse files Browse the repository at this point in the history
Fix postgres snapshot name reuse by removing its template flag before
trying to delete it.

Fixes #2822
  • Loading branch information
stevenh committed Oct 18, 2024
1 parent f1632b3 commit e26accc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func (c *PostgresContainer) Snapshot(ctx context.Context, opts ...SnapshotOption
// execute the commands to create the snapshot, in order
if err := c.execCommandsSQL(ctx,
// Drop the snapshot database if it already exists
fmt.Sprintf(`UPDATE pg_database SET datistemplate = FALSE WHERE datname = '%s'`, snapshotName),
fmt.Sprintf(`DROP DATABASE IF EXISTS "%s"`, snapshotName),
// Create a copy of the database to another database to use as a template now that it was fully migrated
fmt.Sprintf(`CREATE DATABASE "%s" WITH TEMPLATE "%s" OWNER "%s"`, snapshotName, c.dbName, c.user),
Expand Down
28 changes: 28 additions & 0 deletions modules/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,34 @@ func TestSnapshotWithOverrides(t *testing.T) {
})
}

func TestSnapshotDuplicate(t *testing.T) {
ctx := context.Background()

dbname := "other-db"
user := "other-user"
password := "other-password"

ctr, err := postgres.Run(
ctx,
"docker.io/postgres:16-alpine",
postgres.WithDatabase(dbname),
postgres.WithUsername(user),
postgres.WithPassword(password),
postgres.BasicWaitStrategies(),
)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

_, _, err = ctr.Exec(ctx, []string{"psql", "-U", user, "-d", dbname, "-c", "CREATE TABLE users (id SERIAL, name TEXT NOT NULL, age INT NOT NULL)"})
require.NoError(t, err)

err = ctr.Snapshot(ctx, postgres.WithSnapshotName("other-snapshot"))
require.NoError(t, err)

err = ctr.Snapshot(ctx, postgres.WithSnapshotName("other-snapshot"))
require.NoError(t, err)
}

func TestSnapshotWithDockerExecFallback(t *testing.T) {
ctx := context.Background()

Expand Down

0 comments on commit e26accc

Please sign in to comment.