Skip to content

Commit

Permalink
fix: #6909 migration for SQLite & PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-rocket committed Oct 4, 2023
1 parent 2fa0ae7 commit f638b06
Showing 1 changed file with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { MigrationInterface, QueryRunner, SelectQueryBuilder } from "typeorm";
import { createDefaultEmailTemplates } from "./../../email-template/email-template.seed";
import { EmailTemplate } from "./../../core/entities/internal";
import { MigrationInterface, QueryRunner } from "typeorm";
import * as chalk from 'chalk';
import { EmailTemplateEnum } from "@gauzy/contracts";
import { EmailTemplateUtils } from "email-template/utils";

export class MigrateEmailTemplatesData1643809486960 implements MigrationInterface {

name = 'MigrateEmailTemplatesData1643809486960';

/**
* Up Migration
*
* @param queryRunner
*/
public async up(queryRunner: QueryRunner): Promise<any> {
/**
* Removed default gauzy email templates, again seed them with new one
*/
const query = queryRunner.connection.createQueryBuilder(EmailTemplate, 'email_template');
query.where((qb: SelectQueryBuilder<EmailTemplate>) => {
qb.andWhere(`"${qb.alias}"."organizationId" IS NULL`);
qb.andWhere(`"${qb.alias}"."tenantId" IS NULL`);
});
const emailTemplates = await query.getMany();
await queryRunner.connection.getRepository(EmailTemplate).remove(emailTemplates);

/**
* Reseed all new default email templates
*/
await createDefaultEmailTemplates(queryRunner.connection);
console.log(chalk.yellow(`MigrateEmailTemplatesData1643809486960 start running!`));
const templates = Object.values(EmailTemplateEnum);
await Promise.all(
templates.map(
async (template: EmailTemplateEnum) => {
try {
await EmailTemplateUtils.migrateEmailTemplates(queryRunner, template);
} catch (error) {
console.log(`Error while migrating missing email templates for ${template}`, error);
}
}
)
);
}

public async down(queryRunner: QueryRunner): Promise<any> {}
}
public async down(queryRunner: QueryRunner): Promise<any> { }
}

0 comments on commit f638b06

Please sign in to comment.