mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## Summary
Third PR in the SafeSql migration stack. Flips the input/output types on
`pgMeta.functions/policies/triggers`'s `.create/.update/.remove` to use
`SafeSqlFragment`, and updates every Studio consumer atomically.
### pg-meta
- `pgMeta.functions/policies/triggers` `.create/.update/.remove` now
return `{ sql: SafeSqlFragment }` and accept branded input parameters
(`PGFunctionCreate`, `PGSavedFunction`, `PolicyCreate/UpdateParams`,
`PGTriggerCreate` with branded condition).
- `QueryModifier.toSql()` returns `SafeSqlFragment`.
### Studio consumers updated to the new branded API
- `data/database-functions/*` (query, create/update/delete mutations)
- `data/database-policies/*` (create, update mutations)
- `data/database-triggers/database-trigger-update-transaction-mutation`
- `components/Database/Triggers/TriggerSheet`
- `components/Database/Functions/CreateFunction`
- `components/Auth/Policies/PolicyEditorPanel`
These consumers land atomically with the pg-meta API change because the
input-type strictness flip (string → `SafeSqlFragment` for SQL fields)
forces every call site to update together.
## Stack
- 1/7: #45897 (merged)
- 2/7: #45903 (merged)
- 3/7: this PR
- 4/7–7/7: upcoming
## Test plan
- [x] `pnpm typecheck` passes
- [x] `pnpm --filter @supabase/pg-meta test` passes
- [x] Dev-server smoke test: function editor, policy editor, trigger
sheet
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Refactor**
* Strengthened SQL safety across policy, function, and trigger workflows
by converting raw SQL strings to typed SQL fragments and safer
composition
* Updated editor behavior to handle policy conditions/checks as typed
SQL fragments with improved initialization and template handling
* Aligned query and modifier interfaces to return typed SQL fragments
for safer composition
* **Tests**
* Updated tests to use typed SQL fragments and synchronous builders
where applicable
<!-- review_stack_entry_start -->
[](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45990)
<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
143 lines
3.8 KiB
TypeScript
143 lines
3.8 KiB
TypeScript
import { afterAll, expect, test } from 'vitest'
|
|
|
|
import pgMeta, { safeSql } from '../src/index'
|
|
import { cleanupRoot, createTestDatabase } from './db/utils'
|
|
|
|
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()
|
|
}
|
|
})
|
|
}
|
|
|
|
withTestDatabase('list policies', async ({ executeQuery }) => {
|
|
const { sql, zod } = pgMeta.policies.list()
|
|
const res = zod.parse(await executeQuery(sql))
|
|
const policy = res.find(({ name }) => name === 'categories_update_policy')
|
|
|
|
expect(policy).toMatchInlineSnapshot(
|
|
{ id: expect.any(Number), table_id: expect.any(Number) },
|
|
`
|
|
{
|
|
"action": "PERMISSIVE",
|
|
"check": null,
|
|
"command": "UPDATE",
|
|
"definition": "(current_setting('my.username'::text) = name)",
|
|
"id": Any<Number>,
|
|
"name": "categories_update_policy",
|
|
"roles": [
|
|
"postgres",
|
|
],
|
|
"schema": "public",
|
|
"table": "category",
|
|
"table_id": Any<Number>,
|
|
}
|
|
`
|
|
)
|
|
})
|
|
|
|
withTestDatabase('list policies with included schemas', async ({ executeQuery }) => {
|
|
const { sql, zod } = pgMeta.policies.list({
|
|
includedSchemas: ['public'],
|
|
})
|
|
const res = zod.parse(await executeQuery(sql))
|
|
|
|
expect(res.length).toBeGreaterThan(0)
|
|
res.forEach((policy) => {
|
|
expect(policy.schema).toBe('public')
|
|
})
|
|
})
|
|
|
|
withTestDatabase('retrieve, create, update, delete policies', async ({ executeQuery }) => {
|
|
// Create policy
|
|
const { sql: createSql } = pgMeta.policies.create({
|
|
name: 'test_policy',
|
|
schema: 'public',
|
|
table: 'memes',
|
|
action: 'RESTRICTIVE',
|
|
})
|
|
await executeQuery(createSql)
|
|
|
|
// List to get the created policy
|
|
const { sql: listSql, zod: listZod } = pgMeta.policies.list()
|
|
const policies = listZod.parse(await executeQuery(listSql))
|
|
const createdPolicy = policies.find((p) => p.name === 'test_policy')
|
|
|
|
expect(createdPolicy).toMatchInlineSnapshot(
|
|
{ id: expect.any(Number), table_id: expect.any(Number) },
|
|
`
|
|
{
|
|
"action": "RESTRICTIVE",
|
|
"check": null,
|
|
"command": "ALL",
|
|
"definition": null,
|
|
"id": Any<Number>,
|
|
"name": "test_policy",
|
|
"roles": [
|
|
"public",
|
|
],
|
|
"schema": "public",
|
|
"table": "memes",
|
|
"table_id": Any<Number>,
|
|
}
|
|
`
|
|
)
|
|
|
|
// Update policy
|
|
const { sql: updateSql } = pgMeta.policies.update(createdPolicy!, {
|
|
name: 'updated_policy',
|
|
definition: safeSql`current_setting('my.username') IN (name)`,
|
|
check: safeSql`current_setting('my.username') IN (name)`,
|
|
roles: ['postgres'],
|
|
})
|
|
await executeQuery(updateSql)
|
|
|
|
// Retrieve updated policy
|
|
const { sql: retrieveSql, zod: retrieveZod } = pgMeta.policies.retrieve({
|
|
id: createdPolicy!.id,
|
|
})
|
|
const updatedPolicy = retrieveZod.parse((await executeQuery(retrieveSql))[0])
|
|
|
|
expect(updatedPolicy).toMatchInlineSnapshot(
|
|
{ id: expect.any(Number), table_id: expect.any(Number) },
|
|
`
|
|
{
|
|
"action": "RESTRICTIVE",
|
|
"check": "(current_setting('my.username'::text) = name)",
|
|
"command": "ALL",
|
|
"definition": "(current_setting('my.username'::text) = name)",
|
|
"id": Any<Number>,
|
|
"name": "updated_policy",
|
|
"roles": [
|
|
"postgres",
|
|
],
|
|
"schema": "public",
|
|
"table": "memes",
|
|
"table_id": Any<Number>,
|
|
}
|
|
`
|
|
)
|
|
|
|
// Remove policy
|
|
const { sql: removeSql } = pgMeta.policies.remove(updatedPolicy!)
|
|
await executeQuery(removeSql)
|
|
|
|
// Verify policy is removed
|
|
const { sql: verifyRemoveSql } = pgMeta.policies.retrieve({
|
|
id: updatedPolicy!.id,
|
|
})
|
|
const result = await executeQuery(verifyRemoveSql)
|
|
expect(result).toHaveLength(0)
|
|
})
|