Skip to content

WIP: Reformat gitpod-db with prettier #8664

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

Closed
wants to merge 3 commits into from
Closed
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
60 changes: 32 additions & 28 deletions components/gitpod-db/src/accounting-db.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
* See License.enterprise.txt in the project root folder.
*/

import { Subscription } from '@gitpod/gitpod-protocol/lib/accounting-protocol';
import * as chai from 'chai';
import { suite, test, timeout } from 'mocha-typescript';
import { QueryRunner } from 'typeorm';
import { AccountingDB } from './accounting-db';
import { oneMonthLater, rightAfter, rightBefore } from '@gitpod/gitpod-protocol/lib/util/timeutil';
import { DBAccountEntry } from './typeorm/entity/db-account-entry';
import { TransactionalAccountingDBImpl } from './typeorm/accounting-db-impl';
import { DBWorkspace } from './typeorm/entity/db-workspace';
import { DBWorkspaceInstance } from './typeorm/entity/db-workspace-instance';
import { DBSubscription } from './typeorm/entity/db-subscription';
import { testContainer } from './test-container';
import { TypeORM } from './typeorm/typeorm';
import { Subscription } from "@gitpod/gitpod-protocol/lib/accounting-protocol";
import * as chai from "chai";
import { suite, test, timeout } from "mocha-typescript";
import { QueryRunner } from "typeorm";
import { AccountingDB } from "./accounting-db";
import { oneMonthLater, rightAfter, rightBefore } from "@gitpod/gitpod-protocol/lib/util/timeutil";
import { DBAccountEntry } from "./typeorm/entity/db-account-entry";
import { TransactionalAccountingDBImpl } from "./typeorm/accounting-db-impl";
import { DBWorkspace } from "./typeorm/entity/db-workspace";
import { DBWorkspaceInstance } from "./typeorm/entity/db-workspace-instance";
import { DBSubscription } from "./typeorm/entity/db-subscription";
import { testContainer } from "./test-container";
import { TypeORM } from "./typeorm/typeorm";
const expect = chai.expect;

@suite @timeout(5000)
@suite
@timeout(5000)
export class AccountingDBSpec {

typeORM = testContainer.get<TypeORM>(TypeORM);
db: AccountingDB;
queryRunner: QueryRunner;
Expand All @@ -37,7 +37,7 @@ export class AccountingDBSpec {
this.queryRunner = connection.createQueryRunner();
await this.queryRunner.connect();
await this.queryRunner.startTransaction();
this.db = new TransactionalAccountingDBImpl(this.queryRunner.manager)
this.db = new TransactionalAccountingDBImpl(this.queryRunner.manager);
}

async after() {
Expand All @@ -54,7 +54,7 @@ export class AccountingDBSpec {
startDate: now,
endDate: later,
amount: 1.01,
planId: 'test'
planId: "test",
};
await this.db.newSubscription(subscription);

Expand All @@ -80,7 +80,7 @@ export class AccountingDBSpec {
startDate: now,
endDate: undefined, // open ended
amount: 1.01,
planId: 'test'
planId: "test",
};
await this.db.newSubscription(subscription);

Expand All @@ -104,43 +104,47 @@ export class AccountingDBSpec {
startDate: now,
endDate: undefined, // open ended
amount: 1.01,
planId: 'test'
planId: "test",
};
const dbSubscription = await this.db.newSubscription(subscription);
expectExactlyOne(await this.db.findActiveSubscriptions(now, rightAfter(later)), subscription);
expect(await this.db.findActiveSubscriptions(rightBefore(now), rightBefore(now))).to.be.empty;
Subscription.cancelSubscription(dbSubscription, later);
await this.db.storeSubscription(dbSubscription)
await this.db.storeSubscription(dbSubscription);
expect(await this.db.findActiveSubscriptions(rightAfter(later), rightAfter(later))).to.be.empty;
await this.db.storeSubscription(dbSubscription)
await this.db.storeSubscription(dbSubscription);
expect(await this.db.findActiveSubscriptions(rightAfter(later), rightAfter(later))).to.be.empty;
}

@test public async subscriptionsForUser() {
const now = new Date().toISOString();
const later = oneMonthLater(now, 31)
const later = oneMonthLater(now, 31);
const subscription = <Subscription>{
userId: "Open ended",
startDate: now,
endDate: undefined, // open ended
amount: 1.01,
planId: 'test'
planId: "test",
};
let dbSubscription = await this.db.newSubscription(subscription);
expectExactlyOne(await this.db.findActiveSubscriptionsForUser(subscription.userId, now), subscription);
expect(await this.db.findActiveSubscriptionsForUser(subscription.userId, rightBefore(now))).to.be.an('array').and.empty;
expect(await this.db.findActiveSubscriptionsForUser(subscription.userId, rightBefore(now))).to.be.an("array")
.and.empty;
expectExactlyOne(await this.db.findActiveSubscriptionsForUser(subscription.userId, later), subscription);
Subscription.cancelSubscription(dbSubscription, later);
await this.db.storeSubscription(dbSubscription);

expectExactlyOne(await this.db.findActiveSubscriptionsForUser(subscription.userId, rightBefore(later)), dbSubscription);
expect(await this.db.findActiveSubscriptionsForUser(subscription.userId, later)).to.be.an('array').and.empty;
expectExactlyOne(
await this.db.findActiveSubscriptionsForUser(subscription.userId, rightBefore(later)),
dbSubscription,
);
expect(await this.db.findActiveSubscriptionsForUser(subscription.userId, later)).to.be.an("array").and.empty;
}
}

