From 9def73b2691399e2e88fe6e6c6aeec6b7ba7bb0e Mon Sep 17 00:00:00 2001 From: Alaister Young Date: Thu, 2 Nov 2023 22:41:28 +1100 Subject: [PATCH] fix: enums in other schemas (#18653) --- .../CreateEnumeratedTypeSidePanel.tsx | 3 +++ .../EnumeratedTypes/EditEnumeratedTypeSidePanel.tsx | 6 ++++-- .../Database/EnumeratedTypes/EnumeratedTypes.tsx | 9 ++++++--- .../enumerated-type-create-mutation.ts | 8 ++++++-- .../enumerated-type-update-mutation.ts | 12 +++++++----- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/studio/components/interfaces/Database/EnumeratedTypes/CreateEnumeratedTypeSidePanel.tsx b/studio/components/interfaces/Database/EnumeratedTypes/CreateEnumeratedTypeSidePanel.tsx index 79e3ec4be9917..cc446dc5bfd68 100644 --- a/studio/components/interfaces/Database/EnumeratedTypes/CreateEnumeratedTypeSidePanel.tsx +++ b/studio/components/interfaces/Database/EnumeratedTypes/CreateEnumeratedTypeSidePanel.tsx @@ -32,11 +32,13 @@ import EnumeratedTypeValueRow from './EnumeratedTypeValueRow' interface CreateEnumeratedTypeSidePanelProps { visible: boolean onClose: () => void + schema: string } const CreateEnumeratedTypeSidePanel = ({ visible, onClose, + schema, }: CreateEnumeratedTypeSidePanelProps) => { const submitRef = useRef(null) const { project } = useProjectContext() @@ -80,6 +82,7 @@ const CreateEnumeratedTypeSidePanel = ({ createEnumeratedType({ projectRef: project.ref, connectionString: project.connectionString, + schema, name: data.name, description: data.description?.replaceAll("'", "''"), values: data.values.filter((x) => x.value.length > 0).map((x) => x.value), diff --git a/studio/components/interfaces/Database/EnumeratedTypes/EditEnumeratedTypeSidePanel.tsx b/studio/components/interfaces/Database/EnumeratedTypes/EditEnumeratedTypeSidePanel.tsx index 81f517b1465af..b9d6e352d70a1 100644 --- a/studio/components/interfaces/Database/EnumeratedTypes/EditEnumeratedTypeSidePanel.tsx +++ b/studio/components/interfaces/Database/EnumeratedTypes/EditEnumeratedTypeSidePanel.tsx @@ -44,8 +44,8 @@ const EditEnumeratedTypeSidePanel = ({ const submitRef = useRef(null) const { project } = useProjectContext() const { mutate: updateEnumeratedType, isLoading: isCreating } = useEnumeratedTypeUpdateMutation({ - onSuccess: () => { - toast.success(`Successfully updated type "${name}"`) + onSuccess: (_, vars) => { + toast.success(`Successfully updated type "${vars.name.updated}"`) onClose() }, }) @@ -97,10 +97,12 @@ const EditEnumeratedTypeSidePanel = ({ return console.error('selectedEnumeratedType required') const payload: { + schema: string name: { original: string; updated: string } values: { original: string; updated: string; isNew: boolean }[] description?: string } = { + schema: selectedEnumeratedType.schema, name: { original: selectedEnumeratedType.name, updated: data.name }, values: data.values .filter((x) => x.updatedValue.length !== 0) diff --git a/studio/components/interfaces/Database/EnumeratedTypes/EnumeratedTypes.tsx b/studio/components/interfaces/Database/EnumeratedTypes/EnumeratedTypes.tsx index cfee5a7bdd021..9d72cc5c0a3a7 100644 --- a/studio/components/interfaces/Database/EnumeratedTypes/EnumeratedTypes.tsx +++ b/studio/components/interfaces/Database/EnumeratedTypes/EnumeratedTypes.tsx @@ -78,9 +78,11 @@ const EnumeratedTypes = () => { icon={} /> - + {!isLocked && ( + + )} {isLocked && } @@ -173,6 +175,7 @@ const EnumeratedTypes = () => { setShowCreateTypePanel(false)} + schema={selectedSchema} /> `'${x}'`).join(', ')});` + const createSql = `create type "${schema}"."${name}" as enum (${values + .map((x) => `'${x}'`) + .join(', ')});` const commentSql = - description !== undefined ? `comment on type "${name}" is '${description}';` : '' + description !== undefined ? `comment on type "${schema}"."${name}" is '${description}';` : '' const sql = wrapWithTransaction(`${createSql} ${commentSql}`) const { result } = await executeSql({ projectRef, connectionString, sql }) return result diff --git a/studio/data/enumerated-types/enumerated-type-update-mutation.ts b/studio/data/enumerated-types/enumerated-type-update-mutation.ts index e97b4b74ffcc9..6048694212d78 100644 --- a/studio/data/enumerated-types/enumerated-type-update-mutation.ts +++ b/studio/data/enumerated-types/enumerated-type-update-mutation.ts @@ -9,6 +9,7 @@ import { wrapWithTransaction } from 'data/sql/utils/transaction' export type EnumeratedTypeUpdateVariables = { projectRef: string connectionString: string + schema: string name: { original: string; updated: string } description?: string values?: { original: string; updated: string; isNew: boolean }[] @@ -17,13 +18,14 @@ export type EnumeratedTypeUpdateVariables = { export async function updateEnumeratedType({ projectRef, connectionString, + schema, name, description, values = [], }: EnumeratedTypeUpdateVariables) { const statements: string[] = [] if (name.original !== name.updated) { - statements.push(`alter type "${name.original}" rename to "${name.updated}";`) + statements.push(`alter type "${schema}"."${name.original}" rename to "${name.updated}";`) } if (values.length > 0) { values.forEach((x, idx) => { @@ -32,24 +34,24 @@ export async function updateEnumeratedType({ // Consider if any new enums were added before any existing enums const firstExistingEnumValue = values.find((x) => !x.isNew) statements.push( - `alter type "${name.updated}" add value '${x.updated}' before '${firstExistingEnumValue?.original}';` + `alter type "${schema}"."${name.updated}" add value '${x.updated}' before '${firstExistingEnumValue?.original}';` ) } else { statements.push( - `alter type "${name.updated}" add value '${x.updated}' after '${ + `alter type "${schema}"."${name.updated}" add value '${x.updated}' after '${ values[idx - 1].updated }';` ) } } else if (x.original !== x.updated) { statements.push( - `alter type "${name.updated}" rename value '${x.original}' to '${x.updated}';` + `alter type "${schema}"."${name.updated}" rename value '${x.original}' to '${x.updated}';` ) } }) } if (description !== undefined) { - statements.push(`comment on type "${name.updated}" is '${description}';`) + statements.push(`comment on type "${schema}"."${name.updated}" is '${description}';`) } const sql = wrapWithTransaction(statements.join(' '))