/ opencodehub / src / db / schema / workflows.ts
Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
289 lines · 9.4 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * CI/CD Pipeline Schema - Drizzle ORM
 * Defines workflows, runs, jobs, steps, and artifacts
 */

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

export const workflows = pgTable(
  "workflows",
  {
    id: text("id").primaryKey(),
    repositoryId: text("repository_id")
      .notNull()
      .references(() => repositories.id, { onDelete: "cascade" }),
    name: text("name").notNull(),
    path: text("path").notNull(), // .github/workflows/ci.yml
    state: text("state").notNull().default("active"), // active, disabled, deleted
    badgeUrl: text("badge_url"),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow(),
  },
  (t) => ({
    repoIdx: index("workflows_repo_idx").on(t.repositoryId),
  }),
);

export const workflowRuns = pgTable(
  "workflow_runs",
  {
    id: text("id").primaryKey(),
    workflowId: text("workflow_id")
      .notNull()
      .references(() => workflows.id, { onDelete: "cascade" }),
    repositoryId: text("repository_id")
      .notNull()
      .references(() => repositories.id, { onDelete: "cascade" }),
    runNumber: integer("run_number").notNull(),
    runAttempt: integer("run_attempt").notNull().default(1),
    name: text("name").notNull(),
    displayTitle: text("display_title"),
    status: text("status").notNull().default("queued"), // queued, in_progress, completed
    conclusion: text("conclusion"), // success, failure, cancelled, skipped, neutral, timed_out, action_required
    event: text("event").notNull(), // push, pull_request, schedule, workflow_dispatch, etc.

    // Git context
    headBranch: text("head_branch"),
    headSha: text("head_sha").notNull(),
    baseBranch: text("base_branch"),
    baseSha: text("base_sha"),

    // References
    pullRequestId: text("pull_request_id").references(() => pullRequests.id),
    triggeredById: text("triggered_by_id").references(() => users.id),

    // Timing
    startedAt: timestamp("started_at"),
    completedAt: timestamp("completed_at"),

    // Config from YAML
    workflowConfig: text("workflow_config"), // JSON - parsed workflow YAML

    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow(),
  },
  (t) => ({
    workflowIdx: index("workflow_runs_workflow_idx").on(t.workflowId),
    repoIdx: index("workflow_runs_repo_idx").on(t.repositoryId),
    statusIdx: index("workflow_runs_status_idx").on(t.status),
  }),
);

export const workflowJobs = pgTable(
  "workflow_jobs",
  {
    id: text("id").primaryKey(),
    runId: text("run_id")
      .notNull()
      .references(() => workflowRuns.id, { onDelete: "cascade" }),
    name: text("name").notNull(),
    status: text("status").notNull().default("queued"), // queued, in_progress, completed
    conclusion: text("conclusion"), // success, failure, cancelled, skipped

    // Runner info
    runnerId: text("runner_id"),
    runnerName: text("runner_name"),
    runnerGroupId: text("runner_group_id"),
    runnerGroupName: text("runner_group_name"),

    // Container info
    containerId: text("container_id"),
    containerImage: text("container_image"),

    // Dependencies
    needs: text("needs"), // JSON array of job names this depends on

    // Environment
    environment: text("environment"),
    environmentUrl: text("environment_url"),

    // Matrix
    matrix: text("matrix"), // JSON - matrix configuration for this job instance

    // Timing
    startedAt: timestamp("started_at"),
    completedAt: timestamp("completed_at"),

    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow(),
  },
  (t) => ({
    runIdx: index("workflow_jobs_run_idx").on(t.runId),
  }),
);

