Flask database migrations on heroku - python

With my app I am using flask-script and flask-migrate for database migrations, everything works locally. When, I run
heroku run python manage.py db init
It creates this output:
Running python manage.py db init on ⬢ fpds-scheduler... up, run.1290 (Free)
Creating directory /app/migrations ... done
Creating directory /app/migrations/versions ... done
Generating /app/migrations/README ... done
Generating /app/migrations/script.py.mako ... done
Generating /app/migrations/alembic.ini ... done
Generating /app/migrations/env.py ... done
Please edit configuration/connection/logging settings in '/app/migrations/alembic.ini' before
proceeding.
But when I run heroku run python manage.py db migrate I get an error
alembic.util.exc.CommandError: Path doesn't exist: 'migrations'. Please use the 'init' command to create a new scripts folder.
When I run heroku run bash and look at my directory I can see that there is no migrations folder...
I have tried running giving the command --app fpds-scheduler but that doesn't seem to be working either.
I am not sure what is going wrong?

You must not create the migrations on Heroku itself. The filesystem is ephemeral and anything written programatically will be lost between invocations.
You need to create the migrations locally, commit them to version control, deploy, and only then run them on Heroku.

Related

"manage.py startapp" doesn't create db.sqlite3

I'm following an online tutorial in order to learn Django/Python. I'm using PyCharm Community Edition as my IDE. On Windows 10.
When I run python manage.py startapp myapp at the (venv) prompt in terminal window , no error is shown, and \myapp folder is created with the expected content. However, the file db.sqlite3 is not created, and I can't follow through the rest of the tutorial.
What might be going wrong here?
Thank you very much.
when you start a new django app no database must created.
you can run command
python manage.py migrate
to generate database for your project.
default database is sqlite and stored in file named db.sqlite3
Command python manage.py startapp myapp does not create db.sqlite3.
Run:
python manage.py makemigrations
python manage.py migrate
It will automatically create one if not present.

Automatically run django-admin migrate on startup

