Deploying Django app to Heroku via CircleCI: How to migrate database? - django

How can I run python manage.py makemigrations and python manage.py migrate automatically when deploying a Django app to Heroku via CircleCI. It seems all commands run local to CircleCI, but not on the deployed application in production.
Is there a way?

python manage.py makemigrations should be running locally, and you may commit the migration files along with the code.
Regaridng the migrations, open your Procfile and add this line: release: python manage.py migrate.
This will tell Heroku to migrate before deploying a new version of your code.

Related

Django data was not brought to heroku

I have Django apps, which include users and posts models. The apps work perfectly at local following making migrations and migrating. I tried two different ways to deploy the apps to Heroku. First, using Heroku Git, the apps works on Heroku the same as at local.
When using GitHub, however, all data were not brought to Heroku. I tried to run "python manage.py makemigrations" and "python manage.py migrate" on Heroku, but the data from local was not brought still.
I would really appreciate it if you could please explain and help with the issue using GitHub.
I think you have to migrate on Heroku as well, try:
heroku run python manage.py migrate
See the heroku docs
check your .gitignore file maybe db.sqlite in gitignore file and you can remove it
another option is dumpdata and load in production
you can run
python3 manage.py dumpdata > data.json
and load data in production
python3 manage.py loaddata data.json

What is the easiest way to reset migrations in Heroku CLI?

I recently deployed Django REST API project on Heroku and I wanted to remove migrations and migrate again.
I have tried:
heroku run python manage.py migrate --app cyberminds-backend zero
but it returns:
CommandError: No installed app with label 'zero'
What are the easiest steps or commands to achieve this?

Heroku push does not upload migrations

I have developed a Django project and want to deploy it on a Heroku server. The problem is that my migrations are not uploaded after 'heroku push' command.
After I run
python ./Code/manage.py makemigrations
python ./Code/manage.py migrate
I see my migration files and database locally. But, after that, when I push them to Heroku, they seem not to be there. My website on Heroku gives the error that some tables are not available and when I run bash on the server to see my files there aren't any migration files and the migration folders in my apps only have the init.py file.
I even tried to make the migrations and migrate on the release phase. This is the Procfile:
release: bash ./release_tasks.sh
web: gunicorn --pythonpath Code Code.wsgi --log-file -
and this is release_tasks.sh:
python ./Code/manage.py makemigrations
python ./Code/manage.py migrate
Again, when I push to Heroku everything is ok and I see the correct migration messages, which shows that migration was successfully carried out. But still, there is no migration file on the server and my website gives the same error.
Here is what I don't understand:
screenshot
I have migrations locally and Git is up-to-date, but I don't have them on the server.
As with any code you create locally, you need to add it to git and then commit.
./Code/manage.py makemigrations
git add ....
git commit
git push origin heroku
You should remove the makemigrations step from the release tasks, there is no point running that on Heroku.

Django on Heroku - ProgrammingError at / relation "..." does not exist

I'm getting this error. I know you usually get this error because the databases wasn't properly migrated.
When I run heroku local web, the website works fine when I go to localhost:5000.
However after I deploy the app to heroku with git push heroku master, the error comes up.
In other words, it works in my local environment. But it does not work after deploying to heroku.
I have Heroku-Postgres installed as an add-on in heroku.
What could be causing this?
excute migrations and makemigrations in bash heroku. open the terminal in the local project folder and give the following commands:
heroku run bash
~$ ./manage.py makemigrations
~$ ./manage.py migrate
~$ exit
The following steps did it for me
Make all the necessary migrations (python manage.py makemigrations) and migrate (python manage.py migrate) locally,
push to heroku master
and finally running heroku run python manage.py migrate
Solved the issue
I experienced the same error after making a change to a model and then deploying this change to heroku.
The only way I managed to fix this problem was to do the following:
Reset the database in heroku
Delete the migrations files from the migrations folder for the broken app locally (but keep the directory and the __init__.py file)
Run python manage.py makemigrations and python manage.py migrate. This will repopulate the migrations folder with clean migration files.
Push changes to master (ensure that you do not have migrations directories in .gitignore files.
Deploy changes to heroku
Run the heroku shell heroku run bash
Run python manage.py migrate
I was able to do this because my tables did not have much data in, but I would try to avoid resetting the database if I had more data in my tables.

Adding South to Django project, development & production

Adding South to an existing Django project. I have it installed on both the development machine and the "production" server.
I've done the following on the development machine, then: added South app to settings.py,
python manage.py syncdb
python manage.py convert_to_south myproject.myapp
then changed some models, then
python manage.py schemamigration myproject.myapp --auto
python manage.py migrate myproject.myapp
Seems to work so far. What I am now not so sure about is what to do on the production server. Just repeat all these steps manually? Upload modified settings.py, do syncdb, convert_to_south, upload modified models.py, do schemamigration, migrate? Something different? The tutorial here says something about adding migrations to the version control, so, presumably, they should be uploaded and somehow applied on the production server?
Furthermore, right now I am using sqlite3 on the development machine and mysql on the server - does it make things any different south-wise?
My guide says:
Install South on server. import south from shell just to make sure you are using the same python env.
Add 'south' to INSTALLED_APPS in settings.py.
Upload settings.py.
Restart server
python manage.py syncdb.
Upload new app/models.py and app/migrations/ dir.
Restart server.
python manage.py migrate app --fake 0001
python manage.py migrate app
To make sure the south migration table exists,
python manage.py syncdb
and then
python manage.py migrate myproject.myapp --fake 0001
python manage.py migrate myproject.myapp
That's what's worked for me. :)
No need to do this in Django >= 1.7
i am stuck on this more then 1 hour :)
and at last find 1.7 and more have in build upgrading-from-south
for more info https://docs.djangoproject.com/en/1.7/topics/migrations/#upgrading-from-south
may be this one help you