Skip to content

Commit

Permalink
bugfix: On db wipe, drop custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjat committed Nov 29, 2022
1 parent 1d521ff commit 6b6dd3a
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion storage/postgres/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (c *Client) Name() string {

// Wipe removes all contents of the database.
func (c *Client) Wipe(ctx context.Context) error {
// List, then drop all tables.
rows, err := c.Query(ctx, `
SELECT schemaname, tablename
FROM pg_tables
Expand All @@ -160,5 +161,30 @@ func (c *Client) Wipe(ctx context.Context) error {
return err
}
}
return err

// List, then drop all custom types.
// Query from https://stackoverflow.com/questions/3660787/how-to-list-custom-types-using-postgres-information-schema
rows, err = c.Query(ctx, `
SELECT n.nspname as schema, t.typname as type
FROM pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
AND n.nspname NOT IN ('pg_catalog', 'information_schema');
`)
if err != nil {
return fmt.Errorf("failed to list types: %w", err)
}
for rows.Next() {
var schema, typ string
if err = rows.Scan(&schema, &typ); err != nil {
return err
}
c.logger.Info("dropping type", "schema", schema, "type", typ)
if _, err = c.pool.Exec(ctx, fmt.Sprintf("DROP TYPE %s.%s CASCADE;", schema, typ)); err != nil {
return err
}
}

return nil
}

0 comments on commit 6b6dd3a

Please sign in to comment.