# Path: pgcontents/constants.py
# ALEMBIC_INI_TEMPLATE = f.read()
# 
# ALEMBIC_DIR_LOCATION = join(dirname(__file__), 'alembic')


from contextlib import contextmanager
from os.path import join
from IPython.utils.tempdir import TemporaryDirectory
from pgcontents.constants import (
    ALEMBIC_INI_TEMPLATE,
    ALEMBIC_DIR_LOCATION,
)
import subprocess
"""
Utilities for running migrations.
"""




@contextmanager
def temp_alembic_ini(alembic_dir_location, sqlalchemy_url):
    """
    Temporarily write an alembic.ini file for use with alembic migration
    scripts.
    """
    with TemporaryDirectory() as tempdir:
        alembic_ini_filename = join(tempdir, 'temp_alembic.ini')
        with open(alembic_ini_filename, 'w') as f:
            f.write(
                ALEMBIC_INI_TEMPLATE.format(
                    alembic_dir_location=alembic_dir_location,
                    sqlalchemy_url=sqlalchemy_url,
                )
            )
        yield alembic_ini_filename


def upgrade(db_url, revision):
    """
    Upgrade the given database to revision.
    """

# Path: pgcontents/constants.py
# ALEMBIC_INI_TEMPLATE = f.read()
# 
# ALEMBIC_DIR_LOCATION = join(dirname(__file__), 'alembic')


# Path: pgcontents/utils/migrate.py
from contextlib import contextmanager
from os.path import join
from IPython.utils.tempdir import TemporaryDirectory
from pgcontents.constants import (
    ALEMBIC_INI_TEMPLATE,
    ALEMBIC_DIR_LOCATION,
)
import subprocess

"""
Utilities for running migrations.
"""




@contextmanager
def temp_alembic_ini(alembic_dir_location, sqlalchemy_url):
    """
    Temporarily write an alembic.ini file for use with alembic migration
    scripts.
    """
    with TemporaryDirectory() as tempdir:
        alembic_ini_filename = join(tempdir, 'temp_alembic.ini')
        with open(alembic_ini_filename, 'w') as f:
            f.write(
                ALEMBIC_INI_TEMPLATE.format(
                    alembic_dir_location=alembic_dir_location,
                    sqlalchemy_url=sqlalchemy_url,
                )
            )
        yield alembic_ini_filename


def upgrade(db_url, revision):
    """
    Upgrade the given database to revision.
    """

    with temp_alembic_ini(ALEMBIC_DIR_LOCATION, db_url) as alembic_ini: