From f1b7e1c1e5fe333e83314a2ec0a85c0a79c0955b Mon Sep 17 00:00:00 2001 From: Mnigos Date: Tue, 26 Mar 2024 10:23:14 +0100 Subject: [PATCH] refactor(modules/entities): change unique constraints --- ...-change-unique-constraints-for-entities.ts | 67 +++++++++++++++++++ src/migrations/all.ts | 2 + src/modules/albums/album.entity.ts | 10 ++- src/modules/artists/artist.entity.ts | 10 ++- src/modules/images/image.entity.ts | 7 +- src/modules/tracks/track.entity.ts | 10 ++- 6 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 src/migrations/1711444943012-change-unique-constraints-for-entities.ts diff --git a/src/migrations/1711444943012-change-unique-constraints-for-entities.ts b/src/migrations/1711444943012-change-unique-constraints-for-entities.ts new file mode 100644 index 00000000..af4662b0 --- /dev/null +++ b/src/migrations/1711444943012-change-unique-constraints-for-entities.ts @@ -0,0 +1,67 @@ +import { MigrationInterface, QueryRunner } from 'typeorm' + +export class ChangeUniqueConstraintsForEntities1711444943012 + implements MigrationInterface +{ + name = 'ChangeUniqueConstraintsForEntities1711444943012' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "track" DROP CONSTRAINT "UQ_2315a3e4d182a2a460f2e773693"` + ) + await queryRunner.query( + `ALTER TABLE "track" DROP CONSTRAINT "UQ_10a3b0c40212c31ba55419523a4"` + ) + await queryRunner.query( + `ALTER TABLE "album" DROP CONSTRAINT "UQ_d9c245cc24c616be9b5d72900f5"` + ) + await queryRunner.query( + `ALTER TABLE "album" DROP CONSTRAINT "UQ_195b9a4b8052042a431847461ab"` + ) + await queryRunner.query( + `ALTER TABLE "artist" DROP CONSTRAINT "UQ_941b1071e844e94caec980a929c"` + ) + await queryRunner.query( + `ALTER TABLE "artist" DROP CONSTRAINT "UQ_3df7b480e9f4bc6eecca12a31a0"` + ) + await queryRunner.query( + `ALTER TABLE "track" ADD CONSTRAINT "TRACK_UNIQUE" UNIQUE ("externalId", "href")` + ) + await queryRunner.query( + `ALTER TABLE "album" ADD CONSTRAINT "ALBUM_UNIQUE" UNIQUE ("externalId", "href")` + ) + await queryRunner.query( + `ALTER TABLE "artist" ADD CONSTRAINT "ARTIST_UNIQUE" UNIQUE ("externalId", "href")` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "artist" DROP CONSTRAINT "ARTIST_UNIQUE"` + ) + await queryRunner.query( + `ALTER TABLE "album" DROP CONSTRAINT "ALBUM_UNIQUE"` + ) + await queryRunner.query( + `ALTER TABLE "track" DROP CONSTRAINT "TRACK_UNIQUE"` + ) + await queryRunner.query( + `ALTER TABLE "artist" ADD CONSTRAINT "UQ_3df7b480e9f4bc6eecca12a31a0" UNIQUE ("href")` + ) + await queryRunner.query( + `ALTER TABLE "artist" ADD CONSTRAINT "UQ_941b1071e844e94caec980a929c" UNIQUE ("externalId")` + ) + await queryRunner.query( + `ALTER TABLE "album" ADD CONSTRAINT "UQ_195b9a4b8052042a431847461ab" UNIQUE ("href")` + ) + await queryRunner.query( + `ALTER TABLE "album" ADD CONSTRAINT "UQ_d9c245cc24c616be9b5d72900f5" UNIQUE ("externalId")` + ) + await queryRunner.query( + `ALTER TABLE "track" ADD CONSTRAINT "UQ_10a3b0c40212c31ba55419523a4" UNIQUE ("href")` + ) + await queryRunner.query( + `ALTER TABLE "track" ADD CONSTRAINT "UQ_2315a3e4d182a2a460f2e773693" UNIQUE ("externalId")` + ) + } +} diff --git a/src/migrations/all.ts b/src/migrations/all.ts index 321dc93b..38b4dcad 100644 --- a/src/migrations/all.ts +++ b/src/migrations/all.ts @@ -1,9 +1,11 @@ import { Migration1697539455661 } from './1697539455661-migration' import { FixUserProfileRelation1701713313421 } from './1701713313421-fix-user-profile-relation' import { HistoryMigration1711027664648 } from './1711027664648-history-migration' +import { ChangeUniqueConstraintsForEntities1711444943012 } from './1711444943012-change-unique-constraints-for-entities' export const migrations = [ Migration1697539455661, FixUserProfileRelation1701713313421, HistoryMigration1711027664648, + ChangeUniqueConstraintsForEntities1711444943012, ] diff --git a/src/modules/albums/album.entity.ts b/src/modules/albums/album.entity.ts index b0746c21..c4c883d1 100644 --- a/src/modules/albums/album.entity.ts +++ b/src/modules/albums/album.entity.ts @@ -7,6 +7,7 @@ import { OneToMany, PrimaryGeneratedColumn, Relation, + Unique, } from 'typeorm' import type { Image } from '@modules/images' @@ -14,14 +15,13 @@ import type { Artist } from '@modules/artists' import type { Track } from '@modules/tracks' @Entity() +@Unique('ALBUM_UNIQUE', ['externalId', 'href']) export class Album { @PrimaryGeneratedColumn('uuid') @ApiProperty() id: string - @Column('varchar', { - unique: true, - }) + @Column('varchar') @ApiProperty() externalId: string @@ -41,9 +41,7 @@ export class Album { @ApiProperty({ type: Number }) totalTracks: number - @Column('varchar', { - unique: true, - }) + @Column('varchar') @ApiProperty() href: string diff --git a/src/modules/artists/artist.entity.ts b/src/modules/artists/artist.entity.ts index ba66b8d1..e05dc44a 100644 --- a/src/modules/artists/artist.entity.ts +++ b/src/modules/artists/artist.entity.ts @@ -6,19 +6,19 @@ import { ManyToMany, PrimaryGeneratedColumn, Relation, + Unique, } from 'typeorm' import type { Image } from '@modules/images' @Entity() +@Unique('ARTIST_UNIQUE', ['externalId', 'href']) export class Artist { @PrimaryGeneratedColumn('uuid') @ApiProperty() id: string - @Column('varchar', { - unique: true, - }) + @Column('varchar') @ApiProperty() externalId: string @@ -26,9 +26,7 @@ export class Artist { @ApiProperty() name: string - @Column('varchar', { - unique: true, - }) + @Column('varchar') @ApiProperty() href: string diff --git a/src/modules/images/image.entity.ts b/src/modules/images/image.entity.ts index fc13df9b..4808521f 100644 --- a/src/modules/images/image.entity.ts +++ b/src/modules/images/image.entity.ts @@ -1,9 +1,10 @@ import { ApiProperty } from '@nestjs/swagger' -import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm' +import { Column, Entity, PrimaryGeneratedColumn, Unique } from 'typeorm' import { SdkImage } from '@common/types/spotify' @Entity() +@Unique('IMAGE_UNIQUE', ['url']) export class Image implements SdkImage { @PrimaryGeneratedColumn('uuid') @ApiProperty() @@ -17,9 +18,7 @@ export class Image implements SdkImage { @ApiProperty({ type: Number }) width: number - @Column('varchar', { - unique: true, - }) + @Column('varchar') @ApiProperty() url: string } diff --git a/src/modules/tracks/track.entity.ts b/src/modules/tracks/track.entity.ts index e2a46edd..8256d997 100644 --- a/src/modules/tracks/track.entity.ts +++ b/src/modules/tracks/track.entity.ts @@ -8,6 +8,7 @@ import { OneToMany, PrimaryGeneratedColumn, Relation, + Unique, } from 'typeorm' import type { Album } from '@modules/albums' @@ -15,14 +16,13 @@ import type { Artist } from '@modules/artists' import type { HistoryTrack } from '@modules/history/tracks' @Entity() +@Unique('TRACK_UNIQUE', ['externalId', 'href']) export class Track { @PrimaryGeneratedColumn('uuid') @ApiProperty() id: string - @Column('varchar', { - unique: true, - }) + @Column('varchar') @ApiProperty() externalId: string @@ -30,9 +30,7 @@ export class Track { @ApiProperty() name: string - @Column('varchar', { - unique: true, - }) + @Column('varchar') @ApiProperty() href: string