Skip to content

Commit 266a717

Browse files
committed
feat: add multi-platform support for repositories and connections
- Rename github_accounts table to platform_connections and add platform-specific fields - Add platform column to repos table with updated constraints and indexes - Add platform column to analysis_jobs table with validation - Backfill existing GitHub connections with user data - Update RLS policies for renamed table
1 parent 0692731 commit 266a717

4 files changed

Lines changed: 97 additions & 0 deletions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
-- Rename table
2+
ALTER TABLE github_accounts RENAME TO platform_connections;
3+
4+
-- Add platform discriminator (existing rows are GitHub)
5+
ALTER TABLE platform_connections
6+
ADD COLUMN platform TEXT NOT NULL DEFAULT 'github'
7+
CHECK (platform IN ('github', 'gitlab', 'bitbucket'));
8+
9+
-- Add platform-specific user info
10+
ALTER TABLE platform_connections
11+
ADD COLUMN platform_user_id TEXT,
12+
ADD COLUMN platform_username TEXT,
13+
ADD COLUMN platform_email TEXT,
14+
ADD COLUMN platform_avatar_url TEXT;
15+
16+
-- Add token refresh support (GitLab/Bitbucket use refresh tokens)
17+
ALTER TABLE platform_connections
18+
ADD COLUMN refresh_token_encrypted TEXT,
19+
ADD COLUMN token_expires_at TIMESTAMPTZ;
20+
21+
-- Add primary/disconnect tracking
22+
ALTER TABLE platform_connections
23+
ADD COLUMN is_primary BOOLEAN NOT NULL DEFAULT false,
24+
ADD COLUMN disconnected_at TIMESTAMPTZ;
25+
26+
-- Make github_user_id nullable as it is specific to GitHub
27+
ALTER TABLE platform_connections ALTER COLUMN github_user_id DROP NOT NULL;
28+
29+
-- Update unique constraints
30+
ALTER TABLE platform_connections
31+
DROP CONSTRAINT IF EXISTS github_accounts_user_id_key;
32+
33+
ALTER TABLE platform_connections
34+
ADD CONSTRAINT platform_connections_user_platform_unique
35+
UNIQUE (user_id, platform);
36+
37+
ALTER TABLE platform_connections
38+
ADD CONSTRAINT platform_connections_platform_user_unique
39+
UNIQUE (platform, platform_user_id);
40+
41+
-- Only one primary platform per user
42+
CREATE UNIQUE INDEX platform_connections_one_primary_idx
43+
ON platform_connections (user_id)
44+
WHERE is_primary = true AND disconnected_at IS NULL;
45+
46+
-- RLS policies (update existing)
47+
DROP POLICY IF EXISTS "github_accounts_select_own" ON platform_connections;
48+
DROP POLICY IF EXISTS "github_accounts_insert_own" ON platform_connections;
49+
DROP POLICY IF EXISTS "github_accounts_update_own" ON platform_connections;
50+
51+
CREATE POLICY "Users can view own platforms"
52+
ON platform_connections FOR SELECT
53+
USING (auth.uid() = user_id);
54+
55+
CREATE POLICY "Users can insert own platforms"
56+
ON platform_connections FOR INSERT
57+
WITH CHECK (auth.uid() = user_id);
58+
59+
CREATE POLICY "Users can update own platforms"
60+
ON platform_connections FOR UPDATE
61+
USING (auth.uid() = user_id);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Backfill platform user info from users table
2+
UPDATE platform_connections pc
3+
SET
4+
platform_user_id = u.github_id::text,
5+
platform_username = u.github_username,
6+
platform_email = u.email,
7+
platform_avatar_url = u.avatar_url,
8+
is_primary = true
9+
FROM users u
10+
WHERE pc.user_id = u.id
11+
AND pc.platform = 'github';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Add platform to repos (default github for existing)
2+
ALTER TABLE repos
3+
ADD COLUMN platform TEXT NOT NULL DEFAULT 'github'
4+
CHECK (platform IN ('github', 'gitlab', 'bitbucket'));
5+
6+
-- Rename github_id for clarity
7+
ALTER TABLE repos RENAME COLUMN github_id TO platform_repo_id;
8+
9+
-- Change platform_repo_id type to TEXT to support non-numeric IDs (Bitbucket)
10+
ALTER TABLE repos ALTER COLUMN platform_repo_id TYPE TEXT USING platform_repo_id::text;
11+
12+
-- Add platform-specific fields
13+
ALTER TABLE repos ADD COLUMN platform_owner TEXT;
14+
ALTER TABLE repos ADD COLUMN platform_project_id TEXT;
15+
16+
-- Index for platform queries
17+
CREATE INDEX repos_platform_idx ON repos(platform);
18+
19+
-- Update unique constraint
20+
ALTER TABLE repos DROP CONSTRAINT IF EXISTS repos_github_id_key;
21+
ALTER TABLE repos ADD CONSTRAINT repos_platform_repo_unique
22+
UNIQUE (platform, platform_repo_id);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE analysis_jobs
2+
ADD COLUMN platform TEXT NOT NULL DEFAULT 'github'
3+
CHECK (platform IN ('github', 'gitlab', 'bitbucket'));

0 commit comments

Comments
 (0)