const expectExactlyOne = <T>(result: T[], expectation: T) => {
expect(result.length).to.be.equal(1);
expect(result[0]).to.deep.include(expectation);
}
};

module.exports = new AccountingDBSpec
module.exports = new AccountingDBSpec();
31 changes: 22 additions & 9 deletions components/gitpod-db/src/accounting-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,38 @@
* See License.enterprise.txt in the project root folder.
*/

import { AccountEntry, Subscription, SubscriptionAndUser, Credit } from "@gitpod/gitpod-protocol/lib/accounting-protocol";
import {
AccountEntry,
Subscription,
SubscriptionAndUser,
Credit,
} from "@gitpod/gitpod-protocol/lib/accounting-protocol";
import { DBSubscriptionAdditionalData } from "./typeorm/entity/db-subscription";
import { EntityManager } from "typeorm";

export const TransactionalAccountingDBFactory = Symbol('TransactionalAccountingDBFactory');
export const TransactionalAccountingDBFactory = Symbol("TransactionalAccountingDBFactory");
export interface TransactionalAccountingDBFactory {
(manager: EntityManager): AccountingDB;
}

export const AccountingDB = Symbol('AccountingDB');
export const AccountingDB = Symbol("AccountingDB");

export interface AccountingDB {
newAccountEntry(entry: Omit<AccountEntry, 'uid'>): Promise<AccountEntry>;
newAccountEntry(entry: Omit<AccountEntry, "uid">): Promise<AccountEntry>;
storeAccountEntry(AccountEntry: AccountEntry): void;
findAccountEntriesFor(userId: string, fromDate: string, toDate: string): Promise<AccountEntry[]>;
findOpenCredits(userId: string, date: string): Promise<Credit[]>;

newSubscription(subscription: Omit<Subscription, 'uid'>): Promise<Subscription>;
newSubscription(subscription: Omit<Subscription, "uid">): Promise<Subscription>;
storeSubscription(subscription: Subscription): Promise<Subscription>;
findSubscriptionById(id: string): Promise<Subscription | undefined>;
deleteSubscription(subscription: Subscription): Promise<void>
deleteSubscription(subscription: Subscription): Promise<void>;
findActiveSubscriptions(fromDate: string, toDate: string): Promise<Subscription[]>;
findActiveSubscriptionsForUser(userId: string, fromDate: string): Promise<Subscription[]>;
findActiveSubscriptionsByIdentity(authId: string[], authProvider: string): Promise<{ [authId:string]:SubscriptionAndUser[] }>;
findActiveSubscriptionsByIdentity(
authId: string[],
authProvider: string,
): Promise<{ [authId: string]: SubscriptionAndUser[] }>;
findActiveSubscriptionByPlanID(planID: string, date: string): Promise<Subscription[]>;
findAllSubscriptionsForUser(userId: string): Promise<Subscription[]>;
findSubscriptionsForUserInPeriod(userId: string, fromDate: string, toDate: string): Promise<Subscription[]>;
Expand All @@ -38,7 +46,12 @@ export interface AccountingDB {
hadSubscriptionCreatedWithCoupon(userId: string, coupon: string): Promise<boolean>;
findSubscriptionAdditionalData(paymentReference: string): Promise<DBSubscriptionAdditionalData | undefined>;

transaction<T>(closure: (db: AccountingDB)=>Promise<T>, closures?: ((manager: EntityManager) => Promise<any>)[]): Promise<T>;
transaction<T>(
closure: (db: AccountingDB) => Promise<T>,
closures?: ((manager: EntityManager) => Promise<any>)[],
): Promise<T>;

storeSubscriptionAdditionalData(subscriptionData: DBSubscriptionAdditionalData): Promise<DBSubscriptionAdditionalData>;
storeSubscriptionAdditionalData(
subscriptionData: DBSubscriptionAdditionalData,
): Promise<DBSubscriptionAdditionalData>;
}
20 changes: 14 additions & 6 deletions components/gitpod-db/src/app-installation-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@

import { AppInstallation, AppInstallationPlatform } from "@gitpod/gitpod-protocol";

export const AppInstallationDB = Symbol('AppInstallationDB');
export const AppInstallationDB = Symbol("AppInstallationDB");

export interface AppInstallationDB {

recordNewInstallation(platform: AppInstallationPlatform, source: 'user' | 'platform', installationID: string, ownerUserID?: string, platformUserID?: string): Promise<void>;
recordUninstallation(platform: AppInstallationPlatform, source: 'user' | 'platform', installationID: string): Promise<void>;
recordNewInstallation(
platform: AppInstallationPlatform,
source: "user" | "platform",
installationID: string,
ownerUserID?: string,
platformUserID?: string,
): Promise<void>;
recordUninstallation(
platform: AppInstallationPlatform,
source: "user" | "platform",
installationID: string,
): Promise<void>;

findInstallation(platform: AppInstallationPlatform, installationID: string): Promise<AppInstallation | undefined>;

}
}
6 changes: 3 additions & 3 deletions components/gitpod-db/src/auth-provider-entry-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { AuthProviderEntry as AuthProviderEntry } from "@gitpod/gitpod-protocol";
import { createHash } from "crypto";

