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

npm run dev fails in new t3-app due to missing DATABASE_URL #204

Closed
c-ehrlich opened this issue Jul 16, 2022 · 10 comments · Fixed by #222
Closed

npm run dev fails in new t3-app due to missing DATABASE_URL #204

c-ehrlich opened this issue Jul 16, 2022 · 10 comments · Fixed by #222
Assignees

Comments

@c-ehrlich
Copy link
Member

c-ehrlich commented Jul 16, 2022

Describe the bug
npm run dev fails in a new t3-app because it's missing the DATABASE_URL environment variable. However the default database is sqlite, which doesn't need that environment variable.

To Reproduce
Steps to reproduce the behavior:

  1. Create a t3-app that includes Prisma
  2. yarn dev

Expected behavior
Not sure! Maybe this is ok because we assume that anyone who uses Prisma will want to switch away from sqlite and thus needs a database url? However I don't think it's a great experience - the first thing I want to do after initializing an app is start the dev server and check that everything installed fine, which this prevents.

Would be happy to hear what others think the expected behavior should be.

Screenshots
image

@AdamHolwerda
Copy link

Works after renaming .env-example to .env - agree with the expectation of having a running app example after doing the CLI steps, and also I like the environment variables pattern.

What if the last step of the CLI asked you to enter values for the whatever .env variables aren't already pre-populated and wrote a .env file?

@juliusmarminge
Copy link
Member

juliusmarminge commented Jul 16, 2022

The reason it fails is you need a .env file as Adam pointed out. We could maybe rename the .env-example to .env and that way it would start out of the box?

Fwiw, This is what we do in our CI to get a working build in there.

@c-ehrlich
Copy link
Member Author

The reason it fails is you need a .env file as Adam pointed out. We could maybe rename the .env-example to .env and that way it would start out of the box?

Fwiw, This is what we do in our CI to get a working build in there.

That seems like a reasonable solution.

Requiring a db url string despite sqlite not actually using it still isn't ideal, but I also can't think of a better solution so maybe this is a good way to go for now.

@ochicf
Copy link
Contributor

ochicf commented Jul 17, 2022

IMHO the scaffolded project should be able to run in dev mode just after it's creation, so +1 in having the .env scaffolded aswell.

Also I haven't really used prisma, but could it be possible for the initial DATABASE_URL be defined in the .env pointing to the sqlite local file? That would hint the user that sqlite is being used by default in dev, but maybe an additional comment could be added.

Note that this would also serve as an example to users that they should NOT hardcode the database url in the schema.prisma file as it's currently done with the sqlite db.

Examples:

// schema.prisma

datasource db {
    provider = "sqlite"
    url      = env("DATABASE_URL")
}
// .env

# Prisma
DATABASE_URL="file:./db.sqlite" # <- if changing provider, remember to update `/prisma/schema.prisma`

@juliusmarminge
Copy link
Member

juliusmarminge commented Jul 17, 2022

Requiring a db url string despite sqlite not actually using it still isn't ideal, but I also can't think of a better solution so maybe this is a good way to go for now.

Not sure if file:./db.sqlite is a valid Zod url? If it is we could have that as the default DATABASE_URL?

// schema.prisma
datasource db {
    provider = "sqlite"
    url      = env("DATABASE_URL")
}
# .env-example
# Prisma
DATABASE_URL=file:./db.sqlite
// installers/envVars.ts
await Promise.all([
  fs.copy(envSchemaSrc, envSchemaDest, { overwrite: true }),
  fs.rename(envExample, envDest),
]);

EDIT: @ochicf beat me to it ;)

@c-ehrlich
Copy link
Member Author

I like this a lot. If it doesn't pass the zod check, we could add .or(z.literal('file:./db.sqlite')) to the schema, which I think shouldn't mess up the type safety?

@ochicf
Copy link
Contributor

ochicf commented Jul 17, 2022

I'm not proficient with zod, but could it be a more relaxed check to allow strings starting with file: in case consumers want to change the path or if any other provider uses local urls?

Not sure if this would have some side effects (ex: if the file is gitignored)

@juliusmarminge
Copy link
Member

Not sure if file:./db.sqlite is a valid Zod url?

Just checked and seems to validate just fine.

@c-ehrlich
Copy link
Member Author

I can implement this, but would probably wait until I see what happens with #205 to avoid a potential merge conflict

@c-ehrlich
Copy link
Member Author

fixed with #222

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 a pull request may close this issue.

4 participants