Zhuyifei1999 has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/368604 )

Change subject: Handle duplicates in table 'star'
......................................................................

Handle duplicates in table 'star'

* An unique key is added to the table so the data cannot be
  duplicated and stored
* On addition sql may raise IntegrityError. Handle it as if the
  star action is successful.

Bug: T165169
Change-Id: I29ea782b32c0411a70f0deabb2296d4774daf6a8
---
M quarry/web/app.py
M tables.sql
2 files changed, 10 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/quarry/web 
refs/changes/04/368604/1

diff --git a/quarry/web/app.py b/quarry/web/app.py
index 568b4be..78452e8 100644
--- a/quarry/web/app.py
+++ b/quarry/web/app.py
@@ -12,6 +12,7 @@
 import output
 import os
 from sqlalchemy import desc, func
+from sqlalchemy.exc import IntegrityError
 from redissession import RedisSessionInterface
 from connections import Connections
 from utils.pagination import RangeBasedPagination
@@ -110,7 +111,13 @@
         star.user = get_user()
         star.query = query
         g.conn.session.add(star)
-        g.conn.session.commit()
+        try:
+            g.conn.session.commit()
+        except IntegrityError as e:
+            if e[0] == 1062:  # Duplicate
+                g.conn.session.rollback()
+            else:
+                raise
         return ""
     else:
         return "Query not found", 404
@@ -282,6 +289,7 @@
     qrun = g.conn.session.query(QueryRun).get(qrun_id)
     reader = SQLiteResultReader(qrun, app.config['OUTPUT_PATH_TEMPLATE'])
     response = output.get_formatted_response(format, qrun, reader, 
resultset_id)
+    response.encoding = request.args.get('encoding', 'utf-8')
     if request.args.get('download', 'false') == 'true':
         # Download this!
         if qrun.rev.query.title:
diff --git a/tables.sql b/tables.sql
index 2dfadbf..53fff60 100644
--- a/tables.sql
+++ b/tables.sql
@@ -56,3 +56,4 @@
 );
 CREATE INDEX IF NOT EXISTS star_user_id_index ON star(user_id);
 CREATE INDEX IF NOT EXISTS star_query_id_index ON star(query_id);
+CREATE UNIQUE INDEX IF NOT EXISTS star_user_query_index ON star(user_id, 
query_id);

-- 
To view, visit https://gerrit.wikimedia.org/r/368604
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I29ea782b32c0411a70f0deabb2296d4774daf6a8
Gerrit-PatchSet: 1
Gerrit-Project: analytics/quarry/web
Gerrit-Branch: master
Gerrit-Owner: Zhuyifei1999 <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to