Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

implement delete index functionality

Joshen Lim630d1404d012
diff --git a/studio/components/interfaces/Database/Indexes/ConfirmDeleteIndexModal.tsx b/studio/components/interfaces/Database/Indexes/ConfirmDeleteIndexModal.tsx
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/studio/components/interfaces/Database/Indexes/CreateIndexSidePanel.tsx b/studio/components/interfaces/Database/Indexes/CreateIndexSidePanel.tsx
index 85176b28042..01c6a54e55e 100644
--- a/studio/components/interfaces/Database/Indexes/CreateIndexSidePanel.tsx
+++ b/studio/components/interfaces/Database/Indexes/CreateIndexSidePanel.tsx
@@ -27,7 +27,7 @@ const CreateIndexSidePanel = ({ visible, onClose }: CreateIndexSidePanelProps) =
const [selectedColumns, setSelectedColumns] = useState<string[]>([])
const [selectedIndexType, setSelectedIndexType] = useState<string>(INDEX_TYPES[0].value)
- const { refetch: refetchSchemaIndexes } = useIndexesQuery({
+ const { refetch: refetchIndexes } = useIndexesQuery({
schema: selectedSchema,
projectRef: project?.ref,
connectionString: project?.connectionString,
@@ -57,8 +57,8 @@ const CreateIndexSidePanel = ({ visible, onClose }: CreateIndexSidePanelProps) =
})
const { mutate: execute, isLoading: isExecuting } = useExecuteSqlMutation({
- onSuccess(data) {
- refetchSchemaIndexes()
+ onSuccess() {
+ refetchIndexes()
onClose()
ui.setNotification({ category: 'success', message: `Successfully created index` })
},
diff --git a/studio/components/interfaces/Database/Indexes/Indexes.tsx b/studio/components/interfaces/Database/Indexes/Indexes.tsx
index a4893bafa54..ca35fc12275 100644
--- a/studio/components/interfaces/Database/Indexes/Indexes.tsx
+++ b/studio/components/interfaces/Database/Indexes/Indexes.tsx
@@ -2,12 +2,18 @@ import { observer } from 'mobx-react-lite'
import Link from 'next/link'
import { useState } from 'react'
import {
+ AlertDescription_Shadcn_,
+ AlertTitle_Shadcn_,
+ Alert_Shadcn_,
Button,
IconAlertCircle,
+ IconAlertTriangle,
IconExternalLink,
IconSearch,
+ IconTrash,
Input,
Listbox,
+ Modal,
SidePanel,
} from 'ui'
@@ -19,16 +25,22 @@ import { useSchemasQuery } from 'data/database/schemas-query'
import Table from 'components/to-be-cleaned/Table'
import CodeEditor from 'components/ui/CodeEditor'
import CreateIndexSidePanel from './CreateIndexSidePanel'
+import ConfirmationModal from 'components/ui/ConfirmationModal'
+import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
+import { useStore } from 'hooks'
const Indexes = () => {
+ const { ui } = useStore()
const [search, setSearch] = useState('')
const [selectedSchema, setSelectedSchema] = useState('public')
- const [selectedIndex, setSelectedIndex] = useState<DatabaseIndex>()
const [showCreateIndex, setShowCreateIndex] = useState(false)
+ const [selectedIndex, setSelectedIndex] = useState<DatabaseIndex>()
+ const [selectedIndexToDelete, setSelectedIndexToDelete] = useState<DatabaseIndex>()
const { project } = useProjectContext()
const {
data: allIndexes,
+ refetch: refetchIndexes,
error: indexesError,
isLoading: isLoadingIndexes,
isSuccess: isSuccessIndexes,
@@ -48,6 +60,21 @@ const Indexes = () => {
connectionString: project?.connectionString,
})
+ const { mutate: execute, isLoading: isExecuting } = useExecuteSqlMutation({
+ onSuccess() {
+ refetchIndexes()
+ setSelectedIndexToDelete(undefined)
+ ui.setNotification({ category: 'success', message: `Successfully deleted index` })
+ },
+ onError(error) {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Failed to delete index: ${error.message}`,
+ })
+ },
+ })
+
const sortedIndexes = (allIndexes?.result ?? []).sort(
(a, b) => a.table.localeCompare(b.table) || a.name.localeCompare(b.name)
)
@@ -56,6 +83,15 @@ const Indexes = () => {
? sortedIndexes.filter((index) => index.name.includes(search) || index.table.includes(search))
: sortedIndexes
+ const onConfirmDeleteIndex = (index: DatabaseIndex) => {
+ if (!project) return console.error('Project is required')
+ execute({
+ projectRef: project.ref,
+ connectionString: project.connectionString,
+ sql: `drop index if exists "${index.name}"`,
+ })
+ }
+
return (
<>
<div className="pb-8">
@@ -179,10 +215,16 @@ const Indexes = () => {
<p title={index.name}>{index.name}</p>
</Table.td>
<Table.td>
- <div className="flex justify-end items-center">
+ <div className="flex justify-end items-center space-x-2">
<Button type="default" onClick={() => setSelectedIndex(index)}>
View definition
</Button>
+ <Button
+ type="default"
+ className="px-1"
+ icon={<IconTrash />}
+ onClick={() => setSelectedIndexToDelete(index)}
+ />
</div>
</Table.td>
</Table.tr>
@@ -216,6 +258,48 @@ const Indexes = () => {
</SidePanel>
<CreateIndexSidePanel visible={showCreateIndex} onClose={() => setShowCreateIndex(false)} />
+
+ <ConfirmationModal
+ danger
+ size="medium"
+ loading={isExecuting}
+ visible={selectedIndexToDelete !== undefined}
+ header={
+ <>
+ Confirm to delete index <code className="text-sm">{selectedIndexToDelete?.name}</code>
+ </>
+ }
+ buttonLabel="Confirm delete"
+ buttonLoadingLabel="Deleting..."
+ onSelectConfirm={() =>
+ selectedIndexToDelete !== undefined ? onConfirmDeleteIndex(selectedIndexToDelete) : {}
+ }
+ onSelectCancel={() => setSelectedIndexToDelete(undefined)}
+ >
+ <Modal.Content>
+ <div className="py-6">
+ <Alert_Shadcn_ variant="warning">
+ <IconAlertTriangle strokeWidth={2} />
+ <AlertTitle_Shadcn_>This action cannot be undone</AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ Deleting an index that is still in use will cause queries to slow down, in some
+ cases causing major issues.
+ </AlertDescription_Shadcn_>
+ </Alert_Shadcn_>
+ <ul className="mt-4 space-y-5">
+ <li className="flex gap-3">
+ <div>
+ <strong className="text-sm">Before deleting this index, consider:</strong>
+ <ul className="space-y-2 mt-2 text-sm text-light">
+ <li className="list-disc ml-6">This index is no longer in use</li>
+ <li className="list-disc ml-6">Disabling the index instead of deleting it</li>
+ </ul>
+ </div>
+ </li>
+ </ul>
+ </div>
+ </Modal.Content>
+ </ConfirmationModal>
</>
)
}
diff --git a/studio/data/database/indexes-query.ts b/studio/data/database/indexes-query.ts
index a1ad90d860a..ca24b67c786 100644
--- a/studio/data/database/indexes-query.ts
+++ b/studio/data/database/indexes-query.ts
@@ -9,6 +9,7 @@ export type DatabaseIndex = {
schema: string
table: string
definition: string
+ enabled: boolean
}
export const getIndexesQuery = ({ schema }: { schema: string }) => {
@@ -16,8 +17,10 @@ export const getIndexesQuery = ({ schema }: { schema: string }) => {
SELECT schemaname as "schema",
tablename as "table",
indexname as "name",
- indexdef as "definition"
-FROM pg_indexes
+ indexdef as "definition",
+ indisvalid as "enabled"
+FROM pg_indexes a
+ LEFT JOIN pg_index b ON b.indexrelid = a.indexname::regclass
WHERE schemaname = '${schema}';
`.trim()