export const AuthProviderEntryDB = Symbol('AuthProviderEntryDB');
export const AuthProviderEntryDB = Symbol("AuthProviderEntryDB");

export interface AuthProviderEntryDB {
storeAuthProvider(ap: AuthProviderEntry, updateOAuthRevision: boolean): Promise<AuthProviderEntry>;
Expand All @@ -21,5 +21,5 @@ export interface AuthProviderEntryDB {
}

export function hashOAuth(oauth: AuthProviderEntry["oauth"]): string {
return createHash('sha256').update(JSON.stringify(oauth)).digest('hex');
}
return createHash("sha256").update(JSON.stringify(oauth)).digest("hex");
}
26 changes: 14 additions & 12 deletions components/gitpod-db/src/auth-provider-entry.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
* See License.enterprise.txt in the project root folder.
*/

import * as chai from 'chai';
import { suite, test, timeout } from 'mocha-typescript';
import { testContainer } from './test-container';
import { TypeORM } from './typeorm/typeorm';
import { AuthProviderEntryDB } from '.';
import { DBAuthProviderEntry } from './typeorm/entity/db-auth-provider-entry';
import { DeepPartial } from '@gitpod/gitpod-protocol/lib/util/deep-partial';
import * as chai from "chai";
import { suite, test, timeout } from "mocha-typescript";
import { testContainer } from "./test-container";
import { TypeORM } from "./typeorm/typeorm";
import { AuthProviderEntryDB } from ".";
import { DBAuthProviderEntry } from "./typeorm/entity/db-auth-provider-entry";
import { DeepPartial } from "@gitpod/gitpod-protocol/lib/util/deep-partial";
const expect = chai.expect;

