IntegrationOverviewTabV2.utils.ts47 lines · main
1import { getEnableDatabaseExtensionSQL } from '@supabase/pg-meta'
2
3import { DatabaseExtension } from '@/data/database-extensions/database-extensions-query'
4
5export const getEnableExtensionsSQL = ({
6 extensions,
7 extensionsSchema,
8}: {
9 extensions: DatabaseExtension[]
10 extensionsSchema: {
11 [key: string]: { schema: string; value: string | undefined }
12 }
13}) => {
14 return extensions
15 .map((extension) => {
16 /**
17 * [Joshen] Hard-coding pg_cron here as this is enforced on our end (Not via pg_available_extension_versions)
18 * Also temp hardcoding to `extensions` for now, but we should be retrieving the default schema from `pg_available_extension_versions`
19 * Am checking with pg-meta team whether we can just return that data directly from the /pg-meta/extensions endpoint, rather
20 * than using dashboard's `useDatabaseExtensionDefaultSchemaQuery` - we can technically save a query if so
21 */
22 const { name, default_version: version } = extension
23 const createSchema = extensionsSchema[name].schema === 'custom'
24 const schema =
25 name === 'pg_cron'
26 ? 'pg_catalog'
27 : createSchema
28 ? (extensionsSchema[name].value as string)
29 : extensionsSchema[name].schema
30
31 return getEnableDatabaseExtensionSQL({
32 schema,
33 name,
34 version,
35 cascade: true,
36 createSchema,
37 })
38 })
39 .filter(Boolean)
40 .join('\n\n')
41 .trim()
42}
43
44export const getExtensionDefaultSchema = (ext?: DatabaseExtension) => {
45 if (!ext) return null
46 return ext.name === 'pg_cron' ? 'pg_catalog' : ext.default_version_schema
47}