from flask import Flask
from configuration import Configuration
from flask_migrate import Migrate, init, migrate, upgrade
from models import database
from sqlalchemy_utils import database_exists, create_database, drop_database

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

migrateObject = Migrate(application, database)

done = False
while not done:
    try:
        #only from docker
        #if database_exists(application.config["SQLALCHEMY_DATABASE_URI"]):
        #    drop_database(application.config["SQLALCHEMY_DATABASE_URI"])

        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()
            done = True

    except Exception as exception:
        print(exception)



