Files
vitfit-r2/V2/app/modules/users/migrations/003-users-v2.js
T
2026-06-13 08:03:45 -04:00

48 lines
2.1 KiB
JavaScript

'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
// Create Users table if it doesn't exist (new installs)
const tableDesc = await queryInterface.describeTable('Users').catch(() => null);
if (!tableDesc) {
await queryInterface.createTable('Users', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
organizationId: { type: Sequelize.INTEGER },
username: { type: Sequelize.STRING, allowNull: false, unique: true },
email: { type: Sequelize.STRING, unique: true },
password: { type: Sequelize.STRING, allowNull: false },
role: { type: Sequelize.ENUM('SystemAdmin', 'Owner', 'Coach', 'Client'), allowNull: false, defaultValue: 'Coach' },
firstName: { type: Sequelize.STRING },
lastName: { type: Sequelize.STRING },
isActive: { type: Sequelize.BOOLEAN, defaultValue: true },
lastLoginAt: { type: Sequelize.DATE },
passwordChangedAt: { type: Sequelize.DATE },
createdAt: { type: Sequelize.DATE, allowNull: false },
updatedAt: { type: Sequelize.DATE, allowNull: false },
});
return;
}
// V1 upgrade path: add missing columns
const addIfMissing = async (col, def) => {
if (!tableDesc[col]) {
await queryInterface.addColumn('Users', col, def);
}
};
await addIfMissing('organizationId', { type: Sequelize.INTEGER });
await addIfMissing('email', { type: Sequelize.STRING });
await addIfMissing('firstName', { type: Sequelize.STRING });
await addIfMissing('lastName', { type: Sequelize.STRING });
await addIfMissing('isActive', { type: Sequelize.BOOLEAN, defaultValue: true });
await addIfMissing('lastLoginAt', { type: Sequelize.DATE });
await addIfMissing('passwordChangedAt', { type: Sequelize.DATE });
},
async down(queryInterface) {
// No-op for V1 upgrade path; would need to be more specific for rollback
await queryInterface.dropTable('Users').catch(() => {});
},
};