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.
This commit is contained in:
@@ -1,105 +1,41 @@
|
|||||||
import { Genre } from 'src/genres/entities/genre.entity';
|
|
||||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
export class SeedGenres1707420468655 implements MigrationInterface {
|
export class SeedGenres1707420468655 implements MigrationInterface {
|
||||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
const genres = [
|
const genres = [
|
||||||
{
|
{ id: '3676ab4e-1b4f-4568-836a-43f84b1a6228', name: 'Action' },
|
||||||
id: '3676ab4e-1b4f-4568-836a-43f84b1a6228',
|
{ id: '9d8be5ab-b956-4571-9b4e-1e25fb8b6e44', name: 'Adventure' },
|
||||||
name: 'Action',
|
{ 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: '9d8be5ab-b956-4571-9b4e-1e25fb8b6e44', // REMOVE
|
{ id: 'ed2515e3-da9a-4291-82bf-4ebe06831407', name: 'Western' },
|
||||||
name: 'Adventure',
|
{ 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: 'b7bac6dd-2a86-4191-ac1e-c92be7b8b841',
|
{ id: '51e56f26-bbb4-44e8-ac01-d7aa8ef81f3b', name: 'Documentary' },
|
||||||
name: 'Comedy',
|
{ 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: 'e9e4285c-b6f3-46ef-a991-bd9f1537eae1',
|
{ id: '0029a12a-e9b9-421f-90c0-446c61afb4dd', name: 'Science Fiction' },
|
||||||
name: 'Crime',
|
{ 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: 'cc22046e-0b43-426c-9e70-b91ac6a601ba', // REMOVE
|
{ id: 'f99f1239-2d6c-45e5-87ef-ea1df6a223dc', name: 'Musical' },
|
||||||
name: 'Experimental',
|
{ 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: 'ed2515e3-da9a-4291-82bf-4ebe06831407',
|
{ id: '03342d8a-9fa0-4df1-9047-52c226b43b05', name: 'Biopic' },
|
||||||
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',
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
|
|
||||||
await queryRunner.manager.save(
|
for (const genre of genres) {
|
||||||
queryRunner.manager.create<Genre, Record<string, unknown>>(Genre, 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<void> {
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
await queryRunner.manager.delete(Genre, {});
|
await queryRunner.query(`DELETE FROM "genres"`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,54 +1,31 @@
|
|||||||
import { Genre } from 'src/genres/entities/genre.entity';
|
|
||||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
export class GenresModifications1709299646834 implements MigrationInterface {
|
export class GenresModifications1709299646834 implements MigrationInterface {
|
||||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
const newGenres = [
|
// Add 'Dance' genre
|
||||||
{
|
await queryRunner.query(
|
||||||
id: 'bfb5f952-8dd9-45f4-bb27-530fa728250c',
|
`INSERT INTO "genres" ("id", "name") VALUES ($1, $2) ON CONFLICT ("id") DO NOTHING`,
|
||||||
name: 'Dance',
|
['bfb5f952-8dd9-45f4-bb27-530fa728250c', 'Dance'],
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const deleteGenres = [
|
|
||||||
{
|
|
||||||
id: 'b20d0c86-b2d4-4fb0-8b39-dc855591fa24',
|
|
||||||
name: 'Fiction',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
await queryRunner.manager.save(
|
|
||||||
queryRunner.manager.create<Genre, Record<string, unknown>>(
|
|
||||||
Genre,
|
|
||||||
newGenres,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
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<void> {
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
const newGenres = [
|
// Remove 'Dance' genre
|
||||||
{
|
await queryRunner.query(
|
||||||
id: 'bfb5f952-8dd9-45f4-bb27-530fa728250c',
|
`DELETE FROM "genres" WHERE "id" = $1`,
|
||||||
name: 'Dance',
|
['bfb5f952-8dd9-45f4-bb27-530fa728250c'],
|
||||||
},
|
);
|
||||||
];
|
|
||||||
|
|
||||||
const deleteGenres = [
|
// Restore 'Fiction' genre
|
||||||
{
|
await queryRunner.query(
|
||||||
id: 'b20d0c86-b2d4-4fb0-8b39-dc855591fa24',
|
`INSERT INTO "genres" ("id", "name") VALUES ($1, $2) ON CONFLICT ("id") DO NOTHING`,
|
||||||
name: 'Fiction',
|
['b20d0c86-b2d4-4fb0-8b39-dc855591fa24', 'Fiction'],
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
await queryRunner.manager.delete(Genre, newGenres);
|
|
||||||
|
|
||||||
await queryRunner.manager.save(
|
|
||||||
queryRunner.manager.create<Genre, Record<string, unknown>>(
|
|
||||||
Genre,
|
|
||||||
deleteGenres,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user