# Path: gloss/models.py
# class Merge(Base, GlossSubrecord):
#     new_reference_id = Column(Integer, ForeignKey('glossolaliareference.id'))
#     new_reference = relationship(
#         "GlossolaliaReference", foreign_keys=[new_reference_id]
#     )
# 
#     @classmethod
#     def _to_messages(cls, identifier, issuing_source, session):
#         merge = cls.get_latest_merge(session, issuing_source, identifier)
# 
#         if merge:
#             identifier = session.query(PatientIdentifier).filter(
#                 PatientIdentifier.gloss_reference == merge.new_reference
#             ).filter(
#                 PatientIdentifier.issuing_source == issuing_source
#             ).one_or_none()
# 
#             if not identifier:
#                 err = "unable to find a patient identifier for gloss reference {}"
#                 raise err.format(identifier)
#             return [message_type.PatientMergeMessage(
#                 new_id=identifier.identifier
#             )]
# 
#         return []
# 
#     @classmethod
#     def get_latest_merge(cls, session, issuing_source, identifier):
#         """ although an identifier can only have a single merge, that merge
#             can have multiple merges, so lets only return the most recent merge
#         """
#         merge = cls.query_from_identifier(
#             identifier, issuing_source, session
#         ).one_or_none()
# 
#         while merge:
#             if merge.gloss_reference == merge.new_reference:
#                 raise ValueError(
#                     "incorrect merge {} points to itself".format(merge.id)
#                 )
# 
#             new_merge = session.query(cls).filter(
#                 cls.gloss_reference == merge.new_reference
#             ).one_or_none()
# 
#             if not new_merge:
#                 return merge
#             else:
#                 merge = new_merge


from alembic import op
from gloss.models import Merge
import sqlalchemy as sa
"""empty message

Revision ID: 6c9ec3813c7a
Revises: 440bea99abd6
Create Date: 2016-04-19 15:54:50.394941

"""

# revision identifiers, used by Alembic.
revision = '6c9ec3813c7a'
down_revision = '440bea99abd6'
branch_labels = None
depends_on = None



def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('merge', sa.Column('new_reference_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'merge', 'glossolaliareference', ['new_reference_id'], ['id'])
    connection = op.get_bind()
    Session = sa.orm.sessionmaker()
    session = Session(bind=connection)

# Path: gloss/models.py
# class Merge(Base, GlossSubrecord):
#     new_reference_id = Column(Integer, ForeignKey('glossolaliareference.id'))
#     new_reference = relationship(
#         "GlossolaliaReference", foreign_keys=[new_reference_id]
#     )
# 
#     @classmethod
#     def _to_messages(cls, identifier, issuing_source, session):
#         merge = cls.get_latest_merge(session, issuing_source, identifier)
# 
#         if merge:
#             identifier = session.query(PatientIdentifier).filter(
#                 PatientIdentifier.gloss_reference == merge.new_reference
#             ).filter(
#                 PatientIdentifier.issuing_source == issuing_source
#             ).one_or_none()
# 
#             if not identifier:
#                 err = "unable to find a patient identifier for gloss reference {}"
#                 raise err.format(identifier)
#             return [message_type.PatientMergeMessage(
#                 new_id=identifier.identifier
#             )]
# 
#         return []
# 
#     @classmethod
#     def get_latest_merge(cls, session, issuing_source, identifier):
#         """ although an identifier can only have a single merge, that merge
#             can have multiple merges, so lets only return the most recent merge
#         """
#         merge = cls.query_from_identifier(
#             identifier, issuing_source, session
#         ).one_or_none()
# 
#         while merge:
#             if merge.gloss_reference == merge.new_reference:
#                 raise ValueError(
#                     "incorrect merge {} points to itself".format(merge.id)
#                 )
# 
#             new_merge = session.query(cls).filter(
#                 cls.gloss_reference == merge.new_reference
#             ).one_or_none()
# 
#             if not new_merge:
#                 return merge
#             else:
#                 merge = new_merge


# Path: alembic/versions/6c9ec3813c7a_.py
from alembic import op
from gloss.models import Merge
import sqlalchemy as sa

"""empty message

Revision ID: 6c9ec3813c7a
Revises: 440bea99abd6
Create Date: 2016-04-19 15:54:50.394941

"""

# revision identifiers, used by Alembic.
revision = '6c9ec3813c7a'
down_revision = '440bea99abd6'
branch_labels = None
depends_on = None



def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('merge', sa.Column('new_reference_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'merge', 'glossolaliareference', ['new_reference_id'], ['id'])
    connection = op.get_bind()
    Session = sa.orm.sessionmaker()
    session = Session(bind=connection)

    all_merges = session.query(Merge).all()