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
37 changes: 37 additions & 0 deletions server/src/internal/customers/CusService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
eq,
getTableColumns,
ilike,
inArray,
or,
sql,
type Table,
Expand Down Expand Up @@ -432,6 +433,42 @@ export class CusService {
return results;
}

/** Deletes customers in batches to avoid locking all rows at once. */
static async safeDeleteByOrgId({
db,
orgId,
env,
batchSize = 250,
}: {
db: DrizzleCli;
orgId: string;
env: AppEnv;
batchSize?: number;
}) {
if (env === AppEnv.Live)
throw new Error("Cannot delete all customers under org in live mode");

while (true) {
const batch = await db
.select({ internal_id: customers.internal_id })
.from(customers)
.where(and(eq(customers.org_id, orgId), eq(customers.env, env)))
.limit(batchSize);

if (batch.length === 0) break;

const ids = batch.map((r) => r.internal_id);

await db.delete(customers).where(
and(
inArray(customers.internal_id, ids),
eq(customers.org_id, orgId),
eq(customers.env, env),
),
);
}
}

static async getByVercelId({
ctx,
vercelInstallationId,
Expand Down
28 changes: 27 additions & 1 deletion server/src/internal/features/FeatureService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@autumn/shared";
import type { DrizzleCli } from "@server/db/initDrizzle.js";
import { notNullish } from "@server/utils/genUtils.js";
import { and, eq } from "drizzle-orm";
import { and, eq, inArray } from "drizzle-orm";
import type { Logger } from "@/external/logtail/logtailUtils.js";
import { clearOrgCache } from "../orgs/orgUtils/clearOrgCache.js";

Expand Down Expand Up @@ -215,4 +215,30 @@ export class FeatureService {
.delete(features)
.where(and(eq(features.org_id, orgId), eq(features.env, env)));
}

/** Deletes features in batches to avoid locking all rows at once. */
static async safeDeleteByOrgId({
db,
orgId,
env,
batchSize = 250,
}: {
db: DrizzleCli;
orgId: string;
env: AppEnv;
batchSize?: number;
}) {
while (true) {
const batch = await db
.select({ internal_id: features.internal_id })
.from(features)
.where(and(eq(features.org_id, orgId), eq(features.env, env)))
.limit(batchSize);

if (batch.length === 0) break;

const ids = batch.map((r) => r.internal_id);
await db.delete(features).where(inArray(features.internal_id, ids));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ export const handleNukeOrganisationConfiguration = createRoute({
return c.json({ error: "Cannot clear non-sandbox orgs" }, 400);
}

await CusService.deleteByOrgId({
await CusService.safeDeleteByOrgId({
db,
orgId: org.id,
env: AppEnv.Sandbox,
});

await ProductService.deleteByOrgId({
await ProductService.safeDeleteByOrgId({
db,
orgId: org.id,
env: AppEnv.Sandbox,
});
await FeatureService.deleteByOrgId({
await FeatureService.safeDeleteByOrgId({
db,
orgId: org.id,
env: AppEnv.Sandbox,
Expand Down
26 changes: 26 additions & 0 deletions server/src/internal/products/ProductService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,32 @@ export class ProductService {
.where(and(eq(products.org_id, orgId), eq(products.env, env)));
}

/** Deletes products in batches to avoid locking all rows at once. */
static async safeDeleteByOrgId({
db,
orgId,
env,
batchSize = 250,
}: {
db: DrizzleCli;
orgId: string;
env: AppEnv;
batchSize?: number;
}) {
while (true) {
const batch = await db
.select({ internal_id: products.internal_id })
.from(products)
.where(and(eq(products.org_id, orgId), eq(products.env, env)))
.limit(batchSize);

if (batch.length === 0) break;

const ids = batch.map((r) => r.internal_id);
await db.delete(products).where(inArray(products.internal_id, ids));
}
}

static async getDeletionText({
db,
productId,
Expand Down
Loading