How to update db setting in django at runtime? - django

To be more specific, I want to retrieve db setting from a config server when django project starts, and use it to setup django db connection
Someday in the future, the setting in the config server may be changed (for example, change the user password) and pushed to django project then reset the db connection, so I can use new setting without restarting django project or updating project code
Is there a way to do that?
Or what's the right way to hide the db sensitive information (password, etc) from django project code?
Any helps will be grateful, thanks~

Related

Django transfer database to different Django project without messing up the logic

I built a Django project with a few models. Now I created a second server with the same setup. This one is meant to be the deployment server. Databases are separate from the dev-server.
However can you tell me if I can simply copy the databases from the dev server to the deploy or will the Django logic, since I also mean the user models and permissions etc.
The tables which I created myself are no problem to transfer to the new server. However I am wondering If Django gets confused when I also transfer something like the auth_user Model.
Should this work since I also just copied the backend logic as well?

Implement LiteSync on Django

I've tried to write sync sqlite database between 2 different Server but same app and DB. But when i turn on litesync.io in console, it doesnt synchronize the Django project with Django project in another server. I know it's wrong because i must configure it first in django project, but i've tried to change NAME of DATABASES into URI and it not work. I've tried it in console to execute query, it works, but not in Django Project. So, how to implement litesync on Django project? How to save data into Sqlite DB by URI?

Connect to databases dynamically in Django

I'm new to Django.
My project database architecture is like
Having 'Auth_DB' contains all the users login details.
Once user logged in, then in need to fetch the details from the other DB. Here i have databases for each user individually, like:
user_0001
user_0002
user_0003
user_0004
user_0005
Can any one please help how can i achieve this.
Thanks in Advance.
Followed Django multiple and dynamic databases
In summary, this is the process:
Add the new database to settings (at runtime)
Create a file to store these settings for reloading when the server is restarted (at runtime)
Run a script which loads the saved settings files (whenever the server is restarted)

How to use a different database for Heroku review apps?

I have a deployment pipeline on Heroku which recently started using review apps. This means I have an app - let's call it CI-APP -- which is being created from the master branch.
Every time a pull request is made, a review app is created. We are using Django in our project and so I also added the migrate command to the release phase in the project, so that database migrations can be done automatically.
Today, a coworker submitted a pull request which contained some database changes. The problem is that the migration was ran, and since review apps seem to be using the same database as the app they are suppose to merge to, the migration was applied and now my app CI-APP stopped working...since the code base no longer matches the database structure.
I searched a lot about how to use completely different databases for the review apps compared to the parent app, but to no avail (there are some resources mentioning how you can copy db contents, but that is not what I need).
Any suggestion ?
Update
Ok, so it seems that Heroku does create a new database for the review app, however: the review app copies all of its environment variables from the parent, including the DATABASE_URL (this seems to be the only way to actually create the review app : https://s3.amazonaws.com/heroku-devcenter-files/article-images/1461071037-initial_set_up_review_apps.png)
I think I can do some black magic in the postdeploy script, but since the database generated url can be something such as HEROKU_POSTGRESQL_{color}_URL, I am not sure how to find it ....
To do that, create the app.json file at the root of your project instead of using the heroku dashboard. In this file, you can specify what environment variables to inherit from the parent.
From the heroku docs:
"env": {
"INHERIT_THIS_CONFIG_VAR": {
"required": true
},
"DONT_INHERIT_THIS_CONFIG_VAR": "production"
},
This allows you to specify which database you want to use for the review app. Looking at the documentation of the heroku postgres addon (i assume you're using postgres):
As part of the provisioning process, a DATABASE_URL config var is
added to your app’s configuration. This contains the URL your app uses
to access the database.
So the database_url config variable will be created by the adddon. You simply need to not put it in the app.json file, and it will be created automatically.
Check that you do not have the DATABASE_URL set in the Pipeline Settings CONFIG VARS in Heroku either.. if it is set there, then it seems the Review App will use that as the DB link and not the one created when the Review App is created.

Django Session with Memcached or ElastiCache, does the Django code need to be changed?

I have an app using DB as session backend, and I realize that Django allows a Memcached-like (memcached, ElastiCache) session backend.
Reading the doc at Django site, I found the setting file can specify that change. My question is whether my view logic code files need any change.
No. You shouldn't have to change anything. If you deploy the code on a live site, active sessions will be lost (all users will be logged out).