Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] Move towards sync deletion (away from PeriodicDeleter) - step I/II #18833

Merged
merged 8 commits into from
Sep 29, 2023
76 changes: 0 additions & 76 deletions components/gitpod-db/src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ export class GitpodTableDescriptionProvider implements TableDescriptionProvider
timeColumn: "_lastModified",
deletionColumn: "deleted",
},
{
name: "d_b_oauth_auth_code_entry",
primaryKeys: ["id"],
timeColumn: "_lastModified",
},
{
name: "d_b_installation_admin",
primaryKeys: ["id"],
timeColumn: "_lastModified",
},
{
name: "d_b_volume_snapshot",
primaryKeys: ["id"],
Expand All @@ -72,52 +62,12 @@ export class GitpodTableDescriptionProvider implements TableDescriptionProvider
primaryKeys: ["id"],
timeColumn: "_lastModified",
},
{
name: "d_b_user_storage_resource",
primaryKeys: ["userId", "uri"],
timeColumn: "_lastModified",
deletionColumn: "deleted",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use this table for years now... 🙈

dependencies: ["d_b_user"],
},
{
name: "d_b_workspace_instance_user",
primaryKeys: ["instanceId", "userId"],
timeColumn: "_lastModified",
dependencies: ["d_b_user"],
},
{
name: "d_b_workspace_report_entry",
primaryKeys: ["uid"],
timeColumn: "time",
dependencies: [],
},
{
name: "d_b_snapshot",
primaryKeys: ["id"],
timeColumn: "creationTime",
dependencies: [],
},
{
name: "d_b_email_domain_filter",
primaryKeys: ["domain"],
timeColumn: "_lastModified",
},
{
name: "d_b_app_installation",
primaryKeys: ["platform", "installationID", "state"],
timeColumn: "creationTime",
},
{
name: "d_b_token_entry",
primaryKeys: ["uid"],
deletionColumn: "deleted",
timeColumn: "_lastModified",
},
{
name: "d_b_user_env_var",
primaryKeys: ["id", "userId"],
timeColumn: "_lastModified",
},
{
name: "d_b_gitpod_token",
primaryKeys: ["tokenHash"],
Expand All @@ -137,17 +87,6 @@ export class GitpodTableDescriptionProvider implements TableDescriptionProvider
deletionColumn: "deleted",
timeColumn: "_lastModified",
},
{
name: "d_b_code_sync_collection",
primaryKeys: ["userId", "collection"],
timeColumn: "_lastModified",
},
{
name: "d_b_code_sync_resource",
primaryKeys: ["userId", "kind", "rev", "collection"],
timeColumn: "created",
dependencies: ["d_b_code_sync_collection"],
},
{
name: "d_b_team",
primaryKeys: ["id"],
Expand Down Expand Up @@ -196,16 +135,6 @@ export class GitpodTableDescriptionProvider implements TableDescriptionProvider
deletionColumn: "deleted",
timeColumn: "_lastModified",
},
{
name: "d_b_cost_center",
primaryKeys: ["id", "creationTime"],
timeColumn: "_lastModified",
},
{
name: "d_b_usage",
primaryKeys: ["id"],
timeColumn: "_lastModified",
},
{
name: "d_b_stripe_customer",
primaryKeys: ["stripeCustomerId"],
Expand All @@ -218,11 +147,6 @@ export class GitpodTableDescriptionProvider implements TableDescriptionProvider
timeColumn: "_lastModified",
deletionColumn: "deleted",
},
{
name: "d_b_linked_in_profile",
primaryKeys: ["id"],
timeColumn: "_lastModified",
},
];

