Skip to content

Commit

Permalink
fix(seeding): refactored every seeding migrations into SQL only to be…
Browse files Browse the repository at this point in the history
… able to not break the production one more time

Took 1 hour 21 minutes


Took 19 seconds
  • Loading branch information
Croos3r authored and Dorian Moy committed Oct 12, 2023
1 parent 63e678e commit 57ae1a1
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import Service from "../entities/service.entity";

export class CreateGithubService1696697379896 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Service).save({
id: "github",
imageUrl: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
oauthUrl: "https://github.com/login/oauth/authorize",
});
await queryRunner.query(
`INSERT INTO "service" ("id", "image_url", "oauth_url")
VALUES ('github',
'https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png',
'https://github.com/login/oauth/authorize')`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Service).delete({ id: "github" });
await queryRunner.query(
`DELETE
FROM "service"
WHERE "id" = 'github'`,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { In, MigrationInterface, QueryRunner } from "typeorm";
import ServiceScope from "../entities/service-scope.entity";
import { MigrationInterface, QueryRunner } from "typeorm";

export class CreateGithubServiceScopes1696697647435 implements MigrationInterface {
private readonly GITHUB_SERVICE_SCOPES: Array<string> = [
Expand Down Expand Up @@ -39,18 +38,19 @@ export class CreateGithubServiceScopes1696697647435 implements MigrationInterfac
];

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(ServiceScope).save(
this.GITHUB_SERVICE_SCOPES.map((id) => ({
id,
serviceId: "github",
})),
await queryRunner.query(
`INSERT INTO "service_scope" ("id", "service_id")
VALUES
${this.GITHUB_SERVICE_SCOPES.map((scope) => `('${scope}', 'github')`).join(",")}`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(ServiceScope).delete({
id: In(this.GITHUB_SERVICE_SCOPES),
serviceId: "github",
});
await queryRunner.query(
`DELETE
FROM "service_scope"
WHERE "service_id" = 'github'
AND "id" IN (${this.GITHUB_SERVICE_SCOPES.join(",")}`,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { In, MigrationInterface, QueryRunner } from "typeorm";
import Service from "../entities/service.entity";
import ServiceScope from "../entities/service-scope.entity";
import { MigrationInterface, QueryRunner } from "typeorm";

export class CreateGoogleServiceWithScopes1696808587273 implements MigrationInterface {
private readonly GOOGLE_SERVICE_SCOPES: Array<string> = [
Expand Down Expand Up @@ -279,25 +277,30 @@ export class CreateGoogleServiceWithScopes1696808587273 implements MigrationInte
];

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Service).save({
id: "google",
imageUrl:
"https://lh3.googleusercontent.com/0cDOOJjp8pUGDDFLqHFITEi35uMGZ5wHpZ9KTKridxk71kpR9MfeydpQqG5n8Mvetvkg5iVuZGeL2xMvxgBY_UL-T9p0x_Eo4EAh",
oauthUrl: "https://accounts.google.com/o/oauth2/v2/auth",
});
await queryRunner.manager.getRepository(ServiceScope).save(
this.GOOGLE_SERVICE_SCOPES.map((id) => ({
id,
serviceId: "google",
})),
await queryRunner.query(
`INSERT INTO "service" ("id", "image_url", "oauth_url")
VALUES ('google',
'https://lh3.googleusercontent.com/0cDOOJjp8pUGDDFLqHFITEi35uMGZ5wHpZ9KTKridxk71kpR9MfeydpQqG5n8Mvetvkg5iVuZGeL2xMvxgBY_UL-T9p0x_Eo4EAh',
'https://accounts.google.com/o/oauth2/v2/auth')`,
);
await queryRunner.query(
`INSERT INTO "service_scope" ("id", "service_id")
VALUES
${this.GOOGLE_SERVICE_SCOPES.map((scope) => `('${scope}', 'google')`).join(",")}`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Service).delete({ id: "google" });
await queryRunner.manager.getRepository(ServiceScope).delete({
serviceId: "google",
id: In(this.GOOGLE_SERVICE_SCOPES),
});
await queryRunner.query(
`DELETE
FROM "service"
WHERE "id" = 'google'`,
);
await queryRunner.query(
`DELETE
FROM "service_scope"
WHERE "service_id" = 'google'
AND "id" IN (${this.GOOGLE_SERVICE_SCOPES.join(",")}`,
);
}
}
20 changes: 12 additions & 8 deletions backend/back/src/services/seed/1696814128392-CreateTimerService.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import Service from "../entities/service.entity";

export class CreateTimerService1696814128392 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Service).save({
id: "timer",
imageUrl: "https://cdn-icons-png.flaticon.com/512/1571/1571810.png",
oauthUrl: "",
needConnection: false,
});
await queryRunner.query(
`INSERT INTO "service" ("id", "image_url", "oauth_url", "need_connection")
VALUES ('timer',
'https://cdn-icons-png.flaticon.com/512/1571/1571810.png',
'',
false)`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Service).delete({ id: "timer" });
await queryRunner.query(
`DELETE
FROM "service"
WHERE "id" = 'timer'`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,24 @@ export class AddDescriptionAndFormFlowsForCurrentAreas1697044397539 implements M
];

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Area).save(this.descriptionsAndFormFlows);
for (const { id, serviceId, description, parametersFormFlow } of this.descriptionsAndFormFlows) {
await queryRunner.query(
`UPDATE "area"
SET "parameters_form_flow" = $1,
"description" = $2
WHERE "id" = $3
AND "service_id" = $4`,
[JSON.stringify(parametersFormFlow), description, id, serviceId],
);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Area).update(
this.descriptionsAndFormFlows.map(({ id }) => id),
{
description: "An area description",
parametersFormFlow: [],
},
await queryRunner.query(
`UPDATE "area"
SET "parameters_form_flow" = '[]' AND "description" = 'An area description'
WHERE "id" IN ('send-email', 'seconds-interval')
AND "service_id" IN ('google', 'timer')`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ import Area from "../../services/entities/area.entity";

export class CreateGoogleSendEmailAndTimerSecondIntervalAreasWithScopes1696815504760 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Area).save([
{
id: "send-email",
serviceId: "google",
serviceScopesNeeded: [{ id: "https://www.googleapis.com/auth/gmail.send", serviceId: "google" }],
isAction: false,
},
{
id: "seconds-interval",
serviceId: "timer",
serviceScopesNeeded: [],
isAction: true,
},
]);
await queryRunner.query(
`INSERT INTO "area" ("id", "service_id", "is_action")
VALUES ('send-email', 'google', false),
('seconds-interval', 'timer', true)`,
);
await queryRunner.query(
`INSERT INTO "area_service_scopes_needed_service_scope"
("area_id", "area_service_id", "service_scope_id", "service_scope_service_id")
VALUES ('send-email', 'google', 'https://www.googleapis.com/auth/gmail.send', 'google')`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(Area).delete({
id: In(["send-email", "seconds-interval"]),
serviceId: In(["google", "timer"]),
});
await queryRunner.query(
`DELETE
FROM "area"
WHERE "id" IN ('send-email', 'seconds-interval')
AND "service_id" IN ('google', 'timer')`,
);
}
}

0 comments on commit 57ae1a1

Please sign in to comment.