@@ -29,8 +29,10 @@ class Database(object): | |||||
def _migrate(self, cursor, current): | def _migrate(self, cursor, current): | ||||
"""Migrate the database to the latest schema version.""" | """Migrate the database to the latest schema version.""" | ||||
for version in xrange(current, VERSION): | for version in xrange(current, VERSION): | ||||
print "Migrating to %d..." % version + 1 | |||||
for query in MIGRATIONS[version - 1]: | for query in MIGRATIONS[version - 1]: | ||||
cursor.execute(query) | cursor.execute(query) | ||||
cursor.execute("UPDATE version SET version = ?", (version + 1,)) | |||||
def _check_version(self, migrate): | def _check_version(self, migrate): | ||||
"""Check the database schema version and respond accordingly. | """Check the database schema version and respond accordingly. | ||||
@@ -8,12 +8,21 @@ VERSION = 2 | |||||
MIGRATIONS = [ | MIGRATIONS = [ | ||||
# 1 -> 2 | # 1 -> 2 | ||||
[ | [ | ||||
# drop index on code_hash | |||||
"ALTER TABLE code DROP COLUMN code_hash", | |||||
# change code_id to BIGINT NOT NULL, | |||||
# add key on codelets to codelet_lang | |||||
# add symbol_end_row INT UNSIGNED NOT NULL | |||||
# add symbol_end_col INT UNSIGNED NOT NULL | |||||
"""ALTER TABLE `codelets` | |||||
DROP FOREIGN KEY `codelets_ibfk_1`""", | |||||
"""ALTER TABLE `code` | |||||
DROP KEY `code_hash`, | |||||
DROP COLUMN `code_hash`, | |||||
MODIFY COLUMN `code_id` BIGINT NOT NULL""", | |||||
"""ALTER TABLE `codelets` | |||||
MODIFY COLUMN `codelet_code_id` BIGINT NOT NULL, | |||||
ADD KEY (`codelet_lang`), | |||||
ADD FOREIGN KEY (`codelet_code_id`) | |||||
REFERENCES `code` (`code_id`) | |||||
ON DELETE RESTRICT ON UPDATE CASCADE""", | |||||
"""ALTER TABLE `symbols` | |||||
ADD COLUMN `symbol_end_row` INT UNSIGNED NOT NULL, | |||||
ADD COLUMN `symbol_end_col` INT UNSIGNED NOT NULL""" | |||||
] | ] | ||||
] | ] | ||||
@@ -6,6 +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 (2); | |||||
CREATE TABLE `origins` ( | CREATE TABLE `origins` ( | ||||
`origin_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, | `origin_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, | ||||
@@ -26,7 +27,7 @@ CREATE TABLE `code` ( | |||||
CREATE TABLE `codelets` ( | CREATE TABLE `codelets` ( | ||||
`codelet_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, | `codelet_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, | ||||
`codelet_name` VARCHAR(300) NOT NULL, | `codelet_name` VARCHAR(300) NOT NULL, | ||||
`codelet_code_id` BIGINT UNSIGNED NOT NULL, | |||||
`codelet_code_id` BIGINT NOT NULL, | |||||
`codelet_lang` SMALLINT UNSIGNED DEFAULT NULL, | `codelet_lang` SMALLINT UNSIGNED DEFAULT NULL, | ||||
`codelet_origin` TINYINT UNSIGNED NOT NULL, | `codelet_origin` TINYINT UNSIGNED NOT NULL, | ||||
`codelet_url` VARCHAR(512) NOT NULL, | `codelet_url` VARCHAR(512) NOT NULL, | ||||