set migrate in manage.py

This commit is contained in:
Daniel afx 2022-02-04 15:15:44 +02:00
parent bb5a741806
commit 414b74d3ba
2 changed files with 53 additions and 4 deletions

23
flask/manage-example.py Normal file
View file

@ -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()

View file

@ -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 flask.cli import FlaskGroup
from flask_migrate import Migrate, MigrateCommand
from project import app, db, User
cli = FlaskGroup(app) cli = FlaskGroup(app)
migrate = Migrate(app, db)
cli.add_command('db', MigrateCommand)
@cli.command("create_db") @cli.command("create_db")
def create_db(): def create_db():
@ -12,12 +18,32 @@ def create_db():
db.create_all() db.create_all()
db.session.commit() db.session.commit()
@cli.command("seed_db") @cli.command("seed_db")
def seed_db(): def seed_db():
db.session.add(User(email="daniel@deflax.net")) db.session.add(User(email="daniel@deflax.net"))
db.session.commit() 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__": if __name__ == "__main__":
cli() cli()