use xml schemas to create tables

git-svn-id: file:///home/shish/svn/shimmie2/trunk@703 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2008-01-27 15:32:35 +00:00
parent 2608bafa41
commit 980ebd5189
6 changed files with 115 additions and 104 deletions

View File

@ -145,23 +145,7 @@ class CommentList extends Extension {
global $config; global $config;
if($config->get_int("ext_comments_version") < 1) { if($config->get_int("ext_comments_version") < 1) {
$database->Execute("CREATE TABLE `comments` ( $database->upgrade_schema("ext/comment/schema.xml");
`id` int(11) NOT NULL auto_increment,
`image_id` int(11) NOT NULL,
`owner_id` int(11) NOT NULL,
`owner_ip` char(16) NOT NULL,
`posted` datetime default NULL,
`comment` text NOT NULL,
PRIMARY KEY (`id`),
KEY `comments_image_id` (`image_id`)
)");
$config->set_int("ext_comments_version", 1);
}
if($config->get_int("ext_comments_version") == 1) {
$database->Execute("CREATE INDEX comments_owner_ip ON comments(owner_ip)");
$database->Execute("CREATE INDEX comments_posted ON comments(posted)");
$config->set_int("ext_comments_version", 2);
} }
} }
// }}} // }}}

22
ext/comment/schema.xml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0"?>
<schema version="0.3">
<!-- FIXME: mysql utf8ness -->
<table name="comments">
<field name="id" type="I"><key/><autoincrement/></field>
<field name="image_id" type="I"><notnull/></field><!-- references -->
<field name="owner_id" type="I"><notnull/></field><!-- references -->
<field name="owner_ip" type="C" size="15"><notnull/></field>
<field name="posted" type="T"><notnull/></field>
<field name="end" type="T"><notnull/></field>
<field name="comment" type="X" size="4000"><notnull/></field>
<index name="comments__image_id"><col>image_id</col></index>
<index name="comments__owner_ip"><col>owner_ip</col></index>
<index name="comments__posted"><col>posted</col></index>
<opt platform="mysql">DEFAULT CHARSET='utf8'</opt>
</table>
<sql>
<query>DELETE FROM config WHERE name='ext_comments_version'</query>
<query>INSERT INTO config(name, value) VALUES('ext_comments_version', 3)</query>
</sql>
</schema>

View File

@ -84,21 +84,8 @@ class IPBan extends Extension {
global $database; global $database;
global $config; global $config;
if($config->get_int("ext_ipban_version") < 1) { if($config->get_int("ext_ipban_version") < 3) {
$database->Execute("CREATE TABLE bans ( $database->upgrade_schema("ext/ipban/schema.xml");
id int(11) NOT NULL auto_increment,
ip char(15) default NULL,
date datetime default NULL,
end datetime default NULL,
reason varchar(255) default NULL,
PRIMARY KEY (id)
)");
$config->set_int("ext_ipban_version", 1);
}
if($config->get_int("ext_ipban_version") < 2) {
$database->execute("ALTER TABLE bans CHANGE ip ip CHAR(15) NOT NULL");
$database->execute("ALTER TABLE bans ADD COLUMN banner_id INTEGER NOT NULL");
$config->set_int("ext_ipban_version", 2);
} }
} }
// }}} // }}}

19
ext/ipban/schema.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<schema version="0.3">
<!-- FIXME: mysql utf8ness -->
<table name="bans">
<field name="id" type="I"><key/><autoincrement/></field>
<field name="banner_id" type="I"><notnull/></field>
<field name="ip" type="C" size="15"><notnull/></field>
<field name="date" type="T"><notnull/></field>
<field name="end" type="T"><notnull/></field>
<field name="reason" type="C" size="255"><notnull/></field>
<index name="bans__ip"><col>ip</col></index>
<opt platform="mysql">DEFAULT CHARSET='utf8'</opt>
</table>
<sql>
<query>DELETE FROM config WHERE name='ext_ipban_version'</query>
<query>INSERT INTO config(name, value) VALUES('ext_ipban_version', 3)</query>
</sql>
</schema>

View File

