from flask import Flask;
from configuration import Configuration;
from flask_migrate import Migrate, init, migrate, upgrade;
from models import database, Role, UserRole, User;
from sqlalchemy_utils import database_exists, create_database;

application = Flask(__name__);
application.config.from_object(Configuration);

migrateObject = Migrate(application, database);

done = False;

while not done:
    try:
        if not database_exists(application.config["SQLALCHEMY_DATABASE_URI"]):
            create_database(application.config["SQLALCHEMY_DATABASE_URI"]);

        database.init_app(application);

        with application.app_context() as context:
            init();
            migrate(message="Production migration");
            upgrade();

            adminRole = Role(name="administrator");
            userRole = Role(name="user");

            database.session.add(adminRole);
            database.session.add(userRole);
            database.session.commit();

            admin = User(
                jmbg="0000000000000",
                forename="admin",
                surname="admin",
                email="admin@admin.com",
                password="1"
            );

            database.session.add(admin);
            database.session.commit();

            userRole = UserRole(
                userId=admin.id,
                roleId=adminRole.id
            );

            database.session.add(userRole);
            database.session.commit();
            done = True;

    except Exception as err:
        print(err);

[((7, 14, 7, 29), 'flask.Flask', 'Flask', ({(7, 20, 7, 28): '__name__'}, {}), '(__name__)', False, 'from flask import Flask\n'), ((10, 16, 10, 46), 'flask_migrate.Migrate', 'Migrate', ({(10, 24, 10, 35): 'application', (10, 37, 10, 45): 'database'}, {}), '(application, database)', False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), ((19, 8, 19, 38), 'models.database.init_app', 'database.init_app', ({(19, 26, 19, 37): 'application'}, {}), '(application)', False, 'from models import database, Role, UserRole, User\n'), ((16, 15, 16, 77), 'sqlalchemy_utils.database_exists', 'database_exists', ({(16, 31, 16, 76): "application.config['SQLALCHEMY_DATABASE_URI']"}, {}), "(application.config['SQLALCHEMY_DATABASE_URI'])", False, 'from sqlalchemy_utils import database_exists, create_database\n'), ((17, 12, 17, 74), 'sqlalchemy_utils.create_database', 'create_database', ({(17, 28, 17, 73): "application.config['SQLALCHEMY_DATABASE_URI']"}, {}), "(application.config['SQLALCHEMY_DATABASE_URI'])", False, 'from sqlalchemy_utils import database_exists, create_database\n'), ((22, 12, 22, 18), 'flask_migrate.init', 'init', ({}, {}), '()', False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), ((23, 12, 23, 51), 'flask_migrate.migrate', 'migrate', (), '', False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), ((24, 12, 24, 21), 'flask_migrate.upgrade', 'upgrade', ({}, {}), '()', False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), ((26, 24, 26, 50), 'models.Role', 'Role', (), '', False, 'from models import database, Role, UserRole, User\n'), ((27, 23, 27, 40), 'models.Role', 'Role', (), '', False, 'from models import database, Role, UserRole, User\n'), ((29, 12, 29, 43), 'models.database.session.add', 'database.session.add', ({(29, 33, 29, 42): 'adminRole'}, {}), '(adminRole)', False, 'from models import database, Role, UserRole, User\n'), ((30, 12, 30, 42), 'models.database.session.add', 'database.session.add', ({(30, 33, 30, 41): 'userRole'}, {}), '(userRole)', False, 'from models import database, Role, UserRole, User\n'), ((31, 12, 31, 37), 'models.database.session.commit', 'database.session.commit', ({}, {}), '()', False, 'from models import database, Role, UserRole, User\n'), ((33, 20, 39, 13), 'models.User', 'User', (), '', False, 'from models import database, Role, UserRole, User\n'), ((41, 12, 41, 39), 'models.database.session.add', 'database.session.add', ({(41, 33, 41, 38): 'admin'}, {}), '(admin)', False, 'from models import database, Role, UserRole, User\n'), ((42, 12, 42, 37), 'models.database.session.commit', 'database.session.commit', ({}, {}), '()', False, 'from models import database, Role, UserRole, User\n'), ((44, 23, 47, 13), 'models.UserRole', 'UserRole', (), '', False, 'from models import database, Role, UserRole, User\n'), ((49, 12, 49, 42), 'models.database.session.add', 'database.session.add', ({(49, 33, 49, 41): 'userRole'}, {}), '(userRole)', False, 'from models import database, Role, UserRole, User\n'), ((50, 12, 50, 37), 'models.database.session.commit', 'database.session.commit', ({}, {}), '()', False, 'from models import database, Role, UserRole, User\n')]