penpot/backend/src/app/migrations/sql/0156-add-file-pull-request-tables.sql
Álvaro Tejero Cantero 2a5a235063
✨ Add the design preview of file branching and pull requests
This commit carries the design-branching preview by Juan de la Cruz:
elhombretecla/penpot:branching at 0ac265a3f5, which holds 45 commits
and 3 develop merges and was last synced with develop on 2026-09-16.
The branch is squashed together with a merge of origin/develop at
7c039231f7, so the tree is the merged result.

The merge resolved conflicts in six files:

- backend/src/app/migrations.clj: develop's 0155 and 0156 steps run
  first, then the branching steps; the names differ.
- backend/src/app/rpc/commands/viewer.clj: both requires stay.
- common/src/app/common/flags.cljc: both flags stay.
- frontend/src/app/main/ui/dashboard/file_menu.cljs: "Create branch"
  becomes a menu-item* with the same id, guard and handler.
- frontend/src/app/main/ui/dashboard/grid.cljs: the portal goes with
  develop's menu rewrite; the branches popover stays in the trigger.
- frontend/translations/en.po: both sides' entries stay, with
  develop's source references.

Co-authored-by: Juan de la Cruz <delacruzgarciajuan@gmail.com>
AI-assisted-by: mixed models
2026-10-06 10:36:54 +02:00

66 lines
2.7 KiB
SQL

CREATE TABLE IF NOT EXISTS file_pull_request (
id uuid PRIMARY KEY,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
file_branch_id uuid NOT NULL REFERENCES file_branch(id) ON DELETE CASCADE DEFERRABLE,
-- denormalized from file_branch: source is the BRANCH file, target is main
source_file_id uuid NOT NULL REFERENCES file(id) ON DELETE CASCADE DEFERRABLE,
target_file_id uuid NOT NULL REFERENCES file(id) ON DELETE CASCADE DEFERRABLE,
title text NOT NULL,
description text NULL,
created_by uuid NULL REFERENCES profile(id) ON DELETE SET NULL,
status text NOT NULL DEFAULT 'open'
CHECK (status IN ('open', 'merged', 'closed')),
-- pinned snapshot of the BRANCH file that reviewers see
review_snapshot_id uuid NULL,
-- the branch file's revn when the review snapshot was (re)positioned;
-- branch_revn > review_revn is the cheap "outdated" gate
review_revn bigint NOT NULL DEFAULT 0,
review_updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
closed_at timestamptz NULL,
closed_by uuid NULL REFERENCES profile(id) ON DELETE SET NULL,
deleted_at timestamptz NULL,
CHECK (source_file_id != target_file_id)
);
-- at most one live pull request per branch
CREATE UNIQUE INDEX IF NOT EXISTS file_pull_request__file_branch_id__open__idx
ON file_pull_request(file_branch_id)
WHERE status = 'open' AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS file_pull_request__target_file_id__idx
ON file_pull_request(target_file_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS file_pull_request__review_snapshot_id__idx
ON file_pull_request(review_snapshot_id) WHERE review_snapshot_id IS NOT NULL;
CREATE TABLE IF NOT EXISTS file_pull_request_review (
pull_request_id uuid NOT NULL REFERENCES file_pull_request(id) ON DELETE CASCADE DEFERRABLE,
profile_id uuid NOT NULL REFERENCES profile(id) ON DELETE CASCADE DEFERRABLE,
state text NOT NULL DEFAULT 'pending'
CHECK (state IN ('pending', 'approved', 'changes-requested')),
-- review_revn of the pull request when the verdict was submitted; a
-- verdict is stale when it no longer matches the pr's review_revn
reviewed_revn bigint NULL,
comment text NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (pull_request_id, profile_id)
);
CREATE INDEX IF NOT EXISTS file_pull_request_review__profile_id__idx
ON file_pull_request_review(profile_id);