Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions apps/studio/components/grid/components/grid/ColumnHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pgMeta from '@supabase/pg-meta'
import { getForeignKeyCascadeAction } from 'components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils'
import type { XYCoord } from 'dnd-core'
import { ArrowRight, Key, Lightbulb, Link, Lock } from 'lucide-react'
import { useEffect, useRef } from 'react'
import { useDrag, useDrop } from 'react-dnd'

import { getForeignKeyCascadeAction } from 'components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils'
import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
import { useTableEditorTableStateSnapshot } from 'state/table-editor-table'
import { Tooltip, TooltipContent, TooltipTrigger } from 'ui'

import {
useColumnHasIndexSuggestion,
useTableIndexAdvisor,
Expand Down Expand Up @@ -206,12 +206,14 @@ function renderColumnIcon(
{foreignKey?.targetColumnName}
</p>
</div>
{foreignKey?.updateAction !== FOREIGN_KEY_CASCADE_ACTION.NO_ACTION && (
{foreignKey?.updateAction !==
pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION && (
<p className="text-xs !text-foreground mt-1">
On update: {getForeignKeyCascadeAction(foreignKey?.updateAction)}
</p>
)}
{foreignKey?.deletionAction !== FOREIGN_KEY_CASCADE_ACTION.NO_ACTION && (
{foreignKey?.deletionAction !==
pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION && (
<p className="text-xs !text-foreground mt-1">
On delete: {getForeignKeyCascadeAction(foreignKey?.deletionAction)}
</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pgMeta from '@supabase/pg-meta'
import type { PostgresColumn } from '@supabase/postgres-meta'
import { isNull } from 'lodash'
import type { Dictionary } from 'types'

import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
import type { ForeignKeyConstraint } from 'data/database/foreign-key-constraints-query'
import type { RetrievedTableColumn, RetrieveTableResult } from 'data/tables/table-retrieve-query'
import { uuidv4 } from 'lib/helpers'
import { isNull } from 'lodash'
import { toast } from 'sonner'
import type { Dictionary } from 'types'

import {
ColumnField,
CreateColumnPayload,
Expand Down Expand Up @@ -229,8 +229,10 @@ export const getColumnForeignKey = (
const foreignKeyMeta = foreignKeys.find((fk) => fk.id === foreignKey.id)
return {
...foreignKey,
deletion_action: foreignKeyMeta?.deletion_action ?? FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
update_action: foreignKeyMeta?.update_action ?? FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
deletion_action:
foreignKeyMeta?.deletion_action ?? pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
update_action:
foreignKeyMeta?.update_action ?? pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
}
}
}
Expand All @@ -243,13 +245,13 @@ const formatArrayToPostgresArray = (arrayString: string) => {

export const getForeignKeyCascadeAction = (action?: string) => {
switch (action) {
case FOREIGN_KEY_CASCADE_ACTION.CASCADE:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.CASCADE:
return 'Cascade'
case FOREIGN_KEY_CASCADE_ACTION.RESTRICT:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.RESTRICT:
return 'Restrict'
case FOREIGN_KEY_CASCADE_ACTION.SET_DEFAULT:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.SET_DEFAULT:
return 'Set default'
case FOREIGN_KEY_CASCADE_ACTION.SET_NULL:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.SET_NULL:
return 'Set NULL'
default:
return undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
import pgMeta from '@supabase/pg-meta'

export const FOREIGN_KEY_CASCADE_OPTIONS = [
{ key: 'no-action', label: 'No action', value: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION },
{ key: 'cascade', label: 'Cascade', value: FOREIGN_KEY_CASCADE_ACTION.CASCADE },
{ key: 'restrict', label: 'Restrict', value: FOREIGN_KEY_CASCADE_ACTION.RESTRICT },
{ key: 'set-default', label: 'Set default', value: FOREIGN_KEY_CASCADE_ACTION.SET_DEFAULT },
{ key: 'set-null', label: 'Set NULL', value: FOREIGN_KEY_CASCADE_ACTION.SET_NULL },
{
key: 'no-action',
label: 'No action',
value: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
},
{
key: 'cascade',
label: 'Cascade',
value: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.CASCADE,
},
{
key: 'restrict',
label: 'Restrict',
value: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.RESTRICT,
},
{
key: 'set-default',
label: 'Set default',
value: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.SET_DEFAULT,
},
{
key: 'set-null',
label: 'Set NULL',
value: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.SET_NULL,
},
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PostgresTable } from '@supabase/postgres-meta'
import pgMeta from '@supabase/pg-meta'
import { sortBy } from 'lodash'
import { ArrowRight, Database, HelpCircle, Loader2, Table, X } from 'lucide-react'
import { Fragment, useEffect, useState } from 'react'
Expand All @@ -13,7 +14,6 @@ import {

import { DocsButton } from 'components/ui/DocsButton'
import InformationBox from 'components/ui/InformationBox'
import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
import { useSchemasQuery } from 'data/database/schemas-query'
import { useTableQuery } from 'data/tables/table-retrieve-query'
import { useTablesQuery } from 'data/tables/tables-query'
Expand All @@ -33,8 +33,8 @@ const EMPTY_STATE: ForeignKey = {
schema: 'public',
table: '',
columns: [] as { source: string; target: string }[],
deletionAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
updateAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
deletionAction: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
updateAction: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
}

interface ForeignKeySelectorProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
import pgMeta from '@supabase/pg-meta'
import type { ForeignKeyConstraint } from 'data/database/foreign-key-constraints-query'
import { HelpCircle } from 'lucide-react'
import { Tooltip, TooltipContent, TooltipTrigger } from 'ui'
Expand Down Expand Up @@ -29,7 +29,7 @@ export const generateCascadeActionDescription = (
const actionName = getForeignKeyCascadeAction(cascadeAction) ?? 'No action'

switch (cascadeAction) {
case FOREIGN_KEY_CASCADE_ACTION.NO_ACTION:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION:
return (
<>
<span className="text-foreground-light">{actionName}</span>: {actionVerb} a record from{' '}
Expand All @@ -38,7 +38,7 @@ export const generateCascadeActionDescription = (
existing in this table that reference it
</>
)
case FOREIGN_KEY_CASCADE_ACTION.CASCADE:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.CASCADE:
return (
<>
<span className="text-foreground-light">{actionName}</span>: {actionVerb} a record from{' '}
Expand All @@ -47,7 +47,7 @@ export const generateCascadeActionDescription = (
reference it in this table
</>
)
case FOREIGN_KEY_CASCADE_ACTION.RESTRICT:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.RESTRICT:
return (
<>
<span className="text-foreground-light">{actionName}</span>
Expand All @@ -65,7 +65,7 @@ export const generateCascadeActionDescription = (
existing referencing rows from this table.
</>
)
case FOREIGN_KEY_CASCADE_ACTION.SET_DEFAULT:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.SET_DEFAULT:
return (
<>
<span className="text-foreground-light">{actionName}</span>: {actionVerb} a record from{' '}
Expand All @@ -74,7 +74,7 @@ export const generateCascadeActionDescription = (
<span className="text-amber-900 opacity-75">default value</span>
</>
)
case FOREIGN_KEY_CASCADE_ACTION.SET_NULL:
case pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.SET_NULL:
return (
<>
<span className="text-foreground-light">{actionName}</span>: {actionVerb} a record from{' '}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
import pgMeta from '@supabase/pg-meta'
import type { ForeignKey } from './ForeignKeySelector/ForeignKeySelector.types'
import type { ColumnField } from './SidePanelEditor.types'

Expand Down Expand Up @@ -288,8 +288,8 @@ describe('createTable', () => {
schema: 'public',
table: 'users',
columns: [{ source: 'user_id', target: 'id' }],
deletionAction: FOREIGN_KEY_CASCADE_ACTION.CASCADE,
updateAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
deletionAction: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.CASCADE,
updateAction: pgMeta.tableEditor.FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
},
]

Expand All @@ -306,7 +306,7 @@ describe('createTable', () => {
const sqlCall = mockExecuteSql.mock.calls[0][0]
expect(sqlCall.sql).toContain('ADD FOREIGN KEY')
expect(sqlCall.sql).toContain('REFERENCES')
expect(sqlCall.sql).toContain('"users"')
expect(sqlCall.sql).toContain('users')
expect(sqlCall.sql).toContain('ON DELETE CASCADE')
})

Expand Down
Loading
Loading