Search code examples
djangogitherokugitignore

Local -> Github working | Local -> Heroku not working


I have a Django application which is present in my local system.

As I want to ignore db.sqlite3 files while transferring the repository, I have put the following in .gitignore

db.sqlite3

I push it to Github using:

git push origin master

When I do this, the updated db.sqlite3 from local system is NOT transferred to git.

As the next step, I need to transfer the files from local system to Heroku using:

git push heroku master

However, it seems that the file from Github is copied to heroku, which is weird.

Perhaps my understanding of the git push heroku master is incorrect.

Deployment method is using heroku cli

To check this weird way of working :

  1. I added couple of entries in db.sqlite3 in my local system
  2. I made a small change to the code in my local system
  3. I made new entries in the Django application which is deployed to heroku
  4. I pushed the application to Github using git push origin master and checked the timestamp on db.sqlite3 in git and it wasn't changed - I downloaded the db.sqlite3 from git and checked, the new entries that I made to the local system weren't there. This is good.
  5. I pushed the application to Heroku using git push heroku master and found that the entries which I made in step 3 are gone and the entries in step 1 are also not reflected.
  6. I checked my Github db.sqlite3 file and heroku db.sqlite3 file and they matched.

My requirements are as follows :

  1. The changes to the data in db that I make in my local system should not reflect in the application deployed to heroku (I believe therefore .gitignore -> db.sqlite3)

  2. The structural and the application changes should only go to production.

Any pointers in the right direction ?


Solution

  • I figured this out, like my last two queries.

    I was misled by this command :

    git update-index --assume-unchanged db.sqlite3
    

    Though this link clearly tells not do to so.

    For the solution, git and .gitignore works perfectly fine (stating the obvious) . It requires only one entry called db.sqlite3 and you need to ensure that you do not send db.sqlite3 to heroku. You need to have your .gitignore file updated with db.sqlite3 and use PostgreSQL in heroku.

    When I did this, I received an error called django-session not setup. Basically it meant that PostgreSQL is not ready for use. You need to ensure that you are ready to follow the steps below.


    Few things to remember :

    1. When experimenting with Django, locally you use db.sqlite3 and eager to send the database file db.sqlite3 to heroku and do not make entries in .gitignore. Don't do that.
    2. In local, use db.sqlite3 and while deploying to heroku , use PostgreSQL
    3. Create a virtual environment using pipenv
    4. Use pipenv install psycopg2
    5. Use heroku run bash -a <appname>
    6. Go to manage.py folder and run python manage.py migrate
    7. Create your superuser python manage.py createsuperuser

    This worked for me. I shall come back and update this a bit more. Three days of brain-wreck.

    Finally keep searching in Github , we have a goldmine of problems and solutions already provided. Sometimes we just need to connect the dots.