Skip to content

Commit

Permalink
refactor(modules/entities): change unique constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Mar 27, 2024
1 parent 5e1e37e commit f1b7e1c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class ChangeUniqueConstraintsForEntities1711444943012
implements MigrationInterface
{
name = 'ChangeUniqueConstraintsForEntities1711444943012'

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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")`
)
}
}
2 changes: 2 additions & 0 deletions src/migrations/all.ts
Original file line number Diff line number Diff line change
@@ -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,
]
10 changes: 4 additions & 6 deletions src/modules/albums/album.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import {
OneToMany,
PrimaryGeneratedColumn,
Relation,
Unique,
} from 'typeorm'

import type { Image } from '@modules/images'
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

Expand All @@ -41,9 +41,7 @@ export class Album {
@ApiProperty({ type: Number })
totalTracks: number

@Column('varchar', {
unique: true,
})
@Column('varchar')
@ApiProperty()
href: string

Expand Down
10 changes: 4 additions & 6 deletions src/modules/artists/artist.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,27 @@ 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

@Column('varchar')
@ApiProperty()
name: string

@Column('varchar', {
unique: true,
})
@Column('varchar')
@ApiProperty()
href: string

Expand Down
7 changes: 3 additions & 4 deletions src/modules/images/image.entity.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -17,9 +18,7 @@ export class Image implements SdkImage {
@ApiProperty({ type: Number })
width: number

@Column('varchar', {
unique: true,
})
@Column('varchar')
@ApiProperty()
url: string
}
10 changes: 4 additions & 6 deletions src/modules/tracks/track.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@ import {
OneToMany,
PrimaryGeneratedColumn,
Relation,
Unique,
} from 'typeorm'

import type { Album } from '@modules/albums'
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

@Column('varchar')
@ApiProperty()
name: string

@Column('varchar', {
unique: true,
})
@Column('varchar')
@ApiProperty()
href: string

Expand Down

0 comments on commit f1b7e1c

Please sign in to comment.