@suite @timeout(5000)
@suite
@timeout(5000)
export class AuthProviderEntryDBSpec {

typeORM = testContainer.get<TypeORM>(TypeORM);
db = testContainer.get<AuthProviderEntryDB>(AuthProviderEntryDB);

Expand All @@ -40,7 +40,7 @@ export class AuthProviderEntryDBSpec {
id: "0049b9d2-005f-43c2-a0ae-76377805d8b8",
host,
ownerId,
status: 'verified',
status: "verified",
type: "GitHub",
oauthRevision: undefined,
deleted: false,
Expand Down Expand Up @@ -87,8 +87,10 @@ export class AuthProviderEntryDBSpec {

const loadedAp = await this.db.findByHost(ap.host);
expect(loadedAp, "findByHost()").to.deep.equal(ap);
expect(loadedAp?.oauthRevision, "findByHost()").to.equal("e05ea6fab8efcaba4b3246c2b2d3931af897c3bc2c1cf075c31614f0954f9840");
expect(loadedAp?.oauthRevision, "findByHost()").to.equal(
"e05ea6fab8efcaba4b3246c2b2d3931af897c3bc2c1cf075c31614f0954f9840",
);
}
}

module.exports = AuthProviderEntryDBSpec
module.exports = AuthProviderEntryDBSpec;
28 changes: 14 additions & 14 deletions components/gitpod-db/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
* See License-AGPL.txt in the project root for license information.
*/

import { injectable } from 'inversify';
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
import { getEnvVarParsed, getEnvVar } from '@gitpod/gitpod-protocol/lib/env';
import { ConnectionConfig } from 'mysql';
import { injectable } from "inversify";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { getEnvVarParsed, getEnvVar } from "@gitpod/gitpod-protocol/lib/env";
import { ConnectionConfig } from "mysql";

@injectable()
export class Config {
get dbConfig(): DatabaseConfig {
// defaults to be used only in tests
const dbSetup = {
host: process.env.DB_HOST || 'localhost',
port: getEnvVarParsed('DB_PORT', Number.parseInt, '3306'),
username: process.env.DB_USERNAME || 'gitpod',
password: process.env.DB_PASSWORD || 'test',
database: process.env.DB_NAME || 'gitpod'
host: process.env.DB_HOST || "localhost",
port: getEnvVarParsed("DB_PORT", Number.parseInt, "3306"),
username: process.env.DB_USERNAME || "gitpod",
password: process.env.DB_PASSWORD || "test",
database: process.env.DB_NAME || "gitpod",
};

log.info(`Using DB: ${dbSetup.host}:${dbSetup.port}/${dbSetup.database}`);
Expand All @@ -33,17 +33,17 @@ export class Config {
port: dbConfig.port,
user: dbConfig.username,
password: dbConfig.password,
database: dbConfig.database
database: dbConfig.database,
};
}

get dbEncryptionKeys(): string {
return getEnvVar('DB_ENCRYPTION_KEYS');
return getEnvVar("DB_ENCRYPTION_KEYS");
}

get deletedEntryGCConfig(): DeletedEntryGCConfig {
const enabled = getEnvVar('DB_DELETED_ENTRIES_GC_ENABLED', 'true') === 'true';
const intervalMS = parseInt(getEnvVar('DB_DELETED_ENTRIES_GC_INTERVAL', (10 * 60 * 1000).toString()));
const enabled = getEnvVar("DB_DELETED_ENTRIES_GC_ENABLED", "true") === "true";
const intervalMS = parseInt(getEnvVar("DB_DELETED_ENTRIES_GC_INTERVAL", (10 * 60 * 1000).toString()));
return { enabled, intervalMS };
}
}
Expand All @@ -59,4 +59,4 @@ export interface DatabaseConfig {
export interface DeletedEntryGCConfig {
enabled: boolean;
intervalMS: number;
}
}
Loading