diff --git a/knex/migrations/20251109095712_remove_orphan_mentor_role.js b/knex/migrations/20251109095712_remove_orphan_mentor_role.js new file mode 100644 index 0000000..b886330 --- /dev/null +++ b/knex/migrations/20251109095712_remove_orphan_mentor_role.js @@ -0,0 +1,33 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = async function(knex) { + return knex.transaction(async (trx) => { + const mentorRole = await trx('roles').where({ role_name: 'mentor' }).first(); + + if (!mentorRole) { + console.warn('Mentor role not found — skipping cleanup.'); + return; + } + + await trx('user_roles') + .where('role_id', mentorRole.id) + .whereIn('user_id', function () { + this.select('user_id') + .from('user_roles') + .where('role_id', mentorRole.id) + .whereNotIn('user_id', trx('mentors').select('user_id')); + }) + .del(); + }); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = async function(_) { + // This migration is not reversible because deleted rows cannot be recovered. + console.warn('⚠️ This migration is not reversible (cannot recover deleted rows)'); +}; \ No newline at end of file diff --git a/services/mentor_service.js b/services/mentor_service.js index 79635bc..331682b 100644 --- a/services/mentor_service.js +++ b/services/mentor_service.js @@ -40,7 +40,6 @@ async function createMentor(userId, mentorData) { status: 'pending', about: mentorData.about, contact: mentorData.contact, - tags: mentorData.tags }; const mentorInfo = await repo.createMentor(mentor); diff --git a/tests/services/mentor_service.test.js b/tests/services/mentor_service.test.js index 33798ba..986d8b9 100644 --- a/tests/services/mentor_service.test.js +++ b/tests/services/mentor_service.test.js @@ -36,8 +36,7 @@ describe('Mentor Service', () => { user_id: defaultUserId, status: MENTOR_STATUSES.pending, about: mentorData.about, - contact: mentorData.contact, - tags: mentorData.tags + contact: mentorData.contact }); expect(tagsRepo.updateMentorTags).toHaveBeenCalledWith(createdMentor.id, mentorData.tags); expect(rolesService.getAdminEmails).toHaveBeenCalled();