From 4a635620c418c27c43400a1dc5d4184f04c582ef Mon Sep 17 00:00:00 2001 From: Adrian Pilarczyk Date: Thu, 24 Aug 2023 11:56:28 +0200 Subject: [PATCH] feat: :sparkles: improve migration scripts visibility (#918) * feat: :sparkles: add emojis * refactor: :truck: migration file --- ...s-migration.ts => 1.13-taxes-migration.ts} | 11 ++++---- .../migrations/app-webhook-migrator.ts | 26 +++++++++---------- .../migrations/app-webhook-repository.ts | 14 +++++----- .../taxes/scripts/migrations/run-migration.ts | 4 +-- apps/taxes/scripts/migrations/run-report.ts | 4 +-- 5 files changed, 31 insertions(+), 28 deletions(-) rename apps/taxes/scripts/migrations/{taxes-migration.ts => 1.13-taxes-migration.ts} (87%) diff --git a/apps/taxes/scripts/migrations/taxes-migration.ts b/apps/taxes/scripts/migrations/1.13-taxes-migration.ts similarity index 87% rename from apps/taxes/scripts/migrations/taxes-migration.ts rename to apps/taxes/scripts/migrations/1.13-taxes-migration.ts index 954c886..3c184d6 100644 --- a/apps/taxes/scripts/migrations/taxes-migration.ts +++ b/apps/taxes/scripts/migrations/1.13-taxes-migration.ts @@ -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. } diff --git a/apps/taxes/scripts/migrations/app-webhook-migrator.ts b/apps/taxes/scripts/migrations/app-webhook-migrator.ts index 11bdfcc..cf08cba 100644 --- a/apps/taxes/scripts/migrations/app-webhook-migrator.ts +++ b/apps/taxes/scripts/migrations/app-webhook-migrator.ts @@ -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, ); } diff --git a/apps/taxes/scripts/migrations/app-webhook-repository.ts b/apps/taxes/scripts/migrations/app-webhook-repository.ts index e0d58b2..4cb1126 100644 --- a/apps/taxes/scripts/migrations/app-webhook-repository.ts +++ b/apps/taxes/scripts/migrations/app-webhook-repository.ts @@ -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; } diff --git a/apps/taxes/scripts/migrations/run-migration.ts b/apps/taxes/scripts/migrations/run-migration.ts index c087370..ed527e3 100644 --- a/apps/taxes/scripts/migrations/run-migration.ts +++ b/apps/taxes/scripts/migrations/run-migration.ts @@ -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; } } diff --git a/apps/taxes/scripts/migrations/run-report.ts b/apps/taxes/scripts/migrations/run-report.ts index 0a9f3a3..c481964 100644 --- a/apps/taxes/scripts/migrations/run-report.ts +++ b/apps/taxes/scripts/migrations/run-report.ts @@ -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; } }