MSSQL - Changed DataTypes.DATE to use DATETIMEOFFSET datatype#7201
Merged
sushantdhiman merged 1 commit intosequelize:masterfrom Feb 7, 2017
Merged
MSSQL - Changed DataTypes.DATE to use DATETIMEOFFSET datatype#7201sushantdhiman merged 1 commit intosequelize:masterfrom
sushantdhiman merged 1 commit intosequelize:masterfrom
Conversation
Author
felixfbecker
approved these changes
Feb 6, 2017
sushantdhiman
approved these changes
Feb 7, 2017
Author
|
In case someone is looking to migrate existing const listAllDatetime2ColumnsSql = 'SELECT TABLE_NAME AS tableName, COLUMN_NAME AS columnName, DATA_TYPE AS dataType, IS_NULLABLE AS isNullable FROM INFORMATION_SCHEMA.COLUMNS WHERE DATA_TYPE=\'datetime2\';';
const listAllDatetimeOffsetColumnsSql = 'SELECT TABLE_NAME AS tableName, COLUMN_NAME AS columnName, DATA_TYPE AS dataType, IS_NULLABLE AS isNullable FROM INFORMATION_SCHEMA.COLUMNS WHERE DATA_TYPE=\'datetimeoffset\';';
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.sequelize.transaction(transaction => {
return queryInterface
.sequelize.query(listAllDatetime2ColumnsSql, { transaction })
.then(result => {
result = result[0];
const columns = [];
for (const row of result) {
columns.push(queryInterface.changeColumn(row.tableName, row.columnName, {
type: Sequelize.DATE,
allowNull: row.isNullable === 'YES' ? true : false
}, { transaction }));
}
return Promise.all(columns);
});
});
},
down: function (queryInterface, Sequelize) {
return queryInterface.sequelize.transaction(transaction => {
return queryInterface
.sequelize.query(listAllDatetimeOffsetColumnsSql, { transaction })
.then(result => {
result = result[0];
const columns = [];
for (const row of result) {
const nullSnippet = row.isNullable === 'YES' ? 'NULL' : 'NOT NULL';
const attribute = {};
attribute[row.columnName] = `DATETIME2 ${nullSnippet}`;
const changeColumnSql = queryInterface.QueryGenerator.changeColumnQuery(row.tableName, attribute);
columns.push(queryInterface.sequelize.query(changeColumnSql, { transaction }));
}
return Promise.all(columns);
});
});
}
}; |
|
Hi! When I run this migration, I get an ERROR:
Apparently, I need to drop constraints (which sequelize auto-generates for my timestamps createdAt and updatedAt) first before this migration works? Thanks! ...Trying to slog thru the ver 3 -> 4 breaking changes! |
|
For anyone interested, here's a MS SQL Server CURSOR that DROPS those timestamp constraints ... it'll need some slight modification for your needs, I was using two tables called 'po' and 'requisition': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request check-list
npm run testornpm run test-DIALECTpass with this change (including linting)?Futurein the changelog?Description of change
Closes #5403