import subprocess
from os import environ

docker_db = subprocess.Popen(['docker-compose', 'up', 'database'], stdout=subprocess.PIPE)
try:
    environ['DATABASE_URL'] = "postgresql://matchinit:matchinit@localhost/matchinit"

    from app import *

    out = ""
    while "ready to accept connections" not in out:
        out = str(docker_db.stdout.readline())

    db.drop_all()
    db.create_all()

    # initialize main channel
    from Models.ChannelModel import create_main_channel
    create_main_channel()

    print(f"initiated database @ { environ['DATABASE_URL'] }")


finally:
    docker_db.terminate()
