forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemaMigrations.ts
More file actions
236 lines (220 loc) · 8.5 KB
/
Copy pathschemaMigrations.ts
File metadata and controls
236 lines (220 loc) · 8.5 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Copyright 2026 GitProxy Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Pool } from 'pg';
/**
* A single, immutable schema change. Append new migrations with the next
* `version`; never edit or reorder entries that have already shipped, since
* deployed databases record which versions they have applied.
*/
export interface Migration {
version: number;
name: string;
sql: string;
}
/**
* Ordered, append-only list of schema migrations.
*
* Version 1 is the initial schema. Because every statement uses
* `CREATE TABLE/INDEX IF NOT EXISTS`, databases that were already bootstrapped
* by the pre-migration code adopt the runner transparently: the statements are
* no-ops and version 1 is simply recorded as applied.
*/
export const MIGRATIONS: Migration[] = [
{
version: 1,
name: 'initial_schema',
sql: `
CREATE TABLE IF NOT EXISTS users (
_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
password TEXT,
git_account TEXT NOT NULL,
admin BOOLEAN NOT NULL DEFAULT FALSE,
oidc_id TEXT UNIQUE,
display_name TEXT,
title TEXT
);
CREATE TABLE IF NOT EXISTS repos (
_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
project TEXT NOT NULL DEFAULT '',
name TEXT NOT NULL,
url TEXT NOT NULL UNIQUE,
users JSONB NOT NULL DEFAULT '{"canPush":[],"canAuthorise":[]}'::jsonb,
date_created TEXT,
last_modified TEXT
);
ALTER TABLE repos ADD COLUMN IF NOT EXISTS date_created TEXT;
ALTER TABLE repos ADD COLUMN IF NOT EXISTS last_modified TEXT;
CREATE INDEX IF NOT EXISTS repos_name_idx ON repos (name);
CREATE TABLE IF NOT EXISTS pushes (
id TEXT PRIMARY KEY,
timestamp BIGINT NOT NULL,
type TEXT,
error BOOLEAN NOT NULL DEFAULT FALSE,
blocked BOOLEAN NOT NULL DEFAULT FALSE,
allow_push BOOLEAN NOT NULL DEFAULT FALSE,
authorised BOOLEAN NOT NULL DEFAULT FALSE,
canceled BOOLEAN NOT NULL DEFAULT FALSE,
rejected BOOLEAN NOT NULL DEFAULT FALSE,
data JSONB NOT NULL
);
CREATE INDEX IF NOT EXISTS pushes_timestamp_idx ON pushes (timestamp DESC);
`,
},
{
version: 2,
name: 'user_public_keys_and_optional_email',
sql: `
ALTER TABLE users ADD COLUMN IF NOT EXISTS public_keys JSONB NOT NULL DEFAULT '[]'::jsonb;
ALTER TABLE users ALTER COLUMN email DROP NOT NULL;
ALTER TABLE users DROP CONSTRAINT IF EXISTS users_email_key;
-- Email uniqueness is best-effort, like the mongo/fs backends: a real
-- address can only be claimed once, but any number of users may have no
-- email (the AD "mail" attribute is optional, for instance).
CREATE UNIQUE INDEX IF NOT EXISTS users_email_unique
ON users (email) WHERE email IS NOT NULL AND email <> '';
`,
},
{
version: 3,
name: 'migration_bookkeeping',
sql: `
-- Bookkeeping for the cross-backend migration framework in src/db/migrations.
-- That framework records logical migrations by string id through the Sink
-- hooks; this table is its postgres storage, and is separate from the
-- schema_migrations table that versions the DDL below.
CREATE TABLE IF NOT EXISTS migrations (
id TEXT PRIMARY KEY
);
`,
},
{
version: 4,
name: 'repo_users_table',
sql: `
CREATE TABLE IF NOT EXISTS repo_users (
repo_id UUID NOT NULL REFERENCES repos(_id) ON DELETE CASCADE,
username TEXT NOT NULL,
role TEXT NOT NULL CHECK (role IN ('canPush', 'canAuthorise')),
PRIMARY KEY (repo_id, username, role)
);
CREATE INDEX IF NOT EXISTS repo_users_repo_id_idx ON repo_users (repo_id);
-- Backfill the normalised table from the existing JSONB permissions. The
-- legacy repos.users column is dropped in a later migration once the adapter
-- reads and writes repo_users instead.
-- Usernames are lowercased to match the runtime writers (addUserToRole and
-- friends lowercase on insert), so legacy mixed-case entries stay
-- retrievable; ON CONFLICT collapses any case-only duplicates.
INSERT INTO repo_users (repo_id, username, role)
SELECT r._id, lower(elem.username), 'canPush'
FROM repos r,
jsonb_array_elements_text(coalesce(r.users->'canPush', '[]'::jsonb)) AS elem(username)
ON CONFLICT DO NOTHING;
INSERT INTO repo_users (repo_id, username, role)
SELECT r._id, lower(elem.username), 'canAuthorise'
FROM repos r,
jsonb_array_elements_text(coalesce(r.users->'canAuthorise', '[]'::jsonb)) AS elem(username)
ON CONFLICT DO NOTHING;
`,
},
{
version: 5,
name: 'drop_repos_users_jsonb',
// The repo adapter now reads and writes permissions via repo_users, so the
// legacy JSONB column (backfilled in migration 4) is no longer used.
sql: `ALTER TABLE repos DROP COLUMN IF EXISTS users;`,
},
{
version: 6,
name: 'pushes_hot_path_indexes',
sql: `
-- Covering index for the repo activity rollup: the scan becomes index-only
-- and never detoasts the large push JSONB documents.
CREATE INDEX IF NOT EXISTS pushes_rollup_idx
ON pushes ((data->>'url'), timestamp)
INCLUDE (error, rejected, canceled, authorised, blocked, allow_push)
WHERE type = 'push';
-- Matches the default dashboard query for pushes pending review.
CREATE INDEX IF NOT EXISTS pushes_pending_idx
ON pushes (timestamp DESC)
WHERE type = 'push' AND blocked AND NOT error AND NOT authorised AND NOT allow_push;
-- User profile lookups filter on JSONB expressions; index both predicates.
CREATE INDEX IF NOT EXISTS pushes_user_email_idx
ON pushes ((data->>'userEmail'))
WHERE type = 'push';
CREATE INDEX IF NOT EXISTS pushes_reviewer_idx
ON pushes ((lower(data->'attestation'->'reviewer'->>'username')))
WHERE type = 'push';
`,
},
];
const SCHEMA_MIGRATIONS_TABLE_SQL = `
CREATE TABLE IF NOT EXISTS schema_migrations (
version INTEGER PRIMARY KEY,
name TEXT NOT NULL,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
`;
// Fixed, arbitrary advisory-lock key. Serialises migration runs across
// concurrently starting processes so each migration is applied exactly once.
const MIGRATION_ADVISORY_LOCK_KEY = 4815162342;
/**
* Apply any not-yet-applied migrations in version order, inside a single
* transaction guarded by a transaction-scoped advisory lock.
*
* Safe to call on every process start: already-applied migrations are skipped,
* and concurrent callers block on the lock rather than racing. The lock is
* acquired before the `schema_migrations` table is touched so two processes
* booting against a brand-new database cannot both seed version 1.
*
* NOTE: all pending migrations run in one transaction, so a future statement
* that cannot run transactionally (e.g. `CREATE INDEX CONCURRENTLY`) will need
* dedicated handling — not required for the current schema.
*/
export const runMigrations = async (pool: Pool): Promise<void> => {
const client = await pool.connect();
try {
await client.query('BEGIN');
// Transaction-scoped lock; auto-released on COMMIT/ROLLBACK.
await client.query('SELECT pg_advisory_xact_lock($1)', [MIGRATION_ADVISORY_LOCK_KEY]);
await client.query(SCHEMA_MIGRATIONS_TABLE_SQL);
const { rows } = await client.query<{ version: number }>(
'SELECT version FROM schema_migrations',
);
const applied = new Set(rows.map((row) => row.version));
const pending = [...MIGRATIONS]
.sort((a, b) => a.version - b.version)
.filter((migration) => !applied.has(migration.version));
for (const migration of pending) {
await client.query(migration.sql);
await client.query('INSERT INTO schema_migrations (version, name) VALUES ($1, $2)', [
migration.version,
migration.name,
]);
}
await client.query('COMMIT');
} catch (err) {
try {
await client.query('ROLLBACK');
} catch {
// best-effort rollback; the original error is rethrown below
}
throw err;
} finally {
client.release();
}
};