IntegrationOverviewTabV2.utils.ts 1.6 KB

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