2

Does anyone have an example of successfully using flask-migrate on pythonanywhere? Does anyone have a simple example of what an app.py using migration should look like? Something along the lines of:

from flask import Flask, request, render_template
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.script import Manager
app = Flask(__name__ )
app.secret_key = 'This is really unique and secret'
app.config['SQLALCHEMY_DATABASE_URI'] = '<whatever>'
db = SQLAlchemy(app)
db.create_all()
manager = Manager( app )
migrate = Migrate( app, db )
manager.add_command('db', MigrateCommand )

I find that when I try to run

python app.py db init

it fails to generate the migration repository. Could this be a file permission issue on pythonanywhere?

Peter Cotton
  • 1,671
  • 14
  • 17

1 Answers1

1

Author of Flask-Migrate here.

The idea is that you generate your db repository in your development environment. You have to commit your repository along with your source files to source control.

Then when you install the application on your hosting service all you need to do is run the upgrade command to get your db created and migrated to the latest revision.

Update: base on your comments below, you want to develop an application from scratch. I just tested this myself, and was able to create a db repository, create a migration, and apply it. What I did is start the pythonanywhere bash console. Here is a copy of my complete session:

17:39 ~ $ mkdir dbtest
17:39 ~ $ cd dbtest
17:39 ~/dbtest $ virtualenv venv
New python executable in venv/bin/python2.7
Also creating executable in venv/bin/python
Installing setuptools............done.
Installing pip...............done.
17:39 ~/dbtest $ . venv/bin/activate
(venv)17:39 ~/dbtest $ pip install flask flask-migrate
...
(venv)17:42 ~/dbtest $ vi dbtest.py
... (entered flask-migrate example code, see below)
(venv)17:47 ~/dbtest $ python dbtest.py 
usage: dbtest.py [-?] {shell,db,runserver} ...

positional arguments:
  {shell,db,runserver}
    shell               Runs a Python shell inside Flask application context.
    db                  Perform database migrations
    runserver           Runs the Flask development server i.e. app.run()

optional arguments:
  -?, --help            show this help message and exit
(venv)17:47 ~/dbtest $ python dbtest.py db init                                                                                                                                                                                             
  Creating directory /home/miguelgrinberg/dbtest/migrations ... done
  Creating directory /home/miguelgrinberg/dbtest/migrations/versions ... done
  Generating /home/miguelgrinberg/dbtest/migrations/README ... done
  Generating /home/miguelgrinberg/dbtest/migrations/alembic.ini ... done
  Generating /home/miguelgrinberg/dbtest/migrations/env.py ... done
  Generating /home/miguelgrinberg/dbtest/migrations/script.py.mako ... done
  Generating /home/miguelgrinberg/dbtest/migrations/env.pyc ... done
  Please edit configuration/connection/logging settings in '/home/miguelgrinberg/dbtest/migrations/alembic.ini' before proceeding.
(venv)17:54 ~/dbtest $ python dbtest.py db migrate                                                                                                                                                                                          
INFO  [alembic.migration] Context impl SQLiteImpl.
INFO  [alembic.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'user'
  Generating /home/miguelgrinberg/dbtest/migrations/versions/1c4aa671e23a_.py ... done
(venv)17:54 ~/dbtest $ python dbtest.py db upgrade                                                                                                                                                                                          
INFO  [alembic.migration] Context impl SQLiteImpl.
INFO  [alembic.migration] Will assume non-transactional DDL.
INFO  [alembic.migration] Running upgrade None -> 1c4aa671e23a, empty message
(venv)17:55 ~/dbtest $ ls -l
total 8
-rw-r--r-- 1 miguelgrinberg registered_users 3072 Sep 28  2014 app.db
-rwxrwxr-x 1 miguelgrinberg registered_users  511 Sep 28 17:48 dbtest.py
drwxrwxr-x 3 miguelgrinberg registered_users  100 Sep 28 17:55 migrations
drwxrwxr-x 6 miguelgrinberg registered_users   52 Sep 28 17:41 venv

The example application that I used to test this is below:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'

db = SQLAlchemy(app)
migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

class User(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(128))

if __name__ == '__main__':
    manager.run()

Any chance you already have a migration repository created? Or a database?

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Hi Miguel. Because I haven't got as far as generating the migration scripts in the dev environment (I use pythonanywhere as a dev environ) it isn't clear to me if source control is absolutely a requirement for migration (or just good practice). Perhaps I read the section of your O'Reilly book too quickly. – Peter Cotton Sep 28 '14 at 16:21
  • In this context what I meant by "source control" is that you keep your migration files together with your source code. I assumed you were deploying the application to pythonanywhere, not developing directly there. Let me add other ideas to my answer. – Miguel Grinberg Sep 28 '14 at 17:37
  • File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 142, in set_main_option self.file_config.set(self.config_ini_section, name, value) File "/usr/lib/python2.7/ConfigParser.py", line 753, in set ConfigParser.set(self, section, option, value) File "/usr/lib/python2.7/ConfigParser.py", line 396, in set raise NoSectionError(section) ConfigParser.NoSectionError: No section: 'alembic' – Peter Cotton Sep 29 '14 at 01:32
  • I haven't tried your method of setting up venv (but probably should). Though alembic is certainly installed so perhaps there is a quick config fix? – Peter Cotton Sep 29 '14 at 01:34
  • @PeterCotton: it seems alembic isn't finding the config file. When using Flask-Migrate the config file is not in a default place, I'm putting it inside the `migrations` directory, while Alembic places it by default one directory up. Make sure you are not mixing usages of straight Alembic with Flask-Migrate, maybe that's where the disconnect is. – Miguel Grinberg Sep 29 '14 at 01:42