Skip to content

Commit

Permalink
refactor: drop promise utils (#6746)
Browse files Browse the repository at this point in the history
* stop using PromiseUtils.create & extractValue as they're doing nothing

because we never use PromiseUtils.create, PromiseUtils.extract was technically
never used either - the only case we were using this was in a test
where we can replace it with Promise.resolve

* stop using PromiseUtils.settle in test 1014

there was no reason to use this call in the test
as it was not using the results and only used the `Promise.all`
functionality

* use Promise.all instead of PromiseUtils.runInSequence in tests

in these cases of PromiseUtils.runInSequence in tests there was no need
for us to be running them in sequence - so instead we could use Promise.all
& Array.map for a replacement.  removes the dependency on PromiseUtils &
also speeds up our tests

* run tests sequentially for those that deal with ActiveRecord

because the activerecord mechanism creates a "global" scope through
the class that ActiveRecord is applied to we have to run through the
connections sequentially or end up with them being all over the place
as far as what activerecord model is connected to what connection

* use standard async/await + for/of instead of runInSequence

in cases where actual order of the runs matter we can do for/of
and then await any of the results - because none of the usages of
runInSequence that rely on the correct order actually use the results

* use Promise.all on runInSequence cases where order doesn't matter

* drop PromiseUtils altogether

* sequentially run when dealing with QueryRunner

queryrunner is not 'thread-safe' or async safe

* drop the test to lookup by Promise

before, the test wasn't validating that you could lookup by promise
the test was verifying that if you used something that wasn't a promise
but instead had a magic __value__ variable you'd get a lookup

that's not a promise, unfortunately

I can't find that a promise may be passed into the find options anywhere
in the documentation so I've removed this test
  • Loading branch information
imnotjames committed Sep 21, 2020
1 parent f2356dc commit 1b29591
Show file tree
Hide file tree
Showing 37 changed files with 419 additions and 439 deletions.
6 changes: 4 additions & 2 deletions src/connection/Connection.ts
Expand Up @@ -36,7 +36,6 @@ import {EntitySchema} from "../";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
import {ObjectUtils} from "../util/ObjectUtils";
import {PromiseUtils} from "../";
import {IsolationLevel} from "../driver/types/IsolationLevel";
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";
import {DriverUtils} from "../driver/DriverUtils";
Expand Down Expand Up @@ -268,7 +267,10 @@ export class Connection {
if (metadata.database && databases.indexOf(metadata.database) === -1)
databases.push(metadata.database);
});
await PromiseUtils.runInSequence(databases, database => queryRunner.clearDatabase(database));

for (const database of databases) {
await queryRunner.clearDatabase(database);
}
} else {
await queryRunner.clearDatabase();
}
Expand Down
14 changes: 10 additions & 4 deletions src/driver/aurora-data-api/AuroraDataApiQueryRunner.ts
Expand Up @@ -16,7 +16,7 @@ import {TableIndexOptions} from "../../schema-builder/options/TableIndexOptions"
import {TableUnique} from "../../schema-builder/table/TableUnique";
import {BaseQueryRunner} from "../../query-runner/BaseQueryRunner";
import {Broadcaster} from "../../subscriber/Broadcaster";
import {ColumnType, PromiseUtils} from "../../index";
import {ColumnType} from "../../index";
import {TableCheck} from "../../schema-builder/table/TableCheck";
import {IsolationLevel} from "../types/IsolationLevel";
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
Expand Down Expand Up @@ -469,7 +469,9 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu
* Creates a new columns from the column in the table.
*/
async addColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.addColumn(tableOrName, column));
for (const column of columns) {
await this.addColumn(tableOrName, column);
}
}

/**
Expand Down Expand Up @@ -682,7 +684,9 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu
* Changes a column in the table.
*/
async changeColumns(tableOrName: Table|string, changedColumns: { newColumn: TableColumn, oldColumn: TableColumn }[]): Promise<void> {
await PromiseUtils.runInSequence(changedColumns, changedColumn => this.changeColumn(tableOrName, changedColumn.oldColumn, changedColumn.newColumn));
for (const {oldColumn, newColumn} of changedColumns) {
await this.changeColumn(tableOrName, oldColumn, newColumn)
}
}

