defmodule GitGud.Repo.Migrations.CreatePullRequests do
use Ecto.Migration
def change do
create table(:pull_requests) do
add :repository_id, references(:repositories, on_delete: :delete_all), null: false
add :author_id, references(:users, on_delete: :nilify_all)
add :number, :integer, null: false
add :title, :string, null: false, size: 255
add :body, :text
# open | closed | merged
add :state, :string, null: false, default: "open"
# Branch references (short names). Source = the head; target = where
# the PR will be merged into.
add :source_ref, :string, null: false, size: 250
add :target_ref, :string, null: false, size: 250
# SHA snapshots — refreshed when the PR is rebased / new commits land.
add :head_sha, :binary, null: false
add :base_sha, :binary, null: false
# Cached mergeability probe.
# nil = not yet probed; true/false = result of last probe.
add :mergeable, :boolean
add :mergeable_checked_at, :utc_datetime
add :closed_at, :utc_datetime
add :merged_at, :utc_datetime
add :merged_by_id, references(:users, on_delete: :nilify_all)
add :merge_commit_sha, :binary
timestamps(type: :utc_datetime)
end
create unique_index(:pull_requests, [:repository_id, :number])
create index(:pull_requests, [:repository_id, :state])
create index(:pull_requests, [:author_id])
execute """
ALTER TABLE pull_requests
ADD COLUMN search_vector tsvector
GENERATED ALWAYS AS (
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(body, '')), 'B')
) STORED;
""",
"ALTER TABLE pull_requests DROP COLUMN search_vector;"
create index(:pull_requests, [:search_vector], using: :gin)
create table(:pr_comments) do
add :pull_request_id, references(:pull_requests, on_delete: :delete_all), null: false
add :author_id, references(:users, on_delete: :nilify_all)
add :body, :text, null: false
# Optional inline review anchor — file + line within head_sha.
# When both are nil, it's a top-level discussion comment.
add :file_path, :string
add :line, :integer
add :commit_sha, :binary
timestamps(type: :utc_datetime)
end
create index(:pr_comments, [:pull_request_id, :inserted_at])
create table(:pr_reviews) do
add :pull_request_id, references(:pull_requests, on_delete: :delete_all), null: false
add :reviewer_id, references(:users, on_delete: :nilify_all), null: false
# approved | changes_requested | commented
add :state, :string, null: false
add :body, :text
timestamps(type: :utc_datetime)
end
create index(:pr_reviews, [:pull_request_id])
end
end
2.8 KiB · text
5af55d9