Skip to content

feat(schema folder): ORM 665 rework schema finding logic - #26663

Merged
FGoessler merged 5 commits into
mainfrom
feat/ORM-665-rework-schema-finding-logic
Mar 19, 2025
Merged

FGoessler merged 5 commits into
mainfrom
feat/ORM-665-rework-schema-finding-logic

Conversation

@FGoessler

@FGoessler FGoessler commented Mar 18, 2025 •

Copy link
Copy Markdown
Contributor

ORM-665 Rework schema folder finding logic

Summary

  • ./prisma/schema as a folder is not checked by default anymore.
  • Migrations, views and typed sql folders are placed next to the schema file that contains the datasource block.
  • We use a universal schemaPath property in prisma.config.ts to point to a folder or file.

Remove default folder resolving

Instead of trying to find a Prisma Schema folder implicitly based on default locations we want to simplify the resolve logic to:

  • --schema CLI option with either folder or filename
  • prisma.config.ts - either folder or filename explicitly given
  • package.json -> prisma.schema - path to folder or filename given
  • Default locations of ./prisma/schema.prisma or ./schema.prisma - no folder support

(So far we would also check ./prisma/schema if the preview feature is on.)

Why this change? => So it is very explicit for the user that they switched from a default location of a single file to a folder / custom based setup which will change where we look for things.

Change default placement of migrations, views and typed sql folders

Also so far the schema folder has been the reference location to which we place the migrations, views and typed sql folders NEXT to it. Not into it. E.g.:

<<Current/old new state>>
/project-dir
|- /prisma
  |- /schema
    |- schema.prisma
  |- /migrations
  |- /views
  |- /sql

This was likely done this way to preserve the location of the migrations folder when switching from default single file (./prisma/schema.prisma) to default folder location (./prisma/schema).

Ideally we would also support glob patterns to find schema files across a larger variety of paths and enable more flexibility for the user.Even with the current refactoring, that is not easily possible, as we still have some legacy places that rely on the schema path structure (e.g. generators and env var loading).
To eventually support glob pattern based loading we cannot build our logic around a single file or directory path being passed in.

=> Determine the default location of those directories based on the location of the schema file that contains the datasource block. We already do this to find SQLite database files.

<<Expected new state  - given a schema path of e.g. `--schema=./prisma/schema`>>
/project-dir
|- /prisma
  |- /migrations
  |- /views
  |- /sql
  |- /more-schemas
    |- foo.schema
    |- bar.schema
  |- data-source-schema.prisma

Note

Note: With ORM-663 and ORM-664 we will get additional customizable options.

Why this change? => Consistent behavior of how paths work in relation to schema files. Also most people should have their main schema.prisma file on the root of their specified schema path folder.

Unify config options

Most schema config options can take either a directory or a file. E.g. via the cli args or the package.json field. But for the prisma.config.ts we went with an explicit distinction. With the new code structure we don't really need this. We can easily detect whether a given path is a file or a folder and adjust our logic based on that.

Also moving forward we should treat everything as "multi schema file". Having a single schema file is just a special case of that whose details are only relevant during the initial file loading.

So prisma.config.ts becomes:

export default defineConfig({
  earlyAccess: true,
  schema: path.join(process.cwd(), 'prisma', 'schema.prisma'),
})

Why this change? => It's easier to configure, reduces config and code complexity on our end, is aligned with how most other tools accept paths and opens an easy way forward should we later on want to support glob patterns.

@github-actions

github-actions Bot commented Mar 18, 2025 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
packages/client/runtime/library.js 193.91 KB (0%)
packages/client/runtime/library.d.ts 81 B (0%)
packages/client/runtime/binary.js 648.81 KB (0%)
packages/client/runtime/binary.d.ts 26 B (0%)
packages/client/runtime/edge.js 164.07 KB (0%)
packages/client/runtime/edge-esm.js 163.55 KB (0%)
packages/client/runtime/wasm.js 126.94 KB (0%)
packages/client/runtime/index-browser.js 34.16 KB (0%)
packages/client/runtime/index-browser.d.ts 89 B (0%)
packages/cli/build/index.js 2.62 MB (-0.03% 🔽)
packages/client/prisma-client-0.0.0.tgz 4.94 MB (0%)
packages/cli/prisma-0.0.0.tgz 4.01 MB (-0.01% 🔽)
packages/bundle-size/da-workers-libsql/output.tgz 912.65 KB (0%)
packages/bundle-size/da-workers-neon/output.tgz 989.29 KB (0%)
packages/bundle-size/da-workers-pg/output.tgz 993.3 KB (0%)
packages/bundle-size/da-workers-pg-worker/output.tgz 977.62 KB (0%)
packages/bundle-size/da-workers-planetscale/output.tgz 929.9 KB (0%)
packages/bundle-size/da-workers-d1/output.tgz 1.26 MB (0%)

