#!/usr/bin/env python

import os

from flask.cli import FlaskGroup
from app import app, db
from models import Role, User

from flask_migrate import Migrate, upgrade

cli = FlaskGroup(app)

migrate = Migrate()
migrate.init_app(app, db)

@cli.command("format_db")
def format_db():
    db.drop_all()
    db.create_all()
    db.session.commit()

@cli.command("seed_db")
def seed_db():
    # create user roles
    Role.insert_roles()

@cli.command("upgrade_db")
def upgrade_db():
    # migrate database to latest revision
    upgrade()

@cli.command("restore_db")
def restore_db(restore_file):
    """ recreate db from grid export with python3 manage.py restore /path/grid.tar.bz2 """
    print(str(restore_file))
    #TODO
    from app.models import User
    db.session.add(User(email=str(user), password=str(password), confirmed=True, confirmed_on=datetime.datetime.now()))
    db.session.commit()


if __name__ == "__main__":
    cli()
 
