diff --git a/oml/setup.py b/oml/setup.py index 2e9318c..fde2b22 100644 --- a/oml/setup.py +++ b/oml/setup.py @@ -416,32 +416,8 @@ def upgrade_db(old, new=None): if not len(l.items): l.remove() if old <= '20160107-509-e0857fc': - with db.session() as session: - sql = "SELECT COUNT(*) AS c,list_id,item_id FROM listitem GROUP BY list_id, item_id HAVING c>1" - doubles = [r for r in session.execute(sql)] - for r in doubles: - params = {'list_id': r[1], 'item_id': r[2]} - sql = "DELETE FROM listitem WHERE list_id = :list_id AND item_id = :item_id" - session.execute(sql, params) - sql = "INSERT INTO listitem (list_id, item_id) VALUES (:list_id, :item_id)" - session.execute(sql, params) - session.commit() - sql = 'CREATE UNIQUE INDEX IF NOT EXISTS listitem_index on listitem(list_id,item_id)' - session.execute(sql) - session.commit() - - sql = "SELECT COUNT(*) AS c,user_id,item_id FROM useritem GROUP BY user_id, item_id HAVING c>1" - doubles = [r for r in session.execute(sql)] - for r in doubles: - params = {'user_id': r[1], 'item_id': r[2]} - sql = "DELETE FROM useritem WHERE user_id = :user_id AND item_id = :item_id" - session.execute(sql, params) - sql = "INSERT INTO useritem (user_id, item_id) VALUES (:user_id, :item_id)" - session.execute(sql, params) - session.commit() - sql = 'CREATE UNIQUE INDEX IF NOT EXISTS useritem_index on useritem(user_id,item_id)' - session.execute(sql) - session.commit() + add_useritem_index() + add_listitem_index() if old <= '20160111-603-90648f9' and not new: if not db.table_exists('user_metadata'): run_sql('''CREATE TABLE user_metadata ( @@ -499,8 +475,8 @@ def update_database(): 'CREATE INDEX ix_find_key ON find ("key")', 'CREATE INDEX ix_useritem_user ON useritem ("user_id")', 'CREATE INDEX ix_user_metadata_data_hash ON user_metadata (data_hash)', - '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 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(id, user_id)', ] layout = db.get_layout() @@ -518,7 +494,12 @@ def update_database(): for index in indexes: name = index.split('INDEX ')[1].split()[0] if name not in layout['indexes']: - sql.append(index) + if name == 'useritem_index': + add_useritem_index() + elif name == 'listitem_index': + add_listitem_index() + else: + sql.append(index) index_names.add(name) for index in set(name for name in layout['indexes'] if not name.startswith('ix_sort_')) - index_names: sql.append('DROP INDEX ' + index) @@ -531,3 +512,32 @@ def update_database(): import item.models item.models.update_sort_table() +def add_listitem_index(): + with db.session() as session: + sql = "SELECT COUNT(*) AS c,list_id,item_id FROM listitem GROUP BY list_id, item_id HAVING c>1" + doubles = [r for r in session.execute(sql)] + for r in doubles: + params = {'list_id': r[1], 'item_id': r[2]} + sql = "DELETE FROM listitem WHERE list_id = :list_id AND item_id = :item_id" + session.execute(sql, params) + sql = "INSERT INTO listitem (list_id, item_id) VALUES (:list_id, :item_id)" + session.execute(sql, params) + session.commit() + sql = 'CREATE UNIQUE INDEX IF NOT EXISTS listitem_index on listitem(list_id,item_id)' + session.execute(sql) + session.commit() + +def add_useritem_index(): + with db.session() as session: + sql = "SELECT COUNT(*) AS c,user_id,item_id FROM useritem GROUP BY user_id, item_id HAVING c>1" + doubles = [r for r in session.execute(sql)] + for r in doubles: + params = {'user_id': r[1], 'item_id': r[2]} + sql = "DELETE FROM useritem WHERE user_id = :user_id AND item_id = :item_id" + session.execute(sql, params) + sql = "INSERT INTO useritem (user_id, item_id) VALUES (:user_id, :item_id)" + session.execute(sql, params) + session.commit() + sql = 'CREATE UNIQUE INDEX IF NOT EXISTS useritem_index on useritem(user_id,item_id)' + session.execute(sql) + session.commit()