From c81b405a365067e9ab8854317a659f99df9830b4 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Fri, 4 Sep 2020 12:37:04 +0200 Subject: [PATCH] fix: Unnecessary migrations for fulltext indices (#6634) * fix: Unnecessary migrations for fulltext indices Fixes #6633 (see issue for root cause explanation) * test: Enable all tests * refactor: Add `isFullTextColumnTypeSupported()` method to Driver interface * fix: Include isFullTextColumnTypeSupported method in SqlServerDriver --- src/driver/Driver.ts | 5 ++++ .../aurora-data-api/AuroraDataApiDriver.ts | 7 +++++ src/driver/cockroachdb/CockroachDriver.ts | 7 +++++ src/driver/mongodb/MongoDriver.ts | 7 +++++ src/driver/mysql/MysqlDriver.ts | 7 +++++ src/driver/oracle/OracleDriver.ts | 7 +++++ src/driver/postgres/PostgresDriver.ts | 7 +++++ src/driver/sap/SapDriver.ts | 7 +++++ .../sqlite-abstract/AbstractSqliteDriver.ts | 7 +++++ src/driver/sqlserver/SqlServerDriver.ts | 7 +++++ src/schema-builder/RdbmsSchemaBuilder.ts | 2 +- .../options/TableIndexOptions.ts | 6 ++-- test/github-issues/6633/entity/Test.ts | 11 +++++++ test/github-issues/6633/issue-6633.ts | 29 +++++++++++++++++++ 14 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 test/github-issues/6633/entity/Test.ts create mode 100644 test/github-issues/6633/issue-6633.ts diff --git a/src/driver/Driver.ts b/src/driver/Driver.ts index 4041f58693..3027e4afaf 100644 --- a/src/driver/Driver.ts +++ b/src/driver/Driver.ts @@ -193,6 +193,11 @@ export interface Driver { */ isUUIDGenerationSupported(): boolean; + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean; + /** * Creates an escaped parameter. */ diff --git a/src/driver/aurora-data-api/AuroraDataApiDriver.ts b/src/driver/aurora-data-api/AuroraDataApiDriver.ts index 901ff22435..e85e545aa2 100644 --- a/src/driver/aurora-data-api/AuroraDataApiDriver.ts +++ b/src/driver/aurora-data-api/AuroraDataApiDriver.ts @@ -750,6 +750,13 @@ export class AuroraDataApiDriver implements Driver { return false; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return true; + } + /** * Creates an escaped parameter. */ diff --git a/src/driver/cockroachdb/CockroachDriver.ts b/src/driver/cockroachdb/CockroachDriver.ts index 3347250361..8cacf57118 100644 --- a/src/driver/cockroachdb/CockroachDriver.ts +++ b/src/driver/cockroachdb/CockroachDriver.ts @@ -649,6 +649,13 @@ export class CockroachDriver implements Driver { return true; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return false; + } + /** * Creates an escaped parameter. */ diff --git a/src/driver/mongodb/MongoDriver.ts b/src/driver/mongodb/MongoDriver.ts index 7106532467..f1d1b190e2 100644 --- a/src/driver/mongodb/MongoDriver.ts +++ b/src/driver/mongodb/MongoDriver.ts @@ -389,6 +389,13 @@ export class MongoDriver implements Driver { return false; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return false; + } + /** * Creates an escaped parameter. */ diff --git a/src/driver/mysql/MysqlDriver.ts b/src/driver/mysql/MysqlDriver.ts index 291beaa68f..d55a960ef4 100644 --- a/src/driver/mysql/MysqlDriver.ts +++ b/src/driver/mysql/MysqlDriver.ts @@ -798,6 +798,13 @@ export class MysqlDriver implements Driver { return false; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return true; + } + /** * Creates an escaped parameter. */ diff --git a/src/driver/oracle/OracleDriver.ts b/src/driver/oracle/OracleDriver.ts index 134ed6bba7..42575f6108 100644 --- a/src/driver/oracle/OracleDriver.ts +++ b/src/driver/oracle/OracleDriver.ts @@ -623,6 +623,13 @@ export class OracleDriver implements Driver { return false; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return false; + } + /** * Creates an escaped parameter. */ diff --git a/src/driver/postgres/PostgresDriver.ts b/src/driver/postgres/PostgresDriver.ts index e9eae242c6..59684c74bf 100644 --- a/src/driver/postgres/PostgresDriver.ts +++ b/src/driver/postgres/PostgresDriver.ts @@ -912,6 +912,13 @@ export class PostgresDriver implements Driver { return true; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return false; + } + get uuidGenerator(): string { return this.options.uuidExtension === "pgcrypto" ? "gen_random_uuid()" : "uuid_generate_v4()"; } diff --git a/src/driver/sap/SapDriver.ts b/src/driver/sap/SapDriver.ts index 9479277503..85b6a7f2f2 100644 --- a/src/driver/sap/SapDriver.ts +++ b/src/driver/sap/SapDriver.ts @@ -624,6 +624,13 @@ export class SapDriver implements Driver { return false; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return true; + } + /** * Creates an escaped parameter. */ diff --git a/src/driver/sqlite-abstract/AbstractSqliteDriver.ts b/src/driver/sqlite-abstract/AbstractSqliteDriver.ts index dc406c7bb7..79fd5ef7f8 100644 --- a/src/driver/sqlite-abstract/AbstractSqliteDriver.ts +++ b/src/driver/sqlite-abstract/AbstractSqliteDriver.ts @@ -594,6 +594,13 @@ export abstract class AbstractSqliteDriver implements Driver { return false; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return false; + } + /** * Creates an escaped parameter. */ diff --git a/src/driver/sqlserver/SqlServerDriver.ts b/src/driver/sqlserver/SqlServerDriver.ts index 20e7f9e11d..5954431de0 100644 --- a/src/driver/sqlserver/SqlServerDriver.ts +++ b/src/driver/sqlserver/SqlServerDriver.ts @@ -640,6 +640,13 @@ export class SqlServerDriver implements Driver { return true; } + /** + * Returns true if driver supports fulltext indices. + */ + isFullTextColumnTypeSupported(): boolean { + return false; + } + /** * Creates an escaped parameter. */ diff --git a/src/schema-builder/RdbmsSchemaBuilder.ts b/src/schema-builder/RdbmsSchemaBuilder.ts index 2c625c9a6e..d5bd9f2aa0 100644 --- a/src/schema-builder/RdbmsSchemaBuilder.ts +++ b/src/schema-builder/RdbmsSchemaBuilder.ts @@ -277,7 +277,7 @@ export class RdbmsSchemaBuilder implements SchemaBuilder { if (indexMetadata.isSpatial !== tableIndex.isSpatial) return true; - if (indexMetadata.isFulltext !== tableIndex.isFulltext) + if (this.connection.driver.isFullTextColumnTypeSupported() && indexMetadata.isFulltext !== tableIndex.isFulltext) return true; if (indexMetadata.columns.length !== tableIndex.columnNames.length) diff --git a/src/schema-builder/options/TableIndexOptions.ts b/src/schema-builder/options/TableIndexOptions.ts index c75228e2fd..b37be3d790 100644 --- a/src/schema-builder/options/TableIndexOptions.ts +++ b/src/schema-builder/options/TableIndexOptions.ts @@ -30,10 +30,10 @@ export interface TableIndexOptions { /** * The FULLTEXT modifier indexes the entire column and does not allow prefixing. - * Works only in MySQL. + * Supported only in MySQL & SAP HANA. */ isFulltext?: boolean; - + /** * Fulltext parser. * Works only in MySQL. @@ -45,4 +45,4 @@ export interface TableIndexOptions { */ where?: string; -} \ No newline at end of file +} diff --git a/test/github-issues/6633/entity/Test.ts b/test/github-issues/6633/entity/Test.ts new file mode 100644 index 0000000000..61443c6dcd --- /dev/null +++ b/test/github-issues/6633/entity/Test.ts @@ -0,0 +1,11 @@ +import { Entity, PrimaryColumn, Index, Column } from "../../../../src"; + +@Entity() +export class Test { + @PrimaryColumn() + id: number; + + @Index("description_index", { fulltext: true }) + @Column() + description: string; +} diff --git a/test/github-issues/6633/issue-6633.ts b/test/github-issues/6633/issue-6633.ts new file mode 100644 index 0000000000..31df86b5b8 --- /dev/null +++ b/test/github-issues/6633/issue-6633.ts @@ -0,0 +1,29 @@ +import "reflect-metadata"; +import { expect } from "chai"; +import { Connection } from "../../../src"; +import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; +import { Post } from "../4440/entity/Post"; + +describe("github issues > #6633 Fulltext indices continually dropped & re-created", () => { + + let connections: Connection[]; + before(async () => { + connections = await createTestingConnections({ + entities: [Post], + schemaCreate: true, + dropSchema: true + }); + }); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("should not create migrations for fulltext indices", () => + Promise.all(connections.map(async (connection) => { + const sqlInMemory = await connection.driver.createSchemaBuilder().log(); + + expect(sqlInMemory.upQueries).to.eql([]); + expect(sqlInMemory.downQueries).to.eql([]); + } + )) + ); +});