From 7638c8de703850ef1a13c8ab0147b65e453bb173 Mon Sep 17 00:00:00 2001 From: shish Date: Fri, 6 Jul 2007 04:02:24 +0000 Subject: [PATCH] database schema upgrader git-svn-id: file:///home/shish/svn/shimmie2/trunk@231 7f39781d-f577-437e-ae19-be835c7a54ca --- ext/upgrade/main.php | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 ext/upgrade/main.php diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php new file mode 100644 index 00000000..0abdcc8a --- /dev/null +++ b/ext/upgrade/main.php @@ -0,0 +1,66 @@ +do_things(); + } + } + + private function do_things() { + global $config; + global $database; + + if($config->get_bool("in_upgrade")) { + return; + } + + + if(!is_numeric($config->get_string("db_version"))) { + $config->set_int("db_version", 2); + } + + if($config->get_int("db_version") == 2) { + $database->Execute("CREATE TABLE layout ( + title varchar(64) primary key not null, + section varchar(32) not null default \"left\", + position int not null default 50, + visible enum('Y', 'N') default 'Y' not null + )"); + $config->set_int("db_version", 3); + } + + if($config->get_int("db_version") == 3) { + $config->set_bool("in_upgrade", true); + $database->Execute("RENAME TABLE tags TO old_tags"); + $database->Execute("CREATE TABLE tags ( + id int not null auto_increment primary key, + tag varchar(64) not null unique, + count int not null default 0, + KEY tags_count(count) + )"); + $database->Execute("CREATE TABLE image_tags ( + image_id int NOT NULL default 0, + tag_id int NOT NULL default 0, + UNIQUE KEY image_id_tag_id (image_id,tag_id), + KEY tags_tag_id (tag_id), + KEY tags_image_id (image_id) + )"); + $config->set_int("db_version", 4); + $config->set_bool("in_upgrade", false); + } + + if($config->get_int("db_version") == 4) { + $config->set_bool("in_upgrade", true); + $database->Execute("DELETE FROM tags"); + $database->Execute("INSERT INTO tags(tag) SELECT DISTINCT tag FROM old_tags"); + $database->Execute("DELETE FROM image_tags"); + $database->Execute("INSERT INTO image_tags(image_id, tag_id) SELECT old_tags.image_id, tags.id FROM old_tags JOIN tags ON old_tags.tag = tags.tag"); + $database->Execute("UPDATE tags SET count=(SELECT COUNT(image_id) FROM image_tags WHERE tag_id=tags.id GROUP BY tag_id)"); + $config->set_int("db_version", 5); + $config->set_bool("in_upgrade", false); + } + } +} +add_event_listener(new Upgrade(), 5); +?>