Skip to content

fix(db seed): redesign of db seed - #8146

Merged
Jolg42 merged 24 commits into
mainfrom
pr/db-seed-new-behavior
Sep 1, 2021
Merged

Jolg42 merged 24 commits into
mainfrom
pr/db-seed-new-behavior

Conversation

@Jolg42

@Jolg42 Jolg42 commented Jul 7, 2021 •

Copy link
Copy Markdown
Contributor

Internal: https://www.notion.so/prismaio/db-seed-workshop-2a104a37762e4fb7bf3100c0227569ff

prisma db seed is currently a Preview feature.

During this phase, we noticed different issues caused by the complexity of the current implementation.

The Migrations Team decided to remove the complexity and make it a lot easier to understand for users and to maintain.

The new implementation is in this PR.

How it will work is that a prisma.seed property in the package.json of your project will be required if you want to use prisma db seed.

The CLI will give examples that can be copy-pasted.

If you are currently using the previous implementation of prisma db seed and upgrade to this new version then it will throw an error with an upgrade help message which should be easy to fix (add prisma.seed to the package.json)

package.json example for a JavaScript project

  "prisma": {
    "seed": "node prisma/seed.js"
  } 

package.json example for a TypeScript project

  "prisma": {
    "seed": "ts-node prisma/seed.ts"
  }

Example of a prisma/seed.js

async function main() {
  await new Promise((resolve) => setTimeout(resolve, 100))
  console.log('Hello from seed.js')
}

main()
  .then(() => console.log('Goodbye from seed.js'))
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })

npx prisma db seed --preview-feature results in something like
Screen Shot 2021-07-08 at 13 19 39

if file path is invalid

Screen Shot 2021-07-08 at 13 23 44

Screen Shot 2021-07-08 at 13 23 29

if seed exits with error

Screen Shot 2021-07-08 at 13 34 16

Screen Shot 2021-07-08 at 13 33 53

if command is not found

Screen Shot 2021-07-08 at 14 06 55

if we detect a seed.ts file but no "prisma.seed" in package.json

Screen Shot 2021-07-08 at 14 07 40

if we detect a seed.js file but no "prisma.seed" in package.json

Screen Shot 2021-07-08 at 14 09 20

if we detect a seed.sh file but no "prisma.seed" in package.json

Screen Shot 2021-07-08 at 14 16 53

If we didn't detect a seed.ts/.js/.sh we error with

Screen Shot 2021-07-08 at 14 16 09

@Jolg42 Jolg42 added this to the 2.27.0 milestone Jul 7, 2021
@Jolg42

Jolg42 commented Jul 8, 2021 •

Copy link
Copy Markdown
Contributor Author

Can be tried with 2.27.0-integration-db-seed-new-behavior.2 like

npx [email protected] db seed --preview-feature

@Jolg42
Jolg42 requested a review from tomhoule July 8, 2021 12:20

@tomhoule tomhoule left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very nice! The new code is nice and easy to follow.

Comment thread src/packages/migrate/src/utils/seed.ts Resolved
Jolg42 added 3 commits July 9, 2021 09:32
…ehavior

* integration/db-seed-new-behavior:
  fix node types for seed
  small tweaks/debug & add chmod +x for Bash

# Conflicts:
#	src/packages/migrate/src/utils/seed.ts
@Jolg42

Jolg42 commented Jul 9, 2021

Copy link
Copy Markdown
Contributor Author

New changes: warning in migrate dev and migrate reset (we error in db seed)

Screen Shot 2021-07-09 at 09 45 36

Screen Shot 2021-07-09 at 09 46 50

@Jolg42

Jolg42 commented Jul 9, 2021 •

Copy link
Copy Markdown
Contributor Author

Can be tried with

npx [email protected] db seed --preview-feature

Streaming of seed stdout should be smooth now.

Comment thread src/packages/migrate/src/utils/seed.ts Resolved
Comment thread src/packages/migrate/src/utils/seed.ts Resolved
Comment thread src/packages/migrate/src/utils/seed.ts Resolved
Comment thread src/packages/migrate/src/utils/seed.ts Resolved

@janpio janpio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking good.

Do the new tests confirm that the actual seed command was successfully executed and output made it to the executing terminal?

@benbender

benbender commented Aug 11, 2021 •

Copy link
Copy Markdown

@janpio just tried 2.29.0-integration-db-seed-new-behavior.1 in my sveltekit-app. This app has "type": "module", in package.json to notify node that this is an es-module-package (default in sveltekit, many vite-apps and upcoming default for node). Additionally I'm writing my seeds in Typescript.

With the proposed ts-node-setup the seed is failing as before:

> prisma db seed --preview-feature

Environment variables loaded from .env
Prisma schema loaded from db/schema.prisma
Running seed: ts-node "db/seed.ts" ...
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for [redacted]/db/seed.ts

What works though, is to setup ts-node with it's experimental esm-loader: "seed": "node --loader ts-node/esm db/seed.ts"

I think that should be at least reflected in docs/help as this kind of setup will get more common and more users will struggle with the advised setup failing...

@janpio

janpio commented Aug 12, 2021

Copy link
Copy Markdown
Contributor

Thanks @benbender, can you optimally open a new issue so we can start a discussion about this? Thanks.

@Jolg42 Jolg42 mentioned this pull request Aug 13, 2021
5 of 7 tasks
@Jolg42 Jolg42 modified the milestones: 2.28.0, 2.31.0 / 3.0.x Aug 13, 2021
@Jolg42 Jolg42 linked an issue Aug 13, 2021 that may be closed by this pull request
5 of 7 tasks
@Jolg42 Jolg42 mentioned this pull request Aug 20, 2021
47 of 50 tasks
@Jolg42
Jolg42 marked this pull request as ready for review August 26, 2021 14:24
Comment thread packages/migrate/src/utils/seed.ts Resolved
Comment thread packages/migrate/src/commands/DbSeed.ts Resolved
Comment thread packages/migrate/src/utils/seed.ts Resolved

@williamluke4 williamluke4 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work Joel! 🥳

@Jolg42
Jolg42 merged commit 4c23abd into main Sep 1, 2021
@Jolg42
Jolg42 deleted the pr/db-seed-new-behavior branch September 1, 2021 15:40
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.

EPIC: re-design of db seed feature

5 participants