make sure list names are unique per user

This commit is contained in:
j 2016-02-22 19:17:27 +05:30
parent 4571d5b636
commit ba28d78280
2 changed files with 20 additions and 2 deletions

View file

@ -449,6 +449,7 @@ def update_database():
'CREATE UNIQUE INDEX listitem_index on listitem(list_id, item_id)',
'CREATE UNIQUE INDEX useritem_index on useritem(user_id, item_id)',
'CREATE UNIQUE INDEX user_metadata_index ON user_metadata(item_id, user_id)',
'CREATE UNIQUE INDEX list_username_index on list(user_id, name)',
]
layout = db.get_layout()
sql = []
@ -469,6 +470,8 @@ def update_database():
add_useritem_index()
elif name == 'listitem_index':
add_listitem_index()
elif name == 'list_username_index':
add_list_username_index()
else:
sql.append(index)
index_names.add(name)
@ -512,3 +515,19 @@ def add_useritem_index():
sql = 'CREATE UNIQUE INDEX IF NOT EXISTS useritem_index on useritem(user_id,item_id)'
session.execute(sql)
session.commit()
def add_list_username_index():
from sqlalchemy.orm import load_only
import user.models
with db.session() as session:
lists = {}
for l in user.models.List.query.order_by('id'):
if l.public_id in lists:
ids = [i.id for i in l.get_items().options(load_only('id'))]
lists[l.public_id].add_items(ids)
session.delete(l)
else:
lists[l.public_id] = l
session.commit()
sql = 'CREATE UNIQUE INDEX IF NOT EXISTS list_username_index on list(user_id, name)'
session.connection().execute(sql)

View file

@ -4,7 +4,6 @@
from contextlib import closing
import base64
import hashlib
import json
import os
import tarfile
@ -568,5 +567,5 @@ def migrate_11():
def migrate_12():
db.run_sql([
'DROP TABLE IF EXISTS transfer'
]),
])
return 12