@@ -0,0 +1,2947 @@
+# Copyright (C) 2015 Google Inc., authors, and contributors %3Csee AUTHORS file%3E%0A# Licensed under http://www.apache.org/licenses/LICENSE-2.0 %3Csee LICENSE file%3E%0A# Created By: urban@reciprocitylabs.com%0A# Maintained By: urban@reciprocitylabs.com%0A%0A%22%22%22Migrate data to request FCO%0A%0ARevision ID: 3c1619ff36e8%0ARevises: 27684e5f313a%0ACreate Date: 2015-11-10 13:46:05.350000%0A%0A%22%22%22%0A%0A# revision identifiers, used by Alembic.%0Arevision = '3c1619ff36e8'%0Adown_revision = '27684e5f313a'%0A%0Afrom alembic import op%0Afrom sqlalchemy.sql import table, column%0Afrom sqlalchemy.sql.expression import and_%0A%0Arequests_table = table(%0A    'requests',%0A    column('id'),%0A    column('assignee_id'),%0A    column('requestor_id'))%0A%0Arelationships_table = table(%0A    'relationships',%0A    column('id'),%0A    column('source_id'),%0A    column('source_type'),%0A    column('destination_id'),%0A    column('destination_type'),%0A)%0A%0Arelationship_attrs_table = table(%0A    'relationship_attrs',%0A    column('relationship_id'),%0A    column('attr_name'),%0A    column('attr_value'),%0A)%0A%0A%0Adef build_bulk_insert_object(reqid, pid):%0A  return %7B%0A      %22source_type%22: %22Request%22,%0A      %22source_id%22: reqid,%0A      %22destination_type%22: %22Person%22,%0A      %22destination_id%22: pid%0A  %7D%0A%0A%0Adef insert_relationship_attributes(connection,%0A                                   relationships,%0A                                   attr_type,%0A                                   attr_value):%0A  relationship_attrs = %5B%5D%0A  for rel in relationships:%0A    rel = connection.execute(relationships_table.select().where(%0A        and_(%0A            relationships_table.columns.source_id == rel%5B%22source_id%22%5D,%0A            relationships_table.columns.source_type == rel%5B%22source_type%22%5D,%0A            relationships_table.columns.destination_id ==%0A            rel%5B%22destination_id%22%5D,%0A            relationships_table.columns.destination_type ==%0A            rel%5B%22destination_type%22%5D,%0A        ))).fetchall()%0A    if rel:%0A      relid, _, _, _, _ = rel%5B0%5D%0A      relationship_attrs += %5B%7B%0A          %22relationship_id%22: relid,%0A          %22attr_name%22: attr_type,%0A          %22attr_value%22: attr_value%0A      %7D%5D%0A  op.bulk_insert(relationship_attrs_table, relationship_attrs)%0A%0A%0Adef upgrade():%0A  connection = op.get_bind()%0A%0A  req_relationships = connection.execute(requests_table.select()).fetchall()%0A%0A  requestor_relationships = %5B%0A      build_bulk_insert_object(reqid, rid)%0A      for reqid, aid, rid in req_relationships if rid and rid != aid%5D%0A  assignee_relationships = %5B%0A      build_bulk_insert_object(reqid, aid)%0A      for reqid, aid, rid in req_relationships if aid%5D%0A%0A  op.bulk_insert(relationships_table, requestor_relationships)%0A  op.bulk_insert(relationships_table, assignee_relationships)%0A%0A  insert_relationship_attributes(connection, requestor_relationships,%0A                                 %22AssigneeType%22, %22Requester%22)%0A  insert_relationship_attributes(connection, assignee_relationships,%0A                                 %22AssigneeType%22, %22Assignee%22)%0A%0A%0Adef downgrade():%0A  pass%0A

Migrate assignee and requestors to relationships table