Best Way of Migrating DB on deployed project?

When I'm running locally and my schema changes, I just run wasp db migrate-dev - what's the best approach for this when I have a production environment deployed?
10 Replies
kapa.ai
kapa.aiβ€’3mo ago
Hi @CamBlackwood. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- When you have a production environment deployed and your schema changes, you should follow these steps: 1. Run wasp build to rebuild your app. This command will generate the necessary files for deployment in the .wasp/build directory. 2. If you're using a CI/CD pipeline, you should include a step to run prisma migrate deploy before deployment. This command should be run on the production database. It's recommended to not store the production database URL locally due to security reasons. Instead, you can provide it via an environment variable directly in your command line or get it from your hosting provider if they offer such a feature. For example, if you're using Heroku, you can get it with heroku config:get DATABASE_URL -a <app_name>. 3. After running the migration, you can proceed with the deployment of your app. The specific steps for this will depend on your deployment method. For example, if you're deploying manually to Railway, you would run railway up in the .wasp/build directory for the server and repeat all the steps in the .wasp/build/web-app directory for the client. Please note that these steps are based on the best practices recommended by Prisma and Wasp. Always ensure to have a backup of your production database before running any migrations to prevent data loss in case of any errors. Sources: - Migrating to the New Auth System (https://wasp-lang.dev/docs/migrate-from-0-11-to-0-12#1-migrate-to-the-new-auth-system) - Database Commands (https://wasp-lang.dev/docs/general/cli#database-commands) - Deploying with the Wasp CLI (https://wasp-lang.dev/docs/advanced/deployment/cli#deploy) - Deploying Manually (https://wasp-lang.dev/docs/advanced/deployment/manually#updates--redeploying) - Github Issue on running prisma migrate deploy (https://github.com/wasp-lang/wasp/issues/215) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
CamBlackwood
CamBlackwoodOPβ€’3mo ago
@kapa.ai so I should run prisma migrate deploy, then wasp deploy fly deploy for my app deployed on fly?
kapa.ai
kapa.aiβ€’3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, that's correct. Here are the steps you should follow: 1. Run prisma migrate deploy to apply the database migrations to your production database. This command should be run in your CI/CD pipeline or manually before deploying the application. 2. After successfully running the migrations, you can deploy your application using wasp deploy fly deploy. This command will push your client and server live. Please note that you should have the flyctl CLI installed on your machine to deploy to Fly.io. Also, ensure that your Fly.io account has the necessary credit card information added. Here's how the commands would look:
prisma migrate deploy
wasp deploy fly deploy
prisma migrate deploy
wasp deploy fly deploy
Sources: - Github Issue on running prisma migrate deploy (https://github.com/wasp-lang/wasp/issues/215) - Deploying with the Wasp CLI (https://wasp-lang.dev/docs/advanced/deployment/cli#deploy) - Prerequisites for Fly.io (https://wasp-lang.dev/docs/advanced/deployment/cli) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
CamBlackwood
CamBlackwoodOPβ€’3mo ago
@kapa.ai When I try this, I get "command not found: prisma"
MEE6
MEE6β€’3mo ago
Wohooo @CamBlackwood, you just became a Waspeteer level 6!
kapa.ai
kapa.aiβ€’3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The error "command not found: prisma" means that Prisma is not installed globally on your machine. In Wasp, Prisma is used as a local dependency, so you should run Prisma commands with npx to use the local version. Here's how you should modify the commands:
npx prisma migrate deploy
wasp deploy fly deploy
npx prisma migrate deploy
wasp deploy fly deploy
The npx command is a package runner tool that comes with npm. When you run npx prisma, it will use the local version of Prisma installed in your project. Sources: - Github Issue on running prisma migrate deploy (https://github.com/wasp-lang/wasp/issues/215) - Deploying with the Wasp CLI (https://wasp-lang.dev/docs/advanced/deployment/cli#deploy) - Prerequisites for Fly.io (https://wasp-lang.dev/docs/advanced/deployment/cli) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
CamBlackwood
CamBlackwoodOPβ€’3mo ago
@kapa.ai Error: Could not find a schema.prisma file that is required for this command. You can either provide it with --schema, set it as prisma.schema in your package.json or put it into the default location ./prisma/schema.prisma https://pris.ly/d/prisma-schema-location
kapa.ai
kapa.aiβ€’3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The error message you're seeing indicates that the prisma migrate deploy command is unable to locate the schema.prisma file. This file is required for the command to run successfully. In Wasp, the schema.prisma file is located in the db/ directory at the root of your project, not in the default prisma/ directory that Prisma expects. To resolve this issue, you should specify the path to the schema.prisma file using the --schema option when running the prisma migrate deploy command. Here's how you should modify the command:
npx prisma migrate deploy --schema=./db/schema.prisma
npx prisma migrate deploy --schema=./db/schema.prisma
This command tells Prisma to look for the schema.prisma file in the db/ directory of your project. Sources: - Github Issue on running prisma migrate deploy (https://github.com/wasp-lang/wasp/issues/215) - Github Issue on updating python to python3 in Dockerfile (https://github.com/wasp-lang/wasp/issues/572) - Github Issue on adding command wasp db ui (https://github.com/wasp-lang/wasp/issues/80) - Github Issue on railway up failing to build docker container (https://github.com/wasp-lang/wasp/issues/877) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Filip
Filipβ€’3mo ago
Man this bot really takes you down the wrong path sometimes... Sorry @CamBlackwood Anyway, to deploy a production database, you don't really need to do anything special. The server dockerfile will connect to the database and migrate it when you deploy the server. Check it out: https://github.com/wasp-lang/wasp/blob/a6573c2201a16b38a5f9f9c3c7b9bbaafb677b9e/waspc/src/Wasp/Generator/ServerGenerator.hs#L127
CamBlackwood
CamBlackwoodOPβ€’3mo ago
@sodic yeah no worries, I usually find it works nice! I went ahead and just deployed everything and it all worked fine πŸ™‚ Final deployment before my launch I think πŸ‘€
Want results from more Discord servers?
Add your server