update
This commit is contained in:
59
scripts/add-primary-role-info.js
Normal file
59
scripts/add-primary-role-info.js
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Migration Script: Add primary_role_info to user_profiles table
|
||||
* This adds a JSON column to store cached role information for fast lookups
|
||||
*/
|
||||
const { sequelize } = require('../config/database');
|
||||
const { QueryTypes } = require('sequelize');
|
||||
|
||||
async function addPrimaryRoleInfo() {
|
||||
try {
|
||||
console.log('🔄 Starting migration: Add primary_role_info column...');
|
||||
|
||||
// Test connection
|
||||
await sequelize.authenticate();
|
||||
console.log('✅ Database connection OK');
|
||||
|
||||
// Check if column already exists
|
||||
const [columns] = await sequelize.query(
|
||||
`SHOW COLUMNS FROM user_profiles LIKE 'primary_role_info'`,
|
||||
{ type: QueryTypes.SELECT }
|
||||
);
|
||||
|
||||
if (columns) {
|
||||
console.log('⚠️ Column primary_role_info already exists, skipping...');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Add column
|
||||
await sequelize.query(`
|
||||
ALTER TABLE user_profiles
|
||||
ADD COLUMN primary_role_info JSON DEFAULT NULL
|
||||
COMMENT 'Thông tin role chính: {role_id, role_code, role_name, school: {id, name}, class: {id, name}, grade: {id, name}}. Nếu null, check UserAssignment'
|
||||
AFTER district
|
||||
`);
|
||||
|
||||
console.log('✅ Column primary_role_info added successfully');
|
||||
|
||||
// Verify column was added
|
||||
const [result] = await sequelize.query(
|
||||
`DESCRIBE user_profiles`,
|
||||
{ type: QueryTypes.SELECT }
|
||||
);
|
||||
|
||||
console.log('\n📊 Table structure:');
|
||||
console.table(result);
|
||||
|
||||
console.log('\n✅ Migration completed successfully!');
|
||||
console.log('\nNext steps:');
|
||||
console.log('1. Populate primary_role_info for existing students using StudentDetail');
|
||||
console.log('2. Run: node scripts/populate-primary-role-info.js');
|
||||
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('❌ Migration failed:', error.message);
|
||||
console.error(error.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
addPrimaryRoleInfo();
|
||||
Reference in New Issue
Block a user