Skip to content
Merged
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
31 changes: 20 additions & 11 deletions packages/rule-engine/src/repositories/db-release-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { eq, takeFirst } from "@ctrlplane/db";
import { db as dbClient } from "@ctrlplane/db/client";
import * as schema from "@ctrlplane/db/schema";

import type {
DeploymentResourceContext,
Policy,
ReleaseTargetIdentifier,
} from "../types.js";
import type { DeploymentResourceContext, Policy } from "../types.js";
import type {
CompleteRelease,
Release,
Expand Down Expand Up @@ -159,7 +155,7 @@ export class DatabaseReleaseRepository implements ReleaseRepository {
* @param release - The release to create
* @returns The created release with ID
*/
async create(
async createRelease(
release: Omit<Release, "id" | "createdAt">,
): Promise<ReleaseWithId> {
return this.db.transaction((tx) =>
Expand All @@ -169,13 +165,12 @@ export class DatabaseReleaseRepository implements ReleaseRepository {

/**
* Creates a new release only if one doesn't already exist with the same version and variables
* @param options - The release target identifier

* @param versionId - The version ID for the release
* @param variables - The variables for the release
* @returns Object indicating if a new release was created and the final release
*/
async upsert(
options: ReleaseTargetIdentifier,
async upsertRelease(
versionId: string,
variables: MaybeVariable[],
): Promise<{ created: boolean; release: ReleaseWithId }> {
Expand Down Expand Up @@ -204,15 +199,29 @@ export class DatabaseReleaseRepository implements ReleaseRepository {
? { created: false, release: latestRelease }
: {
created: true,
release: await this.create({
...options,
release: await this.createRelease({
versionId,
releaseTargetId: this.releaseTarget.id,
variables: _.compact(variables),
}),
};
}

async updateReleaseVariables(
variables: MaybeVariable[],
): Promise<{ created: boolean; release: ReleaseWithId } | null> {
const latestRelease = await this.findLatestRelease();
const versionId = latestRelease?.versionId ?? null;
return versionId == null ? null : this.upsertRelease(versionId, variables);
}

async updateReleaseVersion(
versionId: string,
): Promise<{ created: boolean; release: ReleaseWithId }> {
const latestRelease = await this.findLatestRelease();
return this.upsertRelease(versionId, latestRelease?.variables ?? []);
}

/**
* Sets the desired release for the target
* @param desiredReleaseId - ID of the release to set as desired
Expand Down
9 changes: 2 additions & 7 deletions packages/rule-engine/src/repositories/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type * as schema from "@ctrlplane/db/schema";

import type {
DeploymentResourceContext,
Policy,
ReleaseTargetIdentifier,
} from "../types.js";
import type { DeploymentResourceContext, Policy } from "../types.js";
import type { MaybeVariable, Variable } from "./variables/types.js";

/**
Expand Down Expand Up @@ -63,8 +59,7 @@ export interface ReleaseRepository {
* @param variables - Variables for the release
* @returns Object with created flag and the release
*/
upsert(
options: ReleaseTargetIdentifier,
upsertRelease(
versionId: string,
variables: MaybeVariable[],
): Promise<{ created: boolean; release: ReleaseWithId }>;
Expand Down
Loading