diff --git a/app/commands/commands.py b/app/commands/commands.py index 6ab4b6a..a86427f 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -3,7 +3,7 @@ import click import random from flask import current_app -from sqlalchemy.exc import OperationalError +from sqlalchemy.exc import OperationalError, IntegrityError from sqlalchemy.sql import func from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session @@ -17,9 +17,10 @@ from . import bp @bp.cli.command("feed_from_lesia") def feed_from_lesia(): - """ Feed db with agents from a lesia like mysql database. + """ + Feed db with agents from a lesia like mysql database. - configure that database uri in the db_config.py file. + configure the proper database uri in the db_config.py file. """ Base = automap_base() @@ -29,8 +30,7 @@ def feed_from_lesia(): try: Base.prepare(engine, reflect=True) except OperationalError: - # TODO: use logging facility instead - print("Please, configure the mysql database (see db_config.py)") + current_app.logger.error("Please, configure the mysql database (see db_config.py)") sys.exit(-1) # mapped classes are now created with names by default @@ -142,11 +142,25 @@ def user_delete(user_id): @bp.cli.command('create_db') def create_db(): - """ Create the database structure.""" + """ + Create the database structure. Database should be empty. + + configure the proper database uri in the db_config.py file. + """ db.create_all() admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin') - db.session.add(admin) - db.session.commit() + sqlite_uri = db.engine.url.__str__() if 'sqlite' in db.engine.url.__str__() else None + try: + db.session.add(admin) + db.session.commit() + except IntegrityError: + current_app.logger.error("User admin already exists, database should be empty at create") + if sqlite_uri: + current_app.logger.error("see "+sqlite_uri) + sys.exit(-1) + + if sqlite_uri: + current_app.logger.info("Created sqlite db: "+sqlite_uri) @bp.cli.command('user_add') @@ -159,7 +173,7 @@ def user_add(email, name, login, password): user = User(email=email, name=name, login=login, password=password) db.session.add(user) db.session.commit() - print("added ", name) + current_app.logger.info("added ", name) @bp.cli.command('user_show_all') -- libgit2 0.21.2