@FGoessler FGoessler changed the title feat/ORM 665 rework schema finding logic feat: ORM 665 rework schema finding logic Mar 18, 2025
@FGoessler
FGoessler changed the base branch from main to refactor/ORM-667-uplift-dirs March 18, 2025 09:19
@FGoessler FGoessler added this to the 6.6.0 milestone Mar 18, 2025
@FGoessler
FGoessler force-pushed the feat/ORM-665-rework-schema-finding-logic branch from 8dc0e85 to 1eca050 Compare March 18, 2025 10:51
@FGoessler
FGoessler marked this pull request as ready for review March 18, 2025 11:54
@FGoessler
FGoessler force-pushed the refactor/ORM-667-uplift-dirs branch from dc8ce58 to 6ea340b Compare March 18, 2025 12:07
@FGoessler
FGoessler force-pushed the feat/ORM-665-rework-schema-finding-logic branch from 1eca050 to 4138276 Compare March 18, 2025 12:08
Base automatically changed from refactor/ORM-667-uplift-dirs to main March 18, 2025 14:31
@FGoessler
FGoessler force-pushed the feat/ORM-665-rework-schema-finding-logic branch from 4138276 to cdcc7d3 Compare March 18, 2025 14:49
@FGoessler FGoessler changed the title feat: ORM 665 rework schema finding logic feat(schema folder): ORM 665 rework schema finding logic Mar 18, 2025
@FGoessler
FGoessler requested review from a team and jkomyno and removed request for a team March 18, 2025 15:34
@FGoessler
FGoessler force-pushed the feat/ORM-665-rework-schema-finding-logic branch from 3875ce0 to 17d3877 Compare March 18, 2025 16:00
@FGoessler
FGoessler force-pushed the feat/ORM-665-rework-schema-finding-logic branch from 17d3877 to ab203e2 Compare March 18, 2025 20:28
@FGoessler
FGoessler merged commit 73af33f into main Mar 19, 2025
@FGoessler
FGoessler deleted the feat/ORM-665-rework-schema-finding-logic branch March 19, 2025 15:02
FGoessler added a commit that referenced this pull request Mar 20, 2025
This PR cleans up some remains of unused methods and refactors some more
cases to use the SchemaContext instead of `getSchema` variants.

Note: Based on top of #26663 and
requires it to be merged first.
@AmreeshTyagi

Copy link
Copy Markdown

Thanks for this improvement. I just observed one issue with different use case.

When I am using multiple databases without prismaSchemaFolder feature, migrations were generated in respected schema folders as I am putting different db schema in different folder in single file.

But when I enable prismaSchemaFolder feature so that I can create multiple schema for a single db, all migrations are generated in prisma/migrations folder, which is not correct. Ideally they should be created in respective schema folders.

Here:
prisma/app_db/migrations was created for app_db/schema.prisma with single schema (prismaSchemaFolder was disabled)
prisma/migrations was created for master_db/schema.prisma with multi schema enabled (prismaSchemaFolder was enabled)

image

@FGoessler

Copy link
Copy Markdown
Contributor Author

@AmreeshTyagi I assume you did your testing on Prisma version 6.5.0 or earlier, right?

And do I get your use-case correct when I say:

  • You have multiple independent databases.
  • For each database you have a dedicated folder (/prisma/app_db vs /prisma/log_db).
  • In each folder you want to have: 1 or more .prisma files with the schema and the migrations folder for this database.

And the issue being that once you want to have more than 1 .prisma file per folder aka database you have to activate the prismaSchemaFolder preview feature and then the migrations folder gets placed one level higher in the /prisma directory for all databases. Correct?

If that's all true than the change from this PR should fix this for you with the upcoming 6.6.0 release as we now place the migrations folder next to the .prisma file with the datasource block and not next to the given schema path folder.

(Feel free to try a dev build already - e .g 6.6.0-dev.95)

@AmreeshTyagi

AmreeshTyagi commented Mar 31, 2025 •

Copy link
Copy Markdown

@AmreeshTyagi I assume you did your testing on Prisma version 6.5.0 or earlier, right?

And do I get your use-case correct when I say:

  • You have multiple independent databases.
  • For each database you have a dedicated folder (/prisma/app_db vs /prisma/log_db).
  • In each folder you want to have: 1 or more .prisma files with the schema and the migrations folder for this database.

And the issue being that once you want to have more than 1 .prisma file per folder aka database you have to activate the prismaSchemaFolder preview feature and then the migrations folder gets placed one level higher in the /prisma directory for all databases. Correct?

If that's all true than the change from this PR should fix this for you with the upcoming 6.6.0 release as we now place the migrations folder next to the .prisma file with the datasource block and not next to the given schema path folder.

(Feel free to try a dev build already - e .g 6.6.0-dev.95)

Yes. I have the same use case and I am using v.6.5.0. Good to know that it is fixed and planned in next release soon.

I have tried this dev build and it worked well. Thanks a lot.

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.

3 participants