7

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.

fabien-michel
  • 1,873
  • 19
  • 38
  • Any solutions to this? I don't understand why when you do a "migrate" django automatically creates sqlite3 database but doesn't create a postgresql database – VMMF Sep 05 '20 at 14:48

3 Answers3

0

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)

bakkal
  • 54,350
  • 12
  • 131
  • 107
  • I already retrieve the database name using environment variable. I just asked to know if django would be able to do that natively. If not I will do it manually before. – fabien-michel Nov 25 '16 at 11:20
0

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.

algo99
  • 69
  • 3
-3

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

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Thanks for your response. We only need this in development environment. When working on a feature with database change on a specific branch we are stuck when changing to another branch because django "check" migration state. There is a recommended process ? – fabien-michel Nov 25 '16 at 10:42
  • I guess its not something I've come across as a problem in all the time I've spent developing in django, I don't know if that is down to user process of making sure that branches are left in a stable condition before switching or something else. I guess the logical step would be a [git hook when creating a branch](http://stackoverflow.com/q/14297606/1324033) if that really is what you require – Sayse Nov 25 '16 at 12:20