Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/calm-schema-drops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@tsops/core": patch
"tsops": patch
---

Make built-in drop-schema runtime role cleanup idempotent when the resolved
runtime role was never created.
35 changes: 25 additions & 10 deletions packages/core/src/operations/db-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,7 @@ function buildDropSchemaSqlSteps(

const steps = [buildExternalDependencyGuard(schema)]
if (runtimeRole) {
const quotedRole = quoteIdentifier(runtimeRole)
steps.push(
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${quotedSchema} REVOKE ALL ON TABLES FROM ${quotedRole}`,
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${quotedSchema} REVOKE ALL ON SEQUENCES FROM ${quotedRole}`,
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${quotedSchema} REVOKE ALL ON FUNCTIONS FROM ${quotedRole}`,
`REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA ${quotedSchema} FROM ${quotedRole}`,
`REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA ${quotedSchema} FROM ${quotedRole}`,
`REVOKE ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA ${quotedSchema} FROM ${quotedRole}`,
`REVOKE ALL PRIVILEGES ON SCHEMA ${quotedSchema} FROM ${quotedRole}`
)
steps.push(buildRuntimeRoleCleanupGuard(schema, runtimeRole))
}

steps.push(`DROP SCHEMA IF EXISTS ${quotedSchema} CASCADE`)
Expand All @@ -257,6 +248,30 @@ function buildDropSchemaSqlSteps(
return steps
}

function buildRuntimeRoleCleanupGuard(schema: string, runtimeRole: string): string {
const quotedSchema = quoteIdentifier(schema)
const quotedRole = quoteIdentifier(runtimeRole)
const roleLiteral = sqlLiteral(runtimeRole)
const revokeSteps = [
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${quotedSchema} REVOKE ALL ON TABLES FROM ${quotedRole}`,
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${quotedSchema} REVOKE ALL ON SEQUENCES FROM ${quotedRole}`,
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${quotedSchema} REVOKE ALL ON FUNCTIONS FROM ${quotedRole}`,
`REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA ${quotedSchema} FROM ${quotedRole}`,
`REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA ${quotedSchema} FROM ${quotedRole}`,
`REVOKE ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA ${quotedSchema} FROM ${quotedRole}`,
`REVOKE ALL PRIVILEGES ON SCHEMA ${quotedSchema} FROM ${quotedRole}`
]

return `
DO $tsops$
BEGIN
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = ${roleLiteral}) THEN
${revokeSteps.map((step) => ` EXECUTE ${sqlLiteral(step)};`).join('\n')}
END IF;
END
$tsops$`.trim()
}

function buildExternalDependencyGuard(schema: string): string {
const literalSchema = sqlLiteral(schema)
return `
Expand Down
1 change: 1 addition & 0 deletions tests/overlay-lifecycle-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ describe('overlay lifecycle preview contract', () => {

expect(command).toContain('pg_depend')
expect(command).toContain('cross-schema dependencies')
expect(command).toContain("pg_roles WHERE rolname = 'worken_pr_857_app'")
expect(command).toContain('DO \\$tsops\\$')
expect(command).not.toContain('DO $tsops$')
expect(command.indexOf('pg_depend')).toBeLessThan(command.indexOf('DROP SCHEMA'))
Expand Down