W
Wasp3mo ago
NEROX

Use another Database on Development

I have Docker running and I have several branches of my app. There are some variants in which I have to do wasp db migrate-dev and as there are fields that do not exist and others disappear, that database is completely deleted and when using the previous branch I no longer have data. How can I solve this? Is it possible to have several databases running in docker at the same time?
14 Replies
kapa.ai
kapa.ai3mo ago
Hi @NEROX. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, it is possible to have multiple databases running in Docker at the same time. This can be achieved by running each database on a different Docker container. Each container would have its own isolated environment, including its own database. To avoid conflicts between multiple Wasp projects running on the same machine, you can consider the following strategies: 1. Name the Docker volume by the Wasp app: This way, each app will have its own dedicated volume, and there will be no conflicts between them. 2. Name the database in PostgreSQL by the Wasp app: This way, each app will have its own dedicated database, and there will be no conflicts between them. Remember to set the DATABASE_URL environment variable to the correct connection string for each database. You can do this by adding it to the .env.server file in the root directory of your Wasp project, or by setting it inline when running wasp. Here is an example of how to set it inline:
DATABASE_URL=<my-db-url> wasp ...
DATABASE_URL=<my-db-url> wasp ...
This way, each wasp command will be run on the specific database you want. Please note that these strategies require some research and testing to implement correctly. Also, keep in mind that Wasp currently supports SQLite for development and PostgreSQL for production[^1^][^2^]. ^1^]: [Wasp Docs > Data-model > Databases (https://wasp-lang.dev/docs/data-model/backends) ^2^]: [Github Issue (closed): Improve instructions for running PostgreSQL via Docker to work if you have multiple Wasp projects on same machine (https://github.com/wasp-lang/wasp/issues/237) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, 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?
NEROX
NEROXOP3mo ago
@kapa.ai I'm working with the SaaS template, I'm using this: # If you use wasp start db then you DO NOT need to add a DATABASE_URL env variable here. # DATABASE_URL=
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The DATABASE_URL environment variable is used by Wasp to connect your application to a database. If you're using the wasp start db command, Wasp will automatically start a default PostgreSQL development database for you and connect your application to it. In this case, you don't need to manually set the DATABASE_URL environment variable[^1^]. However, if you want to connect your application to a different database (for example, a production database or a database running on a different server), you will need to set the DATABASE_URL environment variable to the connection string of that database[^1^]. You can set the DATABASE_URL environment variable in the .env.server file in the root directory of your Wasp project. If that file doesn't exist, you can create it[^1^]. Here's an example of how to set the DATABASE_URL environment variable:
DATABASE_URL=postgresql://mydbuser:mypass@localhost:5432/nameofmydb
DATABASE_URL=postgresql://mydbuser:mypass@localhost:5432/nameofmydb
This connection string tells Wasp to connect to a PostgreSQL database running on localhost on port 5432, using the username mydbuser and the password mypass, and to use the database named nameofmydb[^3^]. ^1^]: [Wasp Docs > Data-model > Databases > Connecting to a Database (https://wasp-lang.dev/docs/data-model/backends#connecting-to-a-database) ^3^]: [Wasp Docs > Advanced > Deployment > Deploying Manually > Deploying a Wasp App > 2. Deploying the API Server (backend) > Environment Variables (https://wasp-lang.dev/docs/advanced/deployment/manually#environment-variables) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, 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?
NEROX
NEROXOP3mo ago
@kapa.ai So instead of SQLite it is better to use PostgreSQL to manage the different databases me?
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, using PostgreSQL instead of SQLite for managing different databases in your development environment can be beneficial for several reasons: 1. Feature Compatibility: PostgreSQL supports more features than SQLite, which can be crucial depending on your application's requirements. For instance, PostgreSQL supports a wider range of data types[^1^]. 2. Persistence: Unlike SQLite, which stores data in a file that can be easily deleted, PostgreSQL stores data in a more persistent manner. This can be useful if you want to ensure that your data persists across different branches of your app[^1^]. 3. Production Readiness: Wasp uses PostgreSQL for production. Therefore, using PostgreSQL in your development environment allows you to test your application in an environment that closely matches your production environment[^2^]. 4. Multiple Databases: PostgreSQL allows you to create multiple databases within the same PostgreSQL server. This can be useful if you want to have a separate database for each branch of your app[^3^]. Remember to set the DATABASE_URL environment variable to the correct connection string for each database. You can do this by adding it to the .env.server file in the root directory of your Wasp project, or by setting it inline when running wasp. Here is an example of how to set it inline:
DATABASE_URL=<my-db-url> wasp ...
DATABASE_URL=<my-db-url> wasp ...
This way, each wasp command will be run on the specific database you want. ^1^]: [Github Issue (open): Improve starter templates that are offered on wasp new in Wasp CLI > Comments Part 4 (https://github.com/wasp-lang/wasp/issues/1622) ^2^]: [Wasp Docs > Data-model > Databases (https://wasp-lang.dev/docs/data-model/backends) ^3^]: [Github Issue (closed): Use Postgresql with Prisma for local dev, instead of SQLite > Comments Part 1 (https://github.com/wasp-lang/wasp/issues/173) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, 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?
NEROX
NEROXOP3mo ago
:shork: @kapa.ai I'm already usi ng postreSQL, my main.wasp: datasource db { provider = "postgresql" url = env("DATABASE_URL") } but my .env.server: # If you use wasp start db then you DO NOT need to add a DATABASE_URL env variable here. # DATABASE_URL= I have several branches and Docker, how would you configure step by step the database in develops so that each branch uses its own from this point on?
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To configure each branch of your application to use its own database, you can follow these steps: 1. Create a new PostgreSQL database for each branch: You can do this by running a new Docker container for each database. Here's an example command to run a new PostgreSQL database in a Docker container:
docker run --name mydb-branch1 -p 5432:5432 -v mydb-branch1:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgresWaspDevPass -e POSTGRES_USER=postgresWaspDevUser -e POSTGRES_DB=mydb-branch1 --rm postgres:16

docker run --name mydb-branch1 -p 5432:5432 -v mydb-branch1:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgresWaspDevPass -e POSTGRES_USER=postgresWaspDevUser -e POSTGRES_DB=mydb-branch1 --rm postgres:16

Replace mydb-branch1 with the name of your branch. You'll need to run a similar command for each branch, making sure to use a different name and port for each one[^1^]. 2. Set the DATABASE_URL environment variable for each branch: In the .env.server file in the root directory of your Wasp project, set the DATABASE_URL environment variable to the connection string of the database for that branch. Here's an example:
DATABASE_URL=postgresql://postgresWaspDevUser:postgresWaspDevPass@localhost:5432/mydb-branch1

DATABASE_URL=postgresql://postgresWaspDevUser:postgresWaspDevPass@localhost:5432/mydb-branch1

Replace mydb-branch1 with the name of your branch and 5432 with the port number you used when running the Docker container for that database[^2^]. 3. Commit the .env.server file to each branch: Make sure to commit the .env.server file to the branch it belongs to. This way, each branch will have its own .env.server file with the DATABASE_URL environment variable set to the connection string of its own database. Remember to replace the placeholders in the commands with your actual values. Also, keep in mind that you'll need to have Docker installed and running on your machine to run the Docker commands. ^1^]: [Discord support thread: Schedule email with sendgrid > Answers Part 11 (https://discord.com/channels/686873244791210014/1179895919471247490) ^2^]: [Wasp Docs > Advanced > Deployment > Deploying Manually > Deploying a Wasp App > 2. Deploying the API Server (backend) > Environment Variables (https://wasp-lang.dev/docs/advanced/deployment/manually#environment-variables) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, 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?
NEROX
NEROXOP3mo ago
@kapa.ai How can I make sure I get step 3 right? The last time I made saved changes on GitHub in project, the .env.server and .env.client files were excluded. How can I avoid this?
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books:
MEE6
MEE63mo ago
Wohooo @NEROX, you just became a Waspeteer level 8!
kapa.ai
kapa.ai3mo ago
---------------------- The .env.server and .env.client files are typically excluded from version control by default in Wasp projects to prevent sensitive data like database credentials or API keys from being exposed. This is done using the .gitignore file, which tells Git which files or directories to ignore. If you want to commit changes to these files for each branch, you have a couple of options: 1. Remove the files from the .gitignore file: You can do this by opening the .gitignore file in the root directory of your Wasp project and removing or commenting out the lines that match .env.server and .env.client. However, this is generally not recommended because it can lead to sensitive data being accidentally committed to your repository. 2. Use a different method to store branch-specific configuration: Instead of using the .env.server and .env.client files to store branch-specific configuration, you could use a different method that is not ignored by Git. For example, you could create a new .config file for each branch and store your configuration there. You would then need to update your application code to read from these files instead of the .env files. Remember, if you choose to commit your .env files to your repository, make sure they do not contain any sensitive data. If you need to store sensitive data, consider using a secure method like environment variables or a secrets management service[^1^]. ^1^]: [Wasp Docs > Project > Env Variables > Defining Env Vars in Development (https://wasp-lang.dev/docs/project/env-vars#defining-env-vars-in-development) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, 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?
NEROX
NEROXOP3mo ago
Done! ✅ Working with several Wasp apps and their corresponding databases:partyparrot:
No description
martinsos
martinsos3mo ago
Wow nice job @NEROX ! Here is our related issue for this: https://github.com/wasp-lang/wasp/issues/1972 , it describes your use case and offers some ideas how to solve it, but it seems you found your own way just fine. Just to be clear, so you now have multiple commands, one per branch, for running the datbase for that branch with Docker, right? And then you also set DATABASE_URL to correct value in .env.server and that is it? I wonder though, how do you do that exactly. When you switch from branch B1 to branch B2, what do you do: you run docker command for B2, which you search for in terminal history or you have a bash script to run it? and then you also set DATABASE_URL in .env.server how -> manually, or maybe also some script, maybe some that runs the database?
GitHub
Add support for named managed dev database: wasp db start · Issu...
When using wasp db start we only get the ability to create one dev database in relation to this wasp project. However, sometimes you might want to have multiple development databases that you can s...
NEROX
NEROXOP3mo ago
Correct, As for how I do it now: Manually. At the moment I have 2 apps and with the chatgpt window I have all commands at hand to copy and paste. I stop the two processes (the Docker and Wasp), change branch, run the Docker command (for that branch and its respective DB) and then wasp start. The moment I have many I don't know what will happen.
No description
Want results from more Discord servers?
Add your server