
from ggrc import db
from sqlalchemy import and_
from sqlalchemy.orm import aliased


def resolve_duplicates(model, attr):
  v0, v1 = aliased(model, name="v0"), aliased(model, name="v1")
  query = db.session.query(v0).join(v1, and_(
      getattr(v0, attr) == getattr(v1, attr),
      v0.id > v1.id
  ))
  for v in query:
    setattr(v, attr, getattr(v, attr, model.type) + u"-" + unicode(v.id))
    db.session.add(v)
  db.session.commit()


from ggrc import db
from sqlalchemy import and_
from sqlalchemy.orm import aliased


def resolve_duplicates(model, attr, separator=u"-"):
  v0, v1 = aliased(model, name="v0"), aliased(model, name="v1")
  query = db.session.query(v0).join(v1, and_(
      getattr(v0, attr) == getattr(v1, attr),
      v0.id > v1.id
  ))
  for v in query:
    i = 0
    nattr = "{}{}{}".format(getattr(v, attr, model.type), separator, i)
    while db.session.query(model).\
            filter(getattr(model, attr) == nattr).count():
      i += 1
      nattr = "{}{}{}".format(getattr(v, attr, model.type), separator, i)
    setattr(v, attr, nattr)
    db.session.add(v)
  db.session.commit()

Verify that new attribute doesn't already exist in database
Verify that new attribute doesn't already exist in database

  
  from ggrc import db
  from sqlalchemy import and_
  from sqlalchemy.orm import aliased
  
  
- def resolve_duplicates(model, attr):
+ def resolve_duplicates(model, attr, separator=u"-"):
    v0, v1 = aliased(model, name="v0"), aliased(model, name="v1")
    query = db.session.query(v0).join(v1, and_(
        getattr(v0, attr) == getattr(v1, attr),
        v0.id > v1.id
    ))
    for v in query:
-     setattr(v, attr, getattr(v, attr, model.type) + u"-" + unicode(v.id))
+     i = 0
+     nattr = "{}{}{}".format(getattr(v, attr, model.type), separator, i)
+     while db.session.query(model).\
+             filter(getattr(model, attr) == nattr).count():
+       i += 1
+       nattr = "{}{}{}".format(getattr(v, attr, model.type), separator, i)
+     setattr(v, attr, nattr)
      db.session.add(v)
    db.session.commit()
  
Verify that new attribute doesn't already exist in database
## Code Before:

from ggrc import db
from sqlalchemy import and_
from sqlalchemy.orm import aliased


def resolve_duplicates(model, attr):
  v0, v1 = aliased(model, name="v0"), aliased(model, name="v1")
  query = db.session.query(v0).join(v1, and_(
      getattr(v0, attr) == getattr(v1, attr),
      v0.id > v1.id
  ))
  for v in query:
    setattr(v, attr, getattr(v, attr, model.type) + u"-" + unicode(v.id))
    db.session.add(v)
  db.session.commit()

## Instruction:
Verify that new attribute doesn't already exist in database
## Code After:

from ggrc import db
from sqlalchemy import and_
from sqlalchemy.orm import aliased


def resolve_duplicates(model, attr, separator=u"-"):
  v0, v1 = aliased(model, name="v0"), aliased(model, name="v1")
  query = db.session.query(v0).join(v1, and_(
      getattr(v0, attr) == getattr(v1, attr),
      v0.id > v1.id
  ))
  for v in query:
    i = 0
    nattr = "{}{}{}".format(getattr(v, attr, model.type), separator, i)
    while db.session.query(model).\
            filter(getattr(model, attr) == nattr).count():
      i += 1
      nattr = "{}{}{}".format(getattr(v, attr, model.type), separator, i)
    setattr(v, attr, nattr)
    db.session.add(v)
  db.session.commit()
