Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions packages/db/src/schema/environment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ReleaseCondition } from "@ctrlplane/validators/releases";

Check failure on line 1 in packages/db/src/schema/environment.ts

View workflow job for this annotation

GitHub Actions / Lint

'ReleaseCondition' is defined but never used. Allowed unused vars must match /^_/u
import type { TargetCondition } from "@ctrlplane/validators/targets";
import type { InferSelectModel } from "drizzle-orm";
import type { z } from "zod";
import { relations, sql } from "drizzle-orm";
import {
bigint,
boolean,
integer,
jsonb,
pgEnum,
Expand All @@ -20,7 +21,8 @@
import { targetCondition } from "@ctrlplane/validators/targets";

import { user } from "./auth.js";
import { release } from "./release.js";
import { deployment } from "./deployment.js";
import { release, releaseChannel } from "./release.js";
import { system } from "./system.js";
import { variableSetEnvironment } from "./variable-sets.js";

Expand All @@ -31,13 +33,12 @@
.references(() => system.id, { onDelete: "cascade" }),
name: text("name").notNull(),
description: text("description").default(""),
policyId: uuid("policy_id").references(() => environmentPolicy.id, {
onDelete: "set null",
}),
targetFilter: jsonb("target_filter")
.$type<TargetCondition | null>()
.default(sql`NULL`),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
deletedAt: timestamp("deleted_at", { withTimezone: true }),
expiresAt: timestamp("expires_at", { withTimezone: true }),
});

export type Environment = InferSelectModel<typeof environment>;
Expand Down Expand Up @@ -80,7 +81,7 @@
.references(() => system.id),
approvalRequirement: approvalRequirement("approval_required")
.notNull()
.default("manual"),
.default("automatic"),

successType: environmentPolicyDeploymentSuccessType("success_status")
.notNull()
Expand All @@ -92,13 +93,13 @@

duration: bigint("duration", { mode: "number" }).notNull().default(0),

releaseFilter: jsonb("release_filter")
.$type<ReleaseCondition | null>()
.default(sql`NULL`),

releaseSequencing: releaseSequencingType("release_sequencing")
.notNull()
.default("cancel"),

ephemeralDuration: bigint("ephemeral_duration", { mode: "number" }).default(
0,
),
});

export type EnvironmentPolicy = InferSelectModel<typeof environmentPolicy>;
Expand All @@ -109,6 +110,26 @@

export const updateEnvironmentPolicy = createEnvironmentPolicy.partial();

export const environmentPolicyReleaseChannel = pgTable(
"environment_policy_release_channel",
{
id: uuid("id").primaryKey().defaultRandom(),
policyId: uuid("policy_id")
.notNull()
.references(() => environmentPolicy.id, { onDelete: "cascade" }),
channelId: uuid("channel_id")
.notNull()
.references(() => releaseChannel.id, { onDelete: "cascade" }),
},
(t) => ({
uniq: uniqueIndex().on(t.policyId, t.channelId),
deploymentUniq: uniqueIndex().on(
t.policyId,
sql`(SELECT ${deployment.id} FROM ${releaseChannel} WHERE id = ${t.channelId})`,
),
}),
);

export const recurrenceType = pgEnum("recurrence_type", [
"hourly",
"daily",
Expand Down Expand Up @@ -153,8 +174,14 @@
environmentId: uuid("environment_id")
.notNull()
.references(() => environment.id, { onDelete: "cascade" }),
hasCustomPolicy: boolean("has_custom_policy").notNull().default(false),
},
(t) => ({ uniq: uniqueIndex().on(t.policyId, t.environmentId) }),
(t) => ({
customPolicyUniq: uniqueIndex()
.on(t.environmentId)
.where(sql`is_own_policy = true`),
uniq: uniqueIndex().on(t.policyId, t.environmentId),
}),
Comment on lines +177 to +184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect field reference in unique index customPolicyUniq

The unique index customPolicyUniq references is_own_policy, which is not defined in the environmentPolicyDeployment table. The correct field is hasCustomPolicy. Update the WHERE clause to use hasCustomPolicy instead to ensure the index functions as intended.

Apply this diff to correct the field reference:

 (t) => ({
   customPolicyUniq: uniqueIndex()
     .on(t.environmentId)
-    .where(sql`is_own_policy = true`),
+    .where(sql`has_custom_policy = true`),
   uniq: uniqueIndex().on(t.policyId, t.environmentId),
 }),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
hasCustomPolicy: boolean("has_custom_policy").notNull().default(false),
},
(t) => ({ uniq: uniqueIndex().on(t.policyId, t.environmentId) }),
(t) => ({
customPolicyUniq: uniqueIndex()
.on(t.environmentId)
.where(sql`is_own_policy = true`),
uniq: uniqueIndex().on(t.policyId, t.environmentId),
}),
hasCustomPolicy: boolean("has_custom_policy").notNull().default(false),
},
(t) => ({
customPolicyUniq: uniqueIndex()
.on(t.environmentId)
.where(sql`has_custom_policy = true`),
uniq: uniqueIndex().on(t.policyId, t.environmentId),
}),

);

export type EnvironmentPolicyDeployment = InferSelectModel<
Expand Down
12 changes: 12 additions & 0 deletions packages/db/src/schema/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ import { environment } from "./environment.js";
import { job } from "./job.js";
import { target } from "./target.js";

export const releaseChannel = pgTable("release_channel", {
id: uuid("id").primaryKey().defaultRandom(),
name: text("name").notNull(),
description: text("description").default(""),
deploymentId: uuid("deployment_id")
.notNull()
.references(() => deployment.id, { onDelete: "cascade" }),
releaseFilter: jsonb("release_filter")
.$type<ReleaseCondition | null>()
.default(sql`NULL`),
});
Comment on lines +51 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding uniqueness constraint for channel names within a deployment.

The current schema allows duplicate channel names within the same deployment, which could lead to confusion. Consider adding a unique index on (deploymentId, name).

 export const releaseChannel = pgTable("release_channel", {
   id: uuid("id").primaryKey().defaultRandom(),
   name: text("name").notNull(),
   description: text("description").default(""),
   deploymentId: uuid("deployment_id")
     .notNull()
     .references(() => deployment.id, { onDelete: "cascade" }),
   releaseFilter: jsonb("release_filter")
     .$type<ReleaseCondition | null>()
     .default(sql`NULL`),
- });
+ }, (t) => ({
+   unq: uniqueIndex().on(t.deploymentId, t.name)
+ }));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const releaseChannel = pgTable("release_channel", {
id: uuid("id").primaryKey().defaultRandom(),
name: text("name").notNull(),
description: text("description").default(""),
deploymentId: uuid("deployment_id")
.notNull()
.references(() => deployment.id, { onDelete: "cascade" }),
releaseFilter: jsonb("release_filter")
.$type<ReleaseCondition | null>()
.default(sql`NULL`),
});
export const releaseChannel = pgTable("release_channel", {
id: uuid("id").primaryKey().defaultRandom(),
name: text("name").notNull(),
description: text("description").default(""),
deploymentId: uuid("deployment_id")
.notNull()
.references(() => deployment.id, { onDelete: "cascade" }),
releaseFilter: jsonb("release_filter")
.$type<ReleaseCondition | null>()
.default(sql`NULL`),
}, (t) => ({
unq: uniqueIndex().on(t.deploymentId, t.name)
}));


export const releaseDependency = pgTable(
"release_dependency",
{
Expand Down
Loading