unprovision
This commit is contained in:
parent
22c2f68c2a
commit
1e53bba6e7
6 changed files with 148 additions and 0 deletions
|
|
@ -62,6 +62,32 @@ END $$;`, sqlQuoteLiteral(p.DBUser), sqlQuoteIdent(p.DBUser), sqlQuoteLiteral(p.
|
|||
return nil
|
||||
}
|
||||
|
||||
func (a *App) dropProjectDatabase(ctx context.Context, p Project) error {
|
||||
if !a.cfg.DBProvisionEnabled {
|
||||
return nil
|
||||
}
|
||||
if strings.TrimSpace(p.DBName) == "" || strings.TrimSpace(p.DBUser) == "" {
|
||||
return nil
|
||||
}
|
||||
adminDB, err := sql.Open("pgx", a.cfg.DBProvisionAdminURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db provisioning connect failed: %w", err)
|
||||
}
|
||||
defer adminDB.Close()
|
||||
if err := adminDB.PingContext(ctx); err != nil {
|
||||
return fmt.Errorf("db provisioning ping failed: %w", err)
|
||||
}
|
||||
// Terminate active sessions so DROP DATABASE succeeds.
|
||||
_, _ = adminDB.ExecContext(ctx, `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = $1 AND pid <> pg_backend_pid()`, p.DBName)
|
||||
if _, err := adminDB.ExecContext(ctx, fmt.Sprintf(`DROP DATABASE IF EXISTS %s`, sqlQuoteIdent(p.DBName))); err != nil {
|
||||
return fmt.Errorf("drop database failed: %w", err)
|
||||
}
|
||||
if _, err := adminDB.ExecContext(ctx, fmt.Sprintf(`DROP ROLE IF EXISTS %s`, sqlQuoteIdent(p.DBUser))); err != nil {
|
||||
return fmt.Errorf("drop role failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) projectDatabaseURL(p Project) (string, error) {
|
||||
if p.DBName == "" || p.DBUser == "" || p.DBPassword == "" {
|
||||
return "", nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue