#Separation of Database between Preview and Production

1 messages · Page 1 of 1 (latest)

south spade
#

Hello, I have a question about the MongoDB configuration in Emergent.

Context:
I have a deployed application (FastAPI + React + MongoDB) and I need to have separate databases for the Preview and Production environments so I can run development tests without affecting production data.

What I tried:

I set DB_NAME=database_prod in Emergent Env Variables (for production)

I left DB_NAME=test_database in the local .env file (for preview)

I used load_dotenv(override=False) so that the Env Variables take priority

The problem:
I found that both Preview and Production share the same MongoDB cluster. The localhost:27017 in Preview seems to redirect to the same MongoDB in the cloud. Therefore, any change in Preview affects Production and vice versa.

My questions:

Is it possible to have completely separate databases for Preview and Production?

If it’s not natively possible, what is the recommended solution to run tests without affecting production data?

Thanks.

scarlet harbor
#

Hey,
 I want to let you know that the preview and production apps use separate databases, and it’s always recommended not to add actual data in the preview once your app is deployed, as the preview environment is meant only for testing and development.
To clarify — when you deploy your code for the first time, the preview database data gets copied to the deployed app database. However, once your app is deployed and you start adding new data, that data will not automatically sync with the deployed app database.
If you want to sync the latest data, you’ll need to fork the session and perform a “Replace Deployment.”
Selecting “Keep Existing Database” will continue using your current production data without any changes.Selecting “Fresh/New Database” will copy all data from the preview database to the deployed app, replacing the existing production database entirely.It’s always recommended to add actual or live data directly from the deployed app rather than from the preview, and redeploy afterward if needed.

south spade
#

Hello, thank you very much for your previous response. However, after several exhaustive tests, I can confirm that the database separation between Preview and Production is NOT working as you describe.

What I configured:

Emergent Env Variables: DB_NAME=database_prod

Local .env file: DB_NAME=test_database

Code modified to prioritize system environment variables over the .env file

At the code level, each environment reports that it is using a different database. However, when I create data in either of the two environments, it immediately appears in the other.

According to Emergent’s agent, the issue is the following: localhost:27017 in Preview is not an independent MongoDB instance, but a PROXY that redirects to the same MongoDB cluster in the cloud (customer-apps.iwtnzf.mongodb.net). Even though the applications use different DB_NAME values, both databases (test_database and database_prod) reside on the same server and there seems to be automatic synchronization between them.

What I need is real data separation between Preview and Production so I can run development tests without affecting the production data my employees use.

Could you check why the data is being synchronized between both environments despite using different DB_NAME values? Or is there any additional configuration I should apply?