feat: improve migration scripts visibility (#918)

* feat:  add emojis

* refactor: 🚚 migration file
This commit is contained in:
Adrian Pilarczyk 2023-08-24 11:56:28 +02:00 committed by GitHub
parent 783bd5ec55
commit 4a635620c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 28 deletions

View file

@ -1,5 +1,4 @@
/* eslint-disable multiline-comment-style */
import { orderConfirmedAsyncWebhook } from "../../src/pages/api/webhooks/order-confirmed";
import { AppWebhookMigrator } from "./app-webhook-migrator";
/**
@ -10,18 +9,20 @@ export async function migrateTaxes(webhookMigrator: AppWebhookMigrator) {
// Migration plan:
// 1st step
// 1. Create new ORDER_CONFIRMED webhooks for each Taxes App.
await webhookMigrator.registerWebhookIfItDoesntExist(orderConfirmedAsyncWebhook);
// await webhookMigrator.registerWebhookIfItDoesntExist(orderConfirmedAsyncWebhook);
//
// 2. To confirm if everything is working as expected, we can get all webhooks for apps and check if the ORDER_CONFIRMED webhooks were created.
// await webhookMigrator.getAppWebhooks();
//
// 3. If something went wrong, we can roll back the migration by uncommenting this line:
// await webhookMigrator.DANGEROUS_DELETE_APP_WEBHOOK_BY_NAME(orderConfirmedAsyncWebhook.name);
// It will delete the ORDER_CONFIRMED webhooks created above.
//
// 2nd step (after two weeks)
// 1. Comment the 1st step code above.
// 2. Delete the ORDER_CREATED and ORDER_FULFILLED webhooks by uncommenting this line:
// await webhookMigrator.DANGEROUS_DELETE_APP_WEBHOOK_BY_NAME("OrderCreated");
// await webhookMigrator.DANGEROUS_DELETE_APP_WEBHOOK_BY_NAME("OrderFulfilled");
//
// Migrations completed ✅. The file remains as an artefact.
}

View file

@ -24,7 +24,7 @@ export class AppWebhookMigrator {
appId: string;
appWebhookRepository: AppWebhookRepository;
},
{ mode }: AppWebhookMigratorOptions
{ mode }: AppWebhookMigratorOptions,
) {
this.appWebhookRepository = appWebhookRepository;
@ -56,22 +56,22 @@ export class AppWebhookMigrator {
}
private async deleteWebhookById(webhookId: string) {
console.log(`Webhook ${webhookId} will be deleted`);
console.log(`Webhook ${webhookId} will be deleted`);
if (this.mode === "migrate") {
await this.appWebhookRepository.delete(webhookId);
console.log(`Webhook ${webhookId} deleted`);
console.log(`Webhook ${webhookId} deleted`);
}
}
private async disableWebhookById(webhookId: string) {
console.log(`Webhook ${webhookId} will be disabled`);
console.log(`Webhook ${webhookId} will be disabled`);
if (this.mode === "migrate") {
await this.appWebhookRepository.disable(webhookId);
console.log(`Webhook ${webhookId} disabled`);
console.log(`Webhook ${webhookId} disabled`);
}
}
@ -82,7 +82,7 @@ export class AppWebhookMigrator {
async getAppWebhooks() {
const webhooks = await this.appWebhookRepository.getAll();
console.log(`Webhooks for app ${this.appId}: `, webhooks);
console.log(`📖 Webhooks for app ${this.appId}: `, webhooks);
return webhooks;
}
@ -93,7 +93,7 @@ export class AppWebhookMigrator {
const webhook = webhooks.find((webhook) => webhook.name === webhookName);
if (!webhook) {
console.log(`Webhook ${webhookName} not found`);
console.log(`🚧 Webhook ${webhookName} not found`);
return;
}
@ -111,7 +111,7 @@ export class AppWebhookMigrator {
const webhook = webhooks.find((webhook) => webhook.name === webhookName);
if (!webhook) {
console.log(`Webhook ${webhookName} not found`);
console.log(`🚧 Webhook ${webhookName} not found`);
return;
}
@ -130,16 +130,16 @@ export class AppWebhookMigrator {
const webhookExists = webhooks.some((webhook) => webhook.name === webhookHandler.name);
if (webhookExists) {
console.log(`Webhook ${webhookHandler.name} already exists`);
console.log(`🚧 Webhook ${webhookHandler.name} already exists`);
return;
}
console.log(`Webhook ${webhookHandler.name} will be registered`);
console.log(`Webhook ${webhookHandler.name} will be registered`);
if (this.mode === "migrate") {
await this.registerWebhookFromHandler(webhookHandler);
console.log(`Webhook ${webhookHandler.name} registered`);
console.log(`Webhook ${webhookHandler.name} registered`);
}
}
@ -151,7 +151,7 @@ export class AppWebhookMigrator {
*/
async rollbackWebhookMigrations(
prevWebhookName: string,
nextWebhookHandler: SaleorSyncWebhook | SaleorAsyncWebhook
nextWebhookHandler: SaleorSyncWebhook | SaleorAsyncWebhook,
) {
const webhooks = await this.appWebhookRepository.getAll();
@ -181,6 +181,6 @@ export function createAppWebhookMigrator(env: AuthData, options: AppWebhookMigra
appId: env.appId,
appWebhookRepository,
},
options
options,
);
}

