Forums

Correct way of pushing new code with changes to database table

Hi, you could say I'm a novice at this. Whenever I would push code (to bitbucket) and then pull to production on PA I would get the 'Commit your changes or stash them' error because of changes (migrations) to the local database. So I would download production database -> migrate -> and then stash the changes and overwrite production database with newly migrated local database. I know this is wrong and is going to become an issue as traffic on site grows. I also know that local database should be ignored when pushing but how do you implement database changes in production? If I push new code and try migrate from web console I get error 'CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph:'. Would it be possible to give me a short step by step example on how to correctly apply database changes to production database?

Thank you!

The normal situation is that you would never create migrations on the production server; you would just run them. So the process would be -- if we assume that we're starting with identical codebases locally and in production, and the databases properly migrated on both sides:

  • Make changes locally
  • Create the migrations locally (eg. python manage.py makemigrations if you're using Django)
  • Run the migrations locally (eg. python manage.py migrate), and test that everything works.
  • Check the migrations into git (or hg or whatever) alongside the code changes that made the migrations necessary.
  • Push the committed code, which includes the migrations, to BitBucket (or GitHub, or...)
  • Pull the code down to PythonAnywhere
  • Run the migrations again to update the production DB.

Morning. Ok I understand the above (using django and bitbucket). When you say push the committed code you mean exclude the local database (and any migrations run on it the too). And then run migrate in production?

yes- push the code without the local db, and this would include pushing any code that deals with migrations. You should push that migration code, so that you can run the migration code on PythonAnywhere to actually do the migration on PythonAnywhere.

Great it works thank you. Another off topic question but a production tip. Is it possible to create a statement that automatically sets your production app in django to 'debug=False'? I tried importing current domain and creating an if statement in settings but that had a bunch of errors:

from django.contrib.sites.models import Site
current_site = Site.objects.get_current()
current_site.domain

You get errors there because you cannot use models before the settings have been finalised. This SO post covers a number of different options for managing different settings.

please said me command not a code

@adeemaamir What do you mean by that?