Skip to content

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

Joshen/fe 2558 shift sql queries to contextual folders update codeowners (#42770)

Joshen Limb94538268b54
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index bba0b0d3e3a..75fec2640b8 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -19,6 +19,6 @@
/apps/studio/components/interfaces/Organization/Documents/ @supabase/security
/apps/studio/pages/new/index.tsx @supabase/security
-/apps/studio/data/sql/queries/ @supabase/postgres @avallete
+/apps/studio/data/**/*.sql.ts @supabase/postgres @avallete
/packages/shared-data/compute-disk-limits.ts @supabase/infra
diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.CleanupNotice.tsx b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.CleanupNotice.tsx
index cb31fc6d2e0..2f59734a558 100644
--- a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.CleanupNotice.tsx
+++ b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.CleanupNotice.tsx
@@ -1,4 +1,4 @@
-import { getScheduleDeleteCronJobRunDetailsSql } from 'data/sql/queries/delete-cron-job-run-details'
+import { getScheduleDeleteCronJobRunDetailsSql } from 'data/database-cron-jobs/database-cron-jobs.sql'
import { CheckCircle2, XCircle } from 'lucide-react'
import {
Button,
diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.useCleanupActions.ts b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.useCleanupActions.ts
index 9800370904a..037dd747a7f 100644
--- a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.useCleanupActions.ts
+++ b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.useCleanupActions.ts
@@ -1,11 +1,11 @@
-import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
import {
CTID_BATCH_PAGE_SIZE,
getDeleteOldCronJobRunDetailsByCtidKey,
getDeleteOldCronJobRunDetailsByCtidSql,
getJobRunDetailsPageCountKey,
getJobRunDetailsPageCountSql,
-} from 'data/sql/queries/delete-cron-job-run-details'
+} from 'data/database-cron-jobs/database-cron-jobs.sql'
+import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
import { useCallback, useRef, useState } from 'react'
import { toast } from 'sonner'
diff --git a/apps/studio/components/interfaces/Storage/StorageSettings/StorageFileSizeLimitErrorMessage.tsx b/apps/studio/components/interfaces/Storage/StorageSettings/StorageFileSizeLimitErrorMessage.tsx
index 9a3f9e8c315..7917e07f552 100644
--- a/apps/studio/components/interfaces/Storage/StorageSettings/StorageFileSizeLimitErrorMessage.tsx
+++ b/apps/studio/components/interfaces/Storage/StorageSettings/StorageFileSizeLimitErrorMessage.tsx
@@ -1,9 +1,9 @@
+import { InlineLink } from 'components/ui/InlineLink'
+import { LARGEST_SIZE_LIMIT_BUCKETS_COUNT } from 'data/storage/storage.sql'
import Link from 'next/link'
import { type FieldError } from 'react-hook-form'
-
-import { InlineLink } from 'components/ui/InlineLink'
-import { LARGEST_SIZE_LIMIT_BUCKETS_COUNT } from 'data/sql/queries/get-largest-size-limit-buckets'
import { cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
+
import {
decodeBucketLimitErrorMessage,
formatBytesForDisplay,
diff --git a/apps/studio/data/sql/queries/get-user.ts b/apps/studio/data/auth/auth.sql.ts
similarity index 100%
rename from apps/studio/data/sql/queries/get-user.ts
rename to apps/studio/data/auth/auth.sql.ts
diff --git a/apps/studio/data/auth/user-query.ts b/apps/studio/data/auth/user-query.ts
index 40620107f4b..5bd5b0dcd7d 100644
--- a/apps/studio/data/auth/user-query.ts
+++ b/apps/studio/data/auth/user-query.ts
@@ -1,11 +1,11 @@
import { useQuery } from '@tanstack/react-query'
-
-import { UUID_REGEX } from '@/lib/constants'
import { executeSql, type ExecuteSqlError } from 'data/sql/execute-sql-query'
-import { getUserSQL } from 'data/sql/queries/get-user'
import { UseCustomQueryOptions } from 'types'
+
+import { getUserSQL } from './auth.sql'
import { authKeys } from './keys'
import { User } from './users-infinite-query'
+import { UUID_REGEX } from '@/lib/constants'
type UserVariables = {
projectRef?: string
diff --git a/apps/studio/data/database-cron-jobs/database-cron-jobs-count-estimate-query.ts b/apps/studio/data/database-cron-jobs/database-cron-jobs-count-estimate-query.ts
index cf3686d0b8d..8f645bdff60 100644
--- a/apps/studio/data/database-cron-jobs/database-cron-jobs-count-estimate-query.ts
+++ b/apps/studio/data/database-cron-jobs/database-cron-jobs-count-estimate-query.ts
@@ -1,11 +1,7 @@
import { useQuery } from '@tanstack/react-query'
-
import type { ConnectionVars } from 'data/common.types'
+import { getLiveTupleEstimate, getLiveTupleEstimateKey } from 'data/database/database.sql'
import { executeSql } from 'data/sql/execute-sql-query'
-import {
- getLiveTupleEstimate,
- getLiveTupleEstimateKey,
-} from 'data/sql/queries/get-live-tuple-stats'
import type { UseCustomQueryOptions } from 'types'
type DatabaseCronJobsCountEstimateVariables = ConnectionVars
diff --git a/apps/studio/data/database-cron-jobs/database-cron-jobs-infinite-query.ts b/apps/studio/data/database-cron-jobs/database-cron-jobs-infinite-query.ts
index 84cd0ee9c33..bf3eb3c2fa5 100644
--- a/apps/studio/data/database-cron-jobs/database-cron-jobs-infinite-query.ts
+++ b/apps/studio/data/database-cron-jobs/database-cron-jobs-infinite-query.ts
@@ -2,7 +2,7 @@ import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
import { COST_THRESHOLD_ERROR, executeSql } from 'data/sql/execute-sql-query'
import type { ResponseError, UseCustomInfiniteQueryOptions } from 'types'
-import { getCronJobsSql } from '../sql/queries/get-cron-jobs'
+import { getCronJobsSql } from './database-cron-jobs.sql'
import { databaseCronJobsKeys } from './keys'
export const CRON_JOBS_PAGE_LIMIT = 20
diff --git a/apps/studio/data/database-cron-jobs/database-cron-jobs-minimal-infinite-query.ts b/apps/studio/data/database-cron-jobs/database-cron-jobs-minimal-infinite-query.ts
index 26106ae581a..b833564d2e0 100644
--- a/apps/studio/data/database-cron-jobs/database-cron-jobs-minimal-infinite-query.ts
+++ b/apps/studio/data/database-cron-jobs/database-cron-jobs-minimal-infinite-query.ts
@@ -2,12 +2,12 @@ import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
import { executeSql } from 'data/sql/execute-sql-query'
import type { ResponseError, UseCustomInfiniteQueryOptions } from 'types'
-import { getCronJobsMinimalSql } from '../sql/queries/get-cron-jobs'
import {
CRON_JOBS_PAGE_LIMIT,
CronJob,
DatabaseCronJobRunsVariables,
} from './database-cron-jobs-infinite-query'
+import { getCronJobsMinimalSql } from './database-cron-jobs.sql'
import { databaseCronJobsKeys } from './keys'
export async function getDatabaseCronJobsMinimal({
diff --git a/apps/studio/data/sql/queries/delete-cron-job-run-details.ts b/apps/studio/data/database-cron-jobs/database-cron-jobs.sql.ts
similarity index 70%
rename from apps/studio/data/sql/queries/delete-cron-job-run-details.ts
rename to apps/studio/data/database-cron-jobs/database-cron-jobs.sql.ts
index 8fb28e27c59..5c88e0cef2e 100644
--- a/apps/studio/data/sql/queries/delete-cron-job-run-details.ts
+++ b/apps/studio/data/database-cron-jobs/database-cron-jobs.sql.ts
@@ -1,6 +1,6 @@
import { literal } from '@supabase/pg-meta/src/pg-format'
-import { sqlKeys } from '../keys'
+import { sqlKeys } from '../sql/keys'
const CRON_CLEANUP_SCHEDULE_NAME = 'delete-job-run-details'
const CRON_CLEANUP_SCHEDULE_EXPRESSION = '0 12 * * *'
@@ -89,3 +89,74 @@ export const getScheduleDeleteCronJobRunDetailsKey = (
projectRef: string | undefined,
interval: string
) => sqlKeys.query(projectRef, ['cron-job-run-details', 'schedule', interval])
+
+// [Joshen] Just omits the LEFT JOIN as that's the heavy part
+export const getCronJobsMinimalSql = ({
+ searchTerm,
+ page,
+ limit,
+}: {
+ searchTerm?: string
+ page: number
+ limit: number
+}) =>
+ `
+SELECT
+ job.jobid,
+ job.jobname,
+ job.schedule,
+ job.command,
+ job.active
+FROM
+ cron.job job
+${!!searchTerm ? `WHERE job.jobname ILIKE ${literal(`%${searchTerm}%`)}` : ''}
+ORDER BY job.jobid
+LIMIT ${limit}
+OFFSET ${page * limit};
+`.trim()
+
+export const getCronJobsSql = ({
+ searchTerm,
+ page,
+ limit,
+}: {
+ searchTerm?: string
+ page: number
+ limit: number
+}) =>
+ `
+WITH latest_runs AS (
+ SELECT
+ jobid,
+ status,
+ MAX(start_time) AS latest_run
+ FROM cron.job_run_details
+ GROUP BY jobid, status
+), most_recent_runs AS (
+ SELECT
+ jobid,
+ status,
+ latest_run
+ FROM latest_runs lr1
+ WHERE latest_run = (
+ SELECT MAX(latest_run)
+ FROM latest_runs lr2
+ WHERE lr2.jobid = lr1.jobid
+ )
+)
+SELECT
+ job.jobid,
+ job.jobname,
+ job.schedule,
+ job.command,
+ job.active,
+ mr.latest_run,
+ mr.status
+FROM
+ cron.job job
+LEFT JOIN most_recent_runs mr ON job.jobid = mr.jobid
+${!!searchTerm ? `WHERE job.jobname ILIKE ${literal(`%${searchTerm}%`)}` : ''}
+ORDER BY job.jobid
+LIMIT ${limit}
+OFFSET ${page * limit};
+`.trim()
diff --git a/apps/studio/data/database-cron-jobs/schedule-clean-up-mutation.ts b/apps/studio/data/database-cron-jobs/schedule-clean-up-mutation.ts
index 7923c247524..3ff43316da1 100644
--- a/apps/studio/data/database-cron-jobs/schedule-clean-up-mutation.ts
+++ b/apps/studio/data/database-cron-jobs/schedule-clean-up-mutation.ts
@@ -6,7 +6,7 @@ import type { ResponseError, UseCustomMutationOptions } from 'types'
import {
getScheduleDeleteCronJobRunDetailsKey,
getScheduleDeleteCronJobRunDetailsSql,
-} from '../sql/queries/delete-cron-job-run-details'
+} from './database-cron-jobs.sql'
export type ScheduleCronJobRunDetailsCleanupVariables = {
projectRef: string
diff --git a/apps/studio/data/database-extensions/database-extension-schema-query.ts b/apps/studio/data/database-extensions/database-extension-schema-query.ts
index ef7b2d6ca02..f7683d2d882 100644
--- a/apps/studio/data/database-extensions/database-extension-schema-query.ts
+++ b/apps/studio/data/database-extensions/database-extension-schema-query.ts
@@ -1,8 +1,8 @@
import { useQuery } from '@tanstack/react-query'
import { UseCustomQueryOptions } from 'types'
-import { ExecuteSqlError, executeSql } from '../sql/execute-sql-query'
-import { getDatabaseExtensionDefaultSchemaSQL } from '../sql/queries/get-extension-default-schema'
+import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
+import { getDatabaseExtensionDefaultSchemaSQL } from './database-extensions.sql'
import { databaseExtensionsKeys } from './keys'
type DatabaseExtensionDefaultSchemaVariables = {
diff --git a/apps/studio/data/sql/queries/get-extension-default-schema.ts b/apps/studio/data/database-extensions/database-extensions.sql.ts
similarity index 100%
rename from apps/studio/data/sql/queries/get-extension-default-schema.ts
rename to apps/studio/data/database-extensions/database-extensions.sql.ts
diff --git a/apps/studio/data/sql/queries/get-indexes.ts b/apps/studio/data/database-indexes/database-indexes.sql.ts
similarity index 100%
rename from apps/studio/data/sql/queries/get-indexes.ts
rename to apps/studio/data/database-indexes/database-indexes.sql.ts
diff --git a/apps/studio/data/database-indexes/indexes-query.ts b/apps/studio/data/database-indexes/indexes-query.ts
index d713d7b4f55..25f51d540fa 100644
--- a/apps/studio/data/database-indexes/indexes-query.ts
+++ b/apps/studio/data/database-indexes/indexes-query.ts
@@ -1,8 +1,8 @@
import { useQuery } from '@tanstack/react-query'
-
-import { getIndexesSQL } from 'data/sql/queries/get-indexes'
import { UseCustomQueryOptions } from 'types'
+
import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
+import { getIndexesSQL } from './database-indexes.sql'
import { databaseIndexesKeys } from './keys'
type GetIndexesArgs = {
diff --git a/apps/studio/data/sql/queries/get-live-tuple-stats.ts b/apps/studio/data/database/database.sql.ts
similarity index 92%
rename from apps/studio/data/sql/queries/get-live-tuple-stats.ts
rename to apps/studio/data/database/database.sql.ts
index d96ab8cff41..e1408aa15bb 100644
--- a/apps/studio/data/sql/queries/get-live-tuple-stats.ts
+++ b/apps/studio/data/database/database.sql.ts
@@ -1,5 +1,6 @@
import { literal } from '@supabase/pg-meta/src/pg-format'
-import { sqlKeys } from '../keys'
+
+import { sqlKeys } from '../sql/keys'
export const getLiveTupleEstimate = (table: string, schema: string = 'public') => {
const sql = /* SQL */ `
diff --git a/apps/studio/data/sql/queries/get-cron-jobs.ts b/apps/studio/data/sql/queries/get-cron-jobs.ts
deleted file mode 100644
index aa930819ca7..00000000000
--- a/apps/studio/data/sql/queries/get-cron-jobs.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-import { literal } from '@supabase/pg-meta/src/pg-format'
-
-// [Joshen] Just omits the LEFT JOIN as that's the heavy part
-export const getCronJobsMinimalSql = ({
- searchTerm,
- page,
- limit,
-}: {
- searchTerm?: string
- page: number
- limit: number
-}) =>
- `
-SELECT
- job.jobid,
- job.jobname,
- job.schedule,
- job.command,
- job.active
-FROM
- cron.job job
-${!!searchTerm ? `WHERE job.jobname ILIKE ${literal(`%${searchTerm}%`)}` : ''}
-ORDER BY job.jobid
-LIMIT ${limit}
-OFFSET ${page * limit};
-`.trim()
-
-export const getCronJobsSql = ({
- searchTerm,
- page,
- limit,
-}: {
- searchTerm?: string
- page: number
- limit: number
-}) =>
- `
-WITH latest_runs AS (
- SELECT
- jobid,
- status,
- MAX(start_time) AS latest_run
- FROM cron.job_run_details
- GROUP BY jobid, status
-), most_recent_runs AS (
- SELECT
- jobid,
- status,
- latest_run
- FROM latest_runs lr1
- WHERE latest_run = (
- SELECT MAX(latest_run)
- FROM latest_runs lr2
- WHERE lr2.jobid = lr1.jobid
- )
-)
-SELECT
- job.jobid,
- job.jobname,
- job.schedule,
- job.command,
- job.active,
- mr.latest_run,
- mr.status
-FROM
- cron.job job
-LEFT JOIN most_recent_runs mr ON job.jobid = mr.jobid
-${!!searchTerm ? `WHERE job.jobname ILIKE ${literal(`%${searchTerm}%`)}` : ''}
-ORDER BY job.jobid
-LIMIT ${limit}
-OFFSET ${page * limit};
-`.trim()
diff --git a/apps/studio/data/storage/buckets-max-size-limit-query.ts b/apps/studio/data/storage/buckets-max-size-limit-query.ts
index a047261afc5..6a2c5d01fdf 100644
--- a/apps/studio/data/storage/buckets-max-size-limit-query.ts
+++ b/apps/studio/data/storage/buckets-max-size-limit-query.ts
@@ -1,16 +1,13 @@
import { useQuery, useQueryClient } from '@tanstack/react-query'
-import { useCallback } from 'react'
-
import type { ConnectionVars } from 'data/common.types'
+import { getLiveTupleEstimate, getLiveTupleEstimateKey } from 'data/database/database.sql'
import { executeSql } from 'data/sql/execute-sql-query'
+import { useCallback } from 'react'
+
import {
getLargestSizeLimitBucketsKey,
getLargestSizeLimitBucketsSqlUnoptimized,
-} from 'data/sql/queries/get-largest-size-limit-buckets'
-import {
- getLiveTupleEstimate,
- getLiveTupleEstimateKey,
-} from 'data/sql/queries/get-live-tuple-stats'
+} from './storage.sql'
export const THRESHOLD_FOR_AUTO_QUERYING_BUCKET_LIMITS = 10_000
diff --git a/apps/studio/data/sql/queries/get-largest-size-limit-buckets.ts b/apps/studio/data/storage/storage.sql.ts
similarity index 94%
rename from apps/studio/data/sql/queries/get-largest-size-limit-buckets.ts
rename to apps/studio/data/storage/storage.sql.ts
index 76248f2e789..f2dfedc0485 100644
--- a/apps/studio/data/sql/queries/get-largest-size-limit-buckets.ts
+++ b/apps/studio/data/storage/storage.sql.ts
@@ -1,4 +1,4 @@
-import { sqlKeys } from '../keys'
+import { sqlKeys } from '../sql/keys'
export const LARGEST_SIZE_LIMIT_BUCKETS_COUNT = 50