database schema upgrader

git-svn-id: file:///home/shish/svn/shimmie2/trunk@231 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2007-07-06 04:02:24 +00:00
parent 4a755da2c0
commit 7638c8de70

66
ext/upgrade/main.php Normal file
View file

@ -0,0 +1,66 @@
<?php
class Upgrade extends Extension {
public function receive_event($event) {
if(is_a($event, 'InitExtEvent')) {
$this->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);
?>