From b21d7df6063e750f439ea26b4d6de8d6a3d4aeab Mon Sep 17 00:00:00 2001 From: GloireMutaliko21 Date: Tue, 11 Feb 2025 11:56:38 +0200 Subject: [PATCH 1/7] fix: refact activity-log entity to using employee --- packages/contracts/src/lib/activity-log.model.ts | 14 +++++++------- .../src/lib/activity-log/activity-log.entity.ts | 14 +++++++------- .../src/lib/activity-log/activity-log.service.ts | 4 ++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/contracts/src/lib/activity-log.model.ts b/packages/contracts/src/lib/activity-log.model.ts index 8a0cf01b125..1e18da6f721 100644 --- a/packages/contracts/src/lib/activity-log.model.ts +++ b/packages/contracts/src/lib/activity-log.model.ts @@ -2,15 +2,17 @@ import { ActorTypeEnum, IBasePerEntityType, IBasePerTenantAndOrganizationEntityModel, - ID, JsonData } from './base-entity.model'; -import { IUser } from './user.model'; +import { IEmployeeEntityInput } from './employee.model'; /** * Interface representing an activity log entry. */ -export interface IActivityLog extends IBasePerTenantAndOrganizationEntityModel, IBasePerEntityType { +export interface IActivityLog + extends IBasePerTenantAndOrganizationEntityModel, + IEmployeeEntityInput, + IBasePerEntityType { action: ActionTypeEnum; actorType?: ActorTypeEnum; description?: string; // A short sentence describing the action performed. (E.g John Doe created this on 22.09.2024) @@ -19,8 +21,6 @@ export interface IActivityLog extends IBasePerTenantAndOrganizationEntityModel, updatedValues?: IActivityLogUpdatedValues[]; // Values after update (E.g For task : {title: ' First Task Updated', members: ['Member4Name', 'Member3Name'], projectId: 'project2UUId'}) previousEntities?: IActivityLogUpdatedValues[]; // Stores previous IDs or other values for related entities. Eg : {members: ['member_1_ID', 'member_2_ID']} updatedEntities?: IActivityLogUpdatedValues[]; // Stores updated IDs, or other values for related entities. Eg : {members: ['member_1_ID', 'member_2_ID']}, - creator?: IUser; - creatorId?: ID; data?: JsonData; } @@ -38,6 +38,6 @@ export enum ActionTypeEnum { } /** - * Input type for activity log creation, excluding `creatorId` and `creator`. + * Input type for activity log creation, excluding `employeeId` and `employee`. */ -export interface IActivityLogInput extends Omit {} +export interface IActivityLogInput extends Omit {} diff --git a/packages/core/src/lib/activity-log/activity-log.entity.ts b/packages/core/src/lib/activity-log/activity-log.entity.ts index d41c60db289..9fe8ed88a2b 100644 --- a/packages/core/src/lib/activity-log/activity-log.entity.ts +++ b/packages/core/src/lib/activity-log/activity-log.entity.ts @@ -4,8 +4,8 @@ import { EntityRepositoryType } from '@mikro-orm/core'; import { JoinColumn, RelationId } from 'typeorm'; import { IsArray, IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator'; import { isMySQL, isPostgres } from '@gauzy/config'; -import { BaseEntityEnum, ActionTypeEnum, ActorTypeEnum, IActivityLog, ID, IUser, JsonData } from '@gauzy/contracts'; -import { TenantOrganizationBaseEntity, User } from '../core/entities/internal'; +import { BaseEntityEnum, ActionTypeEnum, ActorTypeEnum, IActivityLog, ID, JsonData, IEmployee } from '@gauzy/contracts'; +import { Employee, TenantOrganizationBaseEntity } from '../core/entities/internal'; import { ColumnIndex, MultiORMColumn, MultiORMEntity, MultiORMManyToOne } from '../core/decorators/entity'; import { ActorTypeTransformerPipe } from '../shared/pipes'; import { MikroOrmActivityLogRepository } from './repository/mikro-orm-activity-log.repository'; @@ -91,9 +91,9 @@ export class ActivityLog extends TenantOrganizationBaseEntity implements IActivi */ /** - * User performed action + * Employee performed action */ - @MultiORMManyToOne(() => User, { + @MultiORMManyToOne(() => Employee, { /** Indicates if relation column value can be nullable or not. */ nullable: true, @@ -101,10 +101,10 @@ export class ActivityLog extends TenantOrganizationBaseEntity implements IActivi onDelete: 'CASCADE' }) @JoinColumn() - creator?: IUser; + employee?: IEmployee; - @RelationId((it: ActivityLog) => it.creator) + @RelationId((it: ActivityLog) => it.employee) @ColumnIndex() @MultiORMColumn({ nullable: true, relationId: true }) - creatorId?: ID; + employeeId?: ID; } diff --git a/packages/core/src/lib/activity-log/activity-log.service.ts b/packages/core/src/lib/activity-log/activity-log.service.ts index fefd6f32d82..2ba7a807b3d 100644 --- a/packages/core/src/lib/activity-log/activity-log.service.ts +++ b/packages/core/src/lib/activity-log/activity-log.service.ts @@ -40,13 +40,13 @@ export class ActivityLogService extends TenantAwareCrudService { async create(input: IActivityLogInput): Promise { try { // Retrieve the current user's ID from the request context - const creatorId = RequestContext.currentUserId(); + const employeeId = RequestContext.currentEmployeeId(); // Retrieve the current tenant ID from the request context or use the provided tenantId const tenantId = RequestContext.currentTenantId() || input.tenantId; // Create the activity log entry using the provided input along with the tenantId and creatorId - return await super.create({ ...input, tenantId, creatorId }); + return await super.create({ ...input, tenantId, employeeId }); } catch (error) { console.log('Error while creating activity log:', error); throw new BadRequestException('Error while creating activity log', error); From 6e934c591620aa6ba30cffe982660e8aa0d4be41 Mon Sep 17 00:00:00 2001 From: GloireMutaliko21 Date: Tue, 11 Feb 2025 12:40:46 +0200 Subject: [PATCH 2/7] feat: activity log migration --- ...38-AlterActivityLogTableUsingEmployeeId.ts | 285 ++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 packages/core/src/lib/database/migrations/1739268372038-AlterActivityLogTableUsingEmployeeId.ts diff --git a/packages/core/src/lib/database/migrations/1739268372038-AlterActivityLogTableUsingEmployeeId.ts b/packages/core/src/lib/database/migrations/1739268372038-AlterActivityLogTableUsingEmployeeId.ts new file mode 100644 index 00000000000..59dfcac496e --- /dev/null +++ b/packages/core/src/lib/database/migrations/1739268372038-AlterActivityLogTableUsingEmployeeId.ts @@ -0,0 +1,285 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; +import * as chalk from 'chalk'; +import { DatabaseTypeEnum } from '@gauzy/config'; + +export class AlterActivityLogTableUsingEmployeeId1739268372038 implements MigrationInterface { + name = 'AlterActivityLogTableUsingEmployeeId1739268372038'; + + /** + * Up Migration + * + * @param queryRunner + */ + public async up(queryRunner: QueryRunner): Promise { + console.log(chalk.yellow(this.name + ' start running!')); + + switch (queryRunner.connection.options.type) { + case DatabaseTypeEnum.sqlite: + case DatabaseTypeEnum.betterSqlite3: + await this.sqliteUpQueryRunner(queryRunner); + break; + case DatabaseTypeEnum.postgres: + await this.postgresUpQueryRunner(queryRunner); + break; + case DatabaseTypeEnum.mysql: + await this.mysqlUpQueryRunner(queryRunner); + break; + default: + throw Error(`Unsupported database: ${queryRunner.connection.options.type}`); + } + } + + /** + * Down Migration + * + * @param queryRunner + */ + public async down(queryRunner: QueryRunner): Promise { + switch (queryRunner.connection.options.type) { + case DatabaseTypeEnum.sqlite: + case DatabaseTypeEnum.betterSqlite3: + await this.sqliteDownQueryRunner(queryRunner); + break; + case DatabaseTypeEnum.postgres: + await this.postgresDownQueryRunner(queryRunner); + break; + case DatabaseTypeEnum.mysql: + await this.mysqlDownQueryRunner(queryRunner); + break; + default: + throw Error(`Unsupported database: ${queryRunner.connection.options.type}`); + } + } + + /** + * PostgresDB Up Migration + * + * @param queryRunner + */ + public async postgresUpQueryRunner(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "activity_log" DROP CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b"`); + await queryRunner.query(`DROP INDEX "public"."IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + await queryRunner.query(`ALTER TABLE "activity_log" RENAME COLUMN "creatorId" TO "employeeId"`); + await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); + await queryRunner.query( + `ALTER TABLE "activity_log" ADD CONSTRAINT "FK_071945a9d4a2322fde08010292c" FOREIGN KEY ("employeeId") REFERENCES "employee"("id") ON DELETE CASCADE ON UPDATE NO ACTION` + ); + } + + /** + * PostgresDB Down Migration + * + * @param queryRunner + */ + public async postgresDownQueryRunner(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "activity_log" DROP CONSTRAINT "FK_071945a9d4a2322fde08010292c"`); + await queryRunner.query(`DROP INDEX "public"."IDX_071945a9d4a2322fde08010292"`); + await queryRunner.query(`ALTER TABLE "activity_log" RENAME COLUMN "employeeId" TO "creatorId"`); + await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); + await queryRunner.query( + `ALTER TABLE "activity_log" ADD CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b" FOREIGN KEY ("creatorId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION` + ); + } + + /** + * SqliteDB and BetterSQlite3DB Up Migration + * + * @param queryRunner + */ + public async sqliteUpQueryRunner(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + await queryRunner.query( + `CREATE TABLE "temporary_activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "creatorId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE)` + ); + await queryRunner.query( + `INSERT INTO "temporary_activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId" FROM "activity_log"` + ); + await queryRunner.query(`DROP TABLE "activity_log"`); + await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); + await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query( + `CREATE TABLE "temporary_activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "employeeId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE)` + ); + await queryRunner.query( + `INSERT INTO "temporary_activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId" FROM "activity_log"` + ); + await queryRunner.query(`DROP TABLE "activity_log"`); + await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); + await queryRunner.query( + `CREATE TABLE "temporary_activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "employeeId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_071945a9d4a2322fde08010292c" FOREIGN KEY ("employeeId") REFERENCES "employee" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)` + ); + await queryRunner.query( + `INSERT INTO "temporary_activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId" FROM "activity_log"` + ); + await queryRunner.query(`DROP TABLE "activity_log"`); + await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); + } + + /** + * SqliteDB and BetterSQlite3DB Down Migration + * + * @param queryRunner + */ + public async sqliteDownQueryRunner(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); + await queryRunner.query( + `CREATE TABLE "activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "employeeId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE)` + ); + await queryRunner.query( + `INSERT INTO "activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId" FROM "temporary_activity_log"` + ); + await queryRunner.query(`DROP TABLE "temporary_activity_log"`); + await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); + await queryRunner.query( + `CREATE TABLE "activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "creatorId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE)` + ); + await queryRunner.query( + `INSERT INTO "activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId" FROM "temporary_activity_log"` + ); + await queryRunner.query(`DROP TABLE "temporary_activity_log"`); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); + await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); + await queryRunner.query( + `CREATE TABLE "activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "creatorId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b" FOREIGN KEY ("creatorId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)` + ); + await queryRunner.query( + `INSERT INTO "activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId" FROM "temporary_activity_log"` + ); + await queryRunner.query(`DROP TABLE "temporary_activity_log"`); + await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + } + + /** + * MySQL Up Migration + * + * @param queryRunner + */ + public async mysqlUpQueryRunner(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`activity_log\` DROP FOREIGN KEY \`FK_b6e9a5c3e1ee65a3bcb8a00de2b\``); + await queryRunner.query(`DROP INDEX \`IDX_b6e9a5c3e1ee65a3bcb8a00de2\` ON \`activity_log\``); + await queryRunner.query(`ALTER TABLE \`activity_log\` CHANGE \`creatorId\` \`employeeId\` varchar(255) NULL`); + await queryRunner.query(`CREATE INDEX \`IDX_071945a9d4a2322fde08010292\` ON \`activity_log\` (\`employeeId\`)`); + await queryRunner.query( + `ALTER TABLE \`activity_log\` ADD CONSTRAINT \`FK_071945a9d4a2322fde08010292c\` FOREIGN KEY (\`employeeId\`) REFERENCES \`employee\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION` + ); + } + + /** + * MySQL Down Migration + * + * @param queryRunner + */ + public async mysqlDownQueryRunner(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`activity_log\` DROP FOREIGN KEY \`FK_071945a9d4a2322fde08010292c\``); + await queryRunner.query(`DROP INDEX \`IDX_071945a9d4a2322fde08010292\` ON \`activity_log\``); + await queryRunner.query(`ALTER TABLE \`activity_log\` CHANGE \`employeeId\` \`creatorId\` varchar(255) NULL`); + await queryRunner.query(`CREATE INDEX \`IDX_b6e9a5c3e1ee65a3bcb8a00de2\` ON \`activity_log\` (\`creatorId\`)`); + await queryRunner.query( + `ALTER TABLE \`activity_log\` ADD CONSTRAINT \`FK_b6e9a5c3e1ee65a3bcb8a00de2b\` FOREIGN KEY (\`creatorId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION` + ); + } +} From 79cfb86cfc26bf874e3e6e5c64f1a3eb1f1a57bc Mon Sep 17 00:00:00 2001 From: GloireMutaliko21 Date: Tue, 11 Feb 2025 12:53:19 +0200 Subject: [PATCH 3/7] fix: coderabbit suggestion --- packages/core/src/lib/activity-log/activity-log.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/lib/activity-log/activity-log.service.ts b/packages/core/src/lib/activity-log/activity-log.service.ts index 2ba7a807b3d..b096caf40d2 100644 --- a/packages/core/src/lib/activity-log/activity-log.service.ts +++ b/packages/core/src/lib/activity-log/activity-log.service.ts @@ -39,7 +39,7 @@ export class ActivityLogService extends TenantAwareCrudService { */ async create(input: IActivityLogInput): Promise { try { - // Retrieve the current user's ID from the request context + // Retrieve the current employee's ID from the request context const employeeId = RequestContext.currentEmployeeId(); // Retrieve the current tenant ID from the request context or use the provided tenantId From 3d4e210b3d615ff8a6c9efe72f5c73864e13c03e Mon Sep 17 00:00:00 2001 From: "Rahul R." Date: Sat, 15 Feb 2025 12:59:55 +0530 Subject: [PATCH 4/7] feat(migration): alter "activity_log" entity [table] for PostgreSQL --- .../contracts/src/lib/activity-log.model.ts | 2 +- .../lib/activity-log/activity-log.entity.ts | 2 +- .../lib/activity-log/activity-log.service.ts | 12 +- ...38-AlterActivityLogTableUsingEmployeeId.ts | 285 ------------------ ...39602849367-AlterActivityLogEntityTable.ts | 179 +++++++++++ 5 files changed, 187 insertions(+), 293 deletions(-) delete mode 100644 packages/core/src/lib/database/migrations/1739268372038-AlterActivityLogTableUsingEmployeeId.ts create mode 100644 packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts diff --git a/packages/contracts/src/lib/activity-log.model.ts b/packages/contracts/src/lib/activity-log.model.ts index 3b02130289e..7da75eaab0c 100644 --- a/packages/contracts/src/lib/activity-log.model.ts +++ b/packages/contracts/src/lib/activity-log.model.ts @@ -32,4 +32,4 @@ export enum ActionTypeEnum { /** * Input type for activity log creation, excluding `employeeId` and `employee`. */ -export interface IActivityLogInput extends OmitFields {} +export interface IActivityLogInput extends OmitFields {} diff --git a/packages/core/src/lib/activity-log/activity-log.entity.ts b/packages/core/src/lib/activity-log/activity-log.entity.ts index 05fa414d195..38ec495fe65 100644 --- a/packages/core/src/lib/activity-log/activity-log.entity.ts +++ b/packages/core/src/lib/activity-log/activity-log.entity.ts @@ -89,7 +89,7 @@ export class ActivityLog extends BasePerEntityType implements IActivityLog { employee?: IEmployee; /** - * Actvity Log Author ID + * Activity Log Author ID */ @ApiPropertyOptional({ type: () => String }) @IsOptional() diff --git a/packages/core/src/lib/activity-log/activity-log.service.ts b/packages/core/src/lib/activity-log/activity-log.service.ts index b096caf40d2..57b61769bb0 100644 --- a/packages/core/src/lib/activity-log/activity-log.service.ts +++ b/packages/core/src/lib/activity-log/activity-log.service.ts @@ -31,7 +31,7 @@ export class ActivityLogService extends TenantAwareCrudService { } /** - * Creates a new activity log entry with the provided input, while associating it with the current user and tenant. + * Creates a new activity log entry with the provided input, while associating it with the current employee and tenant. * * @param input - The data required to create an activity log entry. * @returns The created activity log entry. @@ -39,14 +39,14 @@ export class ActivityLogService extends TenantAwareCrudService { */ async create(input: IActivityLogInput): Promise { try { - // Retrieve the current employee's ID from the request context - const employeeId = RequestContext.currentEmployeeId(); - // Retrieve the current tenant ID from the request context or use the provided tenantId - const tenantId = RequestContext.currentTenantId() || input.tenantId; + const tenantId = RequestContext.currentTenantId() ?? input.tenantId; + + // Retrieve the current employee's ID from the request context + const employeeId = RequestContext.currentEmployeeId() ?? input.employeeId; // Create the activity log entry using the provided input along with the tenantId and creatorId - return await super.create({ ...input, tenantId, employeeId }); + return await super.create({ ...input, employeeId, tenantId }); } catch (error) { console.log('Error while creating activity log:', error); throw new BadRequestException('Error while creating activity log', error); diff --git a/packages/core/src/lib/database/migrations/1739268372038-AlterActivityLogTableUsingEmployeeId.ts b/packages/core/src/lib/database/migrations/1739268372038-AlterActivityLogTableUsingEmployeeId.ts deleted file mode 100644 index 59dfcac496e..00000000000 --- a/packages/core/src/lib/database/migrations/1739268372038-AlterActivityLogTableUsingEmployeeId.ts +++ /dev/null @@ -1,285 +0,0 @@ -import { MigrationInterface, QueryRunner } from 'typeorm'; -import * as chalk from 'chalk'; -import { DatabaseTypeEnum } from '@gauzy/config'; - -export class AlterActivityLogTableUsingEmployeeId1739268372038 implements MigrationInterface { - name = 'AlterActivityLogTableUsingEmployeeId1739268372038'; - - /** - * Up Migration - * - * @param queryRunner - */ - public async up(queryRunner: QueryRunner): Promise { - console.log(chalk.yellow(this.name + ' start running!')); - - switch (queryRunner.connection.options.type) { - case DatabaseTypeEnum.sqlite: - case DatabaseTypeEnum.betterSqlite3: - await this.sqliteUpQueryRunner(queryRunner); - break; - case DatabaseTypeEnum.postgres: - await this.postgresUpQueryRunner(queryRunner); - break; - case DatabaseTypeEnum.mysql: - await this.mysqlUpQueryRunner(queryRunner); - break; - default: - throw Error(`Unsupported database: ${queryRunner.connection.options.type}`); - } - } - - /** - * Down Migration - * - * @param queryRunner - */ - public async down(queryRunner: QueryRunner): Promise { - switch (queryRunner.connection.options.type) { - case DatabaseTypeEnum.sqlite: - case DatabaseTypeEnum.betterSqlite3: - await this.sqliteDownQueryRunner(queryRunner); - break; - case DatabaseTypeEnum.postgres: - await this.postgresDownQueryRunner(queryRunner); - break; - case DatabaseTypeEnum.mysql: - await this.mysqlDownQueryRunner(queryRunner); - break; - default: - throw Error(`Unsupported database: ${queryRunner.connection.options.type}`); - } - } - - /** - * PostgresDB Up Migration - * - * @param queryRunner - */ - public async postgresUpQueryRunner(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE "activity_log" DROP CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b"`); - await queryRunner.query(`DROP INDEX "public"."IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); - await queryRunner.query(`ALTER TABLE "activity_log" RENAME COLUMN "creatorId" TO "employeeId"`); - await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); - await queryRunner.query( - `ALTER TABLE "activity_log" ADD CONSTRAINT "FK_071945a9d4a2322fde08010292c" FOREIGN KEY ("employeeId") REFERENCES "employee"("id") ON DELETE CASCADE ON UPDATE NO ACTION` - ); - } - - /** - * PostgresDB Down Migration - * - * @param queryRunner - */ - public async postgresDownQueryRunner(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE "activity_log" DROP CONSTRAINT "FK_071945a9d4a2322fde08010292c"`); - await queryRunner.query(`DROP INDEX "public"."IDX_071945a9d4a2322fde08010292"`); - await queryRunner.query(`ALTER TABLE "activity_log" RENAME COLUMN "employeeId" TO "creatorId"`); - await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); - await queryRunner.query( - `ALTER TABLE "activity_log" ADD CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b" FOREIGN KEY ("creatorId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION` - ); - } - - /** - * SqliteDB and BetterSQlite3DB Up Migration - * - * @param queryRunner - */ - public async sqliteUpQueryRunner(queryRunner: QueryRunner): Promise { - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); - await queryRunner.query( - `CREATE TABLE "temporary_activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "creatorId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE)` - ); - await queryRunner.query( - `INSERT INTO "temporary_activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId" FROM "activity_log"` - ); - await queryRunner.query(`DROP TABLE "activity_log"`); - await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); - await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query( - `CREATE TABLE "temporary_activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "employeeId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE)` - ); - await queryRunner.query( - `INSERT INTO "temporary_activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId" FROM "activity_log"` - ); - await queryRunner.query(`DROP TABLE "activity_log"`); - await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); - await queryRunner.query( - `CREATE TABLE "temporary_activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "employeeId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_071945a9d4a2322fde08010292c" FOREIGN KEY ("employeeId") REFERENCES "employee" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)` - ); - await queryRunner.query( - `INSERT INTO "temporary_activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId" FROM "activity_log"` - ); - await queryRunner.query(`DROP TABLE "activity_log"`); - await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); - } - - /** - * SqliteDB and BetterSQlite3DB Down Migration - * - * @param queryRunner - */ - public async sqliteDownQueryRunner(queryRunner: QueryRunner): Promise { - await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); - await queryRunner.query( - `CREATE TABLE "activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "employeeId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE)` - ); - await queryRunner.query( - `INSERT INTO "activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId" FROM "temporary_activity_log"` - ); - await queryRunner.query(`DROP TABLE "temporary_activity_log"`); - await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); - await queryRunner.query( - `CREATE TABLE "activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "creatorId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE)` - ); - await queryRunner.query( - `INSERT INTO "activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "employeeId" FROM "temporary_activity_log"` - ); - await queryRunner.query(`DROP TABLE "temporary_activity_log"`); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); - await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); - await queryRunner.query( - `CREATE TABLE "activity_log" ("deletedAt" datetime, "id" varchar PRIMARY KEY NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "isActive" boolean DEFAULT (1), "isArchived" boolean DEFAULT (0), "archivedAt" datetime, "tenantId" varchar, "organizationId" varchar, "entity" varchar NOT NULL, "entityId" varchar NOT NULL, "action" varchar NOT NULL, "actorType" integer, "description" text, "updatedFields" text, "previousValues" text, "updatedValues" text, "previousEntities" text, "updatedEntities" text, "data" text, "creatorId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b" FOREIGN KEY ("creatorId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)` - ); - await queryRunner.query( - `INSERT INTO "activity_log"("deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId") SELECT "deletedAt", "id", "createdAt", "updatedAt", "isActive", "isArchived", "archivedAt", "tenantId", "organizationId", "entity", "entityId", "action", "actorType", "description", "updatedFields", "previousValues", "updatedValues", "previousEntities", "updatedEntities", "data", "creatorId" FROM "temporary_activity_log"` - ); - await queryRunner.query(`DROP TABLE "temporary_activity_log"`); - await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - } - - /** - * MySQL Up Migration - * - * @param queryRunner - */ - public async mysqlUpQueryRunner(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`activity_log\` DROP FOREIGN KEY \`FK_b6e9a5c3e1ee65a3bcb8a00de2b\``); - await queryRunner.query(`DROP INDEX \`IDX_b6e9a5c3e1ee65a3bcb8a00de2\` ON \`activity_log\``); - await queryRunner.query(`ALTER TABLE \`activity_log\` CHANGE \`creatorId\` \`employeeId\` varchar(255) NULL`); - await queryRunner.query(`CREATE INDEX \`IDX_071945a9d4a2322fde08010292\` ON \`activity_log\` (\`employeeId\`)`); - await queryRunner.query( - `ALTER TABLE \`activity_log\` ADD CONSTRAINT \`FK_071945a9d4a2322fde08010292c\` FOREIGN KEY (\`employeeId\`) REFERENCES \`employee\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION` - ); - } - - /** - * MySQL Down Migration - * - * @param queryRunner - */ - public async mysqlDownQueryRunner(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`activity_log\` DROP FOREIGN KEY \`FK_071945a9d4a2322fde08010292c\``); - await queryRunner.query(`DROP INDEX \`IDX_071945a9d4a2322fde08010292\` ON \`activity_log\``); - await queryRunner.query(`ALTER TABLE \`activity_log\` CHANGE \`employeeId\` \`creatorId\` varchar(255) NULL`); - await queryRunner.query(`CREATE INDEX \`IDX_b6e9a5c3e1ee65a3bcb8a00de2\` ON \`activity_log\` (\`creatorId\`)`); - await queryRunner.query( - `ALTER TABLE \`activity_log\` ADD CONSTRAINT \`FK_b6e9a5c3e1ee65a3bcb8a00de2b\` FOREIGN KEY (\`creatorId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION` - ); - } -} diff --git a/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts b/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts new file mode 100644 index 00000000000..0577225db87 --- /dev/null +++ b/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts @@ -0,0 +1,179 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; +import * as chalk from 'chalk'; +import { DatabaseTypeEnum } from '@gauzy/config'; + +export class AlterActivityLogEntityTable1739602849367 implements MigrationInterface { + name = 'AlterActivityLogEntityTable1739602849367'; + + /** + * Up Migration + * + * @param queryRunner + */ + public async up(queryRunner: QueryRunner): Promise { + console.log(chalk.yellow(this.name + ' start running!')); + + switch (queryRunner.connection.options.type) { + case DatabaseTypeEnum.sqlite: + case DatabaseTypeEnum.betterSqlite3: + await this.sqliteUpQueryRunner(queryRunner); + break; + case DatabaseTypeEnum.postgres: + await this.postgresUpQueryRunner(queryRunner); + break; + case DatabaseTypeEnum.mysql: + await this.mysqlUpQueryRunner(queryRunner); + break; + default: + throw Error(`Unsupported database: ${queryRunner.connection.options.type}`); + } + } + + /** + * Down Migration + * + * @param queryRunner + */ + public async down(queryRunner: QueryRunner): Promise { + switch (queryRunner.connection.options.type) { + case DatabaseTypeEnum.sqlite: + case DatabaseTypeEnum.betterSqlite3: + await this.sqliteDownQueryRunner(queryRunner); + break; + case DatabaseTypeEnum.postgres: + await this.postgresDownQueryRunner(queryRunner); + break; + case DatabaseTypeEnum.mysql: + await this.mysqlDownQueryRunner(queryRunner); + break; + default: + throw Error(`Unsupported database: ${queryRunner.connection.options.type}`); + } + } + + /** + * PostgresDB Up Migration + * + * @param queryRunner + */ + public async postgresUpQueryRunner(queryRunner: QueryRunner): Promise { + // Step 1: Drop the existing foreign key constraint on "creatorId" in the "activity_log" table. + console.log('Step 1: Dropping foreign key constraint on "creatorId" from "activity_log"...'); + await queryRunner.query(`ALTER TABLE "activity_log" DROP CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b"`); + + // Step 2: Drop the existing index on "creatorId". + console.log('Step 2: Dropping index "IDX_b6e9a5c3e1ee65a3bcb8a00de2" from "activity_log"...'); + await queryRunner.query(`DROP INDEX "public"."IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + + // Step 3: Add new column "employeeId" of type uuid. + console.log('Step 3: Adding new column "employeeId" to "activity_log"...'); + await queryRunner.query(`ALTER TABLE "activity_log" ADD "employeeId" uuid`); + + // Step 4: Copy data from "creatorId" to "employeeId" using employee mapping. + console.log('Step 4: Copying data from "creatorId" to "employeeId" via employee table mapping...'); + await queryRunner.query(` + UPDATE "activity_log" al + SET "employeeId" = ( + SELECT e.id + FROM "employee" e + WHERE e."userId" = al."creatorId" + ORDER BY e."createdAt" DESC + LIMIT 1 + ) + WHERE al."creatorId" IS NOT NULL + `); + + // Step 5: Drop the old "creatorId" column. + console.log('Step 5: Dropping column "creatorId" from "activity_log"...'); + await queryRunner.query(`ALTER TABLE "activity_log" DROP COLUMN "creatorId"`); + + // Step 6: Recreate the index on "employeeId" in "activity_log". + console.log('Step 6: Recreate the index on "employeeId" in "activity_log"...'); + await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId")`); + + // Step 7: Add a new foreign key constraint on "employeeId" referencing "employee"("id"). + console.log('Step 7: Adding foreign key constraint on "employeeId" referencing "employee"(id)...'); + await queryRunner.query(` + ALTER TABLE "activity_log" + ADD CONSTRAINT "FK_071945a9d4a2322fde08010292c" + FOREIGN KEY ("employeeId") REFERENCES "employee"("id") ON DELETE CASCADE ON UPDATE NO ACTION + `); + } + + /** + * PostgresDB Down Migration + * + * @param queryRunner + */ + public async postgresDownQueryRunner(queryRunner: QueryRunner): Promise { + // Step 1: Drop the foreign key constraint on column "employeeId". + console.log('Step 1: Dropping foreign key constraint on "employeeId" from "activity_log"...'); + await queryRunner.query(`ALTER TABLE "activity_log" DROP CONSTRAINT "FK_071945a9d4a2322fde08010292c"`); + + // Step 2: Drop the index on "employeeId". + console.log('Step 2: Dropping index "IDX_071945a9d4a2322fde08010292" from "activity_log"...'); + await queryRunner.query(`DROP INDEX "public"."IDX_071945a9d4a2322fde08010292"`); + + // Step 3: Add back the old column "creatorId". + console.log('Step 3: Adding column "creatorId" back to "activity_log"...'); + await queryRunner.query(`ALTER TABLE "activity_log" ADD "creatorId" uuid`); + + // Step 4: Copy data from "employeeId" to "creatorId" using employee mapping. + console.log('Step 4: Copying data from "employeeId" to "creatorId" via employee table mapping...'); + await queryRunner.query(` + UPDATE "activity_log" AS al + SET "creatorId" = ( + SELECT e."userId" + FROM "employee" AS e + WHERE e."id" = al."employeeId" + ORDER BY e."createdAt" DESC + LIMIT 1 + ) + WHERE al."employeeId" IS NOT NULL; + `); + + // Step 5: Drop the new column "employeeId". + console.log('Step 5: Dropping column "employeeId" from "activity_log"...'); + await queryRunner.query(`ALTER TABLE "activity_log" DROP COLUMN "employeeId"`); + + // Step 6: Recreate the index on the restored "creatorId" column. + console.log('Step 6: Creating index on "creatorId" in "activity_log"...'); + await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId")`); + + // Step 7: Re-add the original foreign key constraint on "creatorId" referencing "user"(id). + console.log('Step 7: Adding foreign key constraint on "creatorId" referencing "user"(id)...'); + await queryRunner.query(` + ALTER TABLE "activity_log" + ADD CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b" + FOREIGN KEY ("creatorId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION + `); + } + + /** + * SqliteDB and BetterSQlite3DB Up Migration + * + * @param queryRunner + */ + public async sqliteUpQueryRunner(queryRunner: QueryRunner): Promise {} + + /** + * SqliteDB and BetterSQlite3DB Down Migration + * + * @param queryRunner + */ + public async sqliteDownQueryRunner(queryRunner: QueryRunner): Promise {} + + /** + * MySQL Up Migration + * + * @param queryRunner + */ + public async mysqlUpQueryRunner(queryRunner: QueryRunner): Promise {} + + /** + * MySQL Down Migration + * + * @param queryRunner + */ + public async mysqlDownQueryRunner(queryRunner: QueryRunner): Promise {} +} From 3c11c052e81d0133b877867d68fe0b6088c1caa9 Mon Sep 17 00:00:00 2001 From: "Rahul R." Date: Sat, 15 Feb 2025 14:13:56 +0530 Subject: [PATCH 5/7] feat(migration): alter "activity_log" entity [table] for MySQL --- ...39602849367-AlterActivityLogEntityTable.ts | 88 ++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts b/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts index 0577225db87..742447ba64b 100644 --- a/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts +++ b/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts @@ -168,12 +168,96 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf * * @param queryRunner */ - public async mysqlUpQueryRunner(queryRunner: QueryRunner): Promise {} + public async mysqlUpQueryRunner(queryRunner: QueryRunner): Promise { + // Step 1: Drop the existing foreign key constraint on "creatorId". + console.log('Step 1: Dropping foreign key constraint on "creatorId" from "activity_log"...'); + await queryRunner.query(`ALTER TABLE \`activity_log\` DROP FOREIGN KEY \`FK_b6e9a5c3e1ee65a3bcb8a00de2b\``); + + // Step 2: Drop the existing index on "creatorId". + console.log('Step 2: Dropping index "creatorId" from "activity_log"...'); + await queryRunner.query(`DROP INDEX \`IDX_b6e9a5c3e1ee65a3bcb8a00de2\` ON \`activity_log\``); + + // Step 3: Add new column "employeeId". + console.log('Step 3: Adding new column "employeeId" to "activity_log"...'); + await queryRunner.query(`ALTER TABLE \`activity_log\` ADD \`employeeId\` varchar(255) NULL`); + + // Step 4: Copy data from "creatorId" to "employeeId" using employee mapping. + console.log('Step 4: Copying data from "creatorId" to "employeeId" via employee table mapping...'); + await queryRunner.query(` + UPDATE \`activity_log\` AS al + SET al.\`employeeId\` = ( + SELECT e.\`id\` + FROM \`employee\` AS e + WHERE al.\`creatorId\` = e.\`userId\` + ORDER BY e.\`createdAt\` DESC + LIMIT 1 + ) + WHERE al.\`creatorId\` IS NOT NULL + `); + + // Step 5: Drop the old "creatorId" column. + console.log('Step 5: Dropping column "creatorId" from "activity_log"...'); + await queryRunner.query(`ALTER TABLE \`activity_log\` DROP COLUMN \`creatorId\``); + + // Step 6: Recreate the index on "employeeId" in "activity_log". + console.log('Step 6: Recreate the index on "employeeId" in "activity_log"...'); + await queryRunner.query(`CREATE INDEX \`IDX_071945a9d4a2322fde08010292\` ON \`activity_log\` (\`employeeId\`)`); + + // Step 7: Add a new foreign key constraint on "employeeId" referencing "employee"(id). + console.log('Step 7: Adding foreign key constraint on "employeeId" referencing "employee"(id)...'); + await queryRunner.query(` + ALTER TABLE \`activity_log\` + ADD CONSTRAINT \`FK_071945a9d4a2322fde08010292c\` + FOREIGN KEY (\`employeeId\`) REFERENCES \`employee\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION + `); + } /** * MySQL Down Migration * * @param queryRunner */ - public async mysqlDownQueryRunner(queryRunner: QueryRunner): Promise {} + public async mysqlDownQueryRunner(queryRunner: QueryRunner): Promise { + // Step 1: Drop the foreign key constraint on "employeeId". + console.log('Step 1: Dropping foreign key constraint on "employeeId" from "activity_log"...'); + await queryRunner.query(`ALTER TABLE \`activity_log\` DROP FOREIGN KEY \`FK_071945a9d4a2322fde08010292c\``); + + // Step 2: Drop the index on "employeeId". + console.log('Step 2: Dropping index "IDX_071945a9d4a2322fde08010292" from "activity_log"...'); + await queryRunner.query(`DROP INDEX \`IDX_071945a9d4a2322fde08010292\` ON \`activity_log\``); + + // Step 3: Add back the old column "creatorId" back to "activity_log". + console.log('Step 3: Adding column "creatorId" back to "activity_log"...'); + await queryRunner.query(`ALTER TABLE \`activity_log\` ADD \`creatorId\` varchar(255) NULL`); + + // Step 4: Copy data from "employeeId" to "creatorId" using employee mapping. + console.log('Step 4: Copying data from "employeeId" to "creatorId" via employee table mapping...'); + await queryRunner.query(` + UPDATE \`activity_log\` AS al + SET al.\`creatorId\` = ( + SELECT e.\`userId\` + FROM \`employee\` AS e + WHERE e.\`id\` = al.\`employeeId\` + ORDER BY e.\`createdAt\` DESC + LIMIT 1 + ) + WHERE al.\`employeeId\` IS NOT NULL + `); + + // Step 5: Drop the new column "employeeId". + console.log('Step 5: Dropping column "employeeId" from "activity_log"...'); + await queryRunner.query(`ALTER TABLE \`activity_log\` DROP COLUMN \`employeeId\``); + + // Step 6: Recreate the index on the restored "creatorId" column. + console.log('Step 6: Recreate the index on "creatorId" in "activity_log"...'); + await queryRunner.query(`CREATE INDEX \`IDX_b6e9a5c3e1ee65a3bcb8a00de2\` ON \`activity_log\` (\`creatorId\`)`); + + // Step 7: Re-add the original foreign key constraint on "creatorId" referencing "user"(id). + console.log('Step 7: Adding foreign key constraint on "creatorId" referencing "user"(id) in "activity_log"...'); + await queryRunner.query(` + ALTER TABLE \`activity_log\` + ADD CONSTRAINT \`FK_b6e9a5c3e1ee65a3bcb8a00de2b\` + FOREIGN KEY (\`creatorId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION + `); + } } From 4307c12d6b203b2cd95004b1255f7ff9717b697b Mon Sep 17 00:00:00 2001 From: "Rahul R." Date: Sat, 15 Feb 2025 15:00:49 +0530 Subject: [PATCH 6/7] feat(migration): alter "activity_log" entity [table] for SQLite --- ...39602849367-AlterActivityLogEntityTable.ts | 636 +++++++++++++++++- 1 file changed, 634 insertions(+), 2 deletions(-) diff --git a/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts b/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts index 742447ba64b..aa0e4aa9934 100644 --- a/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts +++ b/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts @@ -154,14 +154,646 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf * * @param queryRunner */ - public async sqliteUpQueryRunner(queryRunner: QueryRunner): Promise {} + public async sqliteUpQueryRunner(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + + await queryRunner.query(` + CREATE TABLE "temporary_activity_log" ( + "deletedAt" datetime, + "id" varchar PRIMARY KEY NOT NULL, + "createdAt" datetime NOT NULL DEFAULT (datetime('now')), + "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), + "isActive" boolean DEFAULT (1), + "isArchived" boolean DEFAULT (0), + "archivedAt" datetime, + "tenantId" varchar, + "organizationId" varchar, + "entity" varchar NOT NULL, + "entityId" varchar NOT NULL, + "action" varchar NOT NULL, + "actorType" integer, + "description" text, + "updatedFields" text, + "previousValues" text, + "updatedValues" text, + "previousEntities" text, + "updatedEntities" text, + "data" text, + "creatorId" varchar, + CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, + CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE + ) + `); + + // Step: Copy data from the existing "activity_log" table into "temporary_activity_log". + console.log('Inserting data into "temporary_activity_log" from "activity_log"...'); + await queryRunner.query(` + INSERT INTO "temporary_activity_log" ( + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "creatorId" + ) + SELECT + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "creatorId" + FROM "activity_log" + `); + + await queryRunner.query(`DROP TABLE "activity_log"`); + await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); + + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); + + await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + + // Step 1: Create the temporary table "temporary_activity_log" with the extended schema, + // where "employeeId" will be populated from the old "creatorId" column. + console.log('Step 1: Creating temporary table "temporary_activity_log" with extended schema...'); + await queryRunner.query(` + CREATE TABLE "temporary_activity_log" ( + "deletedAt" datetime, + "id" varchar PRIMARY KEY NOT NULL, + "createdAt" datetime NOT NULL DEFAULT (datetime('now')), + "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), + "isActive" boolean DEFAULT (1), + "isArchived" boolean DEFAULT (0), + "archivedAt" datetime, + "tenantId" varchar, + "organizationId" varchar, + "entity" varchar NOT NULL, + "entityId" varchar NOT NULL, + "action" varchar NOT NULL, + "actorType" integer, + "description" text, + "updatedFields" text, + "previousValues" text, + "updatedValues" text, + "previousEntities" text, + "updatedEntities" text, + "data" text, + "employeeId" varchar, + CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, + CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE + ) + `); + + // Step 2: Copy data from the existing "activity_log" table into "temporary_activity_log". + // Here, we map the old "creatorId" to the new "employeeId" column. + console.log('Step 2: Copying data from "activity_log" into "temporary_activity_log"...'); + await queryRunner.query(` + INSERT INTO "temporary_activity_log" ( + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "employeeId" + ) + SELECT + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "creatorId" + FROM "activity_log" + `); + + await queryRunner.query(`DROP TABLE "activity_log"`); + await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); + + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); + + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); + + // Step 1: Create the temporary table "temporary_activity_log" with the extended schema. + console.log('Step 1: Creating temporary table "temporary_activity_log" with extended schema...'); + await queryRunner.query(` + CREATE TABLE "temporary_activity_log" ( + "deletedAt" datetime, + "id" varchar PRIMARY KEY NOT NULL, + "createdAt" datetime NOT NULL DEFAULT (datetime('now')), + "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), + "isActive" boolean DEFAULT (1), + "isArchived" boolean DEFAULT (0), + "archivedAt" datetime, + "tenantId" varchar, + "organizationId" varchar, + "entity" varchar NOT NULL, + "entityId" varchar NOT NULL, + "action" varchar NOT NULL, + "actorType" integer, + "description" text, + "updatedFields" text, + "previousValues" text, + "updatedValues" text, + "previousEntities" text, + "updatedEntities" text, + "data" text, + "employeeId" varchar, + CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, + CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "FK_071945a9d4a2322fde08010292c" FOREIGN KEY ("employeeId") REFERENCES "employee" ("id") ON DELETE CASCADE ON UPDATE NO ACTION + ) + `); + + // Step 2: Copy data from the existing "activity_log" table into "temporary_activity_log". + console.log('Step 2: Copying data from "activity_log" into "temporary_activity_log"...'); + await queryRunner.query(` + INSERT INTO "temporary_activity_log" ( + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "employeeId" + ) + SELECT + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "employeeId" + FROM "activity_log" + `); + + await queryRunner.query(`DROP TABLE "activity_log"`); + await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); + + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); + } /** * SqliteDB and BetterSQlite3DB Down Migration * * @param queryRunner */ - public async sqliteDownQueryRunner(queryRunner: QueryRunner): Promise {} + public async sqliteDownQueryRunner(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + + await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); + + // Step 1: Create the new "activity_log" table with the restored schema. + console.log('Step 1: Creating new "activity_log" table with restored schema...'); + await queryRunner.query(` + CREATE TABLE "activity_log" ( + "deletedAt" datetime, + "id" varchar PRIMARY KEY NOT NULL, + "createdAt" datetime NOT NULL DEFAULT (datetime('now')), + "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), + "isActive" boolean DEFAULT (1), + "isArchived" boolean DEFAULT (0), + "archivedAt" datetime, + "tenantId" varchar, + "organizationId" varchar, + "entity" varchar NOT NULL, + "entityId" varchar NOT NULL, + "action" varchar NOT NULL, + "actorType" integer, + "description" text, + "updatedFields" text, + "previousValues" text, + "updatedValues" text, + "previousEntities" text, + "updatedEntities" text, + "data" text, + "employeeId" varchar, + CONSTRAINT "FK_d42f36e39404cb6455254deb360" + FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, + CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" + FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE + ) + `); + + // Step 2: Insert data into the new "activity_log" table from "temporary_activity_log". + console.log('Step 2: Inserting data into "activity_log" from "temporary_activity_log"...'); + await queryRunner.query(` + INSERT INTO "activity_log" ( + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "employeeId" + ) + SELECT + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "employeeId" + FROM "temporary_activity_log" + `); + + await queryRunner.query(`DROP TABLE "temporary_activity_log"`); + + await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + + await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + + await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); + + // Step 1: Create the new "activity_log" table with the original schema. + console.log('Step 1: Creating new "activity_log" table with restored schema (including "creatorId")...'); + await queryRunner.query(` + CREATE TABLE "activity_log" ( + "deletedAt" datetime, + "id" varchar PRIMARY KEY NOT NULL, + "createdAt" datetime NOT NULL DEFAULT (datetime('now')), + "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), + "isActive" boolean DEFAULT (1), + "isArchived" boolean DEFAULT (0), + "archivedAt" datetime, + "tenantId" varchar, + "organizationId" varchar, + "entity" varchar NOT NULL, + "entityId" varchar NOT NULL, + "action" varchar NOT NULL, + "actorType" integer, + "description" text, + "updatedFields" text, + "previousValues" text, + "updatedValues" text, + "previousEntities" text, + "updatedEntities" text, + "data" text, + "creatorId" varchar, + CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, + CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE + ) + `); + + // Step 2: Copy data from "temporary_activity_log" into the new "activity_log" table. + // Note: Here we map the old "employeeId" column from temporary table to "creatorId" in the new table. + console.log('Step 2: Inserting data into "activity_log" from "temporary_activity_log"...'); + await queryRunner.query(` + INSERT INTO "activity_log" ( + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "creatorId" + ) + SELECT + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "employeeId" + FROM "temporary_activity_log" + `); + + await queryRunner.query(`DROP TABLE "temporary_activity_log"`); + + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); + + await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); + await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); + await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); + await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); + await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); + await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); + await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); + await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + + await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); + + // Step 1: Create the new "activity_log" table with the restored original schema. + console.log('Step 1: Creating new "activity_log" table with restored schema (including "creatorId")...'); + await queryRunner.query(` + CREATE TABLE "activity_log" ( + "deletedAt" datetime, + "id" varchar PRIMARY KEY NOT NULL, + "createdAt" datetime NOT NULL DEFAULT (datetime('now')), + "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), + "isActive" boolean DEFAULT (1), + "isArchived" boolean DEFAULT (0), + "archivedAt" datetime, + "tenantId" varchar, + "organizationId" varchar, + "entity" varchar NOT NULL, + "entityId" varchar NOT NULL, + "action" varchar NOT NULL, + "actorType" integer, + "description" text, + "updatedFields" text, + "previousValues" text, + "updatedValues" text, + "previousEntities" text, + "updatedEntities" text, + "data" text, + "creatorId" varchar, + CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, + CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b" FOREIGN KEY ("creatorId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION + ) + `); + + // Step 2: Copy data from "temporary_activity_log" into the new "activity_log" table. + console.log('Step 2: Inserting data into "activity_log" from "temporary_activity_log"...'); + await queryRunner.query(` + INSERT INTO "activity_log" ( + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "creatorId" + ) + SELECT + "deletedAt", + "id", + "createdAt", + "updatedAt", + "isActive", + "isArchived", + "archivedAt", + "tenantId", + "organizationId", + "entity", + "entityId", + "action", + "actorType", + "description", + "updatedFields", + "previousValues", + "updatedValues", + "previousEntities", + "updatedEntities", + "data", + "creatorId" + FROM "temporary_activity_log" + `); + + await queryRunner.query(`DROP TABLE "temporary_activity_log"`); + + await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); + await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); + await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); + await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); + await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); + await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); + await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); + await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); + await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); + } /** * MySQL Up Migration From 5c35909762bd958c44bdaf3e7a652f872d939431 Mon Sep 17 00:00:00 2001 From: "Rahul R." Date: Mon, 17 Feb 2025 12:27:32 +0530 Subject: [PATCH 7/7] feat(migration): alter "activity_log" entity [table] for SQLite --- ...39602849367-AlterActivityLogEntityTable.ts | 325 ++++-------------- 1 file changed, 61 insertions(+), 264 deletions(-) diff --git a/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts b/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts index aa0e4aa9934..f3f10481f49 100644 --- a/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts +++ b/packages/core/src/lib/database/migrations/1739602849367-AlterActivityLogEntityTable.ts @@ -155,6 +155,8 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf * @param queryRunner */ public async sqliteUpQueryRunner(queryRunner: QueryRunner): Promise { + // Step 1: Drop existing indexes from the current "activity_log" table. + console.log('Step 1: Dropping existing indexes from "activity_log"...'); await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); @@ -165,6 +167,8 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); + // Step 2: Create the temporary table "temporary_activity_log" with extra columns. + console.log('Step 2: Creating temporary table "temporary_activity_log" with new columns...'); await queryRunner.query(` CREATE TABLE "temporary_activity_log" ( "deletedAt" datetime, @@ -188,13 +192,15 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf "updatedEntities" text, "data" text, "creatorId" varchar, + "employeeId" varchar, CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, - CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE + CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "FK_071945a9d4a2322fde08010292c" FOREIGN KEY ("employeeId") REFERENCES "employee" ("id") ON DELETE CASCADE ON UPDATE NO ACTION ) `); - // Step: Copy data from the existing "activity_log" table into "temporary_activity_log". - console.log('Inserting data into "temporary_activity_log" from "activity_log"...'); + // Step 3: Copy data from the old "activity_log" table into "temporary_activity_log". + console.log('Step 3: Copying data from "activity_log" into "temporary_activity_log"...'); await queryRunner.query(` INSERT INTO "temporary_activity_log" ( "deletedAt", @@ -244,137 +250,26 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf FROM "activity_log" `); - await queryRunner.query(`DROP TABLE "activity_log"`); - await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); - - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); - - await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - - // Step 1: Create the temporary table "temporary_activity_log" with the extended schema, - // where "employeeId" will be populated from the old "creatorId" column. - console.log('Step 1: Creating temporary table "temporary_activity_log" with extended schema...'); - await queryRunner.query(` - CREATE TABLE "temporary_activity_log" ( - "deletedAt" datetime, - "id" varchar PRIMARY KEY NOT NULL, - "createdAt" datetime NOT NULL DEFAULT (datetime('now')), - "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), - "isActive" boolean DEFAULT (1), - "isArchived" boolean DEFAULT (0), - "archivedAt" datetime, - "tenantId" varchar, - "organizationId" varchar, - "entity" varchar NOT NULL, - "entityId" varchar NOT NULL, - "action" varchar NOT NULL, - "actorType" integer, - "description" text, - "updatedFields" text, - "previousValues" text, - "updatedValues" text, - "previousEntities" text, - "updatedEntities" text, - "data" text, - "employeeId" varchar, - CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, - CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE - ) - `); - - // Step 2: Copy data from the existing "activity_log" table into "temporary_activity_log". - // Here, we map the old "creatorId" to the new "employeeId" column. - console.log('Step 2: Copying data from "activity_log" into "temporary_activity_log"...'); + // Step 4: Copy data from "creatorId" to "employeeId" using employee mapping. + console.log('Step 4: Updating "employeeId" from "creatorId"...'); await queryRunner.query(` - INSERT INTO "temporary_activity_log" ( - "deletedAt", - "id", - "createdAt", - "updatedAt", - "isActive", - "isArchived", - "archivedAt", - "tenantId", - "organizationId", - "entity", - "entityId", - "action", - "actorType", - "description", - "updatedFields", - "previousValues", - "updatedValues", - "previousEntities", - "updatedEntities", - "data", - "employeeId" + UPDATE "temporary_activity_log" + SET "employeeId" = ( + SELECT e."id" + FROM "employee" e + WHERE e."userId" = "temporary_activity_log"."creatorId" + LIMIT 1 ) - SELECT - "deletedAt", - "id", - "createdAt", - "updatedAt", - "isActive", - "isArchived", - "archivedAt", - "tenantId", - "organizationId", - "entity", - "entityId", - "action", - "actorType", - "description", - "updatedFields", - "previousValues", - "updatedValues", - "previousEntities", - "updatedEntities", - "data", - "creatorId" - FROM "activity_log" + WHERE "creatorId" IS NOT NULL `); + // Step 5: Drop the old "activity_log" table and rename the temporary table. + console.log('Step 5: Dropping the old "activity_log" table and renaming the temporary table...'); await queryRunner.query(`DROP TABLE "activity_log"`); await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); - - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); - - // Step 1: Create the temporary table "temporary_activity_log" with the extended schema. - console.log('Step 1: Creating temporary table "temporary_activity_log" with extended schema...'); + // Step 6: Create the temporary table "temporary_activity_log" with the extended schema. + console.log('Step 6: Creating temporary table "temporary_activity_log" with extended schema...'); await queryRunner.query(` CREATE TABLE "temporary_activity_log" ( "deletedAt" datetime, @@ -404,8 +299,8 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf ) `); - // Step 2: Copy data from the existing "activity_log" table into "temporary_activity_log". - console.log('Step 2: Copying data from "activity_log" into "temporary_activity_log"...'); + // Step 7: Copy data from the existing "activity_log" table into "temporary_activity_log". + console.log('Step 7: Copying data from "activity_log" into "temporary_activity_log"...'); await queryRunner.query(` INSERT INTO "temporary_activity_log" ( "deletedAt", @@ -455,9 +350,15 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf FROM "activity_log" `); + // Step 8: Drop the old "activity_log" table and rename the new clean table. + console.log( + 'Step 8: Dropping old "activity_log" table and renaming "temporary_activity_log" to "activity_log"...' + ); await queryRunner.query(`DROP TABLE "activity_log"`); await queryRunner.query(`ALTER TABLE "temporary_activity_log" RENAME TO "activity_log"`); + // Step 9: Create final indexes on the new "activity_log" table. + console.log('Step 9: Creating indexes on the new "activity_log" table...'); await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); @@ -475,6 +376,8 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf * @param queryRunner */ public async sqliteDownQueryRunner(queryRunner: QueryRunner): Promise { + // Step 1: Drop existing indexes from the final "activity_log" table. + console.log('Step 1: Dropping existing indexes from "activity_log"...'); await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); @@ -485,10 +388,12 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); + // Step 2: Rename the current "activity_log" table to "temporary_activity_log". + console.log('Step 2: Renaming "activity_log" to "temporary_activity_log"...'); await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); - // Step 1: Create the new "activity_log" table with the restored schema. - console.log('Step 1: Creating new "activity_log" table with restored schema...'); + // Step 3: Create the new "activity_log" table with the restored schema. + console.log('Step 3: Creating new "activity_log" table with restored schema...'); await queryRunner.query(` CREATE TABLE "activity_log" ( "deletedAt" datetime, @@ -511,16 +416,16 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf "previousEntities" text, "updatedEntities" text, "data" text, + "creatorId" varchar, "employeeId" varchar, - CONSTRAINT "FK_d42f36e39404cb6455254deb360" - FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, - CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" - FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE + CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, + CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "FK_b6e9a5c3e1ee65a3bcb8a00de2b" FOREIGN KEY ("creatorId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION ) `); - // Step 2: Insert data into the new "activity_log" table from "temporary_activity_log". - console.log('Step 2: Inserting data into "activity_log" from "temporary_activity_log"...'); + // Step 4: Insert data into the new "activity_log" table from "temporary_activity_log". + console.log('Step 4: Inserting data into "activity_log" from "temporary_activity_log"...'); await queryRunner.query(` INSERT INTO "activity_log" ( "deletedAt", @@ -570,138 +475,26 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf FROM "temporary_activity_log" `); - await queryRunner.query(`DROP TABLE "temporary_activity_log"`); - - await queryRunner.query(`CREATE INDEX "IDX_071945a9d4a2322fde08010292" ON "activity_log" ("employeeId") `); - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - - await queryRunner.query(`DROP INDEX "IDX_071945a9d4a2322fde08010292"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - - await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); - - // Step 1: Create the new "activity_log" table with the original schema. - console.log('Step 1: Creating new "activity_log" table with restored schema (including "creatorId")...'); - await queryRunner.query(` - CREATE TABLE "activity_log" ( - "deletedAt" datetime, - "id" varchar PRIMARY KEY NOT NULL, - "createdAt" datetime NOT NULL DEFAULT (datetime('now')), - "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), - "isActive" boolean DEFAULT (1), - "isArchived" boolean DEFAULT (0), - "archivedAt" datetime, - "tenantId" varchar, - "organizationId" varchar, - "entity" varchar NOT NULL, - "entityId" varchar NOT NULL, - "action" varchar NOT NULL, - "actorType" integer, - "description" text, - "updatedFields" text, - "previousValues" text, - "updatedValues" text, - "previousEntities" text, - "updatedEntities" text, - "data" text, - "creatorId" varchar, - CONSTRAINT "FK_d42f36e39404cb6455254deb360" FOREIGN KEY ("tenantId") REFERENCES "tenant" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, - CONSTRAINT "FK_3e7ec906ac1026a6c9779e82a21" FOREIGN KEY ("organizationId") REFERENCES "organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE - ) - `); - - // Step 2: Copy data from "temporary_activity_log" into the new "activity_log" table. - // Note: Here we map the old "employeeId" column from temporary table to "creatorId" in the new table. - console.log('Step 2: Inserting data into "activity_log" from "temporary_activity_log"...'); + // Step 5: Update "creatorId" from "employeeId" using employee table mapping + console.log('Step 5: Updating "creatorId" from "employeeId" via employee table mapping...'); await queryRunner.query(` - INSERT INTO "activity_log" ( - "deletedAt", - "id", - "createdAt", - "updatedAt", - "isActive", - "isArchived", - "archivedAt", - "tenantId", - "organizationId", - "entity", - "entityId", - "action", - "actorType", - "description", - "updatedFields", - "previousValues", - "updatedValues", - "previousEntities", - "updatedEntities", - "data", - "creatorId" + UPDATE "activity_log" + SET "creatorId" = ( + SELECT e."userId" + FROM "employee" e + WHERE e."id" = "activity_log"."employeeId" + LIMIT 1 ) - SELECT - "deletedAt", - "id", - "createdAt", - "updatedAt", - "isActive", - "isArchived", - "archivedAt", - "tenantId", - "organizationId", - "entity", - "entityId", - "action", - "actorType", - "description", - "updatedFields", - "previousValues", - "updatedValues", - "previousEntities", - "updatedEntities", - "data", - "employeeId" - FROM "temporary_activity_log" + WHERE "employeeId" IS NOT NULL `); + // Step 6: Drop the old "activity_log" table and rename the temporary table. + console.log('Step 6: Dropping the old "temporary_activity_log" table and renaming the "activity_log" table...'); await queryRunner.query(`DROP TABLE "temporary_activity_log"`); - - await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); - await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `); - await queryRunner.query(`CREATE INDEX "IDX_ef0a3bcee9c0305f755d5add13" ON "activity_log" ("entityId") `); - await queryRunner.query(`CREATE INDEX "IDX_c60ac1ac95c2d901afd2f68909" ON "activity_log" ("entity") `); - await queryRunner.query(`CREATE INDEX "IDX_3e7ec906ac1026a6c9779e82a2" ON "activity_log" ("organizationId") `); - await queryRunner.query(`CREATE INDEX "IDX_d42f36e39404cb6455254deb36" ON "activity_log" ("tenantId") `); - await queryRunner.query(`CREATE INDEX "IDX_eb63f18992743f35225ae4e77c" ON "activity_log" ("isArchived") `); - await queryRunner.query(`CREATE INDEX "IDX_4a88f1b97dd306d919f844828d" ON "activity_log" ("isActive") `); - await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); - - await queryRunner.query(`DROP INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2"`); - await queryRunner.query(`DROP INDEX "IDX_691ba0d5b57cd5adea2c9cc285"`); - await queryRunner.query(`DROP INDEX "IDX_695624cb02a5da0e86cd4489c0"`); - await queryRunner.query(`DROP INDEX "IDX_ef0a3bcee9c0305f755d5add13"`); - await queryRunner.query(`DROP INDEX "IDX_c60ac1ac95c2d901afd2f68909"`); - await queryRunner.query(`DROP INDEX "IDX_3e7ec906ac1026a6c9779e82a2"`); - await queryRunner.query(`DROP INDEX "IDX_d42f36e39404cb6455254deb36"`); - await queryRunner.query(`DROP INDEX "IDX_eb63f18992743f35225ae4e77c"`); - await queryRunner.query(`DROP INDEX "IDX_4a88f1b97dd306d919f844828d"`); - await queryRunner.query(`ALTER TABLE "activity_log" RENAME TO "temporary_activity_log"`); - // Step 1: Create the new "activity_log" table with the restored original schema. - console.log('Step 1: Creating new "activity_log" table with restored schema (including "creatorId")...'); + // Step 7: Create the new "activity_log" table with the restored original schema. + console.log('Step 7: Creating new "activity_log" table with restored schema (including "creatorId")...'); await queryRunner.query(` CREATE TABLE "activity_log" ( "deletedAt" datetime, @@ -731,8 +524,8 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf ) `); - // Step 2: Copy data from "temporary_activity_log" into the new "activity_log" table. - console.log('Step 2: Inserting data into "activity_log" from "temporary_activity_log"...'); + // Step 8: Copy data from "temporary_activity_log" into the new "activity_log" table. + console.log('Step 8: Inserting data into "activity_log" from "temporary_activity_log"...'); await queryRunner.query(` INSERT INTO "activity_log" ( "deletedAt", @@ -782,8 +575,12 @@ export class AlterActivityLogEntityTable1739602849367 implements MigrationInterf FROM "temporary_activity_log" `); + // Step 9: Dropping the old "temporary_activity_log" table. + console.log('Step 9: Dropping the old "temporary_activity_log" table...'); await queryRunner.query(`DROP TABLE "temporary_activity_log"`); + // Step 10: Create final indexes on the new "activity_log" table. + console.log('Step 10: Creating indexes on the new "activity_log" table...'); await queryRunner.query(`CREATE INDEX "IDX_b6e9a5c3e1ee65a3bcb8a00de2" ON "activity_log" ("creatorId") `); await queryRunner.query(`CREATE INDEX "IDX_691ba0d5b57cd5adea2c9cc285" ON "activity_log" ("actorType") `); await queryRunner.query(`CREATE INDEX "IDX_695624cb02a5da0e86cd4489c0" ON "activity_log" ("action") `);