Merging different alembic revision heads fails on heroku - python

I'm working to modify a cookiecutter Flask app.
locally I have deleted the migration folder and sqllite db a couple of times during development. I've pushed my chnages to heroku.
When trying to migrate the heroku postgresdb :
$ heroku run python manage.py db upgrade
.....
alembic.util.CommandError: Multiple head revisions are present for given argument 'head'; please specify a specific target revision, '<branchname>#head' to narrow to a specific head, or 'heads' for all heads
following http://alembic.readthedocs.org/en/latest/branches.html I tried:
$ heroku run python manage.py db merge heads
Running python manage.py db merge heads on myapp... up, run.9635
Generating /app/migrations/versions/35888775_.py ... done
Then I tried:
$ heroku run python manage.py db upgrade
Running python manage.py db upgrade on myapp... up, run.7021
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
....
"%s#head" % branch_label if branch_label else "head")
alembic.util.CommandError: Multiple head revisions are present for given argument 'head'; please specify a specific target revision, '<branchname>#head' to narrow to a specific head
, or 'heads' for all heads
How can I merge the revision heads into one and make sure this is synced with my development version?

I contacted heroku support and got the following back (which worked for me):
Hi,
To remove a folder from your local repository, git rm needs to be run. Could you please try something like below?
$ git rm -r migrations
$ git commit -m 'Remove migrations directory'
$ git push heroku master
To see differences between actual files and what are registered onto your local repository, git status may be useful.
Please let us know if you have any difficulty here.

Related

Heroku No migrations to apply

I'm having trouble with my database at Heroku:
I've added some models for my App and whenever I run makemigrations it detects those changes. However when I run migrate it only says no migrations to apply. Please help!
I'VE FINALLY FIXED IT!!! This is what I did:
First I did a backup Download by heroku pg:backups:restore capture
then heroku pg:backups:restore download
it made a "latest.dump" file
then I used heroku pg:reset
it deleted the recent database of host
then I applied my fixes on my models and forms
new fields has to have "defaults=None" for models with Data you want to preserve (If you won't do this, the merge for this model won't take effect and will be cancelled)
then I run heroku run bash
run python manage.py makemigrations appname
then migrate
and then used "pg_restore --verbose --clean --no-acl --no-owner -h yourDBhost -U yourDBuser -d yourDBname latest.dump" (Your database credentials will be found on your postgresql addon settings) and then type or copy-paste yourDBpassword(from credentials also) to merge the backed up database with the new one with changes
and know it's done and perfectly working!

Error while deploy my django website on heroku

I created a personal portfolio website using django and it also includes a blog. You can see the exact directory listing and source code in my github repository by clicking here
I have the procfile and the requirements.txt files as said in the heroku website and did the following in command prompt as directed by heroku :
$ heroku login
$ heroku git:clone -a appname
$ cd appname
$ git add .
$ git commit -am "make it better"
$ git push heroku master
Now I see the following error while deploying and the push fails :
Warning: Your application is missing a Procfile. This file tells Heroku how to run your application.
Yes there is a procfile in the directory though.
Please help me deploy this website in heroku.
Your file is called Procfile.txt. It should be called just Procfile.
It might be that you made a .txt file instead of a non-extension one or that you named it procfile and not Procfile. Heroku is sensitive about capitalisation.

Django on Heroku: relation does not exist

