Skip to content

Commit 65b4a16

Browse files
committed
Normalize existing website column of organization
1 parent cc8e07d commit 65b4a16

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DROP INDEX idx_org_tenant_website ON organizations;
2+
3+
-- it's a destructive operation in up migration, so we can't really undo it
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CREATE UNIQUE INDEX idx_org_tenant_website ON organizations (tenantId, website);
2+
3+
DO
4+
$$
5+
DECLARE
6+
org_record organizations%ROWTYPE;
7+
correct_url text;
8+
BEGIN
9+
FOR org_record IN (SELECT *
10+
FROM organizations
11+
WHERE website LIKE 'http://%'
12+
OR website LIKE 'https://%'
13+
OR website LIKE 'www.%')
14+
LOOP
15+
correct_url := (SELECT CASE
16+
WHEN position('www.' in org_record.website) > 0 THEN
17+
split_part(split_part(org_record.website, 'www.', 2), '/', 1)
18+
ELSE
19+
split_part(split_part(org_record.website, '//', 2), '/', 1)
20+
END);
21+
RAISE NOTICE 'org id: %, fixed url: %', org_record.id, correct_url;
22+
UPDATE organizations SET website = correct_url WHERE id = org_record.id;
23+
END LOOP;
24+
COMMIT;
25+
END;
26+
$$;

0 commit comments

Comments
 (0)