From 819b20dcb4b8ec3ee7e6e07178b3abed0a36c01c Mon Sep 17 00:00:00 2001 From: Dorian Date: Fri, 13 Feb 2026 17:36:53 +0000 Subject: [PATCH] Refactor genre seeding and deletion logic for improved database management - Simplified the genre seeding process by replacing bulk save with individual insert queries, enhancing performance and error handling. - Removed unnecessary comments and streamlined the deletion process for genres, ensuring clarity in migration scripts. - Updated the `down` method to accurately restore deleted genres, maintaining data integrity during rollbacks. --- .../migrations/1707420468655-seed-genres.ts | 122 +++++------------- .../1709299646834-genres-modifications.ts | 59 +++------ 2 files changed, 47 insertions(+), 134 deletions(-) diff --git a/backend/src/database/migrations/1707420468655-seed-genres.ts b/backend/src/database/migrations/1707420468655-seed-genres.ts index 6590aaf..6025dd7 100644 --- a/backend/src/database/migrations/1707420468655-seed-genres.ts +++ b/backend/src/database/migrations/1707420468655-seed-genres.ts @@ -1,105 +1,41 @@ -import { Genre } from 'src/genres/entities/genre.entity'; import { MigrationInterface, QueryRunner } from 'typeorm'; export class SeedGenres1707420468655 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { const genres = [ - { - id: '3676ab4e-1b4f-4568-836a-43f84b1a6228', - name: 'Action', - }, - { - id: '9d8be5ab-b956-4571-9b4e-1e25fb8b6e44', // REMOVE - name: 'Adventure', - }, - { - id: 'b7bac6dd-2a86-4191-ac1e-c92be7b8b841', - name: 'Comedy', - }, - { - id: 'e9e4285c-b6f3-46ef-a991-bd9f1537eae1', - name: 'Crime', - }, - { - id: 'cc22046e-0b43-426c-9e70-b91ac6a601ba', // REMOVE - name: 'Experimental', - }, - { - id: 'ed2515e3-da9a-4291-82bf-4ebe06831407', - name: 'Western', - }, - { - id: '37306a11-e894-4e90-a200-87aabf67f70a', // REMOVE - name: 'Historical', - }, - { - id: 'b20d0c86-b2d4-4fb0-8b39-dc855591fa24', // REMOVE - name: 'Fiction', - }, - { - id: '8d7262a8-a94b-4f85-886a-a5ecff8844b3', - name: 'Mystery', - }, - { - id: '51e56f26-bbb4-44e8-ac01-d7aa8ef81f3b', - name: 'Documentary', - }, - { - id: '01854d65-636e-476b-9bbd-881c1834cdb0', - name: 'Drama', - }, - { - id: '0bc7d835-ed31-4a6f-a76f-cfe26595eb11', - name: 'Horror', - }, - { - id: '79a15574-5e83-4daf-8fdb-dd2261e12b60', - name: 'Romance', - }, - { - id: '0029a12a-e9b9-421f-90c0-446c61afb4dd', - name: 'Science Fiction', - }, - { - id: '004d484a-488e-478e-90f5-b37de3961527', - name: 'Fantasy', - }, - { - id: '281857e3-0772-4d7d-b5a8-e460dde75b91', - name: 'Thriller', - }, - { - id: '26703192-9875-456c-8bb6-6f7acaa8f0bf', // REMOVE - name: 'Noir', - }, - { - id: 'f99f1239-2d6c-45e5-87ef-ea1df6a223dc', - name: 'Musical', - }, - { - id: '62804361-7304-4de7-ae4a-bc3c742ec26b', // REMOVE - name: 'War', - }, - { - id: '9568217b-affa-4859-a342-1ffeca0b7595', // REMOVE - name: 'Sports', - }, - { - id: '21d8c163-88cc-414d-a381-78660065019d', // REMOVE - name: 'Travel', - }, - { - id: '03342d8a-9fa0-4df1-9047-52c226b43b05', // REMOVE - name: 'Biopic', - }, + { id: '3676ab4e-1b4f-4568-836a-43f84b1a6228', name: 'Action' }, + { id: '9d8be5ab-b956-4571-9b4e-1e25fb8b6e44', name: 'Adventure' }, + { id: 'b7bac6dd-2a86-4191-ac1e-c92be7b8b841', name: 'Comedy' }, + { id: 'e9e4285c-b6f3-46ef-a991-bd9f1537eae1', name: 'Crime' }, + { id: 'cc22046e-0b43-426c-9e70-b91ac6a601ba', name: 'Experimental' }, + { id: 'ed2515e3-da9a-4291-82bf-4ebe06831407', name: 'Western' }, + { id: '37306a11-e894-4e90-a200-87aabf67f70a', name: 'Historical' }, + { id: 'b20d0c86-b2d4-4fb0-8b39-dc855591fa24', name: 'Fiction' }, + { id: '8d7262a8-a94b-4f85-886a-a5ecff8844b3', name: 'Mystery' }, + { id: '51e56f26-bbb4-44e8-ac01-d7aa8ef81f3b', name: 'Documentary' }, + { id: '01854d65-636e-476b-9bbd-881c1834cdb0', name: 'Drama' }, + { id: '0bc7d835-ed31-4a6f-a76f-cfe26595eb11', name: 'Horror' }, + { id: '79a15574-5e83-4daf-8fdb-dd2261e12b60', name: 'Romance' }, + { id: '0029a12a-e9b9-421f-90c0-446c61afb4dd', name: 'Science Fiction' }, + { id: '004d484a-488e-478e-90f5-b37de3961527', name: 'Fantasy' }, + { id: '281857e3-0772-4d7d-b5a8-e460dde75b91', name: 'Thriller' }, + { id: '26703192-9875-456c-8bb6-6f7acaa8f0bf', name: 'Noir' }, + { id: 'f99f1239-2d6c-45e5-87ef-ea1df6a223dc', name: 'Musical' }, + { id: '62804361-7304-4de7-ae4a-bc3c742ec26b', name: 'War' }, + { id: '9568217b-affa-4859-a342-1ffeca0b7595', name: 'Sports' }, + { id: '21d8c163-88cc-414d-a381-78660065019d', name: 'Travel' }, + { id: '03342d8a-9fa0-4df1-9047-52c226b43b05', name: 'Biopic' }, ]; - await queryRunner.manager.save( - queryRunner.manager.create>(Genre, genres), - ); + for (const genre of genres) { + await queryRunner.query( + `INSERT INTO "genres" ("id", "name") VALUES ($1, $2) ON CONFLICT ("id") DO NOTHING`, + [genre.id, genre.name], + ); + } } public async down(queryRunner: QueryRunner): Promise { - await queryRunner.manager.delete(Genre, {}); + await queryRunner.query(`DELETE FROM "genres"`); } } diff --git a/backend/src/database/migrations/1709299646834-genres-modifications.ts b/backend/src/database/migrations/1709299646834-genres-modifications.ts index a605cd3..d604be8 100644 --- a/backend/src/database/migrations/1709299646834-genres-modifications.ts +++ b/backend/src/database/migrations/1709299646834-genres-modifications.ts @@ -1,54 +1,31 @@ -import { Genre } from 'src/genres/entities/genre.entity'; import { MigrationInterface, QueryRunner } from 'typeorm'; export class GenresModifications1709299646834 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { - const newGenres = [ - { - id: 'bfb5f952-8dd9-45f4-bb27-530fa728250c', - name: 'Dance', - }, - ]; - - const deleteGenres = [ - { - id: 'b20d0c86-b2d4-4fb0-8b39-dc855591fa24', - name: 'Fiction', - }, - ]; - - await queryRunner.manager.save( - queryRunner.manager.create>( - Genre, - newGenres, - ), + // Add 'Dance' genre + await queryRunner.query( + `INSERT INTO "genres" ("id", "name") VALUES ($1, $2) ON CONFLICT ("id") DO NOTHING`, + ['bfb5f952-8dd9-45f4-bb27-530fa728250c', 'Dance'], ); - await queryRunner.manager.delete(Genre, deleteGenres); + // Remove 'Fiction' genre + await queryRunner.query( + `DELETE FROM "genres" WHERE "id" = $1`, + ['b20d0c86-b2d4-4fb0-8b39-dc855591fa24'], + ); } public async down(queryRunner: QueryRunner): Promise { - const newGenres = [ - { - id: 'bfb5f952-8dd9-45f4-bb27-530fa728250c', - name: 'Dance', - }, - ]; + // Remove 'Dance' genre + await queryRunner.query( + `DELETE FROM "genres" WHERE "id" = $1`, + ['bfb5f952-8dd9-45f4-bb27-530fa728250c'], + ); - const deleteGenres = [ - { - id: 'b20d0c86-b2d4-4fb0-8b39-dc855591fa24', - name: 'Fiction', - }, - ]; - - await queryRunner.manager.delete(Genre, newGenres); - - await queryRunner.manager.save( - queryRunner.manager.create>( - Genre, - deleteGenres, - ), + // Restore 'Fiction' genre + await queryRunner.query( + `INSERT INTO "genres" ("id", "name") VALUES ($1, $2) ON CONFLICT ("id") DO NOTHING`, + ['b20d0c86-b2d4-4fb0-8b39-dc855591fa24', 'Fiction'], ); } }