-
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy path0014_create_source_users.sql
More file actions
122 lines (108 loc) · 4.15 KB
/
0014_create_source_users.sql
File metadata and controls
122 lines (108 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
-- Migration 0014: Create Users for Feed Sources
-- Converts scripts/create-source-users.ts to pure SQL
-- Each feed source without a user gets a linked user profile for RSS article attribution
-- ============================================
-- PART 1: Create users for feed sources without user_id
-- ============================================
-- This DO block creates users for all feed sources that lack one
-- and immediately links them back to the feed source
DO $$
DECLARE
source_record RECORD;
new_user_id TEXT;
safe_username TEXT;
safe_email TEXT;
existing_user_id TEXT;
BEGIN
-- Loop through all feed sources without a linked user
FOR source_record IN
SELECT
id,
name,
slug,
logo_url,
website_url,
description
FROM feed_sources
WHERE user_id IS NULL
LOOP
-- Create unique email for this source (used for idempotency check)
safe_email := 'source-' || source_record.id || '@feeds.codu.co';
-- Check if user with this email already exists (idempotency)
SELECT id INTO existing_user_id FROM "user" WHERE email = safe_email LIMIT 1;
IF existing_user_id IS NOT NULL THEN
-- User already exists, just link it if not already linked
UPDATE feed_sources
SET user_id = existing_user_id
WHERE id = source_record.id AND user_id IS NULL;
RAISE NOTICE 'Linked existing user % to feed source % (%)', existing_user_id, source_record.id, source_record.name;
ELSE
-- Generate a unique user ID
new_user_id := gen_random_uuid()::TEXT;
-- Create safe username from slug or name (max 40 chars total)
safe_username := 'source-' || COALESCE(
source_record.slug,
LOWER(REGEXP_REPLACE(source_record.name, '[^a-zA-Z0-9]', '-', 'g'))
);
safe_username := LEFT(safe_username, 40);
-- Insert the new user
INSERT INTO "user" (
id,
username,
name,
email,
image,
bio,
"websiteUrl",
"emailNotifications",
newsletter,
"createdAt",
"updatedAt"
) VALUES (
new_user_id,
safe_username,
source_record.name,
safe_email,
COALESCE(source_record.logo_url, '/images/person.png'),
COALESCE(LEFT(source_record.description, 200), 'Content from ' || source_record.name),
COALESCE(source_record.website_url, ''),
FALSE, -- No email notifications for source users
FALSE, -- No newsletter for source users
NOW(),
NOW()
);
-- Link the feed source to this user
UPDATE feed_sources
SET user_id = new_user_id
WHERE id = source_record.id;
RAISE NOTICE 'Created user % for feed source % (%)', new_user_id, source_record.id, source_record.name;
END IF;
END LOOP;
END $$;
-- ============================================
-- PART 2: Handle username conflicts
-- ============================================
-- If there are any username conflicts from multiple sources with same name,
-- make them unique by appending source ID
UPDATE "user" u
SET username = u.username || '-' || (
SELECT fs.id::TEXT FROM feed_sources fs WHERE fs.user_id = u.id LIMIT 1
)
WHERE u.email LIKE 'source-%@feeds.codu.co'
AND EXISTS (
SELECT 1 FROM "user" u2
WHERE u2.username = u.username
AND u2.id != u.id
);
-- ============================================
-- PART 3: Summary (for logging)
-- ============================================
DO $$
DECLARE
total_sources INTEGER;
linked_sources INTEGER;
BEGIN
SELECT COUNT(*) INTO total_sources FROM feed_sources;
SELECT COUNT(*) INTO linked_sources FROM feed_sources WHERE user_id IS NOT NULL;
RAISE NOTICE 'Migration 0014 complete: % of % feed sources now have linked users', linked_sources, total_sources;
END $$;