Browse Source

Update schema to v7; correctly order entries in cache.

tags/v1.0^2
Ben Kurtovic 10 years ago
parent
commit
7bac5c0b9a
3 changed files with 17 additions and 6 deletions
  1. +6
    -4
      bitshift/database/__init__.py
  2. +8
    -1
      bitshift/database/migration.py
  3. +3
    -1
      bitshift/database/schema.sql

+ 6
- 4
bitshift/database/__init__.py View File

@@ -63,7 +63,7 @@ class Database(object):
query, args = tree.build_query(page) query, args = tree.build_query(page)
cursor.execute(query, args) cursor.execute(query, args)
ids = [id for id, _ in cursor.fetchall()] 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 return ids, num_results


def _get_authors_for_codelet(self, cursor, codelet_id): 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 query2 = """SELECT cdata_codelet, cache_count_mnt, cache_count_exp
FROM cache FROM cache
INNER JOIN cache_data ON cache_id = cdata_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)" 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] 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_results = int(round(num_results, -num_exp))
num_mnt = num_results / (10 ** num_exp) num_mnt = num_results / (10 ** num_exp)
cursor.execute(query3, (cache_id, num_mnt, 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) codelet_gen = self._get_codelets_from_ids(cursor, ids)
return (num_results, list(codelet_gen)) return (num_results, list(codelet_gen))




+ 8
- 1
bitshift/database/migration.py View File

@@ -3,7 +3,7 @@ Contains information about database schema versions, and SQL queries to update
between them. between them.
""" """


VERSION = 6
VERSION = 7


MIGRATIONS = [ MIGRATIONS = [
# 1 -> 2 # 1 -> 2
@@ -88,6 +88,13 @@ MIGRATIONS = [
DO DO
DELETE FROM `cache` DELETE FROM `cache`
WHERE `cache_created` < DATE_SUB(NOW(), INTERVAL 1 DAY);""" 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`"""
] ]
] ]




+ 3
- 1
bitshift/database/schema.sql View File

@@ -1,4 +1,4 @@
-- Schema version 7


CREATE DATABASE `bitshift` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; CREATE DATABASE `bitshift` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `bitshift`; USE `bitshift`;
@@ -6,7 +6,7 @@ USE `bitshift`;
CREATE TABLE `version` ( CREATE TABLE `version` (
`version` INT UNSIGNED NOT NULL `version` INT UNSIGNED NOT NULL
) ENGINE=InnoDB; ) ENGINE=InnoDB;
INSERT INTO `version` VALUES (6);
INSERT INTO `version` VALUES (7);


CREATE TABLE `origins` ( CREATE TABLE `origins` (
`origin_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, `origin_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
@@ -98,6 +98,7 @@ CREATE TABLE `cache` (
CREATE TABLE `cache_data` ( CREATE TABLE `cache_data` (
`cdata_cache` BIGINT NOT NULL, `cdata_cache` BIGINT NOT NULL,
`cdata_codelet` BIGINT UNSIGNED NOT NULL, `cdata_codelet` BIGINT UNSIGNED NOT NULL,
`cdata_index` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`cdata_cache`, `cdata_codelet`), PRIMARY KEY (`cdata_cache`, `cdata_codelet`),
FOREIGN KEY (`cdata_cache`) FOREIGN KEY (`cdata_cache`)
REFERENCES `cache` (`cache_id`) REFERENCES `cache` (`cache_id`)


Loading…
Cancel
Save