From 7c232f662ada1113529aa9519ce21dbbd96c9e1f Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 18 Jul 2026 21:39:37 +0000 Subject: [PATCH] Fix teacher_reported_today migration: dynamic db name + idempotent CREATE VIEW Two real bugs found from a failing production deploy log: - The migration hardcoded the literal database name "mysql_database" (leaked from local dev) into the typeorm_metadata bookkeeping statements, instead of using the dynamic ${dbName} pattern this codebase's other view migrations use (queryRunner.connection.options.database). Would break against any database not literally named "mysql_database". - CREATE VIEW / DROP VIEW aren't idempotent: MySQL DDL causes an implicit commit independent of the migration's transaction, so if a deploy is retried after a partial failure (view created, tracking row not yet committed), the next attempt fails with "table already exists". Switched to CREATE OR REPLACE VIEW and DROP VIEW IF EXISTS, plus clearing any stale typeorm_metadata row first. Verified by reproducing the exact reported failure against a MySQL database named differently from my sandbox: ran migrations up to this one, manually created the view outside migration tracking (simulating the partial-failure state), then confirmed the fixed migration completes successfully and the view still returns correct data. --- .../1784189504154-AddTeacherReportedTodayView.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/server/src/migrations/1784189504154-AddTeacherReportedTodayView.ts b/server/src/migrations/1784189504154-AddTeacherReportedTodayView.ts index f4c12919..144e30e6 100644 --- a/server/src/migrations/1784189504154-AddTeacherReportedTodayView.ts +++ b/server/src/migrations/1784189504154-AddTeacherReportedTodayView.ts @@ -4,13 +4,16 @@ export class AddTeacherReportedTodayView1784189504154 implements MigrationInterf name = 'AddTeacherReportedTodayView1784189504154' public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE VIEW \`teacher_reported_today\` AS SELECT \`att_report\`.\`user_id\` AS \`userId\`, \`att_report\`.\`teacherReferenceId\` AS \`teacherReferenceId\`, \`att_report\`.\`lessonReferenceId\` AS \`lessonReferenceId\`, \`att_report\`.\`report_date\` AS \`reportDate\`, CONCAT(\`att_report\`.\`user_id\`, '_', \`att_report\`.\`teacherReferenceId\`, '_', \`att_report\`.\`report_date\`, '_', COALESCE(\`att_report\`.\`lessonReferenceId\`, 'null')) AS \`id\`, MIN(MIN(\`att_report\`.\`created_at\`)) OVER (PARTITION BY \`att_report\`.\`user_id\`, \`att_report\`.\`teacherReferenceId\`, \`att_report\`.\`report_date\`) AS \`reportHour\` FROM \`att_reports\` \`att_report\` WHERE \`att_report\`.\`teacherReferenceId\` IS NOT NULL GROUP BY \`att_report\`.\`user_id\`, \`att_report\`.\`teacherReferenceId\`, \`att_report\`.\`report_date\`, \`att_report\`.\`lessonReferenceId\``); - await queryRunner.query(`INSERT INTO \`mysql_database\`.\`typeorm_metadata\`(\`database\`, \`schema\`, \`table\`, \`type\`, \`name\`, \`value\`) VALUES (DEFAULT, ?, DEFAULT, ?, ?, ?)`, ["mysql_database","VIEW","teacher_reported_today","SELECT `att_report`.`user_id` AS `userId`, `att_report`.`teacherReferenceId` AS `teacherReferenceId`, `att_report`.`lessonReferenceId` AS `lessonReferenceId`, `att_report`.`report_date` AS `reportDate`, CONCAT(`att_report`.`user_id`, '_', `att_report`.`teacherReferenceId`, '_', `att_report`.`report_date`, '_', COALESCE(`att_report`.`lessonReferenceId`, 'null')) AS `id`, MIN(MIN(`att_report`.`created_at`)) OVER (PARTITION BY `att_report`.`user_id`, `att_report`.`teacherReferenceId`, `att_report`.`report_date`) AS `reportHour` FROM `att_reports` `att_report` WHERE `att_report`.`teacherReferenceId` IS NOT NULL GROUP BY `att_report`.`user_id`, `att_report`.`teacherReferenceId`, `att_report`.`report_date`, `att_report`.`lessonReferenceId`"]); + const dbName = queryRunner.connection.options.database; + await queryRunner.query(`DELETE FROM \`${dbName}\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","teacher_reported_today",dbName]); + await queryRunner.query(`CREATE OR REPLACE VIEW \`teacher_reported_today\` AS SELECT \`att_report\`.\`user_id\` AS \`userId\`, \`att_report\`.\`teacherReferenceId\` AS \`teacherReferenceId\`, \`att_report\`.\`lessonReferenceId\` AS \`lessonReferenceId\`, \`att_report\`.\`report_date\` AS \`reportDate\`, CONCAT(\`att_report\`.\`user_id\`, '_', \`att_report\`.\`teacherReferenceId\`, '_', \`att_report\`.\`report_date\`, '_', COALESCE(\`att_report\`.\`lessonReferenceId\`, 'null')) AS \`id\`, MIN(MIN(\`att_report\`.\`created_at\`)) OVER (PARTITION BY \`att_report\`.\`user_id\`, \`att_report\`.\`teacherReferenceId\`, \`att_report\`.\`report_date\`) AS \`reportHour\` FROM \`att_reports\` \`att_report\` WHERE \`att_report\`.\`teacherReferenceId\` IS NOT NULL GROUP BY \`att_report\`.\`user_id\`, \`att_report\`.\`teacherReferenceId\`, \`att_report\`.\`report_date\`, \`att_report\`.\`lessonReferenceId\``); + await queryRunner.query(`INSERT INTO \`${dbName}\`.\`typeorm_metadata\`(\`database\`, \`schema\`, \`table\`, \`type\`, \`name\`, \`value\`) VALUES (DEFAULT, ?, DEFAULT, ?, ?, ?)`, [dbName,"VIEW","teacher_reported_today","SELECT `att_report`.`user_id` AS `userId`, `att_report`.`teacherReferenceId` AS `teacherReferenceId`, `att_report`.`lessonReferenceId` AS `lessonReferenceId`, `att_report`.`report_date` AS `reportDate`, CONCAT(`att_report`.`user_id`, '_', `att_report`.`teacherReferenceId`, '_', `att_report`.`report_date`, '_', COALESCE(`att_report`.`lessonReferenceId`, 'null')) AS `id`, MIN(MIN(`att_report`.`created_at`)) OVER (PARTITION BY `att_report`.`user_id`, `att_report`.`teacherReferenceId`, `att_report`.`report_date`) AS `reportHour` FROM `att_reports` `att_report` WHERE `att_report`.`teacherReferenceId` IS NOT NULL GROUP BY `att_report`.`user_id`, `att_report`.`teacherReferenceId`, `att_report`.`report_date`, `att_report`.`lessonReferenceId`"]); } public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`DELETE FROM \`mysql_database\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","teacher_reported_today","mysql_database"]); - await queryRunner.query(`DROP VIEW \`teacher_reported_today\``); + const dbName = queryRunner.connection.options.database; + await queryRunner.query(`DELETE FROM \`${dbName}\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","teacher_reported_today",dbName]); + await queryRunner.query(`DROP VIEW IF EXISTS \`teacher_reported_today\``); } }