From 7bac5c0b9a52bf53f87a8d3c478de611fae88757 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 2 Jun 2014 13:45:51 -0400 Subject: [PATCH] Update schema to v7; correctly order entries in cache. --- bitshift/database/__init__.py | 10 ++++++---- bitshift/database/migration.py | 9 ++++++++- bitshift/database/schema.sql | 5 +++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/bitshift/database/__init__.py b/bitshift/database/__init__.py index 311eb21..56f8b1c 100644 --- a/bitshift/database/__init__.py +++ b/bitshift/database/__init__.py @@ -63,7 +63,7 @@ class Database(object): query, args = tree.build_query(page) cursor.execute(query, args) ids = [id for id, _ in cursor.fetchall()] - num_results = len(ids) # TODO: NotImplemented + num_results = len(ids) # TODO: This is not entirely correct return ids, num_results def _get_authors_for_codelet(self, cursor, codelet_id): @@ -164,9 +164,10 @@ class Database(object): query2 = """SELECT cdata_codelet, cache_count_mnt, cache_count_exp FROM cache INNER JOIN cache_data ON cache_id = cdata_cache - WHERE cache_id = ?""" + WHERE cache_id = ? + ORDER BY cdata_index ASC""" query3 = "INSERT INTO cache VALUES (?, ?, ?, DEFAULT)" - query4 = "INSERT INTO cache_data VALUES (?, ?)" + query4 = "INSERT INTO cache_data VALUES (?, ?, ?)" cache_id = mmh3.hash64(str(page) + ":" + query.serialize())[0] @@ -184,7 +185,8 @@ class Database(object): num_results = int(round(num_results, -num_exp)) num_mnt = num_results / (10 ** num_exp) cursor.execute(query3, (cache_id, num_mnt, num_exp)) - cursor.executemany(query4, [(cache_id, c_id) for c_id in ids]) + cdata = [(cache_id, c_id, i) for i, c_id in enumerate(ids)] + cursor.executemany(query4, cdata) codelet_gen = self._get_codelets_from_ids(cursor, ids) return (num_results, list(codelet_gen)) diff --git a/bitshift/database/migration.py b/bitshift/database/migration.py index 24f744a..428aaf2 100644 --- a/bitshift/database/migration.py +++ b/bitshift/database/migration.py @@ -3,7 +3,7 @@ Contains information about database schema versions, and SQL queries to update between them. """ -VERSION = 6 +VERSION = 7 MIGRATIONS = [ # 1 -> 2 @@ -88,6 +88,13 @@ MIGRATIONS = [ DO DELETE FROM `cache` WHERE `cache_created` < DATE_SUB(NOW(), INTERVAL 1 DAY);""" + ], + # 6 -> 7 + [ + """DELETE FROM `cache`""", + """ALTER TABLE `cache_data` + ADD COLUMN `cdata_index` TINYINT UNSIGNED NOT NULL + AFTER `cdata_codelet`""" ] ] diff --git a/bitshift/database/schema.sql b/bitshift/database/schema.sql index 8634416..c431ce9 100644 --- a/bitshift/database/schema.sql +++ b/bitshift/database/schema.sql @@ -1,4 +1,4 @@ --- Schema version 6 +-- Schema version 7 CREATE DATABASE `bitshift` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; USE `bitshift`; @@ -6,7 +6,7 @@ USE `bitshift`; CREATE TABLE `version` ( `version` INT UNSIGNED NOT NULL ) ENGINE=InnoDB; -INSERT INTO `version` VALUES (6); +INSERT INTO `version` VALUES (7); CREATE TABLE `origins` ( `origin_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, @@ -98,6 +98,7 @@ CREATE TABLE `cache` ( CREATE TABLE `cache_data` ( `cdata_cache` BIGINT NOT NULL, `cdata_codelet` BIGINT UNSIGNED NOT NULL, + `cdata_index` TINYINT UNSIGNED NOT NULL, PRIMARY KEY (`cdata_cache`, `cdata_codelet`), FOREIGN KEY (`cdata_cache`) REFERENCES `cache` (`cache_id`)