|
| 1 | +create table "organizationIdentities" ( |
| 2 | + "organizationId" uuid not null references organizations on delete cascade, |
| 3 | + platform text not null, |
| 4 | + name text not null, |
| 5 | + "sourceId" text, |
| 6 | + "url" text, |
| 7 | + "tenantId" uuid not null references tenants on delete cascade, |
| 8 | + "integrationId" uuid, |
| 9 | + "createdAt" timestamp with time zone default now() not null, |
| 10 | + "updatedAt" timestamp with time zone default now() not null, |
| 11 | + primary key ("organizationId", platform, name), |
| 12 | + unique (platform, name, "tenantId") |
| 13 | +); |
| 14 | +create index "ix_organizationIdentities" on "organizationIdentities" (platform, name, "organizationId"); |
| 15 | +create index "ix_organizationIdentities_tenantId" on "organizationIdentities" ("tenantId"); |
| 16 | +create index "ix_organizationIdentities_organizationId" on "organizationIdentities" ("organizationId"); |
| 17 | +create index "organizationIdentities_createdAt_index" on "organizationIdentities" ("createdAt" desc); |
| 18 | +create index "organizationIdentities_name_index" on "organizationIdentities" (name); |
| 19 | +create trigger organization_identities_updated_at before |
| 20 | +update on "organizationIdentities" for each row execute procedure trigger_set_updated_at(); |
| 21 | + |
| 22 | + |
| 23 | +alter table organizations |
| 24 | + add "weakIdentities" jsonb default '[]'::jsonb not null; |
| 25 | + |
| 26 | +DO |
| 27 | +$$ |
| 28 | + DECLARE |
| 29 | + org organizations%ROWTYPE; |
| 30 | + act activities%ROWTYPE; |
| 31 | + gin integrations%ROWTYPE; |
| 32 | + BEGIN |
| 33 | + FOR org IN SELECT * FROM organizations |
| 34 | + WHERE NOT EXISTS (SELECT 1 FROM "organizationIdentities" WHERE "organizationId" = org.id) |
| 35 | + LOOP |
| 36 | + BEGIN |
| 37 | + -- check for organization activity on github |
| 38 | + SELECT INTO act * FROM activities WHERE "platform" = 'github' AND "organizationId" = org.id LIMIT 1; |
| 39 | + BEGIN |
| 40 | + -- If activity is found |
| 41 | + IF FOUND THEN |
| 42 | + SELECT INTO gin * |
| 43 | + FROM integrations |
| 44 | + WHERE "platform" = 'github' |
| 45 | + AND "tenantId" = org."tenantId" |
| 46 | + AND "deletedAt" IS NULL |
| 47 | + LIMIT 1; |
| 48 | + -- If integration is found |
| 49 | + IF FOUND THEN |
| 50 | + INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "url", |
| 51 | + "sourceId", "integrationId", "tenantId") |
| 52 | + VALUES (org.id, 'github', org.name, org.url, null, gin.id, org."tenantId"); |
| 53 | + -- If integration is not found, `platform` is set to 'custom' and `integrationId` is set to null |
| 54 | + ELSE |
| 55 | + INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "sourceId", |
| 56 | + "integrationId", "tenantId") |
| 57 | + VALUES (org.id, 'custom', org.name, null, null, org."tenantId"); |
| 58 | + END IF; |
| 59 | + -- If no activity is found, `platform` is set to 'custom' and `integrationId` is set to null |
| 60 | + ELSE |
| 61 | + INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "sourceId", |
| 62 | + "integrationId", "tenantId") |
| 63 | + VALUES (org.id, 'custom', org.name, null, null, org."tenantId"); |
| 64 | + END IF; |
| 65 | + EXCEPTION |
| 66 | + WHEN unique_violation THEN |
| 67 | + -- If conflict happens, insert this identity into organizations."weakIdentities" jsonb array |
| 68 | + UPDATE organizations |
| 69 | + SET "weakIdentities" = COALESCE("weakIdentities", '{}'::jsonb) || |
| 70 | + jsonb_build_object('platform', 'github', 'name', org.name) |
| 71 | + WHERE id = org.id; |
| 72 | + END; |
| 73 | + |
| 74 | + -- check for non-null LinkedIn handle |
| 75 | + IF org.linkedin -> 'handle' IS NOT NULL THEN |
| 76 | + BEGIN |
| 77 | + INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "sourceId", |
| 78 | + "integrationId", "tenantId", "url") |
| 79 | + VALUES (org.id, 'linkedin', replace(org.linkedin ->> 'handle', 'company/', ''), null, null, |
| 80 | + org."tenantId", CONCAT('https://linkedin.com/company/', |
| 81 | + replace(org.linkedin ->> 'handle', 'company/', ''))); |
| 82 | + EXCEPTION |
| 83 | + WHEN unique_violation THEN |
| 84 | + -- If conflict happens, insert this identity into organizations."weakIdentities" jsonb array |
| 85 | + UPDATE organizations |
| 86 | + SET "weakIdentities" = COALESCE("weakIdentities", '{}'::jsonb) || |
| 87 | + jsonb_build_object('platform', 'linkedin', 'name', |
| 88 | + replace(org.linkedin ->> 'handle', 'company/', ''), |
| 89 | + 'url', CONCAT('https://linkedin.com/company/', |
| 90 | + replace(org.linkedin ->> 'handle', 'company/', ''))) |
| 91 | + WHERE id = org.id; |
| 92 | + END; |
| 93 | + END IF; |
| 94 | + |
| 95 | + -- check for non-null Twitter handle |
| 96 | + IF org.twitter -> 'handle' IS NOT NULL THEN |
| 97 | + BEGIN |
| 98 | + INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "sourceId", |
| 99 | + "integrationId", "tenantId", "url") |
| 100 | + VALUES (org.id, 'twitter', org.twitter ->> 'handle'::text, null, null, org."tenantId", |
| 101 | + CONCAT('https://twitter.com/', org.twitter ->> 'handle'::text)); |
| 102 | + EXCEPTION |
| 103 | + WHEN unique_violation THEN |
| 104 | + -- If conflict happens, insert this identity into organizations."weakIdentities" jsonb array |
| 105 | + UPDATE organizations |
| 106 | + SET "weakIdentities" = COALESCE("weakIdentities", '{}'::jsonb) || |
| 107 | + jsonb_build_object('platform', 'twitter', 'name', |
| 108 | + org.twitter ->> 'handle', 'url', |
| 109 | + CONCAT('https://twitter.com/', org.twitter ->> 'handle'::text)) |
| 110 | + WHERE id = org.id; |
| 111 | + END; |
| 112 | + END IF; |
| 113 | + EXCEPTION |
| 114 | + WHEN others THEN |
| 115 | + ROLLBACK; -- Rollback the transaction when an unexpected exception occurs |
| 116 | + RAISE NOTICE 'Unexpected error for organization %: %', org.id, SQLERRM; -- Log the error |
| 117 | + END; |
| 118 | + COMMIT; |
| 119 | + END LOOP; |
| 120 | + END ; |
| 121 | +$$; |
| 122 | + |
| 123 | + |
| 124 | +drop index if exists "organizations_name_tenant_id"; |
| 125 | + |
| 126 | +alter table organizations DROP COLUMN "name"; |
| 127 | + |
| 128 | +alter table organizations DROP COLUMN "url"; |
0 commit comments