Reputation: 275
I'm developing an application with Drizzle ORM (pg). I would like to have two identical databases (or schemas)? One for testing/development/debugging and another for production. What is the best way to implement this?
I was originally thinking I would have two DBs and used different connection strings based upon a .env flag. But I don't think switching between dbs in Drizzle works that way. I've also tried two table schemas to no avail. How do people do this?
Upvotes: 4
Views: 4703
Reputation: 8693
I use test
database without docker as its local & I use Docker for production. The full application is really simple. My stack is SQLite + Next.js + Drizzle + Docker.
Upvotes: 0
Reputation: 21
Super simple : You create two scripts in package.json :
npm run dev
script which launches the dev environment with a connection to the dev database.npm run prod
script which launches the prod environment on your local computer but connected to production database.To do this (example with next.js) :
npm install --save-dev cross-env
DB_DEV="xxx"
and DB_PROD="xxx"
"prod" : "cross-env CONNECT_TO_PROD_DB=true next dev"
If you're not using next.js, you just need to change the "next dev" part.
Now in your code where you're initializing your database connection, you can do it based in the CONNECT_TO_PROD_DB environment variable :
const isProdMode = process.env.CONNECT_TO_PROD_DB === "true";
if (isProdMode) {
//initiate prod database connection variables based on the production environment variables.
} else {
//initiate dev database connection variables based on the development environment variables.
}
Upvotes: 2
Reputation: 11
The best way to implement different environment is to have them completely separated. You might only have development and production, or you might have many (e.g. dev, test, staging, prod). This clean separation between environment makes it much easier to manage system updates while minimizing accidents to production data. Mistakes on production data can only be recovered if you have backups, and this can be a slow, painful and expensive process. Of course, if there's a bug that cannot be replicated in lower environments, testing in production might be needed, but should be a last resort.
The way we did this at a previous employer was to have the dev environment completely local, the test environment on a server that everyone could reach and test on, the staging environment was a stale copy of development, and then prod where testers and devs had very limited or no access. The staging environment is what you're asking about, and your best bet is to write a series of scripts that copies your production data, and use environment variables to point to the correct database. When you build your project, Drizzle will look for the environment variable, grab the correct database, and you're good to go :)
Upvotes: 1