171 lines
5.6 KiB
JavaScript
171 lines
5.6 KiB
JavaScript
/**
|
||
* 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();
|