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