Skip to content

Commit

Permalink
feat: only create seeder table if trackable seed found or global flag…
Browse files Browse the repository at this point in the history
… is set
  • Loading branch information
tada5hi committed Jul 25, 2023
1 parent d5e5f63 commit 379c296
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/seeder/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ export class SeederExecutor {
}

async execute(input: SeederOptions = {}) : Promise<SeederEntity[]> {
const queryRunner = this.dataSource.createQueryRunner();
await this.createTableIfNotExist(queryRunner);

const options = await this.buildOptions(input);
if (!options.seeds || options.seeds.length === 0) {
return [];
Expand All @@ -46,10 +43,22 @@ export class SeederExecutor {
options.seeds,
this.options.root,
);

const existing = await this.loadExisting(queryRunner);
const all = await this.buildEntities(seederElements);

let tracking = !!options.seedTracking;
if (!tracking) {
tracking = all.some((seed) => seed.trackExecution());
}

let queryRunner : QueryRunner | undefined;
let existing : SeederEntity[] = [];

if (tracking) {
queryRunner = this.dataSource.createQueryRunner();
await this.createTableIfNotExist(queryRunner);
existing = await this.loadExisting(queryRunner);
}

const isMatch = (seed: SeederEntity) : boolean => {
if (!options.seedName) {
return true;
Expand Down Expand Up @@ -86,7 +95,9 @@ export class SeederExecutor {
});

if (pending.length === 0) {
await queryRunner.release();
if (queryRunner) {
await queryRunner.release();
}

return [];
}
Expand All @@ -111,7 +122,7 @@ export class SeederExecutor {

pending[i].result = await seeder.run(this.dataSource, factoryManager);

if (options.seedTracking || pending[i].trackExecution()) {
if (queryRunner && (options.seedTracking || pending[i].trackExecution())) {
await this.track(queryRunner, pending[i]);
}

Expand All @@ -122,7 +133,9 @@ export class SeederExecutor {
executed.push(pending[i]);
}
} finally {
await queryRunner.release();
if (queryRunner) {
await queryRunner.release();
}
}

return executed;
Expand Down

0 comments on commit 379c296

Please sign in to comment.