View file

@ -1,4 +1,3 @@
import { createGraphQLClient } from "@saleor/apps-shared";
import { Client, gql } from "urql";
import {
CreateAppWebhookDocument,
@ -95,7 +94,7 @@ export class AppWebhookRepository {
.toPromise();
if (error) {
console.log("Was not able to fetch app webhooks", error.message);
console.log("Was not able to fetch app webhooks", error.message);
throw error;
}
@ -109,7 +108,10 @@ export class AppWebhookRepository {
.toPromise();
if (error) {
console.log(`Was not able to create webhook for the app ${variables.appId}`, error.message);
console.log(
`❌ Was not able to create webhook for the app ${variables.appId}`,
error.message,
);
throw error;
}
@ -125,7 +127,7 @@ export class AppWebhookRepository {
.toPromise();
if (error) {
console.log(`Was not able to disable webhook ${id}`, error.message);
console.log(`Was not able to disable webhook ${id}`, error.message);
throw error;
}
@ -141,7 +143,7 @@ export class AppWebhookRepository {
.toPromise();
if (error) {
console.log(`Was not able to enable webhook ${id}`, error.message);
console.log(`Was not able to enable webhook ${id}`, error.message);
throw error;
}
@ -159,7 +161,7 @@ export class AppWebhookRepository {
console.log(data, error);
if (error) {
console.log(`Was not able to delete webhook ${id}`, error.message);
console.log(`Was not able to delete webhook ${id}`, error.message);
throw error;
}

View file

@ -3,7 +3,7 @@
import * as dotenv from "dotenv";
import { createAppWebhookMigrator } from "./app-webhook-migrator";
import { fetchCloudAplEnvs, verifyRequiredEnvs } from "./migration-utils";
import { migrateTaxes } from "./taxes-migration";
import { migrateTaxes } from "./1.13-taxes-migration";
dotenv.config();
@ -29,7 +29,7 @@ const runMigration = async () => {
await migrateTaxes(webhookMigrator);
} catch (error) {
console.log("Error while migrating webhook. Continuing with the next app.");
console.log("Error while migrating webhook. Continuing with the next app.");
continue;
}
}

View file

@ -3,7 +3,7 @@
import * as dotenv from "dotenv";
import { createAppWebhookMigrator } from "./app-webhook-migrator";
import { fetchCloudAplEnvs, verifyRequiredEnvs } from "./migration-utils";
import { migrateTaxes } from "./taxes-migration";
import { migrateTaxes } from "./1.13-taxes-migration";
dotenv.config();
@ -29,7 +29,7 @@ const runReport = async () => {
await migrateTaxes(webhookMigrator);
} catch (error) {
console.log("Error while migrating webhook. Continuing with the next app.");
console.log("Error while migrating webhook. Continuing with the next app.");
continue;
}
}