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);

[((254, 269), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (259, 269), False, 'from flask import Flask\n'), ((335, 365), 'flask_migrate.Migrate', 'Migrate', (['application', 'database'], {}), '(application, database)\n', (342, 365), False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), ((572, 602), 'models.database.init_app', 'database.init_app', (['application'], {}), '(application)\n', (589, 602), False, 'from models import database, Role, UserRole, User\n'), ((423, 485), 'sqlalchemy_utils.database_exists', 'database_exists', (["application.config['SQLALCHEMY_DATABASE_URI']"], {}), "(application.config['SQLALCHEMY_DATABASE_URI'])\n", (438, 485), False, 'from sqlalchemy_utils import database_exists, create_database\n'), ((499, 561), 'sqlalchemy_utils.create_database', 'create_database', (["application.config['SQLALCHEMY_DATABASE_URI']"], {}), "(application.config['SQLALCHEMY_DATABASE_URI'])\n", (514, 561), False, 'from sqlalchemy_utils import database_exists, create_database\n'), ((668, 674), 'flask_migrate.init', 'init', ([], {}), '()\n', (672, 674), False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), ((688, 727), 'flask_migrate.migrate', 'migrate', ([], {'message': '"""Production migration"""'}), "(message='Production migration')\n", (695, 727), False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), ((741, 750), 'flask_migrate.upgrade', 'upgrade', ([], {}), '()\n', (748, 750), False, 'from flask_migrate import Migrate, init, migrate, upgrade\n'), ((777, 803), 'models.Role', 'Role', ([], {'name': '"""administrator"""'}), "(name='administrator')\n", (781, 803), False, 'from models import database, Role, UserRole, User\n'), ((828, 845), 'models.Role', 'Role', ([], {'name': '"""user"""'}), "(name='user')\n", (832, 845), False, 'from models import database, Role, UserRole, User\n'), ((860, 891), 'models.database.session.add', 'database.session.add', (['adminRole'], {}), '(adminRole)\n', (880, 891), False, 'from models import database, Role, UserRole, User\n'), ((905, 935), 'models.database.session.add', 'database.session.add', (['userRole'], {}), '(userRole)\n', (925, 935), False, 'from models import database, Role, UserRole, User\n'), ((949, 974), 'models.database.session.commit', 'database.session.commit', ([], {}), '()\n', (972, 974), False, 'from models import database, Role, UserRole, User\n'), ((997, 1102), 'models.User', 'User', ([], {'jmbg': '"""0000000000000"""', 'forename': '"""admin"""', 'surname': '"""admin"""', 'email': '"""admin@admin.com"""', 'password': '"""1"""'}), "(jmbg='0000000000000', forename='admin', surname='admin', email=\n    'admin@admin.com', password='1')\n", (1001, 1102), False, 'from models import database, Role, UserRole, User\n'), ((1206, 1233), 'models.database.session.add', 'database.session.add', (['admin'], {}), '(admin)\n', (1226, 1233), False, 'from models import database, Role, UserRole, User\n'), ((1247, 1272), 'models.database.session.commit', 'database.session.commit', ([], {}), '()\n', (1270, 1272), False, 'from models import database, Role, UserRole, User\n'), ((1298, 1344), 'models.UserRole', 'UserRole', ([], {'userId': 'admin.id', 'roleId': 'adminRole.id'}), '(userId=admin.id, roleId=adminRole.id)\n', (1306, 1344), False, 'from models import database, Role, UserRole, User\n'), ((1405, 1435), 'models.database.session.add', 'database.session.add', (['userRole'], {}), '(userRole)\n', (1425, 1435), False, 'from models import database, Role, UserRole, User\n'), ((1449, 1474), 'models.database.session.commit', 'database.session.commit', ([], {}), '()\n', (1472, 1474), False, 'from models import database, Role, UserRole, User\n')]