From 64e28cfdaa9ef8572f55d94a6c37898a2edecdc5 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sun, 8 Dec 2013 01:34:19 -0600 Subject: [PATCH] Add migrations with Flask-Migrate --- README.rst | 23 +++++++++---------- {{cookiecutter.repo_name}}/README.rst | 23 ++++++++++++++++++- {{cookiecutter.repo_name}}/manage.py | 3 +++ .../requirements/prod.txt | 3 +++ .../{{cookiecutter.repo_name}}/app.py | 5 ++-- .../{{cookiecutter.repo_name}}/extensions.py | 3 +++ 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/README.rst b/README.rst index 6d9abbb..d00044a 100644 --- a/README.rst +++ b/README.rst @@ -5,11 +5,21 @@ A Flask template for cookiecutter_. .. _cookiecutter: https://github.com/audreyr/cookiecutter +Use it now +---------- +:: + + $ pip install cookiecutter + $ cookiecutter https://github.com/sloria/cookiecutter-flask.git + +You will be asked about your basic info (name, project name, etc.). This info will be used in your new project. + Features -------- - Twitter Bootstrap 3 and starter templates - Flask-SQLAlchemy with basic User model +- Easy database migrations with Flask-Migrate - Flask-WTForms with login and registration forms - Flask-Login for authentication - Procfile for deploying to a PaaS (e.g. Heroku) @@ -30,19 +40,7 @@ Screenshots :target: https://dl.dropboxusercontent.com/u/1693233/github/cookiecutter-flask-02.png.png :alt: Registration form -Using this template -------------------- -:: - - $ pip install cookiecutter - $ cookiecutter https://github.com/sloria/cookiecutter-flask.git - -You will be asked about your basic info (name, project name, etc.). This info will be used in your new project. - -Blueprints? App factories? --------------------------- -If you prefer not to use blueprints or an application factory, check out the ``simple`` branch which has the older structure without these patterns. Note, however, only the ``master`` branch will be actively maintained. Inspiration ----------- @@ -74,6 +72,7 @@ Changelog - Use Factory-Boy for test factories. - Use WebTest for functional testing. - Add Flask-Debugtoolbar. +- Migrations using Flask-Migrate. 0.2.0 (09/21/2013) ****************** diff --git a/{{cookiecutter.repo_name}}/README.rst b/{{cookiecutter.repo_name}}/README.rst index c09cd25..57d6cdc 100644 --- a/{{cookiecutter.repo_name}}/README.rst +++ b/{{cookiecutter.repo_name}}/README.rst @@ -13,10 +13,13 @@ Quickstart git clone https://github.com/{{cookiecutter.github_username}}/{{ cookiecutter.repo_name }} cd {{cookiecutter.repo_name}} pip install -r requirements/dev.txt - python manage.py createdb + python manage.py db init + python manage.py db migrate + python manage.py db upgrade python manage.py server + Deployment ---------- @@ -39,3 +42,21 @@ Running Tests To run all tests, run :: python manage.py test + + +Migrations +---------- + +Whenever a database migration needs to be made. Run the following commmands: +:: + + python manage.py db migrate + +This will generate a new migration script. Then run: +:: + + python manage.py db upgrade + +To apply the migration. + +For a full migration command reference, run ``python manage.py db --help``. diff --git a/{{cookiecutter.repo_name}}/manage.py b/{{cookiecutter.repo_name}}/manage.py index 479c3e4..5c366d0 100644 --- a/{{cookiecutter.repo_name}}/manage.py +++ b/{{cookiecutter.repo_name}}/manage.py @@ -4,6 +4,8 @@ import os import sys import subprocess from flask.ext.script import Manager, Shell, Server +from flask.ext.migrate import MigrateCommand + from {{cookiecutter.repo_name}}.app import create_app from {{cookiecutter.repo_name}}.settings import DevConfig, ProdConfig from {{cookiecutter.repo_name}}.database import db @@ -35,6 +37,7 @@ def createdb(): manager.add_command("server", Server()) manager.add_command("shell", Shell(make_context=_make_context)) +manager.add_command('db', MigrateCommand) if __name__ == '__main__': manager.run() diff --git a/{{cookiecutter.repo_name}}/requirements/prod.txt b/{{cookiecutter.repo_name}}/requirements/prod.txt index cbb202c..999121b 100644 --- a/{{cookiecutter.repo_name}}/requirements/prod.txt +++ b/{{cookiecutter.repo_name}}/requirements/prod.txt @@ -11,6 +11,9 @@ itsdangerous==0.23 Flask-SQLAlchemy==1.0 SQLAlchemy==0.8.3 +# Migrations +Flask-Migrate>=1.0.0 + # Forms Flask-WTF==0.9.3 WTForms==1.0.5 diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py index cecdd09..5a70211 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py @@ -1,12 +1,10 @@ # -*- coding: utf-8 -*- from flask import Flask -from flask.ext.assets import Environment from flask_debugtoolbar import DebugToolbarExtension -from webassets.loaders import PythonLoader from {{cookiecutter.repo_name}}.settings import ProdConfig from {{cookiecutter.repo_name}}.assets import assets -from {{cookiecutter.repo_name}}.extensions import db, login_manager +from {{cookiecutter.repo_name}}.extensions import db, login_manager, migrate from {{cookiecutter.repo_name}} import public, user @@ -28,6 +26,7 @@ def register_extensions(app): login_manager.init_app(app) assets.init_app(app) toolbar = DebugToolbarExtension(app) + migrate.init_app(app, db) return None diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py index ec381fd..2332453 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py @@ -9,3 +9,6 @@ login_manager = LoginManager() from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy() + +from flask.ext.migrate import Migrate +migrate = Migrate()