-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MSSQL - Changed DataTypes.DATE to use DATETIMEOFFSET datatype #7201
Conversation
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':
|
Pull Request check-list
npm run test
ornpm run test-DIALECT
pass with this change (including linting)?Future
in the changelog?Description of change
Closes #5403