Files
supabase/packages/pg-meta/test/schemas.test.ts
Ping-Min LinandJoshen Lim 700365ffba fix(pg-meta): generate privilege, publication, role and schema SQL that servers can statically check (#51457)
## What kind of change does this PR introduce?

Bug fix.

## What is the current behavior?

On a project whose database server statically checks `DO` blocks before
running them (Multigres does), several Studio actions fail:

- Creating a table shows "Failed to update API access privileges for new
table", although the table is created.
- Toggling Realtime for a table fails with `EXECUTE of a runtime-built
statement inside a PL/pgSQL body is not supported`.
- The same error blocks editing a role or schema (rename, owner, drop)
and changing column privileges.

All of these come from SQL that `@supabase/pg-meta` generates. Such a
server only accepts a dynamic `EXECUTE` when it can prove the statement
is fixed, and two shapes in our SQL prevent that:

- parentheses around the payload: `execute(format(...))`
- `%s` conversions in `format()` strings, which substitute raw text (for
example `%s` fed by `<oid>::regclass`, by a `string_agg(...)` subselect,
or by a variable holding a table list)

## What is the new behavior?

`tablePrivileges.grant/revoke`, `columnPrivileges.grant/revoke`,
`publications.update`, `roles.update/remove` and `schemas.update/remove`
generate SQL with no parentheses around the payload and only `%I`/`%L`
conversions:

- Table and column privileges name the table with `%I.%I`, taking the
schema and name from `pg_class`/`pg_namespace`.
- The publication update loops over the tables to drop and to add, with
the same `tables` semantics as before (`undefined` keeps the tables,
`null` publishes all tables, a list replaces them, bare names resolve
through the search path).
- `schemas.remove` picks between `drop schema … cascade` and `…
restrict` with two plain statements instead of a `%s` fed by a `CASE`.
- The grant and revoke format strings are built with `literal()`, so a
quote in a role name can't end the string early.

The statements are equivalent on regular Postgres.

## Additional context

- New tests: replacing, clearing and keeping a publication's tables,
switching between a table list and all tables, and a test (no database
needed) that fails if the generated SQL of any of these builders goes
back to `execute(...)` or `%s`.
- I ran the pg-meta tests against a local Postgres 17. A set of existing
snapshot tests were recorded on Postgres 14 and fail there with and
without this change (for example the new `MAINTAIN` privilege in table
privileges), so CI is the real check for those. The tests covering the
changed SQL pass.
- Some other Studio SQL is blocked on such servers for different reasons
(for example creating foreign servers, and `SET` inside a `DO` block).
It isn't addressed here.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-10-13 09:14:50 -07:00

193 lines
5.9 KiB
TypeScript

import { afterAll, beforeAll, expect, test } from 'vitest'
import pgMeta from '../src/index'
import { cleanupRoot, createTestDatabase } from './db/utils'
beforeAll(async () => {
// Any global setup if needed
})
afterAll(async () => {
await cleanupRoot()
})
const withTestDatabase = (
name: string,
fn: (db: Awaited<ReturnType<typeof createTestDatabase>>) => Promise<void>
) => {
test(name, async () => {
const db = await createTestDatabase()
try {
await fn(db)
} finally {
await db.cleanup()
}
})
}
// Example of refactored tests using the wrapper
withTestDatabase('list with system schemas', async ({ executeQuery }) => {
const { sql, zod } = await pgMeta.schemas.list({ includeSystemSchemas: true })
const res = zod.parse(await executeQuery(sql))
expect(res.find(({ name }) => name === 'pg_catalog')).toMatchInlineSnapshot(
{ id: expect.any(Number) },
`
{
"comment": "system catalog schema",
"id": Any<Number>,
"name": "pg_catalog",
"owner": "postgres",
}
`
)
})
withTestDatabase('list without system schemas', async ({ executeQuery }) => {
const { sql, zod } = await pgMeta.schemas.list({ includeSystemSchemas: false })
const rq = await executeQuery<typeof zod._type>(sql)
const res = zod.parse(rq)
expect(res.find(({ name }) => name === 'pg_catalog')).toMatchInlineSnapshot(`undefined`)
expect(res.find(({ name }) => name === 'public')).toMatchInlineSnapshot(
{ id: expect.any(Number) },
`
{
"comment": "standard public schema",
"id": Any<Number>,
"name": "public",
"owner": "postgres",
}
`
)
})
withTestDatabase('retrieve, create, update, delete', async ({ executeQuery }) => {
// Create schema
const { sql: createSql } = await pgMeta.schemas.create({ name: 's' })
await executeQuery(createSql)
// Get the created schema
const { sql: retrieveSql, zod } = await pgMeta.schemas.retrieve({ name: 's' })
const createRes = zod.parse((await executeQuery(retrieveSql))[0])
expect(createRes).toMatchInlineSnapshot(
{ id: expect.any(Number) },
`
{
"comment": null,
"id": Any<Number>,
"name": "s",
"owner": "postgres",
}
`
)
// Retrieve schema again to verify
const { sql: retrieveSql2, zod: retrieveZod } = await pgMeta.schemas.retrieve({
id: createRes!.id,
})
const retrieveRes = retrieveZod.parse((await executeQuery(retrieveSql2))[0])
expect(retrieveRes).toMatchInlineSnapshot(
{ id: expect.any(Number) },
`
{
"comment": null,
"id": Any<Number>,
"name": "s",
"owner": "postgres",
}
`
)
// Update schema
const { sql: updateSql } = await pgMeta.schemas.update(
{ id: createRes!.id },
{
name: 'ss',
owner: 'postgres',
}
)
await executeQuery(updateSql)
// Get the updated schema
const { sql: retrieveUpdatedSql, zod: retrieveUpdatedZod } = await pgMeta.schemas.retrieve({
name: 'ss',
})
const updateRes = retrieveUpdatedZod.parse((await executeQuery(retrieveUpdatedSql))[0])
expect(updateRes).toMatchInlineSnapshot(
{ id: expect.any(Number) },
`
{
"comment": null,
"id": Any<Number>,
"name": "ss",
"owner": "postgres",
}
`
)
// Delete schema
const { sql: deleteSql } = await pgMeta.schemas.remove({ id: updateRes!.id })
await executeQuery(deleteSql)
// Verify deletion
const { sql: finalRetrieveSql } = await pgMeta.schemas.retrieve({ id: updateRes!.id })
const finalRes = await executeQuery(finalRetrieveSql)
expect(finalRes).toMatchInlineSnapshot(`[]`)
})
withTestDatabase(
'remove with cascade drops the schema and its contents',
async ({ executeQuery }) => {
await executeQuery(`create schema s; create table s.t(); create view s.v as select 1;`)
const { sql: retrieveSql, zod } = pgMeta.schemas.retrieve({ name: 's' })
const { id } = zod.parse((await executeQuery(retrieveSql))[0])!
await executeQuery(pgMeta.schemas.remove({ id }, { cascade: true }).sql)
expect(await executeQuery(retrieveSql)).toEqual([])
expect(
await executeQuery(
`select 1 from pg_class c join pg_namespace n on n.oid = c.relnamespace where n.nspname = 's'`
)
).toEqual([])
}
)
withTestDatabase('remove without cascade only drops an empty schema', async ({ executeQuery }) => {
await executeQuery(`create schema full_s; create table full_s.t(); create schema empty_s;`)
const retrieve = (name: string) => pgMeta.schemas.retrieve({ name })
const fullSchema = retrieve('full_s')
const emptySchema = retrieve('empty_s')
const { id: fullId } = fullSchema.zod.parse((await executeQuery(fullSchema.sql))[0])!
const { id: emptyId } = emptySchema.zod.parse((await executeQuery(emptySchema.sql))[0])!
// Both the default and an explicit `cascade: false` restrict.
await expect(executeQuery(pgMeta.schemas.remove({ id: fullId }).sql)).rejects.toThrow(
/cannot drop schema full_s because other objects depend on it/
)
await expect(
executeQuery(pgMeta.schemas.remove({ id: fullId }, { cascade: false }).sql)
).rejects.toThrow(/cannot drop schema full_s/)
expect(await executeQuery(fullSchema.sql)).toHaveLength(1)
await executeQuery(pgMeta.schemas.remove({ id: emptyId }).sql)
expect(await executeQuery(emptySchema.sql)).toEqual([])
})
withTestDatabase(
'update and remove a schema whose name needs quoting',
async ({ executeQuery }) => {
await executeQuery(`create schema "My Schema";`)
const { sql: retrieveSql, zod } = pgMeta.schemas.retrieve({ name: 'My Schema' })
const { id } = zod.parse((await executeQuery(retrieveSql))[0])!
await executeQuery(pgMeta.schemas.update({ id }, { name: `it's "renamed"` }).sql)
const renamed = pgMeta.schemas.retrieve({ id })
expect(renamed.zod.parse((await executeQuery(renamed.sql))[0])!.name).toBe(`it's "renamed"`)
await executeQuery(pgMeta.schemas.remove({ id }).sql)
expect(await executeQuery(renamed.sql)).toEqual([])
}
)