Files
sena_db_api_layer/alter-context-table.js
silverpro89 aaba22b40c
All checks were successful
Deploy to Production / deploy (push) Successful in 19s
update context API
2026-02-06 11:28:06 +07:00

171 lines
5.6 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Alter context table to update grade column and add/update other fields
*/
const { sequelize } = require('./config/database');
async function alterContextTable() {
try {
console.log('🔄 Starting context table alteration...');
// Test connection first
await sequelize.authenticate();
console.log('✅ Database connection OK');
// Drop old columns if they exist
try {
await sequelize.query(`ALTER TABLE context DROP COLUMN IF EXISTS difficulty`);
console.log('✅ Dropped old difficulty column');
} catch (error) {
console.log(' difficulty column might not exist');
}
try {
await sequelize.query(`ALTER TABLE context DROP COLUMN IF EXISTS isPrompt`);
console.log('✅ Dropped old isPrompt column');
} catch (error) {
console.log(' isPrompt column might not exist');
}
try {
await sequelize.query(`ALTER TABLE context DROP COLUMN IF EXISTS isList`);
console.log('✅ Dropped old isList column');
} catch (error) {
console.log(' isList column might not exist');
}
try {
await sequelize.query(`ALTER TABLE context DROP COLUMN IF EXISTS isApprove`);
console.log('✅ Dropped old isApprove column');
} catch (error) {
console.log(' isApprove column might not exist');
}
try {
await sequelize.query(`ALTER TABLE context DROP COLUMN IF EXISTS prompt`);
console.log('✅ Dropped old prompt column');
} catch (error) {
console.log(' prompt column might not exist');
}
// Modify grade column (change from VARCHAR to INT)
try {
await sequelize.query(`
ALTER TABLE context
MODIFY COLUMN grade INT NOT NULL DEFAULT 100
COMMENT 'It is number of gradeX100 + unitX10 + lesson (e.g., Grade 1 Unit 2 Lesson 3 = 123)'
`);
console.log('✅ Modified grade column to INT');
} catch (error) {
console.log('⚠️ Grade column modification error:', error.message);
// Try adding if not exists
try {
await sequelize.query(`
ALTER TABLE context
ADD COLUMN grade INT NOT NULL DEFAULT 100
COMMENT 'It is number of gradeX100 + unitX10 + lesson (e.g., Grade 1 Unit 2 Lesson 3 = 123)'
`);
console.log('✅ Added grade column');
} catch (addError) {
console.log('⚠️ Could not add grade column:', addError.message);
}
}
// Modify knowledge column
try {
await sequelize.query(`
ALTER TABLE context
MODIFY COLUMN knowledge TEXT DEFAULT ''
COMMENT 'Additional knowledge or information'
`);
console.log('✅ Modified knowledge column');
} catch (error) {
console.log(' Knowledge column might already be correct');
}
// Rename prompt to promptForImage if needed
try {
await sequelize.query(`
ALTER TABLE context
CHANGE COLUMN prompt promptForImage JSON
COMMENT 'Prompt configuration object'
`);
console.log('✅ Renamed prompt to promptForImage');
} catch (error) {
console.log(' Column might already be named promptForImage');
}
// Modify promptForImage if it exists
try {
await sequelize.query(`
ALTER TABLE context
MODIFY COLUMN promptForImage JSON
COMMENT 'Prompt configuration object'
`);
console.log('✅ Modified promptForImage column');
} catch (error) {
// Try adding if not exists
try {
await sequelize.query(`
ALTER TABLE context
ADD COLUMN promptForImage JSON
COMMENT 'Prompt configuration object'
`);
console.log('✅ Added promptForImage column');
} catch (addError) {
console.log(' promptForImage column might already exist');
}
}
// Modify max column
try {
await sequelize.query(`
ALTER TABLE context
MODIFY COLUMN max INT DEFAULT 1
COMMENT 'Maximum number of images or items'
`);
console.log('✅ Modified max column');
} catch (error) {
console.log(' max column might already be correct');
}
// Add status column if not exists
try {
await sequelize.query(`
ALTER TABLE context
ADD COLUMN status INT DEFAULT 0
COMMENT '0: Draft, 1: Enriched, 2: Prompt_Ready, 3: Generating, 4: Image_Ready, 5: Approved'
`);
console.log('✅ Added status column');
} catch (error) {
console.log(' status column might already exist');
// Try modifying if exists
try {
await sequelize.query(`
ALTER TABLE context
MODIFY COLUMN status INT DEFAULT 0
COMMENT '0: Draft, 1: Enriched, 2: Prompt_Ready, 3: Generating, 4: Image_Ready, 5: Approved'
`);
console.log('✅ Modified status column');
} catch (modError) {
console.log(' Status column might already be correct');
}
}
// Show final structure
const [columns] = await sequelize.query('DESCRIBE context');
console.log('\n📊 Context table structure:');
columns.forEach((col, index) => {
console.log(` ${index + 1}. ${col.Field} (${col.Type}) ${col.Null === 'NO' ? 'NOT NULL' : 'NULL'} ${col.Default ? `DEFAULT ${col.Default}` : ''}`);
});
console.log('\n✅ Context table alteration complete!');
process.exit(0);
} catch (error) {
console.error('❌ Error altering context table:', error.message);
console.error(error.stack);
process.exit(1);
}
}
alterContextTable();