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
19 changes: 19 additions & 0 deletions apps/api/src/pkg/testutil/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export abstract class Harness {
workspaceId: this.resources.unkeyWorkspace.id,
createdAt: new Date(),
updatedAt: null,
createdAtM: Date.now(),
updatedAtM: null,
deletedAtM: null,
}));

await this.db.primary.insert(schema.permissions).values(create);
Expand Down Expand Up @@ -206,6 +209,8 @@ export abstract class Harness {
createdAt: new Date(),
updatedAt: null,
description: null,
createdAtM: Date.now(),
updatedAtM: null,
};

return this.db.primary.transaction(async (tx) => {
Expand All @@ -231,6 +236,8 @@ export abstract class Harness {
createdAt: new Date(),
updatedAt: null,
description: null,
createdAtM: Date.now(),
updatedAtM: null,
};
return this.db.primary.transaction(async (tx) => {
const found = await tx.query.roles.findFirst({
Expand Down Expand Up @@ -266,6 +273,9 @@ export abstract class Harness {
planDowngradeRequest: null,
enabled: true,
deleteProtection: true,
createdAtM: Date.now(),
updatedAtM: null,
deletedAtM: null,
};
const userWorkspace: Workspace = {
id: newId("test"),
Expand All @@ -285,6 +295,9 @@ export abstract class Harness {
planDowngradeRequest: null,
enabled: true,
deleteProtection: true,
createdAtM: Date.now(),
updatedAtM: null,
deletedAtM: null,
};

const unkeyKeyAuth: KeyAuth = {
Expand Down Expand Up @@ -326,6 +339,9 @@ export abstract class Harness {
createdAt: new Date(),
deletedAt: null,
deleteProtection: true,
createdAtM: Date.now(),
updatedAtM: null,
deletedAtM: null,
};
const userApi: Api = {
id: newId("test"),
Expand All @@ -337,6 +353,9 @@ export abstract class Harness {
createdAt: new Date(),
deletedAt: null,
deleteProtection: true,
createdAtM: Date.now(),
updatedAtM: null,
deletedAtM: null,
};

return {
Expand Down
7 changes: 5 additions & 2 deletions apps/api/src/routes/v1_apis_deleteApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ export const registerV1ApisDeleteApi = (app: App) =>
const rootKeyId = auth.key.id;

await db.primary.transaction(async (tx) => {
await tx.update(schema.apis).set({ deletedAt: new Date() }).where(eq(schema.apis.id, apiId));
await tx
.update(schema.apis)
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.apis.id, apiId));

await insertUnkeyAuditLog(c, tx, {
workspaceId: authorizedWorkspaceId,
Expand Down Expand Up @@ -124,7 +127,7 @@ export const registerV1ApisDeleteApi = (app: App) =>
});
await tx
.update(schema.keys)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(and(eq(schema.keys.keyAuthId, api.keyAuthId!)));

await insertUnkeyAuditLog(
Expand Down
6 changes: 5 additions & 1 deletion apps/api/src/routes/v1_apis_deleteKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ export const registerV1ApisDeleteKeys = (app: App) =>
.select({ count: sql<string>`count(*)` })
.from(schema.keys)
.where(where);
await tx.update(schema.keys).set({ deletedAt: new Date() }).where(where).execute();
await tx
.update(schema.keys)
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(where)
.execute();
deletedKeys = Number.parseInt(keys.at(0)?.count ?? "0");
});
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/v1_keys_deleteKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const registerV1KeysDeleteKey = (app: App) =>
} else {
await tx
.update(schema.keys)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.keys.id, key.id));
}

Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/routes/v1_keys_verifyKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ describe("key is soft deleted", () => {
const key = await h.createKey();
await h.db.primary
.update(schema.keys)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.keys.id, key.keyId));

const res = await h.post<V1KeysVerifyKeyRequest, V1KeysVerifyKeyResponse>({
Expand All @@ -853,7 +853,7 @@ describe("key exists but keyspace is soft deleted", () => {

await h.db.primary
.update(schema.keyAuth)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.keyAuth.id, h.resources.userKeyAuth.id));

const res = await h.post<V1KeysVerifyKeyRequest, V1KeysVerifyKeyResponse>({
Expand All @@ -879,7 +879,7 @@ describe("key exists but api is soft deleted", () => {

await h.db.primary
.update(schema.apis)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.apis.id, h.resources.userApi.id));

const res = await h.post<V1KeysVerifyKeyRequest, V1KeysVerifyKeyResponse>({
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/routes/v1_migrations_createKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ export const registerV1MigrationsCreateKeys = (app: App) =>
createdAt: new Date(),
roleId,
updatedAt: null,
createdAtM: Date.now(),
updatedAtM: null,
workspaceId: authorizedWorkspaceId,
});
}
Expand All @@ -503,6 +505,8 @@ export const registerV1MigrationsCreateKeys = (app: App) =>
permissionId,
tempId: 0,
updatedAt: null,
createdAtM: Date.now(),
updatedAtM: null,
workspaceId: authorizedWorkspaceId,
});
}
Expand All @@ -526,6 +530,8 @@ export const registerV1MigrationsCreateKeys = (app: App) =>
keyId: key.keyId,
encrypted: encryptionResponse.encrypted,
encryptionKeyId: encryptionResponse.keyId,
createdAt: Date.now(),
updatedAt: null,
});
}
}),
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/v1_ratelimits_deleteOverride.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const registerV1RatelimitDeleteOverride = (app: App) =>
}
await tx
.update(schema.ratelimitOverrides)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.ratelimitOverrides.id, override.id));

