<commit_before>"""
Common database operations.

"""
from microcosm_postgres.migrate import main
from microcosm_postgres.models import Model


def stamp_head(graph):
    """
    Stamp the database with the current head revision.

    """
    main(graph, "stamp", "head")


def create_all(graph):
    """
    Create all database tables.

    """
    Model.metadata.create_all(graph.postgres)
    stamp_head(graph)


def drop_all(graph):
    """
    Drop all database tables.

    """
    Model.metadata.drop_all(graph.postgres)


def recreate_all(graph):
    """
    Drop and add back all database tables.

    """
    drop_all(graph)
    create_all(graph)<commit_msg>Ensure that createall does takes no actions if migrations are possible.<commit_after>"""
Common database operations.

"""
from sqlalchemy.exc import ProgrammingError

from microcosm_postgres.migrate import main
from microcosm_postgres.models import Model


def stamp_head(graph):
    """
    Stamp the database with the current head revision.

    """
    main(graph, "stamp", "head")


def get_current_head(graph):
    """
    Get the current database head revision, if any.

    """
    session = new_session(graph)
    try:
        result = session.execute("SELECT version_num FROM alembic_version")
    except ProgrammingError:
        return None
    else:
        return result.scalar()
    finally:
        session.close()


def create_all(graph):
    """
    Create all database tables.

    """
    head = get_current_head(graph)
    if head is None:
        Model.metadata.create_all(graph.postgres)
        stamp_head(graph)


def drop_all(graph):
    """
    Drop all database tables.

    """
    Model.metadata.drop_all(graph.postgres)
    drop_alembic_table(graph)


def drop_alembic_table(graph):
    """
    Drop the alembic version table.

    """
    try:
        graph.postgres.execute("DROP TABLE alembic_version;")
    except ProgrammingError:
        return False
    else:
        return True


def recreate_all(graph):
    """
    Drop and add back all database tables.

    """
    drop_all(graph)
    create_all(graph)
{'fraction_non_alphanumeric': 0.0994789199431549, 'fraction_numerical': 0.0, 'mean_word_length': 2.9402985074626864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6063941', 'n_tokens_mistral': 654, 'n_tokens_neox': 633, 'n_words': 167}