Skip to content

Commit

Permalink
more explicit division between complementary delete conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
xflr6 committed Dec 1, 2017
1 parent 5823506 commit c975875
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions clld/db/schema_migrations/update_assoc_tables.py
Expand Up @@ -64,14 +64,13 @@ def upgrade(dry=True, verbose=True):
def delete_null_duplicates(tablename, columns, notnull, returning=sa.text('*')):
assert columns
table = sa.table(tablename, *map(sa.column, ['pk'] + columns))
yield table.delete(bind=conn).where(sa.or_(table.c[n] == sa.null() for n in notnull))\
.returning(returning)
any_null = sa.or_(table.c[n] == sa.null() for n in notnull)
yield table.delete(bind=conn).where(any_null).returning(returning)
other = table.alias()
yield table.delete(bind=conn).where(sa.and_(table.c[n] != sa.null() for n in notnull))\
yield table.delete(bind=conn).where(~any_null).returning(returning)\
.where(sa.exists()
.where(sa.and_(table.c[c] == other.c[c] for c in columns))
.where(table.c.pk > other.c.pk))\
.returning(returning)
.where(table.c.pk > other.c.pk))

def print_rows(rows, verbose=verbose):
if not verbose:
Expand Down

0 comments on commit c975875

Please sign in to comment.