mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Refactor / dependency cleanup. ## What is the current behavior? `apps/studio` lists both `@supabase/pg-meta` (workspace package) as a runtime dep and `@supabase/postgres-meta` (external npm package, `^0.64.4`) as a devDependency. The external package is used only for type imports across 44 files — there is no runtime usage and no codegen pipeline that needs it. ## What is the new behavior? Every `Postgres*` type import (`PostgresTable`, `PostgresColumn`, `PostgresPolicy`, `PostgresTrigger`, `PostgresView`, `PostgresMaterializedView`, `PostgresForeignTable`, `PostgresSchema`, `PostgresPublication`, `PostgresRelationship`, `PostgresPrimaryKey`) is replaced with its `PG*` counterpart from `@supabase/pg-meta`, and the external dep is removed from \`apps/studio/package.json\`. Top-level type re-exports were added to \`packages/pg-meta/src/index.ts\` so consumers can import directly from the package root. Two latent issues surfaced by the stricter pg-meta types are also fixed: - \`data/foreign-tables/foreign-tables-query.ts\` was casting foreign-table results as \`PostgresView[]\`; corrected to \`PGForeignTable[]\`. - \`pg-meta\`'s \`PGTrigger\` Zod schema declared \`orientation\`/\`activation\` as \`z.string()\`, inconsistent with pg-meta's own \`getDatabaseTriggerUpdateSQL\` helper that requires the narrow literal unions; tightened to \`z.enum\`. ## Additional context <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Updated internal TypeScript type definitions across the codebase to use the latest type system from `@supabase/pg-meta`. * Removed `@supabase/postgres-meta` dependency. * Enhanced type validation for database triggers and schemas to enforce stricter constraints. [](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45844) <!-- end of auto-generated comment: release notes by coderabbit.ai -->
111 lines
3.9 KiB
TypeScript
111 lines
3.9 KiB
TypeScript
import type { PGPublication, PGTable } from '@supabase/pg-meta'
|
|
import { PermissionAction } from '@supabase/shared-types/out/constants'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { Badge, Switch, TableCell, TableRow, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
|
|
|
|
import { useDatabasePublicationUpdateMutation } from '@/data/database-publications/database-publications-update-mutation'
|
|
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { useProtectedSchemas } from '@/hooks/useProtectedSchemas'
|
|
|
|
interface PublicationsTableItemProps {
|
|
table: PGTable
|
|
selectedPublication: PGPublication
|
|
}
|
|
|
|
export const PublicationsTableItem = ({
|
|
table,
|
|
selectedPublication,
|
|
}: PublicationsTableItemProps) => {
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const { data: protectedSchemas } = useProtectedSchemas()
|
|
const enabledForAllTables = selectedPublication.tables == null
|
|
|
|
const isProtected = protectedSchemas.map((x) => x.name).includes(table.schema)
|
|
|
|
const [checked, setChecked] = useState(
|
|
selectedPublication.tables?.find((x: any) => x.id == table.id) != undefined
|
|
)
|
|
|
|
const { can: canUpdatePublications } = useAsyncCheckPermissions(
|
|
PermissionAction.TENANT_SQL_ADMIN_WRITE,
|
|
'publications'
|
|
)
|
|
|
|
const { mutate: updatePublications, isPending } = useDatabasePublicationUpdateMutation()
|
|
|
|
const toggleReplicationForTable = async (table: PGTable, publication: PGPublication) => {
|
|
if (project === undefined) return console.error('Project is required')
|
|
|
|
const originalChecked = checked
|
|
setChecked(!checked)
|
|
|
|
const publicationTables = publication?.tables ?? []
|
|
const exists = publicationTables.some((x: any) => x.id == table.id)
|
|
const tables = !exists
|
|
? [`${table.schema}.${table.name}`].concat(
|
|
publicationTables.map((t: any) => `${t.schema}.${t.name}`)
|
|
)
|
|
: publicationTables
|
|
.filter((x: any) => x.id != table.id)
|
|
.map((x: any) => `${x.schema}.${x.name}`)
|
|
|
|
updatePublications(
|
|
{
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
id: publication.id,
|
|
tables,
|
|
},
|
|
{
|
|
onSuccess: () => {
|
|
toast.success(
|
|
`Successfully ${checked ? 'disabled' : 'enabled'} replication for ${table.name}`
|
|
)
|
|
},
|
|
onError: (error) => {
|
|
toast.error(`Failed to toggle replication for ${table.name}: ${error.message}`)
|
|
setChecked(originalChecked)
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
return (
|
|
<TableRow key={table.id}>
|
|
<TableCell className="py-3 whitespace-nowrap">{table.name}</TableCell>
|
|
<TableCell className="py-3 whitespace-nowrap text-foreground-light">{table.schema}</TableCell>
|
|
<TableCell className="py-3 whitespace-nowrap hidden lg:table-cell max-w-sm truncate text-foreground-light">
|
|
{table.comment}
|
|
</TableCell>
|
|
<TableCell className="py-3">
|
|
<div className="flex justify-end gap-2">
|
|
{enabledForAllTables ? (
|
|
<Badge>
|
|
<span>Enabled</span>
|
|
<span className="hidden lg:inline-block"> for all tables</span>
|
|
</Badge>
|
|
) : (
|
|
<Tooltip>
|
|
<TooltipTrigger>
|
|
<Switch
|
|
size="small"
|
|
disabled={!canUpdatePublications || isPending || isProtected}
|
|
checked={checked}
|
|
onClick={() => toggleReplicationForTable(table, selectedPublication)}
|
|
/>
|
|
</TooltipTrigger>
|
|
{isProtected && (
|
|
<TooltipContent side="bottom" className="w-64 text-center">
|
|
This table belongs to a protected schema, and its publication cannot be toggled
|
|
</TooltipContent>
|
|
)}
|
|
</Tooltip>
|
|
)}
|
|
</div>
|
|
</TableCell>
|
|
</TableRow>
|
|
)
|
|
}
|