add: migration
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { EntityManager } from "@mikro-orm/postgresql";
|
||||
import { Seeder } from "@mikro-orm/seeder";
|
||||
import { Logger } from "@nestjs/common";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import { TEST_SEED_PREFIX } from "./test-companies.seeder";
|
||||
import { Company } from "../../src/modules/companies/entities/company.entity";
|
||||
|
||||
/** Must match the business ID used in TestCompaniesSeeder. */
|
||||
const TEST_BUSINESS_ID = "e5e760b9-46a5-4194-bb3d-00a46c24d72a";
|
||||
|
||||
export class CleanupTestCompaniesSeeder extends Seeder {
|
||||
private readonly logger = new Logger(CleanupTestCompaniesSeeder.name);
|
||||
|
||||
async run(em: EntityManager): Promise<void> {
|
||||
this.logger.log(`Cleaning up test companies (name like '${TEST_SEED_PREFIX}%') for business ${TEST_BUSINESS_ID}...`);
|
||||
|
||||
const companies = await em.find(
|
||||
Company,
|
||||
{
|
||||
name: { $like: `${TEST_SEED_PREFIX}%` },
|
||||
business: { id: TEST_BUSINESS_ID },
|
||||
deletedAt: null,
|
||||
},
|
||||
{ populate: ["user"] },
|
||||
);
|
||||
|
||||
if (companies.length === 0) {
|
||||
this.logger.log("No test companies found. Nothing to delete.");
|
||||
return;
|
||||
}
|
||||
|
||||
const now = dayjs().toDate();
|
||||
for (const company of companies) {
|
||||
company.deletedAt = now;
|
||||
if (company.user?.deletedAt == null) {
|
||||
company.user.deletedAt = now;
|
||||
}
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
this.logger.log(`Soft-deleted ${companies.length} test companies and their users.`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user