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, 'flask.Flask', 'Flask', (['__name__'], {}), False, 'from flask import Flask\n'), (10, 'flask_migrate.Migrate', 'Migrate', (['application', 'database'], {}), False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), (19, 'models.database.init_app', 'database.init_app', (['application'], {}), False, 'from models import database, Role, UserRole, User\n'), (16, 'sqlalchemy_utils.database_exists', 'database_exists', (["application.config['SQLALCHEMY_DATABASE_URI']"], {}), False, 'from sqlalchemy_utils import database_exists, create_database\n'), (17, 'sqlalchemy_utils.create_database', 'create_database', (["application.config['SQLALCHEMY_DATABASE_URI']"], {}), False, 'from sqlalchemy_utils import database_exists, create_database\n'), (22, 'flask_migrate.init', 'init', ([], {}), False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), (23, 'flask_migrate.migrate', 'migrate', ([], {'message': '"""Production migration"""'}), False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), (24, 'flask_migrate.upgrade', 'upgrade', ([], {}), False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), (26, 'models.Role', 'Role', ([], {'name': '"""administrator"""'}), False, 'from models import database, Role, UserRole, User\n'), (27, 'models.Role', 'Role', ([], {'name': '"""user"""'}), False, 'from models import database, Role, UserRole, User\n'), (29, 'models.database.session.add', 'database.session.add', (['adminRole'], {}), False, 'from models import database, Role, UserRole, User\n'), (30, 'models.database.session.add', 'database.session.add', (['userRole'], {}), False, 'from models import database, Role, UserRole, User\n'), (31, 'models.database.session.commit', 'database.session.commit', ([], {}), False, 'from models import database, Role, UserRole, User\n'), (33, 'models.User', 'User', ([], {'jmbg': '"""0000000000000"""', 'forename': '"""admin"""', 'surname': '"""admin"""', 'email': '"""admin@admin.com"""', 'password': '"""1"""'}), False, 'from models import database, Role, UserRole, User\n'), (41, 'models.database.session.add', 'database.session.add', (['admin'], {}), False, 'from models import database, Role, UserRole, User\n'), (42, 'models.database.session.commit', 'database.session.commit', ([], {}), False, 'from models import database, Role, UserRole, User\n'), (44, 'models.UserRole', 'UserRole', ([], {'userId': 'admin.id', 'roleId': 'adminRole.id'}), False, 'from models import database, Role, UserRole, User\n'), (49, 'models.database.session.add', 'database.session.add', (['userRole'], {}), False, 'from models import database, Role, UserRole, User\n'), (50, 'models.database.session.commit', 'database.session.commit', ([], {}), False, 'from models import database, Role, UserRole, User\n')]