epicweb-dev / epic-stack

This is a Full Stack app starter with the foundational things setup and configured for you to hit the ground running on your next EPIC idea.
https://www.epicweb.dev/epic-stack
MIT License
4.59k stars 376 forks source link

Include production seed script #475

Closed kentcdodds closed 1 year ago

kentcdodds commented 1 year ago

Discussed in https://github.com/epicweb-dev/epic-stack/discussions/474

Originally posted by **arpitdalal** October 5, 2023 I've deployed the app on fly following the steps mentioned in the [deployment documentation](https://github.com/epicweb-dev/epic-stack/blob/main/docs/deployment.md). But signing up gives me this error in the logs: ``` prisma:error Invalid `prisma.session.create()` invocation: An operation failed because it depends on one or more records that were required but not found. Expected 1 records to be connected, found only 0. PrismaClientKnownRequestError: Invalid `prisma.session.create()` invocation: An operation failed because it depends on one or more records that were required but not found. Expected 1 records to be connected, found only 0. at wn.handleRequestError (/myapp/node_modules/@prisma/client/runtime/library.js:123:6730) at wn.handleAndLogRequestError (/myapp/node_modules/@prisma/client/runtime/library.js:123:6119) at wn.request (/myapp/node_modules/@prisma/client/runtime/library.js:123:5839) at l (/myapp/node_modules/@prisma/client/runtime/library.js:128:9763) at signup (file:///myapp/app/utils/auth.server.ts:130:18) at Object.transform (file:///myapp/app/routes/_auth+/onboarding.tsx:85:21) at ZodEffects.safeParseAsync (file:///myapp/node_modules/zod/lib/index.mjs:677:24) at action5 (file:///myapp/app/routes/_auth+/onboarding.tsx:67:21) at /myapp/node_modules/@sentry/src/utils/instrumentServer.ts:207:1 at Object.callRouteActionRR (/myapp/node_modules/@remix-run/server-runtime/dist/data.js:35:16) { code: 'P2025', clientVersion: '5.3.1', meta: { cause: 'Expected 1 records to be connected, found only 0.' } } ``` This error is logged after hitting submit on the onboarding page but this playwright test did pass in GH actions but this isn't working on prod for some reason 🤷‍♂️ I believe it's getting that error when trying to connect roles in the signup function [here](https://github.com/epicweb-dev/epic-stack/blob/b48be8d63f1c3d3b9f88a4c891db6467304aac18/app/utils/auth.server.ts#L133) but there are no roles yet so it cannot connect. Now I haven't created any roles in the DB myself. Do the deployment steps include creating these roles? If not, how do I create them?
kentcdodds commented 1 year ago

In reply to @arpitdalal...

The seed.ts script would sort of work. But I think that wouldn't work well because it doesn't take the current data into consideration and it would be challenging with regard to migrations in the future as well (you may later decide you want to seed more data).

So instead, I think the best solution is to manually change the migration.sql file to create the seed data. That way it's reproducible.

In fact, we could probably do that and remove the code in the seed.ts that creates the roles and permissions.

kiliman commented 1 year ago

Yeah, definitely don't want to use the seed.ts script on production without some major changes... the first thing it does is delete all your data 😱

https://github.com/epicweb-dev/epic-stack/blob/b48be8d63f1c3d3b9f88a4c891db6467304aac18/prisma/seed.ts#L14-L20

arpitdalal commented 1 year ago

Yeah, that's a better idea