public getSortedTables(): TableDescription[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class AuthProviderEntryDBImpl implements AuthProviderEntryDB {

// 2. then mark as deleted
const repo = await this.getAuthProviderRepo();
await repo.update({ id }, { deleted: true });
await repo.delete({ id });
}

async findAll(exceptOAuthRevisions: string[] = []): Promise<AuthProviderEntry[]> {
Expand Down
5 changes: 1 addition & 4 deletions components/gitpod-db/src/typeorm/project-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ export class ProjectDBImpl extends TransactionalDBImpl<ProjectDB> implements Pro
await projectInfoRepo.update(projectId, { deleted: true });
}
const projectUsageRepo = await this.getProjectUsageRepo();
const usage = await projectUsageRepo.findOne({ projectId, deleted: false });
if (usage) {
await projectUsageRepo.update(projectId, { deleted: true });
}
await projectUsageRepo.delete({ projectId });
}

public async findProjectEnvironmentVariable(
Expand Down
3 changes: 1 addition & 2 deletions components/gitpod-db/src/typeorm/team-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ export class TeamDBImpl extends TransactionalDBImpl<TeamDB> implements TeamDB {
"The given user is not currently a member of this organization or does not exist.",
);
}
membership.deleted = true;
await membershipRepo.save(membership);
await membershipRepo.delete(membership);
}

public async findTeamMembershipInviteById(inviteId: string): Promise<TeamMembershipInvite> {
Expand Down
44 changes: 13 additions & 31 deletions components/gitpod-db/src/typeorm/user-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
OAuthUser,
} from "@jmondi/oauth2-server";
import { inject, injectable, optional } from "inversify";
import { EntityManager, Equal, Not, Repository } from "typeorm";
import { EntityManager, Equal, FindOperator, Not, Repository } from "typeorm";
import { v4 as uuidv4 } from "uuid";
import {
BUILTIN_WORKSPACE_PROBE_USER_ID,
Expand Down Expand Up @@ -256,27 +256,12 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us

public async deleteGitpodToken(tokenHash: string): Promise<void> {
const repo = await this.getGitpodTokenRepo();
await repo.query(
`
UPDATE d_b_gitpod_token AS gt
SET gt.deleted = TRUE
WHERE tokenHash = ?;
`,
[tokenHash],
);
await repo.delete({ tokenHash });
}

public async deleteGitpodTokensNamedLike(userId: string, namePattern: string): Promise<void> {
const repo = await this.getGitpodTokenRepo();
await repo.query(
`
UPDATE d_b_gitpod_token AS gt
SET gt.deleted = TRUE
WHERE userId = ?
AND name LIKE ?
`,
[userId, namePattern],
);
await repo.delete({ userId, name: new FindOperator("like", namePattern) });
}

public async storeSingleToken(identity: Identity, token: Token): Promise<TokenEntry> {
Expand Down Expand Up @@ -309,16 +294,14 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us

public async deleteExpiredTokenEntries(date: string): Promise<void> {
const repo = await this.getTokenRepo();
await repo.query(
`
UPDATE d_b_token_entry AS te
SET te.deleted = TRUE
WHERE te.expiryDate != ''
AND te.refreshable != 1
AND te.expiryDate <= ?;
`,
[date],
);
await repo
.createQueryBuilder()
.delete()
.from(DBTokenEntry)
.where("expiryDate != ''")
.andWhere("refreshable != 1")
.andWhere("expiryDate <= :date", { date })
.execute();
}

public async updateTokenEntry(tokenEntry: Partial<TokenEntry> & Pick<TokenEntry, "uid">): Promise<void> {
Expand All @@ -331,8 +314,7 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
const repo = await this.getTokenRepo();
for (const existing of existingTokens) {
if (!shouldDelete || shouldDelete(existing)) {
existing.deleted = true;
await repo.save(existing);
await repo.delete(existing.uid);
}
}
}
Expand Down Expand Up @@ -481,7 +463,7 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us

public async deleteSSHPublicKey(userId: string, id: string): Promise<void> {
const repo = await this.getSSHPublicKeyRepo();
await repo.update({ userId, id }, { deleted: true });
await repo.delete({ userId, id });
}

public async findAllUsers(
Expand Down