Skip to content

Commit

Permalink
fix: enums in other schemas (supabase#18653)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaister authored Nov 2, 2023
1 parent 91da3bf commit 9def73b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLButtonElement>(null)
const { project } = useProjectContext()
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const EditEnumeratedTypeSidePanel = ({
const submitRef = useRef<HTMLButtonElement>(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()
},
})
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ const EnumeratedTypes = () => {
icon={<IconSearch size={14} />}
/>
</div>
<Button type="primary" onClick={() => setShowCreateTypePanel(true)}>
Create type
</Button>
{!isLocked && (
<Button type="primary" onClick={() => setShowCreateTypePanel(true)}>
Create type
</Button>
)}
</div>

{isLocked && <ProtectedSchemaWarning schema={selectedSchema} entity="enumerated types" />}
Expand Down Expand Up @@ -173,6 +175,7 @@ const EnumeratedTypes = () => {
<CreateEnumeratedTypeSidePanel
visible={showCreateTypePanel}
onClose={() => setShowCreateTypePanel(false)}
schema={selectedSchema}
/>

<EditEnumeratedTypeSidePanel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { wrapWithTransaction } from 'data/sql/utils/transaction'
export type EnumeratedTypeCreateVariables = {
projectRef: string
connectionString: string
schema: string
name: string
description?: string
values: string[]
Expand All @@ -17,13 +18,16 @@ export type EnumeratedTypeCreateVariables = {
export async function createEnumeratedType({
projectRef,
connectionString,
schema,
name,
description,
values,
}: EnumeratedTypeCreateVariables) {
const createSql = `create type "${name}" as enum (${values.map((x) => `'${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
Expand Down
12 changes: 7 additions & 5 deletions studio/data/enumerated-types/enumerated-type-update-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }[]
Expand All @@ -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) => {
Expand All @@ -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(' '))
Expand Down

0 comments on commit 9def73b

Please sign in to comment.