-
Notifications
You must be signed in to change notification settings - Fork 11
refactor: update approval record schema and related functions #530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||||||||||||||||||||||||||
| import { sql } from "drizzle-orm"; | ||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||
| pgEnum, | ||||||||||||||||||||||||||||||
| pgTable, | ||||||||||||||||||||||||||||||
| text, | ||||||||||||||||||||||||||||||
| timestamp, | ||||||||||||||||||||||||||||||
| uniqueIndex, | ||||||||||||||||||||||||||||||
| uuid, | ||||||||||||||||||||||||||||||
| } from "drizzle-orm/pg-core"; | ||||||||||||||||||||||||||||||
| import { z } from "zod"; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import { user } from "../auth.js"; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const approvalStatus = pgEnum("approval_status", [ | ||||||||||||||||||||||||||||||
| "approved", | ||||||||||||||||||||||||||||||
| "rejected", | ||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const deploymentVersionApprovalRecord = pgTable( | ||||||||||||||||||||||||||||||
| "deployment_version_approval_record", | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| id: uuid("id").primaryKey().defaultRandom(), | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Link to the deployment version being approved | ||||||||||||||||||||||||||||||
| deploymentVersionId: uuid("deployment_version_id").notNull(), | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // User who performed the approval/rejection action | ||||||||||||||||||||||||||||||
| userId: uuid("user_id") | ||||||||||||||||||||||||||||||
| .references(() => user.id) | ||||||||||||||||||||||||||||||
| .notNull(), | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Status of this approval | ||||||||||||||||||||||||||||||
| status: approvalStatus("status").notNull(), | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Timestamp of when the approval was performed | ||||||||||||||||||||||||||||||
| approvedAt: timestamp("approved_at", { withTimezone: true }).default( | ||||||||||||||||||||||||||||||
| sql`NULL`, | ||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Reason provided by approver | ||||||||||||||||||||||||||||||
| reason: text("reason"), | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| createdAt: timestamp("created_at", { withTimezone: true }) | ||||||||||||||||||||||||||||||
| .notNull() | ||||||||||||||||||||||||||||||
| .defaultNow(), | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate( | ||||||||||||||||||||||||||||||
| () => new Date(), | ||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| (t) => ({ | ||||||||||||||||||||||||||||||
| uniqueDeploymentVersionIdUserId: uniqueIndex( | ||||||||||||||||||||||||||||||
| "unique_deployment_version_id_user_id", | ||||||||||||||||||||||||||||||
| ).on(t.deploymentVersionId, t.userId), | ||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Base validation fields for approval records | ||||||||||||||||||||||||||||||
| export const baseApprovalRecordValidationFields = { | ||||||||||||||||||||||||||||||
| deploymentVersionId: z.string().uuid(), | ||||||||||||||||||||||||||||||
| userId: z.string().uuid(), | ||||||||||||||||||||||||||||||
| status: z.enum(approvalStatus.enumValues), | ||||||||||||||||||||||||||||||
| reason: z.string().optional(), | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
Comment on lines
+58
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validation schema allows DB column - reason: z.string().optional(),
+ reason: z.string().nullable().optional(),📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export enum ApprovalStatus { | ||||||||||||||||||||||||||||||
| Approved = "approved", | ||||||||||||||||||||||||||||||
| Rejected = "rejected", | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing FK to the deployment-version table may break referential integrity
deploymentVersionIdis declared as a bare UUID. Without a.references(() => deploymentVersion.id …)clause the DB won’t enforce that the referenced deployment version actually exists, and cascaded deletes/updates are impossible.(you’ll need to import
deploymentVersionfrom its schema module at the top).📝 Committable suggestion