Skip to content

Commit f7f521e

Browse files
committed
wip
1 parent e6713ba commit f7f521e

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package migrate
2+
3+
import (
4+
"context"
5+
6+
"github.com/charmbracelet/soft-serve/pkg/db"
7+
)
8+
9+
const (
10+
createOrgsTeamsName = "create_orgs_teams"
11+
createOrgsTeamsVersion = 4
12+
)
13+
14+
var createOrgsTeams = Migration{
15+
Name: createOrgsTeamsName,
16+
Version: createOrgsTeamsVersion,
17+
Migrate: func(ctx context.Context, tx *db.Tx) error {
18+
return migrateUp(ctx, tx, createOrgsTeamsVersion, createOrgsTeamsName)
19+
},
20+
Rollback: func(ctx context.Context, tx *db.Tx) error {
21+
return migrateDown(ctx, tx, createOrgsTeamsVersion, createOrgsTeamsName)
22+
},
23+
}

pkg/db/migrate/0004_create_orgs_teams_postgres.down.sql

Whitespace-only changes.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
CREATE TABLE IF NOT EXISTS handles (
2+
id SERIAL PRIMARY KEY,
3+
handle TEXT NOT NULL UNIQUE,
4+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
5+
updated_at TIMESTAMP NOT NULL
6+
);
7+
8+
CREATE TABLE IF NOT EXISTS organizations (
9+
id SERIAL PRIMARY KEY,
10+
email TEXT NOT NULL,
11+
handle_id INTEGER NOT NULL,
12+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
13+
updated_at TIMESTAMP NOT NULL,
14+
CONSTRAINT handle_id_fk
15+
FOREIGN KEY(handle_id) REFERENCES handles(id)
16+
ON DELETE CASCADE
17+
ON UPDATE CASCADE
18+
);
19+
20+
CREATE TABLE IF NOT EXISTS organization_members (
21+
id SERIAL PRIMARY KEY,
22+
org_id INTEGER NOT NULL,
23+
user_id INTEGER NOT NULL,
24+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
25+
updated_at TIMESTAMP NOT NULL,
26+
UNIQUE (org_id, user_id),
27+
CONSTRAINT org_id_fk
28+
FOREIGN KEY(org_id) REFERENCES organizations(id)
29+
ON DELETE CASCADE
30+
ON UPDATE CASCADE,
31+
CONSTRAINT user_id_fk
32+
FOREIGN KEY(user_id) REFERENCES users(id)
33+
ON DELETE CASCADE
34+
ON UPDATE CASCADE
35+
);
36+
37+
CREATE TABLE IF NOT EXISTS teams (
38+
id SERIAL PRIMARY KEY,
39+
name TEXT NOT NULL,
40+
org_id INTEGER NOT NULL,
41+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
42+
updated_at TIMESTAMP NOT NULL,
43+
UNIQUE (name, org_id),
44+
CONSTRAINT org_id_fk
45+
FOREIGN KEY(org_id) REFERENCES organizations(id)
46+
ON DELETE CASCADE
47+
ON UPDATE CASCADE
48+
);
49+
50+
CREATE TABLE IF NOT EXISTS team_members (
51+
id SERIAL PRIMARY KEY,
52+
team_id INTEGER NOT NULL,
53+
user_id INTEGER NOT NULL,
54+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
55+
updated_at TIMESTAMP NOT NULL,
56+
UNIQUE (team_id, user_id),
57+
CONSTRAINT team_id_fk
58+
FOREIGN KEY(team_id) REFERENCES teams(id)
59+
ON DELETE CASCADE
60+
ON UPDATE CASCADE,
61+
CONSTRAINT user_id_fk
62+
FOREIGN KEY(user_id) REFERENCES users(id)
63+
ON DELETE CASCADE
64+
ON UPDATE CASCADE
65+
);
66+
67+
-- Add email to users table
68+
ALTER TABLE users ADD COLUMN email TEXT;
69+
70+
-- Add handle_id to users table
71+
ALTER TABLE users ADD COLUMN handle_id INTEGER;
72+
ALTER TABLE users ADD CONSTRAINT handle_id_fk
73+
FOREIGN KEY(handle_id) REFERENCES handles(id)
74+
ON DELETE CASCADE
75+
ON UPDATE CASCADE;
76+
77+
-- Migrate user username to handles
78+
INSERT INTO handles (handle, updated_at) SELECT username, updated_at FROM users;
79+
80+
-- Update handle_id for users
81+
UPDATE users SET handle_id = handles.id FROM handles WHERE handles.handle = users.username;
82+
83+
-- Make handle_id not null and unique
84+
ALTER TABLE users ALTER COLUMN handle_id SET NOT NULL;
85+
ALTER TABLE users ADD CONSTRAINT handle_id_unique UNIQUE (handle_id);
86+
87+
-- Drop username from users
88+
ALTER TABLE users DROP COLUMN username;
89+
90+
-- Add org_id to repos table
91+
ALTER TABLE repos ADD COLUMN org_id INTEGER;
92+
93+
-- Alter user_id nullness in repos table
94+
ALTER TABLE repos ALTER COLUMN user_id DROP NOT NULL;
95+

pkg/db/migrate/migrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var migrations = []Migration{
1818
createTables,
1919
webhooks,
2020
migrateLfsObjects,
21+
createOrgsTeams,
2122
}
2223

2324
func execMigration(ctx context.Context, tx *db.Tx, version int, name string, down bool) error {

0 commit comments

Comments
 (0)