<commit_before># Copyright 2012 OpenSmigrations.ck LLC.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from sqlalchemy import and_, Index, String, Column, MetaData, Table
from sqlalchemy.sql.expression import select, update


def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    instances = Table('instances', meta, autoload=True)
    migrations = Table('migrations', meta, autoload=True)

    # drop old index:
    i = _old_index(migrations)
    i.drop(migrate_engine)

    # add columns.  a node is the same as a compute node's
    # hypervisor hostname:
    source_node = Column('source_node', String(length=255))
    migrations.create_column(source_node)

    dest_node = Column('dest_node', String(length=255))
    migrations.create_column(dest_node)

    # map compute hosts => list of compute nodes
    nodemap = _map_nodes(meta)

    # update migration and instance records with nodes:
    _update_nodes(nodemap, instances, migrations)

    # add new index:
    migrations = Table('migrations', meta, autoload=True)
    _add_new_index(migrations, migrate_engine)


def downgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    migrations = Table('migrations', meta, autoload=True)

    # drop new columns:
    source_node = Column('source_node', String(length=255))
    migrations.drop_column(source_node)

    dest_node = Column('dest_node', String(length=255))
    migrations.drop_column(dest_node)

    # drop new index:
    _drop_new_index(migrations, migrate_engine)

    # re-add old index:
    i = _old_index(migrations)
    i.create(migrate_engine)


def _map_nodes(meta):
    """Map host to compute node(s) for the purpose of determining which hosts
    are single vs multi-node.
    """

    services = Table('services', meta, autoload=True)
    c_nodes = Table('compute_nodes', meta, autoload=True)

    q = select([services.c.host, c_nodes.c.hypervisor_hostname],

               whereclause=and_(c_nodes.c.deleted == 0,
                                services.c.deleted == 0),

               from_obj=c_nodes.join(services,
                                     c_nodes.c.service_id == services.c.id)
    )

    nodemap = {}

    for (host, node) in q.execute():
        nodes = nodemap.setdefault(host, [])
        nodes.append(node)

    return nodemap


def _add_new_index(migrations, migrate_engine):
    if migrate_engine.name == "mysql":<commit_msg>Update migration so it supports PostgreSQL.<commit_after># Copyright 2012 OpenSmigrations.ck LLC.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from sqlalchemy import and_, Index, String, Column, MetaData, Table
from sqlalchemy.sql.expression import select, update


def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    instances = Table('instances', meta, autoload=True)
    migrations = Table('migrations', meta, autoload=True)

    # drop old index:
    i = _old_index(migrations)
    i.drop(migrate_engine)

    # add columns.  a node is the same as a compute node's
    # hypervisor hostname:
    source_node = Column('source_node', String(length=255))
    migrations.create_column(source_node)

    dest_node = Column('dest_node', String(length=255))
    migrations.create_column(dest_node)

    # map compute hosts => list of compute nodes
    nodemap = _map_nodes(meta)

    # update migration and instance records with nodes:
    _update_nodes(nodemap, instances, migrations)

    # add new index:
    migrations = Table('migrations', meta, autoload=True)
    _add_new_index(migrations, migrate_engine)


def downgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    migrations = Table('migrations', meta, autoload=True)

    # drop new columns:
    source_node = Column('source_node', String(length=255))
    migrations.drop_column(source_node)

    dest_node = Column('dest_node', String(length=255))
    migrations.drop_column(dest_node)

    # drop new index:
    _drop_new_index(migrations, migrate_engine)

    # re-add old index:
    i = _old_index(migrations)
    i.create(migrate_engine)


def _map_nodes(meta):
    """Map host to compute node(s) for the purpose of determining which hosts
    are single vs multi-node.
    """

    services = Table('services', meta, autoload=True)
    c_nodes = Table('compute_nodes', meta, autoload=True)

    q = select([services.c.host, c_nodes.c.hypervisor_hostname],

               whereclause=and_(c_nodes.c.deleted == False,
                                services.c.deleted == False),

               from_obj=c_nodes.join(services,
                                     c_nodes.c.service_id == services.c.id)
    )

    nodemap = {}

    for (host, node) in q.execute():
        nodes = nodemap.setdefault(host, [])
        nodes.append(node)

    return nodemap


def _add_new_index(migrations, migrate_engine):
    if migrate_engine.name == "mysql":
{'fraction_non_alphanumeric': 0.09597989949748743, 'fraction_numerical': 0.007035175879396985, 'mean_word_length': 3.2619557458957886, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6615896', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1801, 'n_words': 506}