8

I have been writing a new Django package that will be pip-installable. I've been stuck for awhile because I'm unsure how to make migrations for my particular package so to allow for the normal workflow of an install to be:

  1. pip install my package
  2. add my package to your "INSTALLED_APPS"
  3. run python manage.py migrate

Currently, my package looks like this:

package_root/
    dist/
    actual_package/
        __init__.py
        models.py
    setup.py

The problem I am facing is that when I package the app and install it using pip install dist/... and then add it to my example apps "INSTALLED_APPS", running python manage.py migrate does not create any tables for the models in actual_package/models.py and hence I (from a users perspective) need to then run python manage.py makemigrations actual_package first, which is not ideal.

Any ideas on how to have the migrations already sorted before a user installs would be excellent.

mattjegan
  • 2,724
  • 1
  • 26
  • 37
  • Are migrations included in the actual package? Unzip the file that you are uploading to PyPI and check if files are there. – Domen Blenkuš Aug 20 '17 at 06:50
  • 1
    No they aren't there because I have not generated any. My question is more how do I set up a good workflow to actually create them so I can package them. Since there is no settings.py or manage.py in my package – mattjegan Aug 20 '17 at 06:51
  • 3
    Oh, I see. Common practice is to include test project (which is not packaged). Here is the project that i am currently working on: https://github.com/genialis/resolwe. To create migrations, we run `./tests/manage.py makemigrations` and that's it. – Domen Blenkuš Aug 20 '17 at 07:06
  • 1
    Ok I see, thanks for that. I think you've given me something to work from. – mattjegan Aug 20 '17 at 07:08

2 Answers2

6

1 - Include the initial migrations in the package - e.g., actual_package/migrations/0001_initial.py

2 - Include python manage.py migrate actual_package as part of the installation process - whether new or update.

3 - If you publish updates to actual_package, include any new migrations.

This should work for both new installations and updates. If the migrations have already been done (e.g., update but no new migrations included) then the migrate command won't hurt.

One key warning: Make sure your package installation checks for the appropriate Django version. There have been a lot of changes between versions and code - and migrations - for one version may not work for another.

  • While this sort of answers my question I guess I really need to know how do we actually go about creating those migrations for the package seeing as packages don't have `manage.py` or `settings.py`. – mattjegan Aug 20 '17 at 04:51
  • @mattjegan Create migrations and include them as part of your package - i.e., you will create them anyway, so include them in the package - along with fixtures, test files, etc. When I have incorporated other packages into my own programs I have always had to add to settings.py myself - in theory could be automated but I don't recommend it - too many possible problems. Telling user to "add to settings.py INSTALLED_APPS and then manage.py migrate newapp" is not unreasonable. Much better than HUGE waste of time I had recently with a "great" app that didn't include migrations or instructions. – manassehkatz-Moving 2 Codidact Aug 20 '17 at 05:00
  • One thing to add: migrations are forwards-compatible. Be sure to create your migrations using the oldest major Django version supported by your package, then they are guaranteed to work on newer versions as well. – knbk Aug 20 '17 at 10:47
  • @manassehkatz I admit I didn't go much past the first page of search results, but all my searches to learn how to run `migrate` as part of a django package install only tell me how to consume a package or run it for my own code, not how to make my package install and then run `migrate`. Do you know of any good resources? – hlongmore Jan 10 '19 at 09:31
1

Reading the comments it seems like the question is how do we go about creating the migration file for our models in the package, i.e. what is the equivalent of python manage.py makemigrations for a package developer.

I went around this by including an example django project in the package at /example/ then if you run makemigrations with the project's manage.py and settings.py, it will magically create migrations in your package migrations folder too!

keyvanm
  • 157
  • 9