|
| 1 | +-- Backup full integration rows (including deprecated columns and nangoMapping in settings) before any modifications |
| 2 | +CREATE TABLE integration.integrations_backup_02_24_2026 AS |
| 3 | +SELECT id, settings, "limitCount", "limitLastResetAt", "emailSentAt", "importHash" FROM integrations; |
| 4 | + |
| 5 | +-- Part A: Drop deprecated columns from integrations table |
| 6 | +ALTER TABLE public.integrations |
| 7 | + DROP COLUMN IF EXISTS "limitCount", |
| 8 | + DROP COLUMN IF EXISTS "limitLastResetAt", |
| 9 | + DROP COLUMN IF EXISTS "emailSentAt", |
| 10 | + DROP COLUMN IF EXISTS "importHash"; |
| 11 | + |
| 12 | +-- Drop same columns from integrationsBackup table (created as SELECT * FROM integrations WHERE 1=2) |
| 13 | +ALTER TABLE public."integrationsBackup" |
| 14 | + DROP COLUMN IF EXISTS "limitCount", |
| 15 | + DROP COLUMN IF EXISTS "limitLastResetAt", |
| 16 | + DROP COLUMN IF EXISTS "emailSentAt", |
| 17 | + DROP COLUMN IF EXISTS "importHash"; |
| 18 | + |
| 19 | +-- Part B: Create nango_mapping table and migrate data from settings JSONB |
| 20 | +CREATE TABLE integration.nango_mapping ( |
| 21 | + "integrationId" UUID NOT NULL, |
| 22 | + "connectionId" TEXT NOT NULL, |
| 23 | + "repositoryId" UUID, |
| 24 | + owner TEXT NOT NULL, |
| 25 | + "repoName" TEXT NOT NULL, |
| 26 | + "createdAt" TIMESTAMPTZ NOT NULL DEFAULT now(), |
| 27 | + "updatedAt" TIMESTAMPTZ NOT NULL DEFAULT now(), |
| 28 | + PRIMARY KEY ("integrationId", "connectionId"), |
| 29 | + FOREIGN KEY ("integrationId") REFERENCES integrations(id) ON DELETE CASCADE, |
| 30 | + FOREIGN KEY ("repositoryId") REFERENCES repositories(id) ON DELETE SET NULL |
| 31 | +); |
| 32 | + |
| 33 | +CREATE UNIQUE INDEX ix_nango_mapping_connectionId ON integration.nango_mapping ("connectionId"); |
| 34 | +CREATE INDEX ix_nango_mapping_repositoryId ON integration.nango_mapping ("repositoryId"); |
| 35 | + |
| 36 | +-- Migrate existing data from settings.nangoMapping |
| 37 | +INSERT INTO integration.nango_mapping ("integrationId", "connectionId", "repositoryId", owner, "repoName") |
| 38 | +SELECT |
| 39 | + i.id, |
| 40 | + nm.key, |
| 41 | + r.id, |
| 42 | + nm.value->>'owner', |
| 43 | + nm.value->>'repoName' |
| 44 | +FROM integrations i |
| 45 | +CROSS JOIN LATERAL jsonb_each(i.settings->'nangoMapping') nm |
| 46 | +LEFT JOIN repositories r |
| 47 | + ON lower(r.url) = lower('https://github.com/' || (nm.value->>'owner') || '/' || (nm.value->>'repoName')) |
| 48 | + AND r."sourceIntegrationId" = i.id |
| 49 | + AND r."deletedAt" IS NULL |
| 50 | +WHERE i.platform = 'github-nango' |
| 51 | + AND i."deletedAt" IS NULL |
| 52 | + AND i.settings->'nangoMapping' IS NOT NULL |
| 53 | +ON CONFLICT DO NOTHING; |
| 54 | + |
| 55 | +-- Remove nangoMapping from settings (only github-nango integrations ever stored this key) |
| 56 | +UPDATE integrations |
| 57 | +SET settings = settings - 'nangoMapping' |
| 58 | +WHERE platform = 'github-nango' |
| 59 | + AND settings->'nangoMapping' IS NOT NULL; |
0 commit comments