From 4fb87fae3cfd903f9b88e142c9f3334efe65eb70 Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Mon, 18 Jan 2021 22:25:23 +0200 Subject: [PATCH] fix: ciBuildId ignores project scope Closes https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker/issues/183 --- .../README.md | 52 ++++++++ .../schema.prisma | 122 ++++++++++++++++++ .../steps.json | 32 +++++ prisma/migrations/migrate.lock | 3 +- prisma/schema.prisma | 4 +- src/builds/builds.service.spec.ts | 11 +- src/builds/builds.service.ts | 5 +- 7 files changed, 219 insertions(+), 10 deletions(-) create mode 100644 prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/README.md create mode 100644 prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/schema.prisma create mode 100644 prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/steps.json diff --git a/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/README.md b/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/README.md new file mode 100644 index 00000000..aee525c5 --- /dev/null +++ b/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/README.md @@ -0,0 +1,52 @@ +# Migration `20210118201534-build--project-id---ci-build-id-constraint` + +This migration has been generated by Pavel Strunkin at 1/18/2021, 10:15:34 PM. +You can check out the [state of the schema](./schema.prisma) after the migration. + +## Database Steps + +```sql +DROP INDEX "Build.ciBuildId_unique" + +CREATE UNIQUE INDEX "Build.projectId_ciBuildId_unique" ON "Build"("projectId", "ciBuildId") +``` + +## Changes + +```diff +diff --git schema.prisma schema.prisma +migration 20201201211711-test-run--temp-ignore-areas-added..20210118201534-build--project-id---ci-build-id-constraint +--- datamodel.dml ++++ datamodel.dml +@@ -1,17 +1,16 @@ + generator client { +- provider = "prisma-client-js" +- previewFeatures = ["atomicNumberOperations"] ++ provider = "prisma-client-js" + } + datasource db { + provider = "postgresql" +- url = "***" ++ url = "***" + } + model Build { + id String @id @default(uuid()) +- ciBuildId String? @unique ++ ciBuildId String? + number Int? + branchName String? + status String? + testRuns TestRun[] +@@ -21,8 +20,10 @@ + createdAt DateTime @default(now()) + user User? @relation(fields: [userId], references: [id]) + userId String? + isRunning Boolean? ++ ++ @@unique([projectId, ciBuildId]) + } + model Project { + id String @id @default(uuid()) +``` + + diff --git a/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/schema.prisma b/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/schema.prisma new file mode 100644 index 00000000..01ef0d55 --- /dev/null +++ b/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/schema.prisma @@ -0,0 +1,122 @@ +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = "***" +} + +model Build { + id String @id @default(uuid()) + ciBuildId String? + number Int? + branchName String? + status String? + testRuns TestRun[] + projectId String + project Project @relation(fields: [projectId], references: [id]) + updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) + user User? @relation(fields: [userId], references: [id]) + userId String? + isRunning Boolean? + + @@unique([projectId, ciBuildId]) +} + +model Project { + id String @id @default(uuid()) + name String + mainBranchName String @default("master") + builds Build[] + buildsCounter Int @default(0) + testVariations TestVariation[] + updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) + + @@unique([name]) +} + +model TestRun { + id String @id @default(uuid()) + imageName String + diffName String? + diffPercent Float? + diffTollerancePercent Float @default(0) + pixelMisMatchCount Int? + status TestStatus + buildId String + build Build @relation(fields: [buildId], references: [id]) + testVariationId String + testVariation TestVariation @relation(fields: [testVariationId], references: [id]) + merge Boolean @default(false) + updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) + // Test variation data + name String @default("") + browser String? + device String? + os String? + viewport String? + baselineName String? + comment String? + baseline Baseline? + branchName String @default("master") + baselineBranchName String? + ignoreAreas String @default("[]") + tempIgnoreAreas String @default("[]") +} + +model TestVariation { + id String @id @default(uuid()) + name String + branchName String @default("master") + browser String? + device String? + os String? + viewport String? + baselineName String? + ignoreAreas String @default("[]") + projectId String + project Project @relation(fields: [projectId], references: [id]) + testRuns TestRun[] + baselines Baseline[] + comment String? + updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) + + @@unique([projectId, name, browser, device, os, viewport, branchName]) +} + +model Baseline { + id String @id @default(uuid()) + baselineName String + testVariationId String + testVariation TestVariation @relation(fields: [testVariationId], references: [id]) + testRunId String? + testRun TestRun? @relation(fields: [testRunId], references: [id]) + updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) +} + +model User { + id String @id @default(uuid()) + email String @unique + password String + firstName String? + lastName String? + apiKey String @unique + isActive Boolean @default(true) + builds Build[] + updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) +} + +enum TestStatus { + failed + new + ok + unresolved + approved +} diff --git a/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/steps.json b/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/steps.json new file mode 100644 index 00000000..095b8973 --- /dev/null +++ b/prisma/migrations/20210118201534-build--project-id---ci-build-id-constraint/steps.json @@ -0,0 +1,32 @@ +{ + "version": "0.3.14-fixed", + "steps": [ + { + "tag": "DeleteDirective", + "location": { + "path": { + "tag": "Field", + "model": "Build", + "field": "ciBuildId" + }, + "directive": "unique" + } + }, + { + "tag": "CreateDirective", + "location": { + "path": { + "tag": "Model", + "model": "Build", + "arguments": [ + { + "name": "", + "value": "[projectId, ciBuildId]" + } + ] + }, + "directive": "unique" + } + } + ] +} \ No newline at end of file diff --git a/prisma/migrations/migrate.lock b/prisma/migrations/migrate.lock index 1cd56966..7fb1fb04 100644 --- a/prisma/migrations/migrate.lock +++ b/prisma/migrations/migrate.lock @@ -11,4 +11,5 @@ 20200909223305-test-variation-project-id-added-into-unique-constraint 20201007145002-builds-counter 20201115155739-ci-build-id-added -20201201211711-test-run--temp-ignore-areas-added \ No newline at end of file +20201201211711-test-run--temp-ignore-areas-added +20210118201534-build--project-id---ci-build-id-constraint \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3edcdb5d..95573aa8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -9,7 +9,7 @@ datasource db { model Build { id String @id @default(uuid()) - ciBuildId String? @unique + ciBuildId String? number Int? branchName String? status String? @@ -21,6 +21,8 @@ model Build { user User? @relation(fields: [userId], references: [id]) userId String? isRunning Boolean? + + @@unique([projectId, ciBuildId]) } model Project { diff --git a/src/builds/builds.service.spec.ts b/src/builds/builds.service.spec.ts index d005ea16..4e45f093 100644 --- a/src/builds/builds.service.spec.ts +++ b/src/builds/builds.service.spec.ts @@ -215,7 +215,10 @@ describe('BuildsService', () => { expect(projectFindOneMock).toHaveBeenCalledWith(createBuildDto.project); expect(buildFindUniqueMock).toHaveBeenCalledWith({ where: { - ciBuildId: createBuildDto.ciBuildId, + projectId_ciBuildId: { + projectId: project.id, + ciBuildId: createBuildDto.ciBuildId, + }, }, }); expect(projectUpdateMock).toHaveBeenCalledWith({ @@ -254,12 +257,6 @@ describe('BuildsService', () => { const result = await service.create(createBuildDto); - expect(projectFindOneMock).toHaveBeenCalledWith(createBuildDto.project); - expect(buildFindUniqueMock).toHaveBeenCalledWith({ - where: { - ciBuildId: createBuildDto.ciBuildId, - }, - }); expect(result).toBe(buildDto); }); }); diff --git a/src/builds/builds.service.ts b/src/builds/builds.service.ts index 5e3bc846..b0ac5a30 100644 --- a/src/builds/builds.service.ts +++ b/src/builds/builds.service.ts @@ -59,7 +59,10 @@ export class BuildsService { if (createBuildDto.ciBuildId) { build = await this.prismaService.build.findUnique({ where: { - ciBuildId: createBuildDto.ciBuildId, + projectId_ciBuildId: { + projectId: project.id, + ciBuildId: createBuildDto.ciBuildId, + }, }, }); }