I built a Django 1.9 project locally with sqlite3 as my default database. I have an application named Download which defines the DownloadedSongs table in models.py:
models.py
from __future__ import unicode_literals
from django.db import models
class DownloadedSongs(models.Model):
song_name = models.CharField(max_length = 255)
song_artist = models.CharField(max_length = 255)
def __str__(self):
return self.song_name + ' - ' + self.song_artist
Now, in order to deploy my local project to Heroku, I added the following lines at the bottom of my settings.py file:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
My application has a form with a couple of text fields, and on submitting that form, the data gets inserted into the DownloadedSongs table. Now, when I deployed my project on Heroku and tried submitting this form, I got the following error:
Exception Type: ProgrammingError at /download/
Exception Value: relation "Download_downloadedsongs" does not exist
LINE 1: INSERT INTO "Download_downloadedsongs" ("song_name", "song_a...
This is how my requirements.txt file looks like:
beautifulsoup4==4.4.1
cssselect==0.9.1
dj-database-url==0.4.1
dj-static==0.0.6
Django==1.9
django-toolbelt==0.0.1
gunicorn==19.6.0
lxml==3.6.0
psycopg2==2.6.1
requests==2.10.0
static3==0.7.0
Also, I did try to run the following commands as well:
heroku run python manage.py makemigrations
heroku run python manage.py migrate
However, the issue still persists. What seems to be wrong here?
Make sure your local migration folder and content is under git version control.
If not, add, commit & push them as follows (assuming you have a migrations folder under <myapp>, and your git remote is called 'heroku'):
git add <myapp>/migrations/*
git commit -m "Fix Heroku deployment"
git push heroku
Wait until the push is successful and you get the local prompt back.
Then log in to heroku and there execute migrate.
To do this in one execution environment, do not launch these as individual heroku commands, but launch a bash shell and execute both commands in there: (do not type the '~$', this represents the Heroku prompt)
heroku run bash
~$ ./manage.py migrate
~$ exit
You must not run makemigrations via heroku run. You must run it locally, and commit the result to git. Then you can deploy that code and run those generated migrations via heroku run python manage.py migrate.
The reason is that heroku run spins up a new dyno each time, with a new filesystem, so any migrations generated in the first command are lost by the time the second command runs. But in any case, migrations are part of your code, and must be in version control.
As Heroku's dynos don't have a filesystem that persists across deploys, a file-based database like SQLite3 isn't going to be suitable. It's a great DB for development/quick prototypes, though. https://stackoverflow.com/a/31395988/784648
So between deploys your entire SQLite database is going to be wiped, you should move onto a dedicated database when you deploy to heroku I think. I know heroku has a free tier for postgres databases which I'd recommend if you just want to test deployment to heroku.
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb
this worked for me.
I know this is old, but I had this issue and found this thread useful.
To sum up, the error can also appear when executing the migrations (which is supposed to create the needed relations in the DB), because recent versions of Django check your urls.py before doing the migrations. In my case - and in many others' it seems, loading urls.py meant loading the views, and some views were class-based and had an attribute defined through get_object_or_404:
class CustomView(ParentCustomView):
phase = get_object_or_404(Phase, code='C')
This is what was evaluated before the migrations actually ran, and caused the error. I fixed it by turning my view's attribute as a property:
class CustomView(ParentCustomView):
#property
def phase(self):
return get_object_or_404(Phase, code='C')
You'll know quite easily if this is the problem you are encountering, as the Traceback will point you towards the problematic view.
Also this problem might not appear in development because you have migrated before creating the view.

Git merge and select local file in conflicts

I have an flask project which I have deployed to openshift, which I maintain locally. I deleted it from openshift and now would like to redeploy it. In the openshift gui, I created the a python app and grabbed the openshift git repo;
ssh://*******#myproject.rhcloud.com/~/git/myproject.git/
I changed origin to the new url with:
git remote set-url origin ssh://*******#myproject.rhcloud.com/~/git/myproject.git/
Now based on https://developers.openshift.com/en/knowledge-base.html#sync-a-new-git-repo-with-an-existing-git-repo I want to pull and merge.
I tried:
$ git pull ssh://*******#myproject.rhcloud.com/~/git/myproject.git/
From ssh://*******#myproject.rhcloud.com/~/git/myproject.git/
* branch HEAD -> FETCH_HEAD
Auto-merging wsgi.py
CONFLICT (add/add): Merge conflict in wsgi.py
Auto-merging .openshift/markers/README.md
CONFLICT (add/add): Merge conflict in .openshift/markers/README.md
Auto-merging .openshift/cron/README.cron
CONFLICT (add/add): Merge conflict in .openshift/cron/README.cron
Automatic merge failed; fix conflicts and then commit the result.
Basically at this point If I understand correctly, I want to choose my local files over the remote files in all cases. What is the best way to do this.
I'm suspecting a rebase may be in order but not sure.
If you want to choose your local files over the remote files, do:
git checkout --ours wsgi.py
git checkout --ours .openshift/markers/README.md
git checkout --ours .openshift/cron/README.cron
git commit -a

Your recommendation to reset a postgress database in heroku after CircleCI tests

I am using Circle CI for tests and pushing a python application to heroku in order to also run Web GUI tests on the machine.
What is your recommended way of filling up the heroku instance with a certain database content?
I do not think that circle CI should access the heroku instance or the database directly?
The heroku deploy hooks only seem to be able to call a web hook. But I would like to run a command to reset the database.
If you're using the built-in heroku deployments, you won't be able to do this, e.g. if your configuration looks like this:
deployment:
staging:
branch: master
heroku:
appname: foo-bar-123
You can instead configure your deployment to run several commands:
deployment:
production:
branch: production
commands:
- "[[ ! -s \"$(git rev-parse --git-dir)/shallow\" ]] || git fetch --unshallow"
- git push git#heroku.com:foo-bar-123.git $CIRCLE_SHA1:refs/heads/master
- heroku run rake db:migrate --app foo-bar-123:
timeout: 400 # if your deploys take a long time
The comment from Two-Bit Alchemist would be a good command to reset the database.
The above examples are taken from: https://circleci.com/docs/continuous-deployment-with-heroku#heroku-setup

Categories