Skip to content

Commit

Permalink
Create migration for dob column
Browse files Browse the repository at this point in the history
  • Loading branch information
john-craft committed Jan 25, 2024
1 parent 9c443c8 commit 103b7e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions users-api/src/migrations/20240119173749-change-dob-data-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
// Add a temporary column
await queryInterface.addColumn('users', 'dob_temp', {
type: Sequelize.DATEONLY,
});

// Convert and move the data from 'dob' to 'dob_temp'
await queryInterface.sequelize.query(`
UPDATE "users"
SET "dob_temp" = "dob"::DATE
`);

// Remove the old 'dob' column
await queryInterface.removeColumn('users', 'dob');

// Rename the 'dob_temp' column to 'dob'
await queryInterface.renameColumn('users', 'dob_temp', 'dob');
},

async down (queryInterface, Sequelize) {
await queryInterface.changeColumn('users', 'dob', {
type: Sequelize.STRING,
allowNull: true
});
}
};
2 changes: 1 addition & 1 deletion users-api/src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = (sequelize, DataTypes) => {
zip: DataTypes.STRING,
age: DataTypes.INTEGER,
gender: DataTypes.STRING,
dob: DataTypes.STRING
dob: DataTypes.DATEONLY
}, {
sequelize,
modelName: 'user',
Expand Down

0 comments on commit 103b7e5

Please sign in to comment.