I'm trying to make the installation procedure of a Django application easier. One of the steps is to run django-admin.py migrate, is there any way this can be automatically run when the application is started with Gunicorn, uWSGI or mod_wsgi.
edit: Clearup up what I mean with startup.
edit2: This is an open-source application I co-maintain. I do not deploy it. It is being deployed by regular system admin's who often don't know anything about Django. One complaint is that the application is hard to deploy because all these manual manage steps needed. Asking them to create some scripts in their deployment tool does not make installation easier. What I want is that when then application is started it checks if there is a database and if not runs manage.py migrate.
edit3: deployment is usually through pip
This would normally go into your deployment script, depending on the routine with which you update the Django application in your target environment.
You not only need to run migrate, but probably a list of calls:
migrate
collectstatic (you'd normally put those in a directory that is then served directly by your proxy (e.g. nginx) because Django is rather comparably slow with those)
compilemessages (generating the *.mo translation files)
To allow the most flexibility in your setup, you can create a Django command called setup.py that can do all of that. This can also load DB fixtures if you need to initialize data.
Here is a snippet from what I do in setup.py:
def handle(self, *args, **options):
LOGGER.info('Setup: translations ...')
call_command('compilemessages', locale=['de'], verbosity=2)
LOGGER.info('Setup: translations ... DONE')
LOGGER.info('Setup: DB check and fixtures ...')
db_error = self.load_db(options)
if db_error:
if options['wait_db']:
while db_error:
LOGGER.warning('%s - retrying in 5 secs...', db_error)
time.sleep(5)
db_error = self.load_db(options)
else:
LOGGER.error(db_error)
LOGGER.info('Setup: DB check and fixtures ... %s', 'FAILED' if db_error else 'OK')
LOGGER.info('Setup: collectstatic to %s ...', settings.STATIC_ROOT)
cs_error = None
try:
call_command('collectstatic', '--clear', '--noinput')
except FileNotFoundError as fnfe:
cs_error = fnfe
LOGGER.warning('Error during "collectstatic": %s', fnfe)
LOGGER.info('Setup: collectstatic ... %s', 'WARN' if cs_error else 'OK')
def load_db(self, options):
try:
call_command('migrate')
if not User.objects.count():
call_command('loaddata', 'auth.json')
LOGGER.info('Setup: loaded fixture auth.json')
except DatabaseError as dbe:
return dbe
If you deploy via GIT you could use a GIT post_merge hook or similar that runs a bash script calling any manage.py commands that you need.
If you are deploying via Docker, you can put the call to ./manage.py setup into the docker-entrypoint.sh script which is called whenever that Docker image is run.
It depends on what process manager you use, if you are starting it manually or via a script just run manage.py migrate before it, i use supervisor and just added it as a program in that like
[program:migrate]
command = python manage.py migrate
autorestart = false
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Any process manager will be able to do it, just make sure you set it to not restart as the command dies after the migrations are done.

Django Heroku Error "Your models have changes that are not yet reflected in a migration"

I recently added a model to my app (UserProfile) and when I pushed the changes to Heroku, I think I accidentally ran heroku run python manage.py makemigrations. Now when I try to run heroku run python manage.py migrate I get the error below
(leaguemaster) benjamins-mbp-2:leaguemaster Ben$ heroku run python manage.py migrate
Running `python manage.py migrate` attached to terminal... up, run.1357
Operations to perform:
Synchronize unmigrated apps: allauth
Apply all migrations: auth, admin, socialaccount, sites, accounts, account, contenttypes, sessions, leagueapp
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
How do I fix this? Please help!
You need to first create the migrations locally, add them to your repository, commit the files with the new migrations and then push to heroku.
The sequence is something like this:
1. (add/modify some someapp/models.py)
2. python manage.py makemigrations someapp
3. python manage.py migrate
4. git add someapp/migrations/*.py (to add the new migration file)
5. git commit -m "added migration for app someapp"
6. git push heroku
7. heroku run python manage.py migrate
1. Make migrations locally
$ python manage.py makemigrations && python manage.py migrate
2. Commit changes and push it on the server
$ git add --all
$ git commit -m "Fixed migrate error"
$ git push heroku master
3. Now run migrate on the server
$ heroku run python manage.py migrate
You also need to be sure that you haven't ignored these migration paths in your .gitingnore file
It sounds like you ran makemigrations after you made changes to your model but before you had an initial migration file. Try to revert your app to the state it was before you added the new model and run makemigrations again to create the initial migration. Then add your updates back in and run makemigrations once more. This will create a second migration from your initial data structure to the new updated one. Then try your deployment.
https://docs.djangoproject.com/en/1.7/topics/migrations/#adding-migrations-to-apps
Answered for my case:
your_field=models.CharField(max_length=9,default=False)
Convert to
your_field=models.CharField(max_length=9,default='False')
My case:
In models.py for the field I wanted to set the default value to False. First default = False my contract without ''. But after running python manage.py migrate, I got the above error. The problem was solved after placing False inside ''.
Sometimes it is necessary for the default value in our field model to be a False string type.
If
default = False
If you write in the model, you will encounter this error.
In fact, depending on the type of field we have, we can not always set the default value of a field in the model to True or False. Must be converted to string for CharField type.

Deploy hook for django syncdb not working on openshift

I created a django app on openshift successfully. But, I'm not able to run syncdb using the following deploy hook.
#!/bin/bash
source $OPENSHIFT_HOMEDIR/python/virtenv/bin/activate
cd $OPENSHIFT_REPO_DIR/wsgi/$OPENSHIFT_APP_NAME
python manage.py syncdb --noinput
What could be wrong? Please help!
I think its simply because you forget to add the right for file execution with chmod +x filename

Unable to migrate Django app with South on Heroku

I've installed South on my existing Django app. This Django app is on Heroku as well.
Without making any changes to the models, I've done the following commands locally:
manage.py schemamigration app_name --initial
manage.py migrate --fake
Then I attempted to push heroku master, and migrate there. But I receive the following:
Running migrations for app_name:
- Nothing to migrate.
- Loading initial data for notecards.
No fixtures found
I get the same message if I try to fake the Heroku migrate.
I figured, this may be ok since technically there is nothing to migrate as the db stays the same.
So I made a small change to one of the models and did the following:
manage.py schemamigration app_name --auto
manage.py migrate
I then pushed to Heroku and attempted to migrate there, and I still receive the following:
Running migrations for app_name:
- Nothing to migrate.
- Loading initial data for notecards.
No fixtures found
Any help with what I'm doing wrong would be great. THanks.
For anyone else who comes across this issue, and for my own reference. Thanks to Chris Pratt for putting me on the right track.
This was resolved by:
Locally:
recursively removing any old migrations git rm -r migrations
flushing old database (this removes all data, but for me this wasnt an issue) manage.py flush
delete migrations folder in app folder
run initial schemamigration manage.py schemamigration --initial
run fake migrate manage.py migrate --fake
push heroku master
On Heroku:
migrate heroku run python manage.py migrate app_name
Then, locally, you can run migrate --auto, push to heroku, and run migrate app_name

Categories

Resources