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
+4 -1
View File
@@ -16,10 +16,13 @@ export async function getOrganizationAvailableDatabases(
projectId?: string, projectId?: string,
) { ) {
const availableDatabases = (await db.query.database.findMany({ const availableDatabases = (await db.query.database.findMany({
where: (db, { eq, or, isNull }) => where: (db, { eq, or, and, isNull }) =>
and(
projectId projectId
? or(isNull(db.projectId), eq(db.projectId, projectId)) ? or(isNull(db.projectId), eq(db.projectId, projectId))
: isNull(db.projectId), : isNull(db.projectId),
isNull(db.deletedAt),
),
with: { with: {
agent: { agent: {
with: { with: {
+5 -2
View File
@@ -2,7 +2,7 @@
import { db } from "@/db"; import { db } from "@/db";
import * as drizzleDb 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 { dispatchNotification } from "@/features/notifications/utils/notifications.dispatch";
import { EventPayload } from "@/features/notifications/types"; import { EventPayload } from "@/features/notifications/types";
import { logger } from "@/lib/logger"; import { logger } from "@/lib/logger";
@@ -113,7 +113,10 @@ export async function checkAgentsHealthError() {
export async function checkDatabasesHealthError() { export async function checkDatabasesHealthError() {
const databases = await db.query.database.findMany({ 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: { with: {
agent: true, agent: true,
alertPolicies: true, alertPolicies: true,
+7 -3
View File
@@ -2,7 +2,7 @@
import { getOrganization } from "@/lib/auth/auth"; import { getOrganization } from "@/lib/auth/auth";
import { db } from "@/db"; import { db } from "@/db";
import { and, eq } from "drizzle-orm"; import { and, eq, isNull } from "drizzle-orm";
import * as drizzleDb from "@/db"; import * as drizzleDb from "@/db";
import { project } from "@/db/schema/06_project"; import { project } from "@/db/schema/06_project";
@@ -10,7 +10,9 @@ export async function getOrganizationProject(organizationId: string) {
return db.query.project.findFirst({ return db.query.project.findFirst({
where: eq(project.organizationId, organizationId), where: eq(project.organizationId, organizationId),
with: { with: {
databases: true, databases: {
where: isNull(drizzleDb.schemas.database.deletedAt),
},
}, },
}); });
} }
@@ -39,7 +41,9 @@ export const getOrganizationProjectDatabases = async ({
eq(drizzleDb.schemas.project.id, projectId), eq(drizzleDb.schemas.project.id, projectId),
), ),
with: { 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 {z} from "zod";
import {ServerActionResult} from "@/types/action-type"; import {ServerActionResult} from "@/types/action-type";
import {db} from "@/db"; 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 * as drizzleDb from "@/db";
import {AgentWith} from "@/db/schema/08_agent"; import {AgentWith} from "@/db/schema/08_agent";
import {withUpdatedAt} from "@/db/utils"; import {withUpdatedAt} from "@/db/utils";
@@ -25,7 +25,9 @@ export const updateAgentOrganizationsAction = userAction
where: eq(drizzleDb.schemas.agent.id, agentId), where: eq(drizzleDb.schemas.agent.id, agentId),
with: { with: {
organizations: true, organizations: true,
databases: true databases: {
where: isNull(drizzleDb.schemas.database.deletedAt),
},
} }
}) as AgentWith; }) as AgentWith;
@@ -70,7 +72,7 @@ export const updateAgentOrganizationsAction = userAction
if (projectIds.length > 0) { if (projectIds.length > 0) {
const databases = await db.query.database.findMany({ 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 } columns: { id: true }
}); });
+4 -2
View File
@@ -2,7 +2,7 @@
import {action, ActionError, userAction} from "@/lib/safe-actions/actions"; import {action, ActionError, userAction} from "@/lib/safe-actions/actions";
import {AgentSchema} from "@/features/agents/schemas/agents.schema"; import {AgentSchema} from "@/features/agents/schemas/agents.schema";
import {z} from "zod"; 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 {db} from "@/db";
import * as drizzleDb from "@/db"; import * as drizzleDb from "@/db";
import {slugify} from "@/utils/slugify"; 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({ const agent = await db.query.agent.findFirst({
where: eq(drizzleDb.schemas.agent.id, parsedInput), where: eq(drizzleDb.schemas.agent.id, parsedInput),
with: { 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 {z} from "zod";
import {ServerActionResult} from "@/types/action-type"; import {ServerActionResult} from "@/types/action-type";
import {db} from "@/db"; 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 {Project} from "@/db/schema/06_project";
import * as drizzleDb from "@/db"; import * as drizzleDb from "@/db";
import {Database} from "@/db/schema/07_database"; import {Database} from "@/db/schema/07_database";
@@ -88,7 +88,9 @@ export const updateProjectAction = userAction
const existing = await db.query.project.findFirst({ const existing = await db.query.project.findFirst({
where: eq(drizzleDb.schemas.project.id, parsedInput.projectId), where: eq(drizzleDb.schemas.project.id, parsedInput.projectId),
with: { 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 () => { export const retentionCleanTask = async () => {
try { try {
const databases = await db.query.database.findMany({ const databases = await db.query.database.findMany({
where: isNull(drizzleDb.schemas.database.deletedAt),
with: { with: {
retentionPolicy: true, retentionPolicy: true,
backups: { backups: {