In a development environment, there is a way to have django automatically create the database (on a postgreSQL server) on "migrate" command like it do when creating test database ?
My final goal is to have one database for each git branch.
In a development environment, there is a way to have django automatically create the database (on a postgreSQL server) on "migrate" command like it do when creating test database ?
My final goal is to have one database for each git branch.
You could script a script with pseudo code below
branch_name=<get branch name from git>
create_database with branch_name as db name
run django migrate
e.g. on Linux with bash it could be
branch_name=git rev-parse --symbolic-full-name --abbrev-ref HEAD
createdb ${branch_name}
python manage.py migrate
The createdb
command is from PostgreSQL
Then your Django settings.py also needs to change to use that DB name, for that you can read the Git branch name from Python in settings.py too:
>>> import subprocess
>>> DBNAME = subprocess.check_output('git rev-parse --symbolic-full-name --abbrev-ref HEAD', shell=True)
>>> DBNAME.replace('\n', '')
'develop'
Then you can use that value in settings.py
(above I am in a git branch develop
)
Yes, Django creates a database on "migrate". But only if it uses SQLite. The documentation states it clearly (https://docs.djangoproject.com/en/3.1/intro/tutorial02/)
For databases other than SQLite
If you’re using a database besides SQLite, make sure you’ve created a database by this point. Do that with
CREATE DATABASE database_name;
within your database’s interactive prompt....
If you’re using SQLite, you don’t need to create anything beforehand - the database file will be created automatically when it is needed.
No, because django is a web framework, not a web server, how you run that is up to you.
Also, having one database per git branch sounds like a nightmare, I'd hope that you are committing migrations and merging appropriately as required