export const workflowSteps = pgTable("workflow_steps", {
  id: text("id").primaryKey(),
  jobId: text("job_id")
    .notNull()
    .references(() => workflowJobs.id, { onDelete: "cascade" }),
  number: integer("number").notNull(),
  name: text("name").notNull(),
  status: text("status").notNull().default("queued"), // queued, in_progress, completed
  conclusion: text("conclusion"), // success, failure, cancelled, skipped

  // Step configuration
  uses: text("uses"), // action reference: actions/checkout@v4
  run: text("run"), // shell command
  shell: text("shell"), // bash, pwsh, python, etc.
  workingDirectory: text("working_directory"),
  env: text("env"), // JSON
  with: text("with"), // JSON - action inputs

  // Conditionals
  if: text("if"),
  continueOnError: boolean("continue_on_error").default(false),
  timeoutMinutes: integer("timeout_minutes"),

  // Timing
  startedAt: timestamp("started_at"),
  completedAt: timestamp("completed_at"),

  createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const workflowLogs = pgTable("workflow_logs", {
  id: text("id").primaryKey(),
  jobId: text("job_id")
    .notNull()
    .references(() => workflowJobs.id, { onDelete: "cascade" }),
  stepId: text("step_id").references(() => workflowSteps.id),
  logLevel: text("log_level").notNull().default("info"), // debug, info, warn, error
  message: text("message").notNull(),
  timestamp: text("timestamp").notNull(),
  lineNumber: integer("line_number"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const workflowArtifacts = pgTable("workflow_artifacts", {
  id: text("id").primaryKey(),
  runId: text("run_id")
    .notNull()
    .references(() => workflowRuns.id, { onDelete: "cascade" }),
  jobId: text("job_id").references(() => workflowJobs.id),
  name: text("name").notNull(),
  sizeBytes: integer("size_bytes").notNull(),
  storagePath: text("storage_path").notNull(),
  mimeType: text("mime_type"),
  downloadCount: integer("download_count").default(0),
  expiresAt: timestamp("expires_at"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const workflowSecrets = pgTable("workflow_secrets", {
  id: text("id").primaryKey(),
  repositoryId: text("repository_id")
    .notNull()
    .references(() => repositories.id, { onDelete: "cascade" }),
  name: text("name").notNull(),
  encryptedValue: text("encrypted_value").notNull(), // AES-GCM encrypted at rest
  environment: text("environment"), // null for repo-level, or environment name
  createdById: text("created_by_id").references(() => users.id),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  updatedAt: timestamp("updated_at").notNull().defaultNow(),
});

export const workflowVariables = pgTable("workflow_variables", {
  id: text("id").primaryKey(),
  repositoryId: text("repository_id")
    .notNull()
    .references(() => repositories.id, { onDelete: "cascade" }),
  name: text("name").notNull(),
  value: text("value").notNull(),
  environment: text("environment"),
  createdById: text("created_by_id").references(() => users.id),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  updatedAt: timestamp("updated_at").notNull().defaultNow(),
});

export const scheduledWorkflows = pgTable("scheduled_workflows", {
  id: text("id").primaryKey(),
  workflowId: text("workflow_id")
    .notNull()
    .references(() => workflows.id, { onDelete: "cascade" }),
  cronExpression: text("cron_expression").notNull(),
  timezone: text("timezone").default("UTC"),
  isEnabled: boolean("is_enabled").default(true),
  lastRunAt: timestamp("last_run_at"),
  nextRunAt: timestamp("next_run_at"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
});

// Relations
export const workflowsRelations = relations(workflows, ({ one, many }) => ({
  repository: one(repositories, {
    fields: [workflows.repositoryId],
    references: [repositories.id],
  }),
  runs: many(workflowRuns),
  schedules: many(scheduledWorkflows),
}));

export const workflowRunsRelations = relations(
  workflowRuns,
  ({ one, many }) => ({
    workflow: one(workflows, {
      fields: [workflowRuns.workflowId],
      references: [workflows.id],
    }),
    repository: one(repositories, {
      fields: [workflowRuns.repositoryId],
      references: [repositories.id],
    }),
    pullRequest: one(pullRequests, {
      fields: [workflowRuns.pullRequestId],
      references: [pullRequests.id],
    }),
    triggeredBy: one(users, {
      fields: [workflowRuns.triggeredById],
      references: [users.id],
    }),
    jobs: many(workflowJobs),
    artifacts: many(workflowArtifacts),
  }),
);

export const workflowJobsRelations = relations(
  workflowJobs,
  ({ one, many }) => ({
    run: one(workflowRuns, {
      fields: [workflowJobs.runId],
      references: [workflowRuns.id],
    }),
    steps: many(workflowSteps),
    logs: many(workflowLogs),
    artifacts: many(workflowArtifacts),
  }),
);

export const workflowStepsRelations = relations(
  workflowSteps,
  ({ one, many }) => ({
    job: one(workflowJobs, {
      fields: [workflowSteps.jobId],
      references: [workflowJobs.id],
    }),
    logs: many(workflowLogs),
  }),
);

// Types
export type Workflow = typeof workflows.$inferSelect;
export type WorkflowRun = typeof workflowRuns.$inferSelect;
export type WorkflowJob = typeof workflowJobs.$inferSelect;
export type WorkflowStep = typeof workflowSteps.$inferSelect;
export type WorkflowLog = typeof workflowLogs.$inferSelect;
export type WorkflowArtifact = typeof workflowArtifacts.$inferSelect;
export type WorkflowSecret = typeof workflowSecrets.$inferSelect;