Skip to content

Instantly share code, notes, and snippets.

@techniq
Created November 19, 2012 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techniq/4113061 to your computer and use it in GitHub Desktop.
Save techniq/4113061 to your computer and use it in GitHub Desktop.
Flask-Script with fixture example
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager, prompt_bool
import application.models
db = SQLAlchemy()
manager = Manager("Perform database operations")
@manager.command
def drop():
"Drops database tables"
if prompt_bool("Are you sure you want to lose all your data"):
db.drop_all()
@manager.command
def create(default_data=True, sample_data=False):
"Creates database tables from sqlalchemy models"
db.create_all()
populate(default_data, sample_data)
@manager.command
def recreate(default_data=True, sample_data=False):
"Recreates database tables (same as issuing 'drop' and then 'create')"
drop()
create(default_data, sample_data)
@manager.command
def populate(default_data=False, sample_data=False):
"Populate database with default data"
from application.fixtures import dbfixture
datasets = []
if default_data:
from application.fixtures.default_data import all
datasets.extend(all)
if sample_data:
from application.fixtures.sample_data import all
datasets.extend(all)
data = dbfixture.data(*datasets)
data.setup()
from sqlalchemy import create_engine
from fixture import SQLAlchemyFixture
from fixture.style import NamedDataStyle
from application import models
from application.database import db
engine = db.engine
dbfixture = SQLAlchemyFixture(env=models, style=NamedDataStyle(), engine=engine)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment