/ opencodehub / src / db / schema / stacked-prs.ts
Swadhin Biswas Feature/ai code reviewer (#80) · 8/17/2026
9740d3d
85 lines · 2.9 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
/**
 * Stacked Pull Requests Schema
 * Enables first-class stacked PR workflows
 */

import { relations } from "drizzle-orm";
import { index, integer, pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { pullRequests } from "./pull-requests";
import { repositories } from "./repositories";
import { users } from "./users";

// PR Stacks - groups of related stacked PRs
export const prStacks = pgTable("pr_stacks", {
    id: text("id").primaryKey(),
    repositoryId: text("repository_id")
        .notNull()
        .references(() => repositories.id, { onDelete: "cascade" }),
    name: text("name"), // Optional stack name
    baseBranch: text("base_branch").notNull(),
    status: text("status").notNull().default("active"), // active, merged, closed
    createdById: text("created_by_id")
        .notNull()
        .references(() => users.id),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow(),
  },
  (t) => ({
    repoIdx: index("pr_stacks_repo_idx").on(t.repositoryId),
  }),
);

// Stack entries - individual PRs in a stack with ordering
export const prStackEntries = pgTable("pr_stack_entries", {
    id: text("id").primaryKey(),
    stackId: text("stack_id")
        .notNull()
        .references(() => prStacks.id, { onDelete: "cascade" }),
    pullRequestId: text("pull_request_id")
        .notNull()
        .references(() => pullRequests.id, { onDelete: "cascade" }),
    stackOrder: integer("stack_order").notNull(), // Position in stack (1 = base)
    parentPrId: text("parent_pr_id").references(() => pullRequests.id),
    createdAt: timestamp("created_at").notNull().defaultNow(),
  },
  (t) => ({
    stackIdx: index("pr_stack_entries_stack_idx").on(t.stackId),
    prIdx: index("pr_stack_entries_pr_idx").on(t.pullRequestId),
    parentPrIdx: index("pr_stack_entries_parent_pr_idx").on(t.parentPrId),
  }),
);

// Relations
export const prStacksRelations = relations(prStacks, ({ one, many }) => ({
    repository: one(repositories, {
        fields: [prStacks.repositoryId],
        references: [repositories.id],
    }),
    createdBy: one(users, {
        fields: [prStacks.createdById],
        references: [users.id],
    }),
    entries: many(prStackEntries),
}));

export const prStackEntriesRelations = relations(prStackEntries, ({ one }) => ({
    stack: one(prStacks, {
        fields: [prStackEntries.stackId],
        references: [prStacks.id],
    }),
    pullRequest: one(pullRequests, {
        fields: [prStackEntries.pullRequestId],
        references: [pullRequests.id],
    }),
    parentPr: one(pullRequests, {
        fields: [prStackEntries.parentPrId],
        references: [pullRequests.id],
    }),
}));

// Types
export type PrStack = typeof prStacks.$inferSelect;
export type NewPrStack = typeof prStacks.$inferInsert;
export type PrStackEntry = typeof prStackEntries.$inferSelect;
export type NewPrStackEntry = typeof prStackEntries.$inferInsert;