Files
sena_db_api_layer/migration_scripts/add-status-etc-fields.js
vuongps38770 72283443ab
All checks were successful
Deploy to Production / deploy (push) Successful in 20s
update
2026-02-28 20:00:38 +07:00

105 lines
4.4 KiB
JavaScript

const { sequelize } = require('../config/database');
/**
* Migration: Thêm trường `status` (INT, default 0) và `etc` (TEXT, default '')
* cho cả bảng `vocab` và `sentences`.
*
* - status: dùng để confirm lại vocab/sentence (0 = chưa confirm, etc.)
* - etc: thông tin bổ sung dạng chuỗi
*/
async function run() {
try {
await sequelize.authenticate();
console.log('✅ Database connected\n');
const queryInterface = sequelize.getQueryInterface();
// ─── VOCAB ───────────────────────────────────────────
console.log('📦 Đang migrate bảng VOCAB...');
// Kiểm tra & thêm status
try {
await queryInterface.addColumn('vocab', 'status', {
type: sequelize.Sequelize.DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
comment: 'Trạng thái confirm (0 = chưa confirm)'
});
console.log(' ✅ Đã thêm cột "status" vào bảng vocab');
} catch (e) {
if (e.message.includes('Duplicate') || e.message.includes('already exists') || e.original?.code === 'ER_DUP_FIELDNAME') {
console.log(' ⚠️ Cột "status" đã tồn tại trong bảng vocab, bỏ qua.');
} else {
throw e;
}
}
// Kiểm tra & thêm etc
try {
await queryInterface.addColumn('vocab', 'etc', {
type: sequelize.Sequelize.DataTypes.TEXT,
defaultValue: '',
allowNull: true,
comment: 'Thông tin bổ sung'
});
console.log(' ✅ Đã thêm cột "etc" vào bảng vocab');
} catch (e) {
if (e.message.includes('Duplicate') || e.message.includes('already exists') || e.original?.code === 'ER_DUP_FIELDNAME') {
console.log(' ⚠️ Cột "etc" đã tồn tại trong bảng vocab, bỏ qua.');
} else {
throw e;
}
}
// ─── SENTENCES ───────────────────────────────────────
console.log('\n📦 Đang migrate bảng SENTENCES...');
// Kiểm tra & thêm status
try {
await queryInterface.addColumn('sentences', 'status', {
type: sequelize.Sequelize.DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
comment: 'Trạng thái confirm (0 = chưa confirm)'
});
console.log(' ✅ Đã thêm cột "status" vào bảng sentences');
} catch (e) {
if (e.message.includes('Duplicate') || e.message.includes('already exists') || e.original?.code === 'ER_DUP_FIELDNAME') {
console.log(' ⚠️ Cột "status" đã tồn tại trong bảng sentences, bỏ qua.');
} else {
throw e;
}
}
// Kiểm tra & thêm etc (sentences đã có etc trong model nhưng chưa chắc có trong DB)
try {
await queryInterface.addColumn('sentences', 'etc', {
type: sequelize.Sequelize.DataTypes.TEXT,
defaultValue: '',
allowNull: true,
comment: 'Thông tin bổ sung'
});
console.log(' ✅ Đã thêm cột "etc" vào bảng sentences');
} catch (e) {
if (e.message.includes('Duplicate') || e.message.includes('already exists') || e.original?.code === 'ER_DUP_FIELDNAME') {
console.log(' ⚠️ Cột "etc" đã tồn tại trong bảng sentences, bỏ qua.');
} else {
throw e;
}
}
console.log('\n─────────────────────────────────────');
console.log('✅ Migration hoàn tất!');
console.log(' - vocab: status (INT, default 0), etc (TEXT, default "")');
console.log(' - sentences: status (INT, default 0), etc (TEXT, default "")');
console.log('─────────────────────────────────────');
process.exit(0);
} catch (error) {
console.error('❌ Lỗi:', error.message);
process.exit(1);
}
}
run();