-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[db][protocol] Implement TeamSubscription2 DB shapes and migration #9655
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the Gitpod Enterprise Source Code License, | ||
* See License.enterprise.txt in the project root folder. | ||
*/ | ||
|
||
import { TeamSubscription2 } from "@gitpod/gitpod-protocol/lib/team-subscription-protocol"; | ||
|
||
export const TeamSubscription2DB = Symbol("TeamSubscription2DB"); | ||
export interface TeamSubscription2DB { | ||
storeEntry(ts: TeamSubscription2): Promise<void>; | ||
findById(id: string): Promise<TeamSubscription2 | undefined>; | ||
findByPaymentRef(teamId: string, paymentReference: string): Promise<TeamSubscription2 | undefined>; | ||
findForTeam(teamId: string, date: string): Promise<TeamSubscription2 | undefined>; | ||
|
||
transaction<T>(code: (db: TeamSubscription2DB) => Promise<T>): Promise<T>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
components/gitpod-db/src/typeorm/entity/db-team-subscription-2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the Gitpod Enterprise Source Code License, | ||
* See License.enterprise.txt in the project root folder. | ||
*/ | ||
|
||
import { Entity, Column, PrimaryColumn, Index } from "typeorm"; | ||
|
||
import { TeamSubscription2 } from "@gitpod/gitpod-protocol/lib/team-subscription-protocol"; | ||
|
||
import { TypeORM } from "../../typeorm/typeorm"; | ||
import { Transformer } from "../../typeorm/transformer"; | ||
|
||
@Entity() | ||
@Index("ind_team_paymentReference", ["teamId", "paymentReference"]) | ||
@Index("ind_team_startdate", ["teamId", "startDate"]) | ||
// on DB but not Typeorm: @Index("ind_lastModified", ["_lastModified"]) // DBSync | ||
export class DBTeamSubscription2 implements TeamSubscription2 { | ||
@PrimaryColumn("uuid") | ||
id: string; | ||
|
||
@Column(TypeORM.UUID_COLUMN_TYPE) | ||
teamId: string; | ||
|
||
@Column() | ||
paymentReference: string; | ||
|
||
@Column() | ||
startDate: string; | ||
|
||
@Column({ | ||
default: "", | ||
transformer: Transformer.MAP_EMPTY_STR_TO_UNDEFINED, | ||
}) | ||
endDate?: string; | ||
|
||
@Column() | ||
planId: string; | ||
|
||
@Column("int") | ||
quantity: number; | ||
|
||
@Column({ | ||
default: "", | ||
transformer: Transformer.MAP_EMPTY_STR_TO_UNDEFINED, | ||
}) | ||
cancellationDate?: string; | ||
|
||
// This column triggers the db-sync deletion mechanism. It's not intended for public consumption. | ||
@Column() | ||
deleted: boolean; | ||
} |
38 changes: 38 additions & 0 deletions
38
components/gitpod-db/src/typeorm/migration/1650526577994-TeamSubscrition2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
import { columnExists, tableExists } from "./helper/helper"; | ||
|
||
export class TeamSubscrition21650526577994 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
"CREATE TABLE IF NOT EXISTS `d_b_team_subscription2` (`id` char(36) NOT NULL, `teamId` char(36) NOT NULL, `paymentReference` varchar(255) NOT NULL, `startDate` varchar(255) NOT NULL, `endDate` varchar(255) NOT NULL DEFAULT '', `planId` varchar(255) NOT NULL, `quantity` int(11) NOT NULL, `cancellationDate` varchar(255) NOT NULL DEFAULT '', `deleted` tinyint(4) NOT NULL DEFAULT '0', `_lastModified` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY (`id`), KEY `ind_team_paymentReference` (`teamId`, `paymentReference`), KEY `ind_team_startDate` (`teamId`, `startDate`), KEY `ind_dbsync` (`_lastModified`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;", | ||
); | ||
if (!(await columnExists(queryRunner, "d_b_subscription", "teamMembershipId"))) { | ||
await queryRunner.query( | ||
"ALTER TABLE `d_b_subscription` ADD COLUMN `teamMembershipId` char(36) NOT NULL DEFAULT ''", | ||
); | ||
} | ||
if (!(await columnExists(queryRunner, "d_b_team_membership", "subscriptionId"))) { | ||
await queryRunner.query( | ||
"ALTER TABLE `d_b_team_membership` ADD COLUMN `subscriptionId` char(36) NOT NULL DEFAULT ''", | ||
); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
if (await tableExists(queryRunner, "d_b_team_subscription2")) { | ||
await queryRunner.query("DROP TABLE `d_b_team_subscription2`"); | ||
} | ||
if (await columnExists(queryRunner, "d_b_subscription", "teamMembershipId")) { | ||
await queryRunner.query("ALTER TABLE `d_b_subscription` DROP COLUMN `teamMembershipId`"); | ||
} | ||
if (await columnExists(queryRunner, "d_b_team_membership", "subscriptionId")) { | ||
await queryRunner.query("ALTER TABLE `d_b_team_membership` DROP COLUMN `subscriptionId`"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
components/gitpod-db/src/typeorm/team-subscription-2-db-impl.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the Gitpod Enterprise Source Code License, | ||
* See License.enterprise.txt in the project root folder. | ||
*/ | ||
|
||
import { injectable, inject } from "inversify"; | ||
import { EntityManager, Repository } from "typeorm"; | ||
|
||
import { TeamSubscription2 } from "@gitpod/gitpod-protocol/lib/team-subscription-protocol"; | ||
|
||
import { TeamSubscription2DB } from "../team-subscription-2-db"; | ||
import { DBTeamSubscription2 } from "./entity/db-team-subscription-2"; | ||
import { TypeORM } from "./typeorm"; | ||
|
||
@injectable() | ||
export class TeamSubscription2DBImpl implements TeamSubscription2DB { | ||
@inject(TypeORM) protected readonly typeORM: TypeORM; | ||
|
||
async transaction<T>(code: (db: TeamSubscription2DB) => Promise<T>): Promise<T> { | ||
const manager = await this.getEntityManager(); | ||
return await manager.transaction(async (manager) => { | ||
return await code(new TransactionalTeamSubscription2DBImpl(manager)); | ||
}); | ||
} | ||
|
||
protected async getEntityManager() { | ||
return (await this.typeORM.getConnection()).manager; | ||
} | ||
|
||
protected async getRepo(): Promise<Repository<DBTeamSubscription2>> { | ||
return (await this.getEntityManager()).getRepository(DBTeamSubscription2); | ||
} | ||
|
||
/** | ||
* Team Subscriptions 2 | ||
*/ | ||
|
||
async storeEntry(ts: TeamSubscription2): Promise<void> { | ||
const repo = await this.getRepo(); | ||
await repo.save(ts); | ||
} | ||
|
||
async findById(id: string): Promise<TeamSubscription2 | undefined> { | ||
const repo = await this.getRepo(); | ||
return repo.findOne(id); | ||
} | ||
|
||
async findByPaymentRef(teamId: string, paymentReference: string): Promise<TeamSubscription2 | undefined> { | ||
const repo = await this.getRepo(); | ||
return repo.findOne({ teamId, paymentReference }); | ||
} | ||
|
||
async findForTeam(teamId: string, date: string): Promise<TeamSubscription2 | undefined> { | ||
const repo = await this.getRepo(); | ||
const query = repo | ||
.createQueryBuilder("ts2") | ||
.where("ts2.teamId = :teamId", { teamId }) | ||
.andWhere("ts2.startDate <= :date", { date }) | ||
.andWhere('ts2.endDate = "" OR ts2.endDate > :date', { date }); | ||
return query.getOne(); | ||
} | ||
} | ||
|
||
export class TransactionalTeamSubscription2DBImpl extends TeamSubscription2DBImpl { | ||
constructor(protected readonly manager: EntityManager) { | ||
super(); | ||
} | ||
|
||
async getEntityManager(): Promise<EntityManager> { | ||
return this.manager; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from #9655 I can read that the subscription already knows the membership. why storing this link in both directions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #9655 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Let's use this as a thread in here.)
So, what is the
DBTeamMembership.subscriptionId
need for then? I couldn't spot any usage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! So, while this direction of the link is indeed not used in the current code, it was requested in the RFC that this link be materialized in both directions:
Source (internal)
And, this made sense to me at the time, because the link between
d_b_team_subscription_slot
andd_b_subscription
also went both ways.As mentioned above, this slight duplication has helped/saved us a few times, for example in customer requests, or to investigate recover from bugs.
Are you proposing to drop the reference from
d_b_team_membership
tod_b_subscription
now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep this model and quickly move on to Usage-Based / Pay-As-You-Go (which will hopefully deprecate all these subscriptions anyway)
Decision (internal Slack)