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

fix(schema-engine): Ignore views for MongoDB Schemas #4167

Merged
merged 7 commits into from
Aug 26, 2023

Conversation

christianledgard
Copy link
Contributor

Related to #18424 and #17006 issues.

Proposed trivial approach: While iterating the collections, if we detect a View, we continue to the next collection.

Related to #18424 and #17006 issues at /prisma/prisma repo.
@christianledgard christianledgard requested a review from a team as a code owner August 24, 2023 12:09
@CLAassistant
Copy link

CLAassistant commented Aug 24, 2023

CLA assistant check
All committers have signed the CLA.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 24, 2023

CodSpeed Performance Report

Merging #4167 will not alter performance

Comparing christianledgard:fix-schema-engine-mongodb-views (1edbbdc) with main (f957933)

Summary

✅ 11 untouched benchmarks

Copy link
Member

@Jolg42 Jolg42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking this because I tried it and while the error message is gone it brings unexpected changes

I ran PRISMA_SCHEMA_ENGINE_BINARY=/Users/j42/Dev/prisma-engines/target/debug/schema-engine npx prisma db pull --force which ran successfully.

Though check the output

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

datasource db {
  provider = "mongodb"
  url      = "mongodb://root:prisma@localhost:27018/hey?authSource=admin"
}

type System.viewsPipeline {
lookup System.viewsPipeline$lookup? @map("$lookup")
project System.viewsPipeline$project? @map("$project")
unwind String? @map("$unwind")
}

type System.viewsPipeline$lookup {
as String
foreignField String
from String
localField String
}

type System.viewsPipeline$project {
id Int @map("_id")
bio String
email Int
name Int
}

model Plan {
  id String @id @default(auto()) @map("_id") @db.ObjectId
}

model ho {
  id String @id @default(auto()) @map("_id") @db.ObjectId
}

model system_views {
  id     String @id @map("_id")
  pipeline System.viewsPipeline[]
  viewOn String

  @@map("system.views")
}

The type System.* are unexpected (and also not valid syntax, no period is allowed in the name).
So it looks like some logic needs to be adapted to ignore introspecting the system collection itself?

@Jolg42
Copy link
Member

Jolg42 commented Aug 25, 2023

I first ran npx prisma db push, then,
I ran in mongosh shell (inside MongoDB Compass)

use hey

db.createView('UserInfo', 'User', [
  {
    $lookup: {
      from: 'Profile',
      localField: '_id',
      foreignField: 'userId',
      as: 'ProfileData',
    },
  },
  {
    $project: {
      _id: 1,
      email: 1,
      name: 1,
      bio: '$ProfileData.bio',
    },
  },
  { $unwind: '$bio' },
])

db.getCollectionInfos()

(where hey is my improvised database name 😄)

It returned

[
  {
    name: 'myModel',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("2c070c30-bdf1-4f9d-abfd-5120d56132a0")
    },
    idIndex: { v: 2, key: [Object], name: '_id_' }
  },
  {
    name: 'system.views',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("d3d65bdb-d7e1-42a4-9083-b605926b321d")
    },
    idIndex: { v: 2, key: [Object], name: '_id_' }
  },
  {
    name: 'UserInfo',
    type: 'view',
    options: { viewOn: 'User', pipeline: [Array] },
    info: { readOnly: true }
  }
]

Based on the docs, it makes sense that the system.views collection makes an appearance inside my hey database:

The .system.views collection contains information about each view in the database.
https://www.mongodb.com/docs/manual/reference/system-collections/#mongodb-data--database-.system.views

@Jolg42
Copy link
Member

Jolg42 commented Aug 25, 2023

I pushed a fix. I'll ask a colleague about how we want to test this.

@Jolg42 Jolg42 added this to the 5.3.0 milestone Aug 25, 2023
@Jolg42 Jolg42 requested a review from tomhoule August 25, 2023 09:25
@Jolg42
Copy link
Member

Jolg42 commented Aug 25, 2023

Note: I verified that the test was not working before and showed the same issues before the fix.

@Jolg42
Copy link
Member

Jolg42 commented Aug 25, 2023

I'm not sure why Clippy errors on unrelated files?

@janpio
Copy link
Member

janpio commented Aug 25, 2023

I'm not sure why Clippy errors on unrelated files?

That's that clippy usually does 😄

@aqrln
Copy link
Member

aqrln commented Aug 25, 2023

I'm not sure why Clippy errors on unrelated files?

Rust 1.72 was released yesterday so probably was now updated on CI.

Copy link
Contributor

@tomhoule tomhoule left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fixes and test look good 💯

// We need to skip system collections, they are only used by MongoDB internally.
// https://www.mongodb.com/docs/manual/reference/system-collections/
if collection_type == mongodb::results::CollectionType::Collection && collection_name.starts_with("system.") {
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these show up because we have a view? That's a bit mysterious.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when there is a view inside the database MongoDB stores some information inside that same database as a system.views (invisible in GUI) collection

@Jolg42
Copy link
Member

Jolg42 commented Aug 25, 2023

I'll wait for the clippy PR to be merged before merging this

@Jolg42 Jolg42 merged commit d7eca61 into prisma:main Aug 26, 2023
48 checks passed
@Jolg42
Copy link
Member

Jolg42 commented Aug 28, 2023

Note: this is now available in 5.3.0-dev.9 if you want to try it out.
It will be released in the official 5.3.0 version on Tuesday, September, 12th.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants