Skip to content

Commit

Permalink
fix: dropIndex now works when providing a tableIndex without name (#8937
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Thellior authored Aug 24, 2022
1 parent 64674e6 commit de8aaac
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 54 deletions.
10 changes: 4 additions & 6 deletions src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1792,12 +1792,7 @@ export class AuroraMysqlQueryRunner
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.createIndexSql(table, index)
const down = this.dropIndexSql(table, index)
Expand Down Expand Up @@ -1836,6 +1831,9 @@ export class AuroraMysqlQueryRunner
`Supplied index ${indexOrName} was not found in table ${table.name}`,
)

// old index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(table, index)
const down = this.createIndexSql(table, index)
await this.executeQueries(up, down)
Expand Down
10 changes: 4 additions & 6 deletions src/driver/cockroachdb/CockroachQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2348,12 +2348,7 @@ export class CockroachQueryRunner
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

// CockroachDB stores unique indices and UNIQUE constraints
if (index.isUnique) {
Expand Down Expand Up @@ -2405,6 +2400,9 @@ export class CockroachQueryRunner
`Supplied index ${indexOrName} was not found in table ${table.name}`,
)

// old index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(table, index)
const down = this.createIndexSql(table, index)
await this.executeQueries(up, down)
Expand Down
10 changes: 4 additions & 6 deletions src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2099,12 +2099,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.createIndexSql(table, index)
const down = this.dropIndexSql(table, index)
Expand Down Expand Up @@ -2143,6 +2138,9 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
`Supplied index ${indexOrName} was not found in table ${table.name}`,
)

// old index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(table, index)
const down = this.createIndexSql(table, index)
await this.executeQueries(up, down)
Expand Down
9 changes: 3 additions & 6 deletions src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2109,12 +2109,7 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.createIndexSql(table, index)
const down = this.dropIndexSql(index)
Expand Down Expand Up @@ -2152,6 +2147,8 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
throw new TypeORMError(
`Supplied index ${indexOrName} was not found in table ${table.name}`,
)
// old index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(index)
const down = this.createIndexSql(table, index)
Expand Down
9 changes: 3 additions & 6 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2899,12 +2899,7 @@ export class PostgresQueryRunner
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.createIndexSql(table, index)
const down = this.dropIndexSql(table, index)
Expand Down Expand Up @@ -2941,6 +2936,8 @@ export class PostgresQueryRunner
throw new TypeORMError(
`Supplied index ${indexOrName} was not found in table ${table.name}`,
)
// old index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(table, index)
const down = this.createIndexSql(table, index)
Expand Down
10 changes: 4 additions & 6 deletions src/driver/sap/SapQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2283,12 +2283,7 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.createIndexSql(table, index)
const down = this.dropIndexSql(table, index)
Expand Down Expand Up @@ -2327,6 +2322,9 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
`Supplied index ${indexOrName} was not found in table ${table.name}`,
)

// old index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(table, index)
const down = this.createIndexSql(table, index)
await this.executeQueries(up, down)
Expand Down
10 changes: 4 additions & 6 deletions src/driver/spanner/SpannerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1333,12 +1333,7 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner {
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.createIndexSql(table, index)
const down = this.dropIndexSql(table, index)
Expand Down Expand Up @@ -1378,6 +1373,9 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner {
`Supplied index ${indexOrName} was not found in table ${table.name}`,
)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(table, index)
const down = this.createIndexSql(table, index)
await this.executeQueries(up, down)
Expand Down
10 changes: 4 additions & 6 deletions src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,12 +1089,7 @@ export abstract class AbstractSqliteQueryRunner
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.createIndexSql(table, index)
const down = this.dropIndexSql(index)
Expand Down Expand Up @@ -1133,6 +1128,9 @@ export abstract class AbstractSqliteQueryRunner
`Supplied index ${indexOrName} was not found in table ${table.name}`,
)

// old index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(index)
const down = this.createIndexSql(table, index)
await this.executeQueries(up, down)
Expand Down
10 changes: 4 additions & 6 deletions src/driver/sqlserver/SqlServerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2484,12 +2484,7 @@ export class SqlServerQueryRunner
: await this.getCachedTable(tableOrName)

// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.createIndexSql(table, index)
const down = this.dropIndexSql(table, index)
Expand Down Expand Up @@ -2528,6 +2523,9 @@ export class SqlServerQueryRunner
`Supplied index was not found in table ${table.name}`,
)

// old index may be passed without name. In this case we generate index name manually.
if (!index.name) index.name = this.generateIndexName(table, index)

const up = this.dropIndexSql(table, index)
const down = this.createIndexSql(table, index)
await this.executeQueries(up, down)
Expand Down
13 changes: 13 additions & 0 deletions src/query-runner/BaseQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PostgresConnectionOptions } from "../driver/postgres/PostgresConnection
import { Query } from "../driver/Query"
import { SqlInMemory } from "../driver/SqlInMemory"
import { SqlServerConnectionOptions } from "../driver/sqlserver/SqlServerConnectionOptions"
import { TableIndex } from "../schema-builder/table/TableIndex"
import { View } from "../schema-builder/view/View"
import { DataSource } from "../data-source/DataSource"
import { Table } from "../schema-builder/table/Table"
Expand Down Expand Up @@ -650,4 +651,16 @@ export abstract class BaseQueryRunner {
await this.query(query, parameters)
}
}

/**
* Generated an index name for a table and index
*/
protected generateIndexName(table: Table, index: TableIndex): string {
// new index may be passed without name. In this case we generate index name manually.
return this.connection.namingStrategy.indexName(
table,
index.columnNames,
index.where,
)
}
}
16 changes: 16 additions & 0 deletions test/github-issues/8936/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Column, Entity, PrimaryColumn } from "../../../../src"

@Entity()
export class User {
@PrimaryColumn({ nullable: false })
id: number

@Column()
firstName: string

@Column()
lastName: string

@Column()
github: string
}
56 changes: 56 additions & 0 deletions test/github-issues/8936/issue-8936.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import "../../utils/test-setup"
import {
QueryFailedError,
QueryRunner,
Repository,
TableIndex,
} from "../../../src"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { Connection } from "../../../src/connection/Connection"
import { User } from "./entity/User"

describe("github issues > #8936 DropIndex with a TableIndex without name is not working", () => {
let connections: Connection[]

const tableIndex: TableIndex = new TableIndex({
columnNames: ["firstName", "lastName"],
isUnique: true,
})

before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should drop the index as expected", () => {
// Create a clone because the createIndex will set the name
const dropTableIndex: TableIndex = tableIndex.clone()

return Promise.all(
connections.map(async (connection) => {
const queryRunner: QueryRunner = connection.createQueryRunner()
const userRepository: Repository<User> =
connection.getRepository(User)
const tableName: string = userRepository.metadata.tableName

// Create the index so it exists when we delete it
await queryRunner.createIndex(tableName, tableIndex)

// Drop the index expecting it not to raise QueryFailed
await queryRunner
.dropIndex(tableName, dropTableIndex)
.should.not.be.rejectedWith(QueryFailedError)
}),
)
})
})

0 comments on commit de8aaac

Please sign in to comment.