madhukar93
madhukar93

Reputation: 515

shared DB across django projects

Our product has a restful API and a server rendered app (the CMS). Both share the database. Both are written in django

The fields and the models needed in both are not mutually exclusive, there are some only particular to the API, some particular to the CMS, and some which are common.

My question is if I run migrations on one of the repos will they try to drop the fields that aren't present in the models of that particular repo, and needed by the other. Will running the migrations individually in both repos keep the database up to date and not pose a problem.

Upvotes: 8

Views: 1603

Answers (4)

madhukar93
madhukar93

Reputation: 515

This was solved by using a schema migration tool external to Django's own. We use yoyo migrations to migrate our schema now.

Upvotes: 4

Cedric Roy
Cedric Roy

Reputation: 31

I think the best things to do would be to have one repo that contains all the fields. This project will be responsible to apply the migrations.

In the other projects, you'll need a db_router containing a function allow_migrate which will return False on your model classes.

Also having different db user with different db permissions can prevent from altering the tables.

Upvotes: 1

Łukasz Haze
Łukasz Haze

Reputation: 56

The only other valid option IMHO (besides merging projects) is turning off automation of Django migrations on common models (Meta.managed = False) and taking table creation & versioning into your own hands. You still can write migration scripts using django.db.migrations but makemigrations command won't do anything for these tables.

Upvotes: 4

Jon Combe
Jon Combe

Reputation: 176

Will running the migrations individually in both repos keep the database up to date and not pose a problem.

Unfortunately, no. As you suspected, changes in one will attempt to override the other.

The easiest thing to do is merge the two projects into one so this problem goes away entirely.

If this isn't an option, can the code be organised in such a way that both projects share the same models.py files? You could do this by perhaps having the models.py files and migrations folders only exist in one project. The second project could have a symlink across to each models.py file it uses. The trick (and the difficult part) will be to make sure you never create migrations for the app which uses the symlinks.

Upvotes: 3

Related Questions