Skip to content

Commit 27ce304

Browse files
committed
app/: review fixes for #54 and #62 (sc-8900): indexes on the foreign keys whose deletes would scan (baselines.review_id and approved_by, builds.created_by_user_id, comments.user_id, comment_threads.resolved_by); credit_transactions.build_id and a gallery day key; snapshot registration as one conditional upsert (a race between two files is a 409 for one of them), resetting a carried row; limits on commits, parent commits, branch, name, nonce, content type and meta; every snapshot validated before any is written; today's 401 messages; the personal organization takes the next slug on a race; credits::usage, plans, allowance and spend
1 parent add224f commit 27ce304

10 files changed

Lines changed: 507 additions & 39 deletions

File tree

‎app/database/migrations/2026_12_03_000200_create_domain_tables.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ CREATE TABLE credit_transactions (
209209
updated_at timestamp with time zone DEFAULT now() NOT NULL,
210210
project_id bigint,
211211
open_source_credits bigint DEFAULT 0 NOT NULL,
212+
-- The build a charge is for (one row per build charge step); NULL for purchases, grants and gallery days.
213+
build_id bigint,
214+
-- A gallery day's roll-up: one row per (organization, project, day), its amount grown as images are served.
215+
day date,
212216
CONSTRAINT credit_transactions_kind_check CHECK ((kind = ANY (ARRAY['purchase'::text, 'grant'::text, 'build'::text, 'render'::text, 'diff'::text, 'summary'::text, 'refund'::text, 'adjustment'::text, 'gallery'::text])))
213217
);
214218
@@ -747,6 +751,8 @@ CREATE INDEX credit_transactions_org_created ON credit_transactions USING btree
747751
CREATE INDEX credit_transactions_project ON credit_transactions USING btree (project_id, created_at);
748752
749753
CREATE UNIQUE INDEX credit_transactions_purchase_once ON credit_transactions USING btree (organization_id, reference) WHERE (kind = 'purchase'::text);
754+
CREATE UNIQUE INDEX credit_transactions_gallery_day ON credit_transactions USING btree (organization_id, project_id, day) WHERE (kind = 'gallery'::text);
755+
CREATE INDEX credit_transactions_build_idx ON credit_transactions USING btree (build_id) WHERE build_id IS NOT NULL;
750756
751757
CREATE INDEX feedback_account_created ON feedback USING btree (account, created_at);
752758
@@ -915,6 +921,11 @@ CREATE INDEX build_groups_source_idx ON build_groups USING btree (source_build_i
915921
CREATE INDEX snapshots_build_group_idx ON snapshots USING btree (build_id, group_key);
916922
917923
-- Hot paths and foreign keys (every delete of a parent row checks these columns).
924+
CREATE INDEX baselines_review_idx ON baselines USING btree (review_id) WHERE review_id IS NOT NULL;
925+
CREATE INDEX baselines_approved_by_idx ON baselines USING btree (approved_by) WHERE approved_by IS NOT NULL;
926+
CREATE INDEX builds_created_by_idx ON builds USING btree (created_by_user_id) WHERE created_by_user_id IS NOT NULL;
927+
CREATE INDEX comments_user_idx ON comments USING btree (user_id) WHERE user_id IS NOT NULL;
928+
CREATE INDEX comment_threads_resolved_by_idx ON comment_threads USING btree (resolved_by) WHERE resolved_by IS NOT NULL;
918929
CREATE INDEX snapshots_render_idx ON snapshots USING btree (render_id) WHERE render_id IS NOT NULL;
919930
920931
CREATE INDEX snapshots_source_blob_idx ON snapshots USING btree (source_blob) WHERE source_blob IS NOT NULL;
@@ -1055,6 +1066,9 @@ CREATE TABLE downloads (
10551066
CREATE INDEX downloads_build_filter ON downloads USING btree (build_id, filter_key, created_at DESC);
10561067
CREATE INDEX downloads_created_brin ON downloads USING brin (created_at);
10571068
1069+
ALTER TABLE ONLY credit_transactions
1070+
ADD CONSTRAINT credit_transactions_build_id_fkey FOREIGN KEY (build_id) REFERENCES builds(id) ON DELETE SET NULL;
1071+
10581072
-- Project tokens (the auth migration) belong to a project.
10591073
ALTER TABLE ONLY project_tokens
10601074
ADD CONSTRAINT project_tokens_project_id_fkey FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;

‎app/src/domain/builds.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ pub async fn create(project: &Project, params: &CreateParams, creator: Option<i6
132132
if params.commit.is_empty() || params.branch.is_empty() {
133133
return invalid("commit and branch are required");
134134
}
135+
let id_like = |s: &str| (1..=64).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_alphanumeric());
136+
if !id_like(&params.commit) {
137+
return invalid("commit must be 1 to 64 letters and digits (a commit SHA)");
138+
}
139+
if params.parent_commits.iter().any(|c| !id_like(c)) {
140+
return invalid("parent_commits must be commit SHAs");
141+
}
142+
if params.branch.len() > 255 || params.branch.chars().any(char::is_control) {
143+
return invalid("branch must be at most 255 characters");
144+
}
145+
if params.name.trim().is_empty() || params.name.len() > 100 {
146+
return invalid("name must be 1 to 100 characters");
147+
}
148+
if params.nonce.as_ref().is_some_and(|n| n.is_empty() || n.len() > 200) {
149+
return invalid("nonce must be 1 to 200 characters");
150+
}
135151
if !(1..=1000).contains(&params.shard_total) {
136152
return invalid("shard_total must be 1 to 1000");
137153
}

0 commit comments

Comments
 (0)