Repository navigation
addConstraint, removeConstraint - #7108
Conversation
Codecov Report
Continue to review full report at Codecov.
|
|
@mickhansen @janmeier @sushantdhiman @felixfbecker Request review.
|
ORM should be able to abstract these changes, It wont help much if these methods cant be used for 50% dialects
You can create new table with constraints and move data to it |
e3f7259 to
5372b2d
Compare
|
@sushantdhiman I've added a |
|
Ping @janmeier @mickhansen |
| queryInterface.addConstraint('Posts', ['username'], { | ||
| type: 'FOREIGN KEY', | ||
| references: { //Required field | ||
| model: 'target_table_name', |
There was a problem hiding this comment.
@mickhansen I used the same naming conventions from migration docs. It has model/key for the references object.
| type: 'FOREIGN KEY', | ||
| references: { //Required field | ||
| model: 'target_table_name', | ||
| key: 'target_column_name', |
| const sql = current.dialect.QueryGenerator; | ||
| const dialect = Support.getTestDialect(); | ||
|
|
||
| if (dialect === 'mssql' || dialect === 'postgres') { |
There was a problem hiding this comment.
Can't remove constraints in mysql?
Please use dialect.features rather dialect naming checks
There was a problem hiding this comment.
No, mysql doesn't have removeConstraint in queryGenerator. We have to override removeConstraint in mysql queryInterface and it ultimately calls QueryGenerator.removeIndexQuery or QueryGenerator.dropForeignKeyQuery which already have tests covered
| const expect = require('chai').expect; | ||
| const dialect = Support.getTestDialect(); | ||
|
|
||
| if (dialect !== 'sqlite') { |
There was a problem hiding this comment.
features check, no constraints in sqlite?
There was a problem hiding this comment.
sqlite implementation of addContraint is a bit different. It has its own queryInterface method and queryGenerator functions. So, i've skipped them. But i'll go ahead and add unit tests for sqlite queryGenerator functions.
bd380af to
c34276d
Compare
| } | ||
| }); | ||
|
|
||
| if(options.where) { |
There was a problem hiding this comment.
space before parens (saw this in multiple places)
There was a problem hiding this comment.
Thanks for catching. I'll fix all occurrences and also add keyword-spacing to eslintrc
| options = Utils.cloneDeep(options); | ||
| options.fields = attributes; | ||
| if (this.sequelize.dialect.name === 'sqlite') { | ||
| return SQLiteQueryInterface.addConstraint.call(this, tableName, options, rawTablename); |
There was a problem hiding this comment.
I don't get this. The dialect-specific query interfaces inherit from the base one. So the user would get a SQLiteQueryInterface if they use SQLite. That may call super, in which case you get an infinite recursion here?
The children should call the parent, not the other way around
There was a problem hiding this comment.
@felixfbecker Only QueryGenerators are inherited. removeColumn also uses the same pattern.
46ee1c6 to
0e9a30d
Compare
| if (constraintSql.indexOf('REFERENCES') !== -1) { | ||
| //Parse out the constraint condition form sql string | ||
| update_action = constraintSql.match(/ON UPDATE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/); | ||
| delete_action = constraintSql.match(/ON DELETE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/); |
There was a problem hiding this comment.
Postgres & mssql returns it in snake case. Used the same here to maintain consistency
There was a problem hiding this comment.
Apologies. Looks like MSSQL is the culprit here. I'm using MSSQL stored procedure sp_helpconstraint and hence can't format the results. I used the same snake_case for other dialects aswell.
Since default constraint in case of mssql are not part of ISO standard, i can't query on INFORMATION_SCHEMA.TABLE_CONSTRAINTS as we query in case of mysql and postgresql.
We could convert the snake_case to camelCase in mssql query.js and use sql aliases in case of mysql & postgres?
There was a problem hiding this comment.
Postgres and MSSQL return format doesn't control variable name we use, does it ?
Just rename update_action => updateAction , delete_action => deleteAction
There was a problem hiding this comment.
Postgres is not a problem. Only MSSQL is. I'll convert snake_case keys to camelCase only in case of MSSQL.
0e9a30d to
2f4e82a
Compare
|
@sushantdhiman Can you take a look at this please? |
| queryInterface.removeIndex('Person', ['firstname', 'lastname']) | ||
| ``` | ||
|
|
||
| ### addConstraint(tableName, indexNameOrAttributes, options) |
There was a problem hiding this comment.
Why this is named as indexNameOrAttributes, I think it should be just attributes. Please explain your rationale behind this naming @harshithkashyap , I may be missing something
There was a problem hiding this comment.
Sorry. It should just be attributes. The signature in the code is correct. But i missed it here. Will change it
| queryInterface.addConstraint('Posts', ['username'], { | ||
| type: 'FOREIGN KEY', | ||
| references: { //Required field | ||
| model: 'target_table_name', |
There was a problem hiding this comment.
You can pass model here @harshithkashyap right, it will get table name from that ?
There was a problem hiding this comment.
I wasn't aware that we could pass model here. I'll modify the code to include the same.
Self Note: Add tests passing model
There was a problem hiding this comment.
@sushantdhiman Since constraints are executed only via migrations, i'll keep it to tableName only for now. Even createTable method expects a string and not a model
There was a problem hiding this comment.
@harshithkashyap Please rename it to table as @mickhansen suggested, that more suited if we dont support passing model
| roles: ['user', 'admin', 'moderator', 'guest'] | ||
| } | ||
| }); | ||
| //Constraint name is automatically constructed. |
There was a problem hiding this comment.
Remove this comment line
| //UNIQUE | ||
| queryInterface.addConstraint('Users', ['email'], { | ||
| type: 'unique', | ||
| name: 'custom_unique_constraint_name' //If not specifed, sequelize automatically creates a named constraint based on constraint type, table & column names |
| ``` | ||
|
|
||
| ### addConstraint(tableName, indexNameOrAttributes, options) | ||
| This method adds a new constraint of the specified type. |
There was a problem hiding this comment.
In the description explain meaning of arguments, and what common options we have. Here you can explain things like options.name and what happens if you dont pass them
| + `DROP TABLE ${quotedBackupTableName};`; | ||
| }, | ||
|
|
||
| alterConstraintQuery(tableName, attributes, createTableSql) { |
There was a problem hiding this comment.
This is a private method, right. I don think user will ever call this as it require createTableSql. It will be good idea to prefix such private methods with _
| if (constraintSql.indexOf('REFERENCES') !== -1) { | ||
| //Parse out the constraint condition form sql string | ||
| update_action = constraintSql.match(/ON UPDATE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/); | ||
| delete_action = constraintSql.match(/ON DELETE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/); |
There was a problem hiding this comment.
Postgres and MSSQL return format doesn't control variable name we use, does it ?
Just rename update_action => updateAction , delete_action => deleteAction
| options = attributes; | ||
| attributes = options.fields; | ||
| } | ||
| if (!options.type) { |
There was a problem hiding this comment.
Empty line before this line
| if (!options.type) { | ||
| throw new Error('Constraint type must be specified through options.type'); | ||
| } | ||
| // testhint argsConform.end |
There was a problem hiding this comment.
whats this comment ? remove this comment
|
|
||
| options = Utils.cloneDeep(options); | ||
| options.fields = attributes; | ||
| if (this.sequelize.dialect.name === 'sqlite') { |
There was a problem hiding this comment.
Empty line before this line
|
@harshithkashyap what happens for SQLite case, does it support |
|
@sushantdhiman No, SQLite doesn't support There are other open issues as well regarding sqlite alter table #7078 and i could not find any other way to go about it. |
871b41e to
4c1fb88
Compare
4c1fb88 to
f6a8580
Compare
|
@harshithkashyap Please rebase this branch and take care of my last comment (Using |
…nstraintError, improve test coverage
| references: { //Required field | ||
| model: 'target_table_name', | ||
| key: 'target_column_name' | ||
| }, |
There was a problem hiding this comment.
@sushantdhiman Since we are changing model to table, would it better to rename key to column as well?
There was a problem hiding this comment.
Probably field will make sense, as in our code base column is usually represented as field
5ff637c to
bf40306
Compare
|
@harshithkashyap Great work ❤️ |
* Added add, remove & show constraints feat * Added showConstraintsQuery for postgres & mysql * [ci skip] Added docs for constraints usage * Added removeIndex support for mysql * Added altering constraints to sqlite dialect * Refactor onDelete, onUpdate to work in sqlite dialect * Throw non existent constraints in removeConstraint query as UnknownConstraintError, improve test coverage * Fix failing tests * Removed dialect naming check in favor of dialect.supports feature check * Improve code coverage, eslint fixes * Review fixes * Review fixes * Renamed model, key to table, field for foreign key contraint definition
|
Which version will include this patch? |
|
@yuanfeiz the next ;) |
|
Cool! Timely deliver! |
Pull Request check-list
npm run testornpm run test-DIALECTpass with this change (including linting)?Futurein the changelog?Description of change
Added
addConstraint,showConstraint,removeConstraintmethods. Closes #5212Supported constraints