/** * 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();