Skip to content

Commit

Permalink
updated README.md to use flask cli in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jul 28, 2017
1 parent 479532f commit 349826e
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,42 @@ This is an example application that handles database migrations through Flask-Mi
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from flask_migrate import Migrate

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()
```

With the above application you can create the database or enable migrations if the database already exists with the following command:

$ python app.py db init
This will add a `migrations` folder to your application. The contents of this folder need to be added to version control along with your other source files.
$ flask db init

Note that the `FLASK_APP` environment variable must be set according to the Flask documentation for this command to work. This will add a `migrations` folder to your application. The contents of this folder need to be added to version control along with your other source files.

You can then generate an initial migration:

$ python app.py db migrate
$ flask db migrate

The migration script needs to be reviewed and edited, as Alembic currently does not detect every change you make to your models. In particular, Alembic is currently unable to detect indexes. Once finalized, the migration script also needs to be added to version control.

Then you can apply the migration to the database:

$ python app.py db upgrade
$ flask db upgrade

Then each time the database models change repeat the `migrate` and `upgrade` commands.

To sync the database in another system just refresh the `migrations` folder from source control and run the `upgrade` command.

To see all the commands that are available run this command:

$ python app.py db --help
$ flask db --help

Resources
---------
Expand Down

0 comments on commit 349826e

Please sign in to comment.