await insertUnkeyAuditLog(c, tx, {
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/routes/v1_ratelimits_limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ export const registerV1RatelimitLimit = (app: App) =>
name: req.namespace,
deletedAt: null,
updatedAt: null,
createdAtM: Date.now(),
deletedAtM: null,
updatedAtM: null,
workspaceId: rootKey.authorizedWorkspaceId,
};
try {
Expand Down
3 changes: 0 additions & 3 deletions apps/dashboard/app/(app)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export default async function Layout({ children }: LayoutProps) {
apis: {
where: (table, { isNull }) => isNull(table.deletedAt),
},
llmGateways: {
columns: { id: true },
},
},
});
if (!workspace) {
Expand Down
1 change: 0 additions & 1 deletion apps/dashboard/app/(app)/mobile-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type Props = {
id: string;
name: string;
}[];
llmGateways: { id: string }[];
};
};

Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/app/api/v1/vercel/integration/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ export async function POST(request: Request) {
case "project.removed": {
await db
.update(schema.vercelBindings)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.vercelBindings.projectId, p.data.payload.project.id));
break;
}
case "integration-configuration.removed": {
await db
.update(schema.vercelBindings)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.vercelBindings.integrationId, p.data.payload.configuration.id));
await db
.update(schema.vercelIntegrations)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.vercelIntegrations.id, p.data.payload.configuration.id));
break;
}
Expand Down
3 changes: 3 additions & 0 deletions apps/dashboard/app/integrations/vercel/callback/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export default async function Page(props: Props) {
accessToken: val.accessToken,
createdAt: new Date(),
deletedAt: null,
createdAtM: Date.now(),
updatedAtM: null,
deletedAtM: null,
};
await db.insert(schema.vercelIntegrations).values(integration).execute();
}
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/lib/trpc/routers/api/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const deleteApi = t.procedure
await db.transaction(async (tx) => {
await tx
.update(schema.apis)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.apis.id, input.apiId));
await insertAuditLogs(tx, ctx.workspace.auditLogBucket.id, {
workspaceId: api.workspaceId,
Expand Down Expand Up @@ -75,7 +75,7 @@ export const deleteApi = t.procedure
if (keyIds.length > 0) {
await tx
.update(schema.keys)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.keys.keyAuthId, api.keyAuthId!));
await insertAuditLogs(
tx,
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/lib/trpc/routers/key/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const deleteKeys = t.procedure
.transaction(async (tx) => {
await tx
.update(schema.keys)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(
and(
eq(schema.keys.workspaceId, workspace.id),
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/lib/trpc/routers/key/deleteRootKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const deleteRootKeys = t.procedure
.transaction(async (tx) => {
await tx
.update(schema.keys)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(
inArray(
schema.keys.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const deleteNamespace = t.procedure
await db.transaction(async (tx) => {
await tx
.update(schema.ratelimitNamespaces)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.ratelimitNamespaces.id, input.namespaceId));

await insertAuditLogs(tx, ctx.workspace.auditLogBucket.id, {
Expand Down Expand Up @@ -70,7 +70,7 @@ export const deleteNamespace = t.procedure
if (overrides.length > 0) {
await tx
.update(schema.ratelimitOverrides)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.ratelimitOverrides.namespaceId, namespace.id))
.catch((_err) => {
throw new TRPCError({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const deleteOverride = t.procedure
await db.transaction(async (tx) => {
await tx
.update(schema.ratelimitOverrides)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.ratelimitOverrides.id, override.id))
.catch((_err) => {
throw new TRPCError({
Expand Down
3 changes: 3 additions & 0 deletions apps/dashboard/lib/trpc/routers/rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,9 @@ export async function upsertPermissions(
description: null,
createdAt: new Date(),
updatedAt: null,
createdAtM: Date.now(),
updatedAtM: null,
deletedAtM: null,
};

newPermissions.push(permission);
Expand Down
2 changes: 2 additions & 0 deletions apps/dashboard/lib/trpc/routers/rbac/upsertPermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export async function upsertPermission(ctx: Context, name: string): Promise<Perm
description: null,
createdAt: new Date(),
updatedAt: null,
createdAtM: Date.now(),
updatedAtM: null,
};

await tx
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/lib/trpc/routers/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ export const vercelRouter = t.router({
await db.transaction(async (tx) => {
await tx
.update(schema.vercelBindings)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.vercelBindings.id, binding.id));
await insertAuditLogs(tx, ctx.workspace.auditLogBucket.id, {
workspaceId: binding.vercelIntegrations.workspace.id,
Expand Down Expand Up @@ -582,7 +582,7 @@ export const vercelRouter = t.router({
await db.transaction(async (tx) => {
await tx
.update(schema.vercelBindings)
.set({ deletedAt: new Date() })
.set({ deletedAt: new Date(), deletedAtM: Date.now() })
.where(eq(schema.vercelBindings.id, binding.id));
await insertAuditLogs(tx, ctx.workspace.auditLogBucket.id, {
workspaceId: integration.workspace.id,
Expand Down
3 changes: 3 additions & 0 deletions apps/dashboard/lib/trpc/routers/workspace/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export const createWorkspace = t.procedure
planDowngradeRequest: null,
enabled: true,
deleteProtection: true,
createdAtM: Date.now(),
updatedAtM: null,
deletedAtM: null,
};
await db
.transaction(async (tx) => {
Expand Down
2 changes: 2 additions & 0 deletions internal/db/src/schema/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { relations } from "drizzle-orm";
import { datetime, index, mysqlEnum, mysqlTable, varchar } from "drizzle-orm/mysql-core";
import { keyAuth } from "./keyAuth";
import { deleteProtection } from "./util/delete_protection";
import { lifecycleDatesMigration } from "./util/lifecycle_dates";
import { workspaces } from "./workspaces";

export const apis = mysqlTable(
Expand All @@ -21,6 +22,7 @@ export const apis = mysqlTable(
createdAt: datetime("created_at", { mode: "date", fsp: 3 }),
deletedAt: datetime("deleted_at", { mode: "date", fsp: 3 }),

...lifecycleDatesMigration,
...deleteProtection,
},
(table) => ({
Expand Down
4 changes: 2 additions & 2 deletions internal/db/src/schema/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { identities, ratelimits } from "./identity";
import { keyAuth } from "./keyAuth";
import { keysPermissions, keysRoles } from "./rbac";
import { embeddedEncrypted } from "./util/embedded_encrypted";
import { lifecycleDatesMigration } from "./util/lifecycle_dates";
import { lifecycleDatesMigration, lifecycleDatesV2 } from "./util/lifecycle_dates";
import { workspaces } from "./workspaces";

export const keys = mysqlTable(
Expand Down Expand Up @@ -149,7 +149,7 @@ export const encryptedKeys = mysqlTable(
keyId: varchar("key_id", { length: 256 })
.notNull()
.references(() => keys.id, { onDelete: "cascade" }),

...lifecycleDatesV2,
...embeddedEncrypted,
},
(table) => ({
Expand Down
Loading
Loading