MySQL · 1698 bytes Raw Blame History
1 -- SPDX-License-Identifier: AGPL-3.0-or-later
2 --
3 -- Profile and organization pinned repositories.
4 --
5 -- A pin set records that the owner has customized their pinned
6 -- repositories. The separate row lets us distinguish "never customized"
7 -- from "customized to zero pins" while keeping the ordered pins table
8 -- normalized and easy to replace transactionally.
9
10 -- +goose Up
11 CREATE TABLE profile_pin_sets (
12 id bigserial PRIMARY KEY,
13 owner_user_id bigint REFERENCES users(id) ON DELETE CASCADE,
14 owner_org_id bigint REFERENCES orgs(id) ON DELETE CASCADE,
15 updated_at timestamptz NOT NULL DEFAULT now(),
16
17 CONSTRAINT profile_pin_sets_owner_xor CHECK (
18 (owner_user_id IS NOT NULL AND owner_org_id IS NULL)
19 OR (owner_user_id IS NULL AND owner_org_id IS NOT NULL)
20 )
21 );
22
23 CREATE UNIQUE INDEX profile_pin_sets_owner_user_uq
24 ON profile_pin_sets (owner_user_id)
25 WHERE owner_user_id IS NOT NULL;
26
27 CREATE UNIQUE INDEX profile_pin_sets_owner_org_uq
28 ON profile_pin_sets (owner_org_id)
29 WHERE owner_org_id IS NOT NULL;
30
31 CREATE TABLE profile_pins (
32 set_id bigint NOT NULL REFERENCES profile_pin_sets(id) ON DELETE CASCADE,
33 repo_id bigint NOT NULL REFERENCES repos(id) ON DELETE CASCADE,
34 position integer NOT NULL,
35 pinned_at timestamptz NOT NULL DEFAULT now(),
36
37 PRIMARY KEY (set_id, repo_id),
38 CONSTRAINT profile_pins_position_range CHECK (position BETWEEN 1 AND 6),
39 CONSTRAINT profile_pins_set_position_uq UNIQUE (set_id, position)
40 );
41
42 CREATE INDEX profile_pins_repo_idx ON profile_pins (repo_id);
43
44 -- +goose Down
45 DROP TABLE IF EXISTS profile_pins;
46 DROP TABLE IF EXISTS profile_pin_sets;
47