/**
Expand Down Expand Up @@ -774,7 +778,9 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.dropColumn(tableOrName, column));
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
}

/**
Expand Down
41 changes: 30 additions & 11 deletions src/driver/cockroachdb/CockroachQueryRunner.ts
Expand Up @@ -17,7 +17,6 @@ import {TableIndexOptions} from "../../schema-builder/options/TableIndexOptions"
import {TableUnique} from "../../schema-builder/table/TableUnique";
import {BaseQueryRunner} from "../../query-runner/BaseQueryRunner";
import {OrmUtils} from "../../util/OrmUtils";
import {PromiseUtils} from "../../";
import {TableCheck} from "../../schema-builder/table/TableCheck";
import {ColumnType} from "../../index";
import {IsolationLevel} from "../types/IsolationLevel";
Expand Down Expand Up @@ -159,7 +158,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
} catch (e) {
if (e.code === "40001") {
await this.query("ROLLBACK TO SAVEPOINT cockroach_restart");
await PromiseUtils.runInSequence(this.queries, q => this.query(q.query, q.parameters));
for (const q of this.queries) {
await this.query(q.query, q.parameters);
}
await this.commitTransaction();
}
}
Expand Down Expand Up @@ -594,7 +595,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Creates a new columns from the column in the table.
*/
async addColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.addColumn(tableOrName, column));
for (const column of columns) {
await this.addColumn(tableOrName, column);
}
}

/**
Expand Down Expand Up @@ -844,7 +847,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Changes a column in the table.
*/
async changeColumns(tableOrName: Table|string, changedColumns: { newColumn: TableColumn, oldColumn: TableColumn }[]): Promise<void> {
await PromiseUtils.runInSequence(changedColumns, changedColumn => this.changeColumn(tableOrName, changedColumn.oldColumn, changedColumn.newColumn));
for (const {oldColumn, newColumn} of changedColumns) {
await this.changeColumn(tableOrName, oldColumn, newColumn);
}
}

/**
Expand Down Expand Up @@ -923,7 +928,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.dropColumn(tableOrName, column));
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
}

/**
Expand Down Expand Up @@ -1014,7 +1021,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Creates new unique constraints.
*/
async createUniqueConstraints(tableOrName: Table|string, uniqueConstraints: TableUnique[]): Promise<void> {
await PromiseUtils.runInSequence(uniqueConstraints, uniqueConstraint => this.createUniqueConstraint(tableOrName, uniqueConstraint));
for (const uniqueConstraint of uniqueConstraints) {
await this.createUniqueConstraint(tableOrName, uniqueConstraint);
}
}

/**
Expand All @@ -1038,7 +1047,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Drops unique constraints.
*/
async dropUniqueConstraints(tableOrName: Table|string, uniqueConstraints: TableUnique[]): Promise<void> {
await PromiseUtils.runInSequence(uniqueConstraints, uniqueConstraint => this.dropUniqueConstraint(tableOrName, uniqueConstraint));
for (const uniqueConstraint of uniqueConstraints) {
await this.dropUniqueConstraint(tableOrName, uniqueConstraint);
}
}

/**
Expand Down Expand Up @@ -1136,7 +1147,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Creates a new foreign keys.
*/
async createForeignKeys(tableOrName: Table|string, foreignKeys: TableForeignKey[]): Promise<void> {
await PromiseUtils.runInSequence(foreignKeys, foreignKey => this.createForeignKey(tableOrName, foreignKey));
for (const foreignKey of foreignKeys) {
await this.createForeignKey(tableOrName, foreignKey);
}
}

/**
Expand All @@ -1158,7 +1171,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Drops a foreign keys from the table.
*/
async dropForeignKeys(tableOrName: Table|string, foreignKeys: TableForeignKey[]): Promise<void> {
await PromiseUtils.runInSequence(foreignKeys, foreignKey => this.dropForeignKey(tableOrName, foreignKey));
for (const foreignKey of foreignKeys) {
await this.dropForeignKey(tableOrName, foreignKey);
}
}

