Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support to string array on dropColumns #7654

Merged
merged 3 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,11 @@ Drops a column in the table.
---

```ts
dropColumns(table: Table|string, columns: TableColumn[]): Promise<void>
dropColumns(table: Table|string, columns: TableColumn[]|string[]): Promise<void>
```

- `table` - Table object or name
- `columns` - array of TableColumn objects to be dropped
- `columns` - array of TableColumn objects or column names to be dropped

Drops a columns in the table.

Expand Down
2 changes: 1 addition & 1 deletion docs/zh_CN/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ dropColumn(table: Table|string, column: TableColumn|string): Promise<void>
---

```ts
dropColumns(table: Table|string, columns: TableColumn[]): Promise<void>
dropColumns(table: Table|string, columns: TableColumn[]|string[]): Promise<void>
```

- `table` - 表对象或名称
Expand Down
2 changes: 1 addition & 1 deletion src/driver/aurora-data-api/AuroraDataApiQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table|string, columns: TableColumn[]|string[]): Promise<void> {
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
Expand Down
2 changes: 1 addition & 1 deletion src/driver/cockroachdb/CockroachQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table|string, columns: TableColumn[]|string[]): Promise<void> {
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
Expand Down
2 changes: 1 addition & 1 deletion src/driver/mongodb/MongoQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ export class MongoQueryRunner implements QueryRunner {
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table | string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table | string, columns: TableColumn[] | string[]): Promise<void> {
throw new Error(`Schema update queries are not supported by MongoDB driver.`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table|string, columns: TableColumn[]|string[]): Promise<void> {
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
Expand Down
2 changes: 1 addition & 1 deletion src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table|string, columns: TableColumn[]|string[]): Promise<void> {
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
Expand Down
2 changes: 1 addition & 1 deletion src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table|string, columns: TableColumn[]|string[]): Promise<void> {
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
Expand Down
2 changes: 1 addition & 1 deletion src/driver/sap/SapQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table|string, columns: TableColumn[]|string[]): Promise<void> {
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
Expand Down
24 changes: 10 additions & 14 deletions src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,27 +456,23 @@ export abstract class AbstractSqliteQueryRunner extends BaseQueryRunner implemen
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table|string, columns: TableColumn[]|string[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);

// clone original table and remove column and its constraints from cloned table
const changedTable = table.clone();
columns.forEach(column => {
changedTable.removeColumn(column);
changedTable.findColumnUniques(column).forEach(unique => changedTable.removeUniqueConstraint(unique));
changedTable.findColumnIndices(column).forEach(index => changedTable.removeIndex(index));
changedTable.findColumnForeignKeys(column).forEach(fk => changedTable.removeForeignKey(fk));
columns.forEach((column: TableColumn|string) => {
const columnInstance = column instanceof TableColumn ? column : table.findColumnByName(column);
if (!columnInstance)
throw new Error(`Column "${column}" was not found in table "${table.name}"`);

changedTable.removeColumn(columnInstance);
changedTable.findColumnUniques(columnInstance).forEach(unique => changedTable.removeUniqueConstraint(unique));
changedTable.findColumnIndices(columnInstance).forEach(index => changedTable.removeIndex(index));
changedTable.findColumnForeignKeys(columnInstance).forEach(fk => changedTable.removeForeignKey(fk));
});

await this.recreateTable(changedTable, table);

// remove column and its constraints from original table.
columns.forEach(column => {
table.removeColumn(column);
table.findColumnUniques(column).forEach(unique => table.removeUniqueConstraint(unique));
table.findColumnIndices(column).forEach(index => table.removeIndex(index));
table.findColumnForeignKeys(column).forEach(fk => table.removeForeignKey(fk));
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/driver/sqlserver/SqlServerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ export class SqlServerQueryRunner extends BaseQueryRunner implements QueryRunner
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
async dropColumns(tableOrName: Table|string, columns: TableColumn[]|string[]): Promise<void> {
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
Expand Down
2 changes: 1 addition & 1 deletion src/query-runner/QueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export interface QueryRunner {
/**
* Drops columns in the table.
*/
dropColumns(table: Table|string, columns: TableColumn[]): Promise<void>;
dropColumns(table: Table|string, columns: TableColumn[]|string[]): Promise<void>;

/**
* Creates a new primary key.
Expand Down
125 changes: 86 additions & 39 deletions test/functional/query-runner/drop-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,94 @@ describe("query runner > drop column", () => {
});
after(() => closeTestingConnections(connections));

it("should correctly drop column and revert drop", () => Promise.all(connections.map(async connection => {

const queryRunner = connection.createQueryRunner();

let table = await queryRunner.getTable("post");
const idColumn = table!.findColumnByName("id")!;
const nameColumn = table!.findColumnByName("name")!;
const versionColumn = table!.findColumnByName("version")!;
idColumn!.should.be.exist;
nameColumn!.should.be.exist;
versionColumn!.should.be.exist;

// better-sqlite3 seems not able to create a check constraint on a non-existing column
if (connection.name === "better-sqlite3") {
await queryRunner.dropCheckConstraints(table!, table!.checks);
}

// In Sqlite 'dropColumns' method is more optimal than 'dropColumn', because it recreate table just once,
// without all removed columns. In other drivers it's no difference between these methods, because 'dropColumns'
// calls 'dropColumn' method for each removed column.
// CockroachDB does not support changing pk.
if (connection.driver instanceof CockroachDriver) {
await queryRunner.dropColumns(table!, [nameColumn, versionColumn]);
} else {
await queryRunner.dropColumns(table!, [idColumn, nameColumn, versionColumn]);
}

table = await queryRunner.getTable("post");
expect(table!.findColumnByName("name")).to.be.undefined;
expect(table!.findColumnByName("version")).to.be.undefined;
if (!(connection.driver instanceof CockroachDriver))
expect(table!.findColumnByName("id")).to.be.undefined;
describe("when columns are instances of TableColumn", () => {
it("should correctly drop column and revert drop", () => Promise.all(connections.map(async connection => {

const queryRunner = connection.createQueryRunner();

let table = await queryRunner.getTable("post");
const idColumn = table!.findColumnByName("id")!;
const nameColumn = table!.findColumnByName("name")!;
const versionColumn = table!.findColumnByName("version")!;
idColumn!.should.be.exist;
nameColumn!.should.be.exist;
versionColumn!.should.be.exist;

// better-sqlite3 seems not able to create a check constraint on a non-existing column
if (connection.name === "better-sqlite3") {
await queryRunner.dropCheckConstraints(table!, table!.checks);
}

// In Sqlite 'dropColumns' method is more optimal than 'dropColumn', because it recreate table just once,
// without all removed columns. In other drivers it's no difference between these methods, because 'dropColumns'
// calls 'dropColumn' method for each removed column.
// CockroachDB does not support changing pk.
if (connection.driver instanceof CockroachDriver) {
await queryRunner.dropColumns(table!, [nameColumn, versionColumn]);
} else {
await queryRunner.dropColumns(table!, [idColumn, nameColumn, versionColumn]);
}

table = await queryRunner.getTable("post");
expect(table!.findColumnByName("name")).to.be.undefined;
expect(table!.findColumnByName("version")).to.be.undefined;
if (!(connection.driver instanceof CockroachDriver))
expect(table!.findColumnByName("id")).to.be.undefined;

await queryRunner.executeMemoryDownSql();

table = await queryRunner.getTable("post");
table!.findColumnByName("id")!.should.be.exist;
table!.findColumnByName("name")!.should.be.exist;
table!.findColumnByName("version")!.should.be.exist;

await queryRunner.release();
})));
});

await queryRunner.executeMemoryDownSql();
describe("when columns are strings", () => {
it("should correctly drop column and revert drop", () => Promise.all(connections.map(async connection => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test to affirm that trying to drop a non-existent column emits an error


table = await queryRunner.getTable("post");
table!.findColumnByName("id")!.should.be.exist;
table!.findColumnByName("name")!.should.be.exist;
table!.findColumnByName("version")!.should.be.exist;
const queryRunner = connection.createQueryRunner();

let table = await queryRunner.getTable("post");
const idColumn = table!.findColumnByName("id")!;
const nameColumn = table!.findColumnByName("name")!;
const versionColumn = table!.findColumnByName("version")!;
idColumn!.should.be.exist;
nameColumn!.should.be.exist;
versionColumn!.should.be.exist;

// better-sqlite3 seems not able to create a check constraint on a non-existing column
if (connection.name === "better-sqlite3") {
await queryRunner.dropCheckConstraints(table!, table!.checks);
}

// In Sqlite 'dropColumns' method is more optimal than 'dropColumn', because it recreate table just once,
// without all removed columns. In other drivers it's no difference between these methods, because 'dropColumns'
// calls 'dropColumn' method for each removed column.
// CockroachDB does not support changing pk.
if (connection.driver instanceof CockroachDriver) {
await queryRunner.dropColumns(table!, ["name", "version"]);
} else {
await queryRunner.dropColumns(table!, ["id", "name", "version"]);
}

table = await queryRunner.getTable("post");
expect(table!.findColumnByName("name")).to.be.undefined;
expect(table!.findColumnByName("version")).to.be.undefined;
if (!(connection.driver instanceof CockroachDriver))
expect(table!.findColumnByName("id")).to.be.undefined;

await queryRunner.executeMemoryDownSql();

await queryRunner.release();
})));
table = await queryRunner.getTable("post");
table!.findColumnByName("id")!.should.be.exist;
table!.findColumnByName("name")!.should.be.exist;
table!.findColumnByName("version")!.should.be.exist;

await queryRunner.release();
})));
});

});