@ -11,83 +11,13 @@ class Upgrade extends Extension {
global $config; global $config;
global $database; global $database;
if($config->get_bool("in_upgrade")) {
return;
}
if(!is_numeric($config->get_string("db_version"))) { if(!is_numeric($config->get_string("db_version"))) {
$config->set_int("db_version", 2); $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) { if($config->get_int("db_version") < 6) {
$config->set_bool("in_upgrade", true); $database->upgrade_schema("ext/upgrade/schema.xml");
$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);
} }
if($config->get_int("db_version") == 5) {
$config->set_bool("in_upgrade", true);
$tables = $database->db->GetCol("SHOW TABLES");
foreach($tables as $table) {
$database->Execute("ALTER TABLE $table CONVERT TO CHARACTER SET utf8");
}
$config->set_int("db_version", 6);
$config->set_bool("in_upgrade", false);
}
/*
if($config->get_int("db_version") == -1) {
$database->Execute("ALTER TABLE users ADD COLUMN parent INTEGER");
$database->Execute("ALTER TABLE users ADD COLUMN is_template ENUM('Y','N') DEFAULT 'N'");
$database->Execute("INSERT INTO users(name, is_template) VALUES(?, 'Y')", array("[Anonymous]"));
$database->Execute("INSERT INTO users(name, is_template) VALUES(?, 'Y')", array("[User]"));
$database->Execute("INSERT INTO users(name, is_template) VALUES(?, 'Y')", array("[Moderator]"));
$database->Execute("INSERT INTO users(name, is_template) VALUES(?, 'Y')", array("[Admin]"));
$anon_id = $database->db->GetOne("SELECT id FROM users WHERE name=?", array("[Anonymous]"));
$user_id = $database->db->GetOne("SELECT id FROM users WHERE name=?", array("[User]"));
$admin_id = $database->db->GetOne("SELECT id FROM users WHERE name=?", array("[Admin]"));
$database->Execute("UPDATE users SET parent=?", array($user_id));
$database->Execute("UPDATE users SET parent=? WHERE password IS NULL", array($anon_id));
$database->Execute("UPDATE users SET parent=? WHERE is_admin='Y'", array($admin_id));
$config->set_int("db_version", 7);
}
*/
} }
} }
add_event_listener(new Upgrade(), 5); add_event_listener(new Upgrade(), 5);

69
ext/upgrade/schema.xml Normal file
View File

@ -0,0 +1,69 @@
<?xml version="1.0"?>
<schema version="0.3">
<!-- FIXME: mysql utf8ness -->
<table name="aliases">
<field name="oldtag" type="C" size="255"><key/><notnull/></field>
<field name="newtag" type="C" size="255"><notnull/></field>
<index name="aliases__unique"><col>oldtag</col><col>newtag</col><unique/></index>
<opt platform="mysql">DEFAULT CHARSET='utf8'</opt>
</table>
<table name="config">
<field name="name" type="C" size="255"><key/><notnull/></field>
<field name="value" type="X" size="4000"></field>
<opt platform="mysql">DEFAULT CHARSET='utf8'</opt>
</table>
<table name="images">
<field name="id" type="I"><key/><autoincrement/></field>
<field name="owner_id" type="I"><notnull/></field>
<field name="owner_ip" type="C" size="15"><notnull/></field>
<field name="filename" type="C" size="64"><notnull/></field>
<field name="filesize" type="I"><notnull/></field>
<field name="hash" type="C" size="32"><notnull/></field>
<field name="ext" type="C" size="4"><notnull/></field>
<field name="source" type="C" size="255"></field>
<field name="width" type="I"><notnull/></field>
<field name="height" type="I"><notnull/></field>
<field name="posted" type="T"><notnull/></field>
<index name="images__owner_id"><col>owner_id</col></index>
<index name="images__hash"><col>hash</col><unique/></index>
<index name="images__width"><col>width</col></index>
<index name="images__height"><col>height</col></index>
<opt platform="mysql">DEFAULT CHARSET='utf8'</opt>
</table>
<table name="users">
<field name="id" type="I"><key/><autoincrement/></field>
<field name="name" type="C" size="32"><notnull/></field>
<field name="pass" type="C" size="32"></field>
<field name="joindate" type="T"><notnull/></field>
<field name="enabled" type="C" size="1"><notnull/><default value="Y"/></field>
<field name="admin" type="C" size="1"><notnull/><default value="N"/></field>
<field name="email" type="C" size="255"></field>
<index name="users__name"><col>name</col><unique/></index>
<opt platform="mysql">DEFAULT CHARSET='utf8'</opt>
</table>
<table name="tags">
<field name="id" type="I"><key/><autoincrement/></field>
<field name="tag" type="C" size="64"><notnull/></field>
<field name="count" type="I"><notnull/><default value="0"/></field>
<index name="tags__tag"><col>tag</col><unique/></index>
<opt platform="mysql">DEFAULT CHARSET='utf8'</opt>
</table>
<table name="image_tags">
<field name="image_id" type="I"><notnull/></field>
<field name="tag_id" type="I"><notnull/></field>
<index name="image_tags__image_id"><col>image_id</col></index>
<index name="image_tags__tag_id"><col>tag_id</col></index>
<index name="image_tags__key"><col>image_id</col><col>tag_id</col><unique/></index>
<opt platform="mysql">DEFAULT CHARSET='utf8'</opt>
</table>
<sql>
<query>DELETE FROM config WHERE name='db_version'</query>
<query>INSERT INTO config(name, value) VALUES('db_version', 6)</query>
</sql>
</schema>