/**
Expand Down Expand Up @@ -1196,7 +1211,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Creates a new indices
*/
async createIndices(tableOrName: Table|string, indices: TableIndex[]): Promise<void> {
await PromiseUtils.runInSequence(indices, index => this.createIndex(tableOrName, index));
for (const index of indices) {
await this.createIndex(tableOrName, index);
}
}

/**
Expand All @@ -1218,7 +1235,9 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
* Drops an indices from the table.
*/
async dropIndices(tableOrName: Table|string, indices: TableIndex[]): Promise<void> {
await PromiseUtils.runInSequence(indices, index => this.dropIndex(tableOrName, index));
for (const index of indices) {
await this.dropIndex(tableOrName, index);
}
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/driver/mysql/MysqlQueryRunner.ts
Expand Up @@ -17,7 +17,7 @@ import {TableIndexOptions} from "../../schema-builder/options/TableIndexOptions"
import {TableUnique} from "../../schema-builder/table/TableUnique";
import {BaseQueryRunner} from "../../query-runner/BaseQueryRunner";
import {Broadcaster} from "../../subscriber/Broadcaster";
import {ColumnType, PromiseUtils} from "../../index";
import {ColumnType} from "../../index";
import {TableCheck} from "../../schema-builder/table/TableCheck";
import {IsolationLevel} from "../types/IsolationLevel";
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
Expand Down Expand Up @@ -519,7 +519,9 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
* Creates a new columns from the column in the table.
*/
async addColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.addColumn(tableOrName, column));
for (const column of columns) {
await this.addColumn(tableOrName, column);
}
}

/**
Expand Down Expand Up @@ -734,7 +736,9 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
* Changes a column in the table.
*/
async changeColumns(tableOrName: Table|string, changedColumns: { newColumn: TableColumn, oldColumn: TableColumn }[]): Promise<void> {
await PromiseUtils.runInSequence(changedColumns, changedColumn => this.changeColumn(tableOrName, changedColumn.oldColumn, changedColumn.newColumn));
for (const {oldColumn, newColumn} of changedColumns) {
await this.changeColumn(tableOrName, oldColumn, newColumn);
}
}

/**
Expand Down Expand Up @@ -826,7 +830,9 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.dropColumn(tableOrName, column));
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/driver/oracle/OracleQueryRunner.ts
Expand Up @@ -17,7 +17,7 @@ import {Broadcaster} from "../../subscriber/Broadcaster";
import {BaseQueryRunner} from "../../query-runner/BaseQueryRunner";
import {OrmUtils} from "../../util/OrmUtils";
import {TableCheck} from "../../schema-builder/table/TableCheck";
import {ColumnType, PromiseUtils} from "../../index";
import {ColumnType} from "../../index";
import {IsolationLevel} from "../types/IsolationLevel";
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
import {ReplicationMode} from "../types/ReplicationMode";
Expand Down Expand Up @@ -507,7 +507,9 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
* Creates a new columns from the column in the table.
*/
async addColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.addColumn(tableOrName, column));
for (const column of columns) {
await this.addColumn(tableOrName, column);
}
}

/**
Expand Down Expand Up @@ -734,7 +736,9 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
* Changes a column in the table.
*/
async changeColumns(tableOrName: Table|string, changedColumns: { newColumn: TableColumn, oldColumn: TableColumn }[]): Promise<void> {
await PromiseUtils.runInSequence(changedColumns, changedColumn => this.changeColumn(tableOrName, changedColumn.oldColumn, changedColumn.newColumn));
for (const {oldColumn, newColumn} of changedColumns) {
await this.changeColumn(tableOrName, oldColumn, newColumn);
}
}

/**
Expand Down Expand Up @@ -806,7 +810,9 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.dropColumn(tableOrName, column));
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
}

/**
Expand Down
37 changes: 27 additions & 10 deletions src/driver/postgres/PostgresQueryRunner.ts
@@ -1,4 +1,3 @@
import {PromiseUtils} from "../../";
import {ObjectLiteral} from "../../common/ObjectLiteral";
import {QueryFailedError} from "../../error/QueryFailedError";
import {QueryRunnerAlreadyReleasedError} from "../../error/QueryRunnerAlreadyReleasedError";
Expand Down Expand Up @@ -574,7 +573,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Creates a new columns from the column in the table.
*/
async addColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.addColumn(tableOrName, column));
for (const column of columns) {
await this.addColumn(tableOrName, column);
}
}

/**
Expand Down Expand Up @@ -892,7 +893,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Changes a column in the table.
*/
async changeColumns(tableOrName: Table|string, changedColumns: { newColumn: TableColumn, oldColumn: TableColumn }[]): Promise<void> {
await PromiseUtils.runInSequence(changedColumns, changedColumn => this.changeColumn(tableOrName, changedColumn.oldColumn, changedColumn.newColumn));
for (const {oldColumn, newColumn} of changedColumns) {
await this.changeColumn(tableOrName, oldColumn, newColumn);
}
}

/**
Expand Down Expand Up @@ -976,7 +979,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await PromiseUtils.runInSequence(columns, column => this.dropColumn(tableOrName, column));
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
}

/**
Expand Down Expand Up @@ -1065,7 +1070,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Creates new unique constraints.
*/
async createUniqueConstraints(tableOrName: Table|string, uniqueConstraints: TableUnique[]): Promise<void> {
await PromiseUtils.runInSequence(uniqueConstraints, uniqueConstraint => this.createUniqueConstraint(tableOrName, uniqueConstraint));
for (const uniqueConstraint of uniqueConstraints) {
await this.createUniqueConstraint(tableOrName, uniqueConstraint);
}
}

/**
Expand All @@ -1087,7 +1094,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Drops unique constraints.
*/
async dropUniqueConstraints(tableOrName: Table|string, uniqueConstraints: TableUnique[]): Promise<void> {
await PromiseUtils.runInSequence(uniqueConstraints, uniqueConstraint => this.dropUniqueConstraint(tableOrName, uniqueConstraint));
for (const uniqueConstraint of uniqueConstraints) {
await this.dropUniqueConstraint(tableOrName, uniqueConstraint);
}
}

/**
Expand Down Expand Up @@ -1204,7 +1213,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Creates a new foreign keys.
*/
async createForeignKeys(tableOrName: Table|string, foreignKeys: TableForeignKey[]): Promise<void> {
await PromiseUtils.runInSequence(foreignKeys, foreignKey => this.createForeignKey(tableOrName, foreignKey));
for (const foreignKey of foreignKeys) {
await this.createForeignKey(tableOrName, foreignKey);
}
}

/**
Expand All @@ -1226,7 +1237,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Drops a foreign keys from the table.
*/
async dropForeignKeys(tableOrName: Table|string, foreignKeys: TableForeignKey[]): Promise<void> {
await PromiseUtils.runInSequence(foreignKeys, foreignKey => this.dropForeignKey(tableOrName, foreignKey));
for (const foreignKey of foreignKeys) {
await this.dropForeignKey(tableOrName, foreignKey);
}
}

/**
Expand All @@ -1249,7 +1262,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Creates a new indices
*/
async createIndices(tableOrName: Table|string, indices: TableIndex[]): Promise<void> {
await PromiseUtils.runInSequence(indices, index => this.createIndex(tableOrName, index));
for (const index of indices) {
await this.createIndex(tableOrName, index);
}
}

/**
Expand All @@ -1271,7 +1286,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Drops an indices from the table.
*/
async dropIndices(tableOrName: Table|string, indices: TableIndex[]): Promise<void> {
await PromiseUtils.runInSequence(indices, index => this.dropIndex(tableOrName, index));
for (const index of indices) {
await this.dropIndex(tableOrName, index);
}
}

/**
Expand Down

0 comments on commit 1b29591

Please sign in to comment.