Rewrote database initialization and clear commands.

This commit is contained in:
Alexis Lahouze 2017-05-13 10:23:59 +02:00
parent a5eeede95e
commit 49197e61ef

View File

@ -19,7 +19,7 @@ import click
from flask import Flask
from flask_alembic import Alembic
from flask_alembic.cli.click import cli as alembic_cli
from flask_alembic import alembic_click
from .models import db
from .views import api, cors
@ -39,25 +39,30 @@ db.init_app(app)
# Database migrations.
alembic = Alembic(app)
app.cli.add_command(alembic_cli, 'db')
# Database initialization.
@app.cli.command()
@click.pass_context
def initdb(ctx):
@alembic_click.command()
def initdb():
""" Create the database ans stamp it. """
click.echo("Get table list.")
tables = db.engine.table_names()
if 'alembic_version' in tables:
ctx.fail("Database already initialized.")
#db.metadata.create_all(bind=db.engine)
#alembic.stamp()
click.echo("Creating database.")
db.metadata.create_all(bind=db.engine)
click.echo("Stamping database.")
alembic.stamp()
click.echo("Database created.")
@alembic_click.command()
def dropdb():
"""Clear database schema. USE CAREFULLY!!"""
click.echo("Clearing database.")
db.metadata.drop_all(bind=db.engine)
click.echo("Database cleared.")
api.init_app(app)
cors.init_app(app)