Rewrote database initialization and clear commands.
This commit is contained in:
parent
a5eeede95e
commit
49197e61ef
@ -19,7 +19,7 @@ import click
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
from flask_alembic import Alembic
|
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 .models import db
|
||||||
from .views import api, cors
|
from .views import api, cors
|
||||||
@ -39,25 +39,30 @@ db.init_app(app)
|
|||||||
# Database migrations.
|
# Database migrations.
|
||||||
alembic = Alembic(app)
|
alembic = Alembic(app)
|
||||||
|
|
||||||
app.cli.add_command(alembic_cli, 'db')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Database initialization.
|
# Database initialization.
|
||||||
@app.cli.command()
|
@alembic_click.command()
|
||||||
@click.pass_context
|
def initdb():
|
||||||
def initdb(ctx):
|
|
||||||
""" Create the database ans stamp it. """
|
""" Create the database ans stamp it. """
|
||||||
|
click.echo("Creating database.")
|
||||||
click.echo("Get table list.")
|
db.metadata.create_all(bind=db.engine)
|
||||||
tables = db.engine.table_names()
|
click.echo("Stamping database.")
|
||||||
|
alembic.stamp()
|
||||||
if 'alembic_version' in tables:
|
|
||||||
ctx.fail("Database already initialized.")
|
|
||||||
|
|
||||||
#db.metadata.create_all(bind=db.engine)
|
|
||||||
#alembic.stamp()
|
|
||||||
click.echo("Database created.")
|
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)
|
api.init_app(app)
|
||||||
cors.init_app(app)
|
cors.init_app(app)
|
||||||
|
Loading…
Reference in New Issue
Block a user