Skip to content

Commit

Permalink
chore: less verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Feb 14, 2024
1 parent 1564371 commit 50a011d
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions modules/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,21 @@ func (c *PostgresContainer) Snapshot(ctx context.Context, opts ...SnapshotOption
snapshotName = config.snapshotName
}

// Drop the snapshot database if it already exists
_, _, err := c.Exec(ctx, []string{"psql", "-U", c.user, "-c", fmt.Sprintf(`DROP DATABASE IF EXISTS "%s"`, snapshotName)})
if err != nil {
return err
}

// Create a copy of the database to another database to use as a template now that it was fully migrated
_, _, err = c.Exec(ctx, []string{"psql", "-U", c.user, "-c", fmt.Sprintf(`CREATE DATABASE "%s" WITH TEMPLATE "%s" OWNER "%s"`, snapshotName, c.dbName, c.user)})
if err != nil {
return err
}

// Snapshot the template database so we can restore it onto our original database going forward
_, _, err = c.Exec(ctx, []string{"psql", "-U", c.user, "-c", fmt.Sprintf(`ALTER DATABASE "%s" WITH is_template = TRUE`, snapshotName)})
if err != nil {
return err
// execute the commands to create the snapshot, in order
cmds := []string{
// Drop the snapshot database if it already exists
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),
// Snapshot the template database so we can restore it onto our original database going forward
fmt.Sprintf(`ALTER DATABASE "%s" WITH is_template = TRUE`, snapshotName),
}

for _, cmd := range cmds {
_, _, err := c.Exec(ctx, []string{"psql", "-U", c.user, "-c", cmd})
if err != nil {
return err
}
}

c.snapshotName = snapshotName
Expand All @@ -211,16 +210,19 @@ func (c *PostgresContainer) Restore(ctx context.Context, opts ...SnapshotOption)
snapshotName = config.snapshotName
}

// Drop the entire database by connecting to the postgres global database
_, _, err := c.Exec(ctx, []string{"psql", "-U", c.user, "-d", "postgres", "-c", fmt.Sprintf(`DROP DATABASE "%s" with (FORCE)`, c.dbName)})
if err != nil {
return err
// execute the commands to restore the snapshot, in order
cmds := []string{
// Drop the entire database by connecting to the postgres global database
fmt.Sprintf(`DROP DATABASE "%s" with (FORCE)`, c.dbName),
// Then restore the previous snapshot
fmt.Sprintf(`CREATE DATABASE "%s" WITH TEMPLATE "%s" OWNER "%s"`, c.dbName, snapshotName, c.user),
}

// Then restore the previous snapshot
_, _, err = c.Exec(ctx, []string{"psql", "-U", c.user, "-d", "postgres", "-c", fmt.Sprintf(`CREATE DATABASE "%s" WITH TEMPLATE "%s" OWNER "%s"`, c.dbName, snapshotName, c.user)})
if err != nil {
return err
for _, cmd := range cmds {
_, _, err := c.Exec(ctx, []string{"psql", "-U", c.user, "-d", "postgres", "-c", cmd})
if err != nil {
return err
}
}

return nil
Expand Down

0 comments on commit 50a011d

Please sign in to comment.