From 49197e61efef4c75462b2dbbcd814e7bcc42b43f Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sat, 13 May 2017 10:23:59 +0200 Subject: [PATCH] Rewrote database initialization and clear commands. --- accountant/__init__.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/accountant/__init__.py b/accountant/__init__.py index 02789c1..48fbeaf 100644 --- a/accountant/__init__.py +++ b/accountant/__init__.py @@ -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)