Skip to content

Commit b945382

Browse files
joshenlimawaseem
andauthored
Joshen/fe 2558 shift sql queries to contextual folders update codeowners (#42770)
## Context Related to Dashboard Scalability, specifically having Postgres team as CODEOWNERS for dashboard queries This is just a clean up as we're currently piling manual queries into one folder under `data/sql/queries`, whereas I reckon it'll be better for each query to sit within their RQ folder for better context. Am opting the naming format for files housing queries to be `*.sql.ts`, and also updating CODEOWNERS to reflect as such Next step will also be to shift all the dashboard queries within pg-meta into studio itself as requested by the pg-meta team <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Consolidated and reorganized internal module structure for the data layer to improve maintainability and reduce redundancy. * Streamlined import paths across components to align with new consolidated module organization. * **Chores** * Updated code ownership patterns to reflect reorganized file structure. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Ali Waseem <waseema393@gmail.com>
1 parent 1d06ec8 commit b945382

19 files changed

Lines changed: 97 additions & 104 deletions

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
/apps/studio/components/interfaces/Organization/Documents/ @supabase/security
2020
/apps/studio/pages/new/index.tsx @supabase/security
2121

22-
/apps/studio/data/sql/queries/ @supabase/postgres @avallete
22+
/apps/studio/data/**/*.sql.ts @supabase/postgres @avallete
2323

2424
/packages/shared-data/compute-disk-limits.ts @supabase/infra

apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.CleanupNotice.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getScheduleDeleteCronJobRunDetailsSql } from 'data/sql/queries/delete-cron-job-run-details'
1+
import { getScheduleDeleteCronJobRunDetailsSql } from 'data/database-cron-jobs/database-cron-jobs.sql'
22
import { CheckCircle2, XCircle } from 'lucide-react'
33
import {
44
Button,

apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.useCleanupActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
21
import {
32
CTID_BATCH_PAGE_SIZE,
43
getDeleteOldCronJobRunDetailsByCtidKey,
54
getDeleteOldCronJobRunDetailsByCtidSql,
65
getJobRunDetailsPageCountKey,
76
getJobRunDetailsPageCountSql,
8-
} from 'data/sql/queries/delete-cron-job-run-details'
7+
} from 'data/database-cron-jobs/database-cron-jobs.sql'
8+
import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
99
import { useCallback, useRef, useState } from 'react'
1010
import { toast } from 'sonner'
1111

