From 8755c22fde4be27cbc7d19058e2cba7de7d25d92 Mon Sep 17 00:00:00 2001 From: John Craft Date: Fri, 19 Jan 2024 11:06:50 -0800 Subject: [PATCH] Create migration for dob column --- .../20240119173749-change-dob-data-type.js | 30 +++++++++++++++++++ users-api/src/models/user.js | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 users-api/src/migrations/20240119173749-change-dob-data-type.js diff --git a/users-api/src/migrations/20240119173749-change-dob-data-type.js b/users-api/src/migrations/20240119173749-change-dob-data-type.js new file mode 100644 index 0000000..7756da8 --- /dev/null +++ b/users-api/src/migrations/20240119173749-change-dob-data-type.js @@ -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 + }); + } +}; diff --git a/users-api/src/models/user.js b/users-api/src/models/user.js index 92eeb5e..3df200d 100644 --- a/users-api/src/models/user.js +++ b/users-api/src/models/user.js @@ -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',