Simplify init database tasks.

This commit is contained in:
Dorian 2017-03-11 16:39:29 -05:00
parent d701575755
commit 5506b0580f
1 changed files with 4 additions and 22 deletions

View File

@ -4,13 +4,8 @@ Tasks for working with the database attached to the API.
:copyright: Copyright 2013-2017, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""
import sys
import traceback
import invoke as inv
import sqlalchemy
import sqlalchemy_utils
from sqlalchemy import exc as sql_error
import rookeries
from rookeries import database as rookeries_db
@ -24,23 +19,10 @@ def init(ctx):
:param ctx: Context of the invoke task.
"""
db_connection = rookeries_db.get_db_connection_from_env()
if not sqlalchemy_utils.database_exists(db_connection):
sqlalchemy_utils.create_database(db_connection)
try:
db_engine = sqlalchemy.create_engine(db_connection, echo=True)
rookeries_db.db.Model.metadata.create_all(db_engine)
except sql_error.OperationalError as err:
print(f'Unable to connect to the database because of "{err}" \n Stack: \n')
traceback.print_tb(sys.exc_info()[2])
raise err
except Exception as err:
print(f'Unable to run server test because of "{err}" \n Stack: \n')
traceback.print_tb(sys.exc_info()[2])
raise err
app = rookeries.make_rookeries_app()
rookeries_db.db.init_app(app)
with app.app_context():
rookeries_db.db.create_all()
@inv.task