apps/studio/components/interfaces/Storage/StorageSettings/StorageFileSizeLimitErrorMessage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { InlineLink } from 'components/ui/InlineLink'
2+
import { LARGEST_SIZE_LIMIT_BUCKETS_COUNT } from 'data/storage/storage.sql'
13
import Link from 'next/link'
24
import { type FieldError } from 'react-hook-form'
3-
4-
import { InlineLink } from 'components/ui/InlineLink'
5-
import { LARGEST_SIZE_LIMIT_BUCKETS_COUNT } from 'data/sql/queries/get-largest-size-limit-buckets'
65
import { cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
6+
77
import {
88
decodeBucketLimitErrorMessage,
99
formatBytesForDisplay,

apps/studio/data/auth/user-query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useQuery } from '@tanstack/react-query'
2-
3-
import { UUID_REGEX } from '@/lib/constants'
42
import { executeSql, type ExecuteSqlError } from 'data/sql/execute-sql-query'
5-
import { getUserSQL } from 'data/sql/queries/get-user'
63
import { UseCustomQueryOptions } from 'types'
4+
5+
import { getUserSQL } from './auth.sql'
76
import { authKeys } from './keys'
87
import { User } from './users-infinite-query'
8+
import { UUID_REGEX } from '@/lib/constants'
99

1010
type UserVariables = {
1111
projectRef?: string

apps/studio/data/database-cron-jobs/database-cron-jobs-count-estimate-query.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { useQuery } from '@tanstack/react-query'
2-
32
import type { ConnectionVars } from 'data/common.types'
3+
import { getLiveTupleEstimate, getLiveTupleEstimateKey } from 'data/database/database.sql'
44
import { executeSql } from 'data/sql/execute-sql-query'
5-
import {
6-
getLiveTupleEstimate,
7-
getLiveTupleEstimateKey,
8-
} from 'data/sql/queries/get-live-tuple-stats'
95
import type { UseCustomQueryOptions } from 'types'
106

117
type DatabaseCronJobsCountEstimateVariables = ConnectionVars

apps/studio/data/database-cron-jobs/database-cron-jobs-infinite-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
22
import { COST_THRESHOLD_ERROR, executeSql } from 'data/sql/execute-sql-query'
33
import type { ResponseError, UseCustomInfiniteQueryOptions } from 'types'
44

5-
import { getCronJobsSql } from '../sql/queries/get-cron-jobs'
5+
import { getCronJobsSql } from './database-cron-jobs.sql'
66
import { databaseCronJobsKeys } from './keys'
77

88
export const CRON_JOBS_PAGE_LIMIT = 20

apps/studio/data/database-cron-jobs/database-cron-jobs-minimal-infinite-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
22
import { executeSql } from 'data/sql/execute-sql-query'
33
import type { ResponseError, UseCustomInfiniteQueryOptions } from 'types'
44

5-
import { getCronJobsMinimalSql } from '../sql/queries/get-cron-jobs'
65
import {
76
CRON_JOBS_PAGE_LIMIT,
87
CronJob,
98
DatabaseCronJobRunsVariables,
109
} from './database-cron-jobs-infinite-query'
10+
import { getCronJobsMinimalSql } from './database-cron-jobs.sql'
1111
import { databaseCronJobsKeys } from './keys'
1212

1313
export async function getDatabaseCronJobsMinimal({

apps/studio/data/sql/queries/delete-cron-job-run-details.ts renamed to apps/studio/data/database-cron-jobs/database-cron-jobs.sql.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { literal } from '@supabase/pg-meta/src/pg-format'
22

3-
import { sqlKeys } from '../keys'
3+
import { sqlKeys } from '../sql/keys'
44

55
const CRON_CLEANUP_SCHEDULE_NAME = 'delete-job-run-details'
66
const CRON_CLEANUP_SCHEDULE_EXPRESSION = '0 12 * * *'
@@ -89,3 +89,74 @@ export const getScheduleDeleteCronJobRunDetailsKey = (
8989
projectRef: string | undefined,
9090
interval: string
9191
) => sqlKeys.query(projectRef, ['cron-job-run-details', 'schedule', interval])
92+
93+
// [Joshen] Just omits the LEFT JOIN as that's the heavy part
94+
export const getCronJobsMinimalSql = ({
95+
searchTerm,
96+
page,
97+
limit,
98+
}: {
99+
searchTerm?: string
100+
page: number
101+
limit: number
102+
}) =>
103+
`
104+
SELECT
105+
job.jobid,
106+
job.jobname,
107+
job.schedule,
108+
job.command,
109+
job.active
110+
FROM
111+
cron.job job
112+
${!!searchTerm ? `WHERE job.jobname ILIKE ${literal(`%${searchTerm}%`)}` : ''}
113+
ORDER BY job.jobid
114+
LIMIT ${limit}
115+
OFFSET ${page * limit};
116+
`.trim()
117+
118+
export const getCronJobsSql = ({
119+
searchTerm,
120+
page,
121+
limit,
122+
}: {
123+
searchTerm?: string
124+
page: number
125+
limit: number
126+
}) =>
127+
`
128+
WITH latest_runs AS (
129+
SELECT
130+
jobid,
131+
status,
132+
MAX(start_time) AS latest_run
133+
FROM cron.job_run_details
134+
GROUP BY jobid, status
135+
), most_recent_runs AS (
136+
SELECT
137+
jobid,
138+
status,
139+
latest_run
140+
FROM latest_runs lr1
141+
WHERE latest_run = (
142+
SELECT MAX(latest_run)
143+
FROM latest_runs lr2
144+
WHERE lr2.jobid = lr1.jobid
145+
)
146+
)
147+
SELECT
148+
job.jobid,
149+
job.jobname,
150+
job.schedule,
151+
job.command,
152+
job.active,
153+
mr.latest_run,
154+
mr.status
155+
FROM
156+
cron.job job
157+
LEFT JOIN most_recent_runs mr ON job.jobid = mr.jobid
158+
${!!searchTerm ? `WHERE job.jobname ILIKE ${literal(`%${searchTerm}%`)}` : ''}
159+
ORDER BY job.jobid
160+
LIMIT ${limit}
161+
OFFSET ${page * limit};
162+
`.trim()

0 commit comments

Comments
 (0)