feat(database): exclude soft-deleted databases from all list queries

This commit is contained in:
charles-gauthereau
2026-07-05 18:31:52 +02:00
parent 64b3bf1b16
commit 8f2d6f0b01
7 changed files with 33 additions and 16 deletions
+7 -4
View File
@@ -16,10 +16,13 @@ export async function getOrganizationAvailableDatabases(
projectId?: string,
) {
const availableDatabases = (await db.query.database.findMany({
where: (db, { eq, or, isNull }) =>
projectId
? or(isNull(db.projectId), eq(db.projectId, projectId))
: isNull(db.projectId),
where: (db, { eq, or, and, isNull }) =>
and(
projectId
? or(isNull(db.projectId), eq(db.projectId, projectId))
: isNull(db.projectId),
isNull(db.deletedAt),
),
with: {
agent: {
with: {
+5 -2
View File
@@ -2,7 +2,7 @@
import { db } from "@/db";
import * as drizzleDb from "@/db";
import { and, eq, gte, isNotNull, lt } from "drizzle-orm";
import { and, eq, gte, isNotNull, isNull, lt } from "drizzle-orm";
import { dispatchNotification } from "@/features/notifications/utils/notifications.dispatch";
import { EventPayload } from "@/features/notifications/types";
import { logger } from "@/lib/logger";
@@ -113,7 +113,10 @@ export async function checkAgentsHealthError() {
export async function checkDatabasesHealthError() {
const databases = await db.query.database.findMany({
where: isNotNull(drizzleDb.schemas.database.lastContact),
where: and(
isNotNull(drizzleDb.schemas.database.lastContact),
isNull(drizzleDb.schemas.database.deletedAt),
),
with: {
agent: true,
alertPolicies: true,
+7 -3
View File
@@ -2,7 +2,7 @@
import { getOrganization } from "@/lib/auth/auth";
import { db } from "@/db";
import { and, eq } from "drizzle-orm";
import { and, eq, isNull } from "drizzle-orm";
import * as drizzleDb from "@/db";
import { project } from "@/db/schema/06_project";
@@ -10,7 +10,9 @@ export async function getOrganizationProject(organizationId: string) {
return db.query.project.findFirst({
where: eq(project.organizationId, organizationId),
with: {
databases: true,
databases: {
where: isNull(drizzleDb.schemas.database.deletedAt),
},
},
});
}
@@ -39,7 +41,9 @@ export const getOrganizationProjectDatabases = async ({
eq(drizzleDb.schemas.project.id, projectId),
),
with: {
databases: true,
databases: {
where: isNull(drizzleDb.schemas.database.deletedAt),
},
},
});
@@ -3,7 +3,7 @@ import {userAction} from "@/lib/safe-actions/actions";
import {z} from "zod";
import {ServerActionResult} from "@/types/action-type";
import {db} from "@/db";
import {and, eq, inArray} from "drizzle-orm";
import {and, eq, inArray, isNull} from "drizzle-orm";
import * as drizzleDb from "@/db";
import {AgentWith} from "@/db/schema/08_agent";
import {withUpdatedAt} from "@/db/utils";
@@ -25,7 +25,9 @@ export const updateAgentOrganizationsAction = userAction
where: eq(drizzleDb.schemas.agent.id, agentId),
with: {
organizations: true,
databases: true
databases: {
where: isNull(drizzleDb.schemas.database.deletedAt),
},
}
}) as AgentWith;
@@ -70,7 +72,7 @@ export const updateAgentOrganizationsAction = userAction
if (projectIds.length > 0) {
const databases = await db.query.database.findMany({
where: (db, { inArray }) => inArray(db.projectId, projectIds),
where: (db, { inArray, and, isNull }) => and(inArray(db.projectId, projectIds), isNull(db.deletedAt)),
columns: { id: true }
});
+4 -2
View File
@@ -2,7 +2,7 @@
import {action, ActionError, userAction} from "@/lib/safe-actions/actions";
import {AgentSchema} from "@/features/agents/schemas/agents.schema";
import {z} from "zod";
import {eq, and, ne, count, desc} from "drizzle-orm";
import {eq, and, ne, count, desc, isNull} from "drizzle-orm";
import {db} from "@/db";
import * as drizzleDb from "@/db";
import {slugify} from "@/utils/slugify";
@@ -87,7 +87,9 @@ export const getAgentAction = userAction.schema(z.string()).action(async ({parse
const agent = await db.query.agent.findFirst({
where: eq(drizzleDb.schemas.agent.id, parsedInput),
with: {
databases: true
databases: {
where: isNull(drizzleDb.schemas.database.deletedAt),
},
}
});
@@ -5,7 +5,7 @@ import {ProjectSchema} from "@/features/projects/schemas/projects.schema";
import {z} from "zod";
import {ServerActionResult} from "@/types/action-type";
import {db} from "@/db";
import {and, eq, inArray} from "drizzle-orm";
import {and, eq, inArray, isNull} from "drizzle-orm";
import {Project} from "@/db/schema/06_project";
import * as drizzleDb from "@/db";
import {Database} from "@/db/schema/07_database";
@@ -88,7 +88,9 @@ export const updateProjectAction = userAction
const existing = await db.query.project.findFirst({
where: eq(drizzleDb.schemas.project.id, parsedInput.projectId),
with: {
databases: true,
databases: {
where: isNull(drizzleDb.schemas.database.deletedAt),
},
},
});
+1
View File
@@ -12,6 +12,7 @@ const log = logger.child({ module: "tasks/database" });
export const retentionCleanTask = async () => {
try {
const databases = await db.query.database.findMany({
where: isNull(drizzleDb.schemas.database.deletedAt),
with: {
retentionPolicy: true,
backups: {