Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal: reformatter is not idempotent #12726

Closed
tomhoule opened this issue Apr 8, 2022 · 0 comments · Fixed by prisma/prisma-engines#3016
Closed

Internal: reformatter is not idempotent #12726

tomhoule opened this issue Apr 8, 2022 · 0 comments · Fixed by prisma/prisma-engines#3016
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. tech/engines/datamodel Issue about parsing/validation/rendering of the Prisma schema tech/engines Issue for tech Engines. topic: internal topic: prisma format CLI: prisma format
Milestone

Comments

@tomhoule
Copy link
Contributor

tomhoule commented Apr 8, 2022

generator client {
  provider        = "prisma-client-js"
  previewFeatures = "mongodb"
}

datasource db {
  provider = "mongodb"
  url      = "m...ty"
}

model Foo {
  id       String   @id @default(auto()) @map("_id") @db.ObjectId
  name     String   @unique
  json     Json
  bar      Bar
  bars     Bar[]
  baz      Baz      @relation(fields: [bazId], references: [id])
  bazId    String   @db.ObjectId
  list     String[]
  jsonList Json[]
}

type Bar {
  label  String
  number Int
}

model Baz {
  id  String @id @default(auto()) @map("_id") @db.ObjectId
  foo Foo?
}

The problem is the ordering of the attributes on bazId. The first reformatter run adds an @unique attribute because it's a 1:1 relation. Due to the way the reformatter is implemented, that attribute lands at the end of the list. The second run applies the expected ordering for attributes, putting @unique before @db.ObjectId.

related: #12580

@tomhoule tomhoule added bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: internal tech/engines Issue for tech Engines. tech/engines/datamodel Issue about parsing/validation/rendering of the Prisma schema team/schema Issue for team Schema. labels Apr 8, 2022
tomhoule added a commit to prisma/prisma-engines that referenced this issue Apr 8, 2022
In 1:1 relations, the reformatter can add `@unique` attributes. If they
come after a native type attribute, the reformatter failed to add
adequate spacing between the attributes, resulting in valid but odd
looking schemas. This commit fixes that problem.

These schemas are also not idempotent, because of interactions between
attribute sorting and missing attributes addition. Issue:
prisma/prisma#12726

closes prisma/prisma#12596
tomhoule added a commit to prisma/prisma-engines that referenced this issue Apr 8, 2022
In 1:1 relations, the reformatter can add `@unique` attributes. If they
come after a native type attribute, the reformatter failed to add
adequate spacing between the attributes, resulting in valid but odd
looking schemas. This commit fixes that problem.

These schemas are also not idempotent, because of interactions between
attribute sorting and missing attributes addition. Issue:
prisma/prisma#12726

closes prisma/prisma#12596
@janpio janpio added the topic: prisma format CLI: prisma format label Apr 8, 2022
tomhoule added a commit to prisma/prisma-engines that referenced this issue Jun 29, 2022
…eformatting

That way we can do pure AST reformatting if we want, and reuse the logic
for relation and relation scalar field additions in code actions. As a
side-effect, that makes the formatter idempotent.

There will be a follow-up PR to split the two functionalities in two
different modules, but I stopped here for readability in this PR.

closes prisma/prisma#12726
tomhoule added a commit to prisma/prisma-engines that referenced this issue Jun 29, 2022
…eformatting

That way we can do pure AST reformatting if we want, and reuse the logic
for relation and relation scalar field additions in code actions. As a
side-effect, that makes the formatter idempotent.

There will be a follow-up PR to split the two functionalities in two
different modules, but I stopped here for readability in this PR.

closes prisma/prisma#12726
@janpio janpio added this to the 4.1.0 milestone Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. tech/engines/datamodel Issue about parsing/validation/rendering of the Prisma schema tech/engines Issue for tech Engines. topic: internal topic: prisma format CLI: prisma format
Projects
None yet
2 participants