Skip to content
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

release channels policy #178

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
41 changes: 36 additions & 5 deletions packages/db/src/schema/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { z } from "zod";
import { relations, sql } from "drizzle-orm";
import {
bigint,
boolean,
integer,
jsonb,
pgEnum,
Expand All @@ -20,7 +21,8 @@ import { releaseCondition } from "@ctrlplane/validators/releases";
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 @@ export const environment = pgTable("environment", {
.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 @@ -99,6 +100,10 @@ export const environmentPolicy = pgTable("environment_policy", {
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 +114,26 @@ export const createEnvironmentPolicy = createInsertSchema(environmentPolicy, {

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 +178,14 @@ export const environmentPolicyDeployment = pgTable(
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