/** * 작업보고서 부적합을 신고 시스템과 연동 * * 변경사항: * 1. work_report_defects 테이블에 issue_report_id 컬럼 추가 * 2. error_type_id를 NULL 허용으로 변경 (신고 연동 시 불필요) * 3. work_issue_reports.report_id (unsigned int)와 타입 일치 필요 */ exports.up = function(knex) { return knex.schema .alterTable('work_report_defects', function(table) { // 1. issue_report_id 컬럼 추가 (unsigned int로 work_issue_reports.report_id와 타입 일치) table.integer('issue_report_id').unsigned().nullable() .comment('work_issue_reports의 report_id (신고된 이슈 연결)') .after('error_type_id'); // 2. 외래키 추가 (work_issue_reports.report_id 참조) table.foreign('issue_report_id') .references('report_id') .inTable('work_issue_reports') .onDelete('SET NULL'); // 3. 인덱스 추가 table.index('issue_report_id'); }) // 4. error_type_id를 NULL 허용으로 변경 .then(function() { return knex.raw(` ALTER TABLE work_report_defects MODIFY COLUMN error_type_id INT NULL COMMENT 'error_types의 id (부적합 원인) - 레거시, issue_report_id 사용 권장' `); }) // 5. 유니크 제약 수정 (issue_report_id도 고려) .then(function() { // 기존 유니크 제약 삭제 return knex.raw(` ALTER TABLE work_report_defects DROP INDEX work_report_defects_report_id_error_type_id_unique `).catch(() => { // 인덱스가 없을 수 있음 - 무시 }); }) .then(function() { // 새 유니크 제약 추가 (report_id + issue_report_id 조합) return knex.raw(` ALTER TABLE work_report_defects ADD UNIQUE INDEX work_report_defects_report_issue_unique (report_id, issue_report_id) `).catch(() => { // 이미 존재할 수 있음 - 무시 }); }); }; exports.down = function(knex) { return knex.schema .alterTable('work_report_defects', function(table) { // 외래키 및 인덱스 삭제 table.dropForeign('issue_report_id'); table.dropIndex('issue_report_id'); table.dropColumn('issue_report_id'); }) // error_type_id를 다시 NOT NULL로 변경 .then(function() { return knex.raw(` ALTER TABLE work_report_defects MODIFY COLUMN error_type_id INT NOT NULL COMMENT 'error_types의 id (부적합 원인)' `); }) // 기존 유니크 제약 복원 .then(function() { return knex.raw(` ALTER TABLE work_report_defects DROP INDEX IF EXISTS work_report_defects_report_issue_unique `).catch(() => {}); }) .then(function() { return knex.raw(` ALTER TABLE work_report_defects ADD UNIQUE INDEX work_report_defects_report_id_error_type_id_unique (report_id, error_type_id) `).catch(() => {}); }); };