Created
November 19, 2012 19:23
-
-
Save techniq/4113061 to your computer and use it in GitHub Desktop.
Flask-Script with fixture example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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