0

Whenever I make updates to my website I lose all data that has been added to my models. For example my models include users and service_calls and when I push a deploy it overwrites my db.sqlite3 file and I'm back to just my superuser being the only user and 0 service calls in my database. How can I push an update without having my db.sqlite3 file overwritten? Is that possible or do I need to have my local db.sqlite3 file updated before deploying and if so how would I go about that.

Thanks in advance

1 Answers1

0

I use such a upgrade strategy and it never gives me problem:

  1. the db.sqlite3 file is not added to git repository. that means dev and prod will be using different databases.
  2. in the migrate directory, put only the init file to git repository, don't add all migrate cache
  3. db.sqlite3 file is backed up in the different way, not along with DJango codes (and it should be this way -- data and code shouldn't be packed together)
  4. every time when I do upgrade the production, I run the migration on prod machine again (include makemigrations, sqlmigrate and migrate). Django migrate will keep old data, if you look at the sql it generates, it will create new table and copy data to new table and remove old table and then rename new table back to old name.
AIMIN PAN
  • 1,563
  • 1
  • 9
  • 13
  • This won't work on Heroku due to its ephemeral filesystem. Please see the linked duplicates and many, many other questions on Stack Overflow and elsewhere. – ChrisGPT was on strike Jun 18 '22 at 21:11
  • (Also, migrations should _never_ be created on production, whether that is Heroku or elsewhere. _Always_ create migrations in development, commit the files, and then _apply them_ on all environments. See [the documentation](https://docs.djangoproject.com/en/4.0/topics/migrations/#workflow): "Once the migration is applied, commit the migration and the models change to your version control system as a single commit - that way, when other developers (or your production servers) check out the code, they’ll get both the changes to your models and the accompanying migration at the same time.") – ChrisGPT was on strike Jun 18 '22 at 21:12