From 414b74d3ba86be1998a7f7a1f8cf8d28d58b3c9b Mon Sep 17 00:00:00 2001 From: Daniel afx Date: Fri, 4 Feb 2022 15:15:44 +0200 Subject: [PATCH] set migrate in manage.py --- flask/manage-example.py | 23 +++++++++++++++++++++++ flask/manage.py | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 flask/manage-example.py diff --git a/flask/manage-example.py b/flask/manage-example.py new file mode 100644 index 0000000..8ceb95f --- /dev/null +++ b/flask/manage-example.py @@ -0,0 +1,23 @@ +from flask.cli import FlaskGroup + +from project import app, db, User + + +cli = FlaskGroup(app) + + +@cli.command("create_db") +def create_db(): + db.drop_all() + db.create_all() + db.session.commit() + + +@cli.command("seed_db") +def seed_db(): + db.session.add(User(email="daniel@deflax.net")) + db.session.commit() + + +if __name__ == "__main__": + cli() diff --git a/flask/manage.py b/flask/manage.py index 8ceb95f..1b4f7fd 100644 --- a/flask/manage.py +++ b/flask/manage.py @@ -1,10 +1,16 @@ +#!/usr/bin/env python + +import os +import subprocess, shlex +from forest import app, db, User + from flask.cli import FlaskGroup - -from project import app, db, User - +from flask_migrate import Migrate, MigrateCommand cli = FlaskGroup(app) +migrate = Migrate(app, db) +cli.add_command('db', MigrateCommand) @cli.command("create_db") def create_db(): @@ -12,12 +18,32 @@ def create_db(): db.create_all() db.session.commit() - @cli.command("seed_db") def seed_db(): db.session.add(User(email="daniel@deflax.net")) db.session.commit() +@cli.command("upgrade_db") +def upgrade_db(): + """Run deployment tasks.""" + from flask_migrate import upgrade + from app.models import Role, User + + # migrate database to latest revision + upgrade() + + # create user roles + Role.insert_roles() + +@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() +