Skip to content

Commit

Permalink
Add storage engine index Type support to MySQL index() and unique() s…
Browse files Browse the repository at this point in the history
…chema (#4756)

Co-authored-by: Elan Ruusamäe <glen@delfi.ee>
  • Loading branch information
OlivierCavadenti and glensc committed Oct 20, 2021
1 parent 792d3be commit d463284
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
20 changes: 17 additions & 3 deletions lib/dialects/mysql/schema/mysql-tablecompiler.js
Expand Up @@ -212,13 +212,23 @@ class TableCompiler_MySQL extends TableCompiler {
}

index(columns, indexName, indexType) {
let storageEngineIndexType;
if (isObject(indexName)) {
({ indexName, storageEngineIndexType } = indexName);
}

indexName = indexName
? this.formatter.wrap(indexName)
: this._indexCommand('index', this.tableNameRaw, columns);
storageEngineIndexType = storageEngineIndexType
? ` using ${storageEngineIndexType}`
: '';
this.pushQuery(
`alter table ${this.tableName()} add${
indexType ? ` ${indexType}` : ''
} index ${indexName}(${this.formatter.columnize(columns)})`
} index ${indexName}(${this.formatter.columnize(
columns
)})${storageEngineIndexType}`
);
}

Expand All @@ -243,9 +253,10 @@ class TableCompiler_MySQL extends TableCompiler {
}

unique(columns, indexName) {
let storageEngineIndexType;
let deferrable;
if (isObject(indexName)) {
({ indexName, deferrable } = indexName);
({ indexName, deferrable, storageEngineIndexType } = indexName);
}
if (deferrable && deferrable !== 'not deferrable') {
this.client.logger.warn(
Expand All @@ -255,10 +266,13 @@ class TableCompiler_MySQL extends TableCompiler {
indexName = indexName
? this.formatter.wrap(indexName)
: this._indexCommand('unique', this.tableNameRaw, columns);
storageEngineIndexType = storageEngineIndexType
? ` using ${storageEngineIndexType}`
: '';
this.pushQuery(
`alter table ${this.tableName()} add unique ${indexName}(${this.formatter.columnize(
columns
)})`
)})${storageEngineIndexType}`
);
}

Expand Down
27 changes: 27 additions & 0 deletions test/integration2/schema/alter.spec.js
Expand Up @@ -5,6 +5,7 @@ const {
getAllDbs,
getKnexForDb,
} = require('../util/knex-instance-provider');
const { isMysql } = require('../../util/db-helpers');

const QUERY_TABLE =
'SELECT sql FROM sqlite_master WHERE type="table" AND tbl_name="alter_table"';
Expand Down Expand Up @@ -55,6 +56,32 @@ describe('Schema', () => {
await knex.schema.dropTable('alter_table');
});

describe('indexes and unique keys', () => {
it('alter table add indexes', async function () {
if (!isMysql(knex)) {
this.skip();
}
await knex.schema
.alterTable('alter_table', (table) => {
table.index(
['column_string', 'column_datetime'],
{ indexName: 'idx_1', storageEngineIndexType: 'BTREE' },
'FULLTEXT'
);
table.unique('column_notNullable', {
indexName: 'idx_2',
storageEngineIndexType: 'HASH',
});
})
.testSql((tester) => {
tester('mysql', [
'alter table `alter_table` add FULLTEXT index `idx_1`(`column_string`, `column_datetime`) using BTREE',
'alter table `alter_table` add unique `idx_2`(`column_notNullable`) using HASH',
]);
});
});
});

describe('alterColumns', () => {
it('alters the type of columns', async () => {
await knex.schema.alterTable('alter_table', (table) => {
Expand Down
37 changes: 36 additions & 1 deletion test/unit/schema-builder/mysql.js
Expand Up @@ -440,6 +440,23 @@ module.exports = function (dialect) {
);
});

it('test adding unique key with storage engine index type', function () {
tableSql = client
.schemaBuilder()
.table('users', function () {
this.unique('foo', {
indexName: 'bar',
storageEngineIndexType: 'HASH',
});
})
.toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table `users` add unique `bar`(`foo`) using HASH'
);
});

it('test adding index', function () {
tableSql = client
.schemaBuilder()
Expand All @@ -462,12 +479,30 @@ module.exports = function (dialect) {
})
.toSQL();

equal(1, tableSql.length);
expect(tableSql.length).to.equal(1);
expect(tableSql[0].sql).to.equal(
'alter table `users` add FULLTEXT index `baz`(`foo`, `bar`)'
);
});

it('test adding index with an index type and storage engine index type', function () {
tableSql = client
.schemaBuilder()
.table('users', function () {
this.index(
['foo', 'bar'],
{ indexName: 'baz', storageEngineIndexType: 'BTREE' },
'UNIQUE'
);
})
.toSQL();

expect(tableSql.length).to.equal(1);
expect(tableSql[0].sql).to.equal(
'alter table `users` add UNIQUE index `baz`(`foo`, `bar`) using BTREE'
);
});

it('test adding foreign key', function () {
tableSql = client
.schemaBuilder()
Expand Down

0 comments on commit d463284

Please sign in to comment.