diff --git a/backend/migrations/20260420000004-make-password-optional.js b/backend/migrations/20260420000004-make-password-optional.js index 99d835a..38cf3d8 100644 --- a/backend/migrations/20260420000004-make-password-optional.js +++ b/backend/migrations/20260420000004-make-password-optional.js @@ -68,34 +68,79 @@ module.exports = { ); } + const existingColumns = new Set(columns.map((col) => col.name)); + + const baseColumns = [ + 'id', + 'uid', + 'name', + 'surname', + 'email', + 'appearance', + 'language', + 'timezone', + 'first_day_of_week', + 'avatar_image', + 'telegram_bot_token', + 'telegram_chat_id', + 'task_summary_enabled', + 'task_summary_frequency', + 'task_summary_last_run', + 'task_summary_next_run', + 'telegram_allowed_users', + 'task_intelligence_enabled', + 'auto_suggest_next_actions_enabled', + 'pomodoro_enabled', + 'productivity_assistant_enabled', + 'next_task_suggestion_enabled', + 'today_settings', + 'sidebar_settings', + 'ui_settings', + 'notification_preferences', + 'keyboard_shortcuts', + 'email_verified', + 'email_verification_token', + 'email_verification_token_expires_at', + 'created_at', + 'updated_at', + ]; + + const aiColumns = [ + 'ai_provider', + 'openai_api_key', + 'ollama_base_url', + 'ollama_model', + ]; + + const columnsToSelect = baseColumns.filter((col) => + existingColumns.has(col) + ); + + const aiColumnsToSelect = aiColumns.filter((col) => + existingColumns.has(col) + ); + + const columnsWithoutPassword = columnsToSelect.filter( + (col) => col !== passwordColumn && col !== 'password_digest' + ); + + const insertColumns = [ + ...columnsWithoutPassword, + 'password_digest', + ...aiColumnsToSelect, + ]; + const selectColumns = [ + ...columnsWithoutPassword, + `${passwordColumn} as password_digest`, + ...aiColumnsToSelect, + ]; + + const insertColumnsStr = insertColumns.join(', '); + const selectColumnsStr = selectColumns.join(', '); + await queryInterface.sequelize.query(` - INSERT INTO users_new ( - id, uid, name, surname, email, password_digest, appearance, language, - timezone, first_day_of_week, avatar_image, telegram_bot_token, - telegram_chat_id, task_summary_enabled, task_summary_frequency, - task_summary_last_run, task_summary_next_run, telegram_allowed_users, - task_intelligence_enabled, auto_suggest_next_actions_enabled, - pomodoro_enabled, productivity_assistant_enabled, - next_task_suggestion_enabled, today_settings, sidebar_settings, - ui_settings, notification_preferences, keyboard_shortcuts, - email_verified, email_verification_token, - email_verification_token_expires_at, created_at, updated_at, - ai_provider, openai_api_key, ollama_base_url, ollama_model - ) - SELECT - id, uid, name, surname, email, - ${passwordColumn} as password_digest, - appearance, language, - timezone, first_day_of_week, avatar_image, telegram_bot_token, - telegram_chat_id, task_summary_enabled, task_summary_frequency, - task_summary_last_run, task_summary_next_run, telegram_allowed_users, - task_intelligence_enabled, auto_suggest_next_actions_enabled, - pomodoro_enabled, productivity_assistant_enabled, - next_task_suggestion_enabled, today_settings, sidebar_settings, - ui_settings, notification_preferences, keyboard_shortcuts, - email_verified, email_verification_token, - email_verification_token_expires_at, created_at, updated_at, - ai_provider, openai_api_key, ollama_base_url, ollama_model + INSERT INTO users_new (${insertColumnsStr}) + SELECT ${selectColumnsStr} FROM users; `); @@ -155,32 +200,65 @@ module.exports = { ); `); + const [columns] = await queryInterface.sequelize.query( + 'PRAGMA table_info(users);' + ); + const existingColumns = new Set(columns.map((col) => col.name)); + + const baseColumns = [ + 'id', + 'uid', + 'name', + 'surname', + 'email', + 'password_digest', + 'appearance', + 'language', + 'timezone', + 'first_day_of_week', + 'avatar_image', + 'telegram_bot_token', + 'telegram_chat_id', + 'task_summary_enabled', + 'task_summary_frequency', + 'task_summary_last_run', + 'task_summary_next_run', + 'telegram_allowed_users', + 'task_intelligence_enabled', + 'auto_suggest_next_actions_enabled', + 'pomodoro_enabled', + 'productivity_assistant_enabled', + 'next_task_suggestion_enabled', + 'today_settings', + 'sidebar_settings', + 'ui_settings', + 'notification_preferences', + 'keyboard_shortcuts', + 'email_verified', + 'email_verification_token', + 'email_verification_token_expires_at', + 'created_at', + 'updated_at', + ]; + + const aiColumns = [ + 'ai_provider', + 'openai_api_key', + 'ollama_base_url', + 'ollama_model', + ]; + + const columnsToSelect = [ + ...baseColumns.filter((col) => existingColumns.has(col)), + ...aiColumns.filter((col) => existingColumns.has(col)), + ]; + + const insertColumnsStr = columnsToSelect.join(', '); + const selectColumnsStr = columnsToSelect.join(', '); + await queryInterface.sequelize.query(` - INSERT INTO users_new ( - id, uid, name, surname, email, password_digest, appearance, language, - timezone, first_day_of_week, avatar_image, telegram_bot_token, - telegram_chat_id, task_summary_enabled, task_summary_frequency, - task_summary_last_run, task_summary_next_run, telegram_allowed_users, - task_intelligence_enabled, auto_suggest_next_actions_enabled, - pomodoro_enabled, productivity_assistant_enabled, - next_task_suggestion_enabled, today_settings, sidebar_settings, - ui_settings, notification_preferences, keyboard_shortcuts, - email_verified, email_verification_token, - email_verification_token_expires_at, created_at, updated_at, - ai_provider, openai_api_key, ollama_base_url, ollama_model - ) - SELECT - id, uid, name, surname, email, password_digest, appearance, language, - timezone, first_day_of_week, avatar_image, telegram_bot_token, - telegram_chat_id, task_summary_enabled, task_summary_frequency, - task_summary_last_run, task_summary_next_run, telegram_allowed_users, - task_intelligence_enabled, auto_suggest_next_actions_enabled, - pomodoro_enabled, productivity_assistant_enabled, - next_task_suggestion_enabled, today_settings, sidebar_settings, - ui_settings, notification_preferences, keyboard_shortcuts, - email_verified, email_verification_token, - email_verification_token_expires_at, created_at, updated_at, - ai_provider, openai_api_key, ollama_base_url, ollama_model + INSERT INTO users_new (${insertColumnsStr}) + SELECT ${selectColumnsStr} FROM users; `);