"""Add nullable version columns without modifying historical data. Safe to rerun.""" from database import get_db_connection def migrate(connection): cursor = connection.cursor() try: for table in ('analysis_batches', 'single_prediction_history'): cursor.execute('SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=%s AND COLUMN_NAME=%s', (table, 'model_version')) if not cursor.fetchone()[0]: cursor.execute(f'ALTER TABLE `{table}` ADD COLUMN `model_version` VARCHAR(64) NULL DEFAULT NULL') print(f'{table}: model_version ditambahkan.') else: print(f'{table}: sudah diperbarui.') connection.commit() finally: cursor.close() if __name__ == '__main__': connection = get_db_connection() try: migrate(connection) print('Selesai. Riwayat lama tetap tersimpan dengan versi tidak tercatat.') finally: connection.close()