不要怂,就是干,撸起袖子干!

Commit 057e5356 by Sushant Committed by GitHub

fix(create-table): support for uniqueKeys (#9946)

1 parent ee9ec57d
......@@ -164,6 +164,14 @@ class QueryInterface {
options = _.clone(options) || {};
if (options && options.uniqueKeys) {
_.forOwn(options.uniqueKeys, uniqueKey => {
if (uniqueKey.customIndex === undefined) {
uniqueKey.customIndex = true;
}
});
}
if (model) {
options.uniqueKeys = options.uniqueKeys || model.uniqueKeys;
}
......
......@@ -60,6 +60,7 @@
"husky": "^0.14.3",
"istanbul": "^0.x",
"lcov-result-merger": "^3.0.0",
"lint-staged": "^7.3.0",
"mocha": "^5.x",
"mysql2": "^1.x",
"pg": "^7.x",
......@@ -89,6 +90,9 @@
"@commitlint/config-angular"
]
},
"lint-staged": {
"*.js": "eslint"
},
"scripts": {
"lint": "eslint lib test --quiet",
"test": "npm run teaser && npm run test-unit && npm run test-integration",
......@@ -127,6 +131,7 @@
"sscce-mysql": "cross-env DIALECT=mysql npm run sscce",
"sscce-postgres": "cross-env DIALECT=postgres npm run sscce",
"sscce-sqlite": "cross-env DIALECT=sqlite npm run sscce",
"commitmsg": "commitlint -E GIT_PARAMS"
"commitmsg": "commitlint -E GIT_PARAMS",
"precommit": "lint-staged"
}
}
......@@ -5,6 +5,7 @@ const expect = chai.expect;
const Support = require('../support');
const DataTypes = require('../../../lib/data-types');
const _ = require('lodash');
const dialect = Support.getTestDialect();
describe(Support.getTestDialectTeaser('QueryInterface'), () => {
beforeEach(function() {
......@@ -34,6 +35,64 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
});
});
it('should create unique constraint with uniqueKeys', function() {
return this.queryInterface.createTable('MyTable', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING
},
email: {
type: DataTypes.STRING
}
}, {
uniqueKeys: {
myCustomIndex: {
fields: ['name', 'email']
},
myOtherIndex: {
fields: ['name']
}
}
}).then(() => {
return this.queryInterface.showIndex('MyTable');
}).then(indexes => {
switch (dialect) {
case 'postgres':
case 'postgres-native':
case 'sqlite':
case 'mssql':
// name + email
expect(indexes[0].unique).to.be.true;
expect(indexes[0].fields[0].attribute).to.equal('name');
expect(indexes[0].fields[1].attribute).to.equal('email');
// name
expect(indexes[1].unique).to.be.true;
expect(indexes[1].fields[0].attribute).to.equal('name');
break;
case 'mysql':
// name + email
expect(indexes[1].unique).to.be.true;
expect(indexes[1].fields[0].attribute).to.equal('name');
expect(indexes[1].fields[1].attribute).to.equal('email');
// name
expect(indexes[2].unique).to.be.true;
expect(indexes[2].fields[0].attribute).to.equal('name');
break;
default:
throw new Error(`Not implemented fpr ${dialect}`);
}
});
});
it('should work with enums (1)', function() {
return this.queryInterface.createTable('SomeTable', {
someEnum: DataTypes.ENUM('value1', 'value2', 'value3')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!