From 6c592dec9934b2d8693c286f4b8dcfbfc042db5d Mon Sep 17 00:00:00 2001 From: Alaister Young Date: Mon, 18 Nov 2024 13:15:37 +0800 Subject: [PATCH] chore: remove useExecuteSqlQuery() part 2 (#30467) * foreign-key-constraints * update entity-types stale time * schemas query * deprecate useExecuteSqlQuery * users count query * database size query * indexes query * keywords query * migrations query * table columns * database functions * database roles query * fdws query * replication lag query * ongoing queries query * vault secrets query * remove unneeded staleTime: 0 * max connections query * fix entity types key in tests * Some fixes --------- Co-authored-by: Joshen Lim --- .../interfaces/Auth/Users/UsersV2.tsx | 3 +- .../Database/Indexes/CreateIndexSidePanel.tsx | 14 +-- .../interfaces/Database/Indexes/Indexes.tsx | 8 +- .../Database/Migrations/Migrations.tsx | 8 +- .../interfaces/Database/Roles/RolesList.tsx | 106 ++++++++---------- .../Database/Wrappers/EditWrapper.tsx | 2 +- .../interfaces/Database/Wrappers/Wrappers.tsx | 2 +- .../DiskManagement/ui/DiskSpaceBar.tsx | 4 +- .../Database/DiskSizeConfiguration.tsx | 2 +- .../Settings/Vault/Secrets/SecretRow.tsx | 4 +- .../SidePanelEditor/SidePanelEditor.tsx | 5 +- .../SidePanelEditor/SidePanelEditor.utils.tsx | 3 +- .../DatabaseLayout/Database.Commands.tsx | 2 +- .../ProjectIntegrations.Commands.tsx | 2 +- .../ui/AIAssistantPanel/AIAssistantPanel.tsx | 4 +- apps/studio/data/__templates/README.md | 4 +- .../create-and-expose-api-schema-mutation.ts | 4 +- apps/studio/data/auth/keys.ts | 15 ++- apps/studio/data/auth/user-create-mutation.ts | 5 +- apps/studio/data/auth/user-delete-mutation.ts | 3 +- apps/studio/data/auth/user-invite-mutation.ts | 4 +- apps/studio/data/auth/users-count-query.ts | 60 +++++++--- apps/studio/data/auth/users-infinite-query.ts | 2 - .../database-column-delete-mutation.ts | 5 +- .../database-functions-create-mutation.ts | 4 +- .../database-functions-delete-mutation.ts | 4 +- .../database-functions-query.ts | 45 +++++--- .../database-functions-update-mutation.ts | 4 +- .../database-roles/database-roles-query.ts | 36 +++--- apps/studio/data/database-roles/keys.ts | 4 + .../database-triggers-query.ts | 1 - .../data/database/database-size-query.ts | 46 +++++--- .../database/foreign-key-constraints-query.ts | 90 +++++++-------- apps/studio/data/database/indexes-query.ts | 67 ++++++----- apps/studio/data/database/keys.ts | 25 ++++- apps/studio/data/database/keywords-query.ts | 48 ++++---- .../data/database/max-connections-query.ts | 50 +++++---- apps/studio/data/database/migrations-query.ts | 64 +++++++---- apps/studio/data/database/schemas-query.ts | 52 +++++---- .../data/database/table-columns-query.ts | 45 +++++--- .../entity-types-infinite-query.ts | 3 +- .../enumerated-types-query.ts | 3 +- apps/studio/data/fdw/fdw-create-mutation.ts | 8 +- apps/studio/data/fdw/fdw-delete-mutation.ts | 8 +- apps/studio/data/fdw/fdw-update-mutation.ts | 8 +- apps/studio/data/fdw/fdws-query.ts | 42 ++++--- apps/studio/data/fdw/keys.ts | 3 + .../foreign-tables/foreign-tables-query.ts | 1 - .../materialized-views-query.ts | 2 + apps/studio/data/read-replicas/keys.ts | 2 + .../data/read-replicas/replica-lag-query.ts | 43 ++++--- apps/studio/data/sql/abort-query-mutation.ts | 2 +- apps/studio/data/sql/execute-sql-query.ts | 7 +- apps/studio/data/sql/keys.ts | 2 + apps/studio/data/sql/ongoing-queries-query.ts | 49 ++++---- apps/studio/data/table-rows/keys.ts | 6 +- apps/studio/data/tables/tables-query.ts | 2 +- .../vault/vault-secret-create-mutation.ts | 5 +- .../vault/vault-secret-delete-mutation.ts | 5 +- .../vault/vault-secret-update-mutation.ts | 5 +- apps/studio/data/vault/vault-secrets-query.ts | 42 ++++--- apps/studio/data/views/views-query.ts | 2 + .../pages/project/[ref]/reports/database.tsx | 6 +- apps/studio/pages/project/[ref]/sql/[id].tsx | 4 +- .../tests/snapshot/spec/table-editor.spec.ts | 4 +- 65 files changed, 640 insertions(+), 480 deletions(-) create mode 100644 apps/studio/data/database-roles/keys.ts create mode 100644 apps/studio/data/fdw/keys.ts diff --git a/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx b/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx index fe00bf292d..73d5dabc78 100644 --- a/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx +++ b/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx @@ -123,8 +123,7 @@ export const UsersV2 = () => { providers: selectedProviders, }) - // eslint-disable-next-line react-hooks/exhaustive-deps - const totalUsers = useMemo(() => countData?.result[0].count ?? 0, [countData?.result[0].count]) + const totalUsers = countData ?? 0 const users = useMemo(() => data?.pages.flatMap((page) => page.result) ?? [], [data?.pages]) const handleScroll = (event: UIEvent) => { diff --git a/apps/studio/components/interfaces/Database/Indexes/CreateIndexSidePanel.tsx b/apps/studio/components/interfaces/Database/Indexes/CreateIndexSidePanel.tsx index 983bb4f428..52d804eccb 100644 --- a/apps/studio/components/interfaces/Database/Indexes/CreateIndexSidePanel.tsx +++ b/apps/studio/components/interfaces/Database/Indexes/CreateIndexSidePanel.tsx @@ -1,3 +1,4 @@ +import { useQueryClient } from '@tanstack/react-query' import { Check, ChevronsUpDown, Loader2 } from 'lucide-react' import Link from 'next/link' import { useEffect, useMemo, useState } from 'react' @@ -6,7 +7,7 @@ import { toast } from 'sonner' import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext' import CodeEditor from 'components/ui/CodeEditor/CodeEditor' import ShimmeringLoader from 'components/ui/ShimmeringLoader' -import { useIndexesQuery } from 'data/database/indexes-query' +import { databaseKeys } from 'data/database/keys' import { useSchemasQuery } from 'data/database/schemas-query' import { useTableColumnsQuery } from 'data/database/table-columns-query' import { useEntityTypesQuery } from 'data/entity-types/entity-types-infinite-query' @@ -43,6 +44,7 @@ interface CreateIndexSidePanelProps { } const CreateIndexSidePanel = ({ visible, onClose }: CreateIndexSidePanelProps) => { + const queryClient = useQueryClient() const { project } = useProjectContext() const [selectedSchema, setSelectedSchema] = useState('public') const [selectedEntity, setSelectedEntity] = useState(undefined) @@ -51,11 +53,7 @@ const CreateIndexSidePanel = ({ visible, onClose }: CreateIndexSidePanelProps) = const [schemaDropdownOpen, setSchemaDropdownOpen] = useState(false) const [tableDropdownOpen, setTableDropdownOpen] = useState(false) const [searchTerm, setSearchTerm] = useState('') - const { refetch: refetchIndexes } = useIndexesQuery({ - schema: selectedSchema, - projectRef: project?.ref, - connectionString: project?.connectionString, - }) + const { data: schemas } = useSchemasQuery({ projectRef: project?.ref, connectionString: project?.connectionString, @@ -80,7 +78,7 @@ const CreateIndexSidePanel = ({ visible, onClose }: CreateIndexSidePanelProps) = const { mutate: execute, isLoading: isExecuting } = useExecuteSqlMutation({ onSuccess: async () => { - await refetchIndexes() + await queryClient.invalidateQueries(databaseKeys.indexes(project?.ref, selectedSchema)) onClose() toast.success(`Successfully created index`) }, @@ -98,7 +96,7 @@ const CreateIndexSidePanel = ({ visible, onClose }: CreateIndexSidePanelProps) = setSearchTerm(value) } - const columns = tableColumns?.result[0]?.columns ?? [] + const columns = tableColumns?.[0]?.columns ?? [] const columnOptions: MultiSelectOption[] = columns .filter((column): column is NonNullable => column !== null) .map((column) => ({ diff --git a/apps/studio/components/interfaces/Database/Indexes/Indexes.tsx b/apps/studio/components/interfaces/Database/Indexes/Indexes.tsx index df7f4256d1..63c139672b 100644 --- a/apps/studio/components/interfaces/Database/Indexes/Indexes.tsx +++ b/apps/studio/components/interfaces/Database/Indexes/Indexes.tsx @@ -53,12 +53,12 @@ const Indexes = () => { }) const { mutate: execute, isLoading: isExecuting } = useExecuteSqlMutation({ - onSuccess() { - refetchIndexes() + onSuccess: async () => { + await refetchIndexes() setSelectedIndexToDelete(undefined) toast.success('Successfully deleted index') }, - onError(error) { + onError: (error) => { toast.error(`Failed to delete index: ${error.message}`) }, }) @@ -69,7 +69,7 @@ const Indexes = () => { const schema = schemas?.find((schema) => schema.name === selectedSchema) const isLocked = protectedSchemas.some((s) => s.id === schema?.id) - const sortedIndexes = sortBy(allIndexes?.result ?? [], (index) => index.name.toLocaleLowerCase()) + const sortedIndexes = sortBy(allIndexes ?? [], (index) => index.name.toLocaleLowerCase()) const indexes = search.length > 0 ? sortedIndexes.filter((index) => index.name.includes(search) || index.table.includes(search)) diff --git a/apps/studio/components/interfaces/Database/Migrations/Migrations.tsx b/apps/studio/components/interfaces/Database/Migrations/Migrations.tsx index 3b76562c52..3119bada86 100644 --- a/apps/studio/components/interfaces/Database/Migrations/Migrations.tsx +++ b/apps/studio/components/interfaces/Database/Migrations/Migrations.tsx @@ -22,8 +22,8 @@ const Migrations = () => { }) const migrations = search.length === 0 - ? data?.result ?? [] - : data?.result.filter( + ? data ?? [] + : data?.filter( (migration) => migration.version.includes(search) || migration.name?.includes(search) ) ?? [] @@ -62,9 +62,9 @@ const Migrations = () => { )} {isSuccess && (
- {data.result.length <= 0 && } + {data.length <= 0 && } - {data.result.length > 0 && ( + {data.length > 0 && ( <>
{
- - - - - {!canUpdateRoles && ( - - -
- You need additional permissions to add a new role -
-
- )} -
+ } + onClick={() => setIsCreatingRole(true)} + tooltip={{ + content: { + side: 'bottom', + text: !canUpdateRoles + ? 'You need additional permissions to add a new role' + : undefined, + }, + }} + > + Add role +
- {supabaseRoles.length > 0 && ( -
-
-

Roles managed by Supabase

- Protected -
- - {isLoading - ? Array.from({ length: 5 }).map((_, i) => ) - : supabaseRoles.map((role) => ( - - ))} +
+
+

Roles managed by Supabase

+ Protected
- )} - {otherRoles.length > 0 && ( -
-
-

Other database roles

-
+ {isLoading + ? Array.from({ length: 5 }).map((_, i) => ) + : supabaseRoles.map((role) => ( + + ))} +
- {isLoading - ? Array.from({ length: 3 }).map((_, i) => ) - : otherRoles.map((role) => ( - - ))} +
+
+

Other database roles

- )} + + {isLoading + ? Array.from({ length: 3 }).map((_, i) => ) + : otherRoles.map((role) => ( + + ))} +
{filterString.length > 0 && filteredRoles.length === 0 && ( diff --git a/apps/studio/components/interfaces/Database/Wrappers/EditWrapper.tsx b/apps/studio/components/interfaces/Database/Wrappers/EditWrapper.tsx index 6cab2ab1fe..af42e06dbd 100644 --- a/apps/studio/components/interfaces/Database/Wrappers/EditWrapper.tsx +++ b/apps/studio/components/interfaces/Database/Wrappers/EditWrapper.tsx @@ -49,7 +49,7 @@ const EditWrapper = () => { connectionString: project?.connectionString, }) - const wrappers = data?.result ?? [] + const wrappers = data ?? [] const foundWrapper = wrappers.find((w) => Number(w.id) === Number(id)) // this call to useImmutableValue should be removed if the redirect after update is also removed const wrapper = useImmutableValue(foundWrapper) diff --git a/apps/studio/components/interfaces/Database/Wrappers/Wrappers.tsx b/apps/studio/components/interfaces/Database/Wrappers/Wrappers.tsx index 83b04d5f72..76c92a2eb5 100644 --- a/apps/studio/components/interfaces/Database/Wrappers/Wrappers.tsx +++ b/apps/studio/components/interfaces/Database/Wrappers/Wrappers.tsx @@ -19,7 +19,7 @@ const Wrappers = ({ isEnabled }: { isEnabled: boolean }) => { const [open, setOpen] = useState('') const [selectedWrapperToDelete, setSelectedWrapperToDelete] = useState() - const wrappers = data?.result ?? [] + const wrappers = data ?? [] return ( <> diff --git a/apps/studio/components/interfaces/DiskManagement/ui/DiskSpaceBar.tsx b/apps/studio/components/interfaces/DiskManagement/ui/DiskSpaceBar.tsx index b634f3d35d..26c007ef66 100644 --- a/apps/studio/components/interfaces/DiskManagement/ui/DiskSpaceBar.tsx +++ b/apps/studio/components/interfaces/DiskManagement/ui/DiskSpaceBar.tsx @@ -6,6 +6,7 @@ import { UseFormReturn } from 'react-hook-form' import { useParams } from 'common' import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext' +import { useRemainingDurationForDiskAttributeUpdate } from 'data/config/disk-attributes-query' import { useDiskUtilizationQuery } from 'data/config/disk-utilization-query' import { useDatabaseSizeQuery } from 'data/database/database-size-query' import { GB } from 'lib/constants' @@ -19,7 +20,6 @@ import { } from 'ui' import { DiskStorageSchemaType } from '../DiskManagement.schema' import { AUTOSCALING_THRESHOLD } from './DiskManagement.constants' -import { useRemainingDurationForDiskAttributeUpdate } from 'data/config/disk-attributes-query' interface DiskSpaceBarProps { form: UseFormReturn @@ -56,7 +56,7 @@ export default function DiskSpaceBar({ form }: DiskSpaceBarProps) { }) const { remainingDuration } = useRemainingDurationForDiskAttributeUpdate({ projectRef: ref }) - const databaseSizeBytes = data?.result[0].db_size ?? 0 + const databaseSizeBytes = data ?? 0 return (
diff --git a/apps/studio/components/interfaces/Settings/Database/DiskSizeConfiguration.tsx b/apps/studio/components/interfaces/Settings/Database/DiskSizeConfiguration.tsx index 0f7ff4d7fb..0763d3e39f 100644 --- a/apps/studio/components/interfaces/Settings/Database/DiskSizeConfiguration.tsx +++ b/apps/studio/components/interfaces/Settings/Database/DiskSizeConfiguration.tsx @@ -62,7 +62,7 @@ const DiskSizeConfiguration = ({ disabled = false }: DiskSizeConfigurationProps) projectRef: project?.ref, connectionString: project?.connectionString, }) - const databaseSizeBytesUsed = data?.result[0].db_size ?? 0 + const databaseSizeBytesUsed = data ?? 0 return (
diff --git a/apps/studio/components/interfaces/Settings/Vault/Secrets/SecretRow.tsx b/apps/studio/components/interfaces/Settings/Vault/Secrets/SecretRow.tsx index 54a7340795..75687538d5 100644 --- a/apps/studio/components/interfaces/Settings/Vault/Secrets/SecretRow.tsx +++ b/apps/studio/components/interfaces/Settings/Vault/Secrets/SecretRow.tsx @@ -103,9 +103,7 @@ const SecretRow = ({ secret, onSelectEdit, onSelectRemove }: SecretRowProps) =>

- +