summaryrefslogtreecommitdiff
path: root/db/update.php
diff options
context:
space:
mode:
authorJan-Philipp Litza <jplitza@sylar.(none)>2011-12-26 16:04:54 +0100
committerJan-Philipp Litza <jplitza@sylar.(none)>2011-12-26 16:04:54 +0100
commit7154097581f231753005aaabdc12d00173b7ce27 (patch)
tree2fdc6e62e01a8f271c2d249756762e9fd91f7754 /db/update.php
parentbc471460fc8cce0bb9d8b5631e8d57fdec6a3b39 (diff)
database update script
Diffstat (limited to 'db/update.php')
-rw-r--r--db/update.php72
1 files changed, 72 insertions, 0 deletions
diff --git a/db/update.php b/db/update.php
new file mode 100644
index 00000000..9c709f69
--- /dev/null
+++ b/db/update.php
@@ -0,0 +1,72 @@
+<?php
+require_once (dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'bootstrap.php');
+require_once ('includes/sys_mysql.php');
+require_once ('config/config.php');
+require_once ('config/config_db.php');
+sql_connect($config['host'], $config['user'], $config['pw'], $config['db']);
+
+error_reporting(E_ALL | E_NOTICE);
+
+define('UPDATE_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'update.d');
+
+function _datetime_to_int($table, $col) {
+ $table = sql_escape($table);
+ $col = sql_escape($col);
+
+ $res = sql_select("DESCRIBE `" . $table . "` `" . $col . "`");
+ if($res[0]['Type'] == "datetime") {
+ sql_query("ALTER TABLE `" . $table . "` ADD `" . $col . "_new` INT NOT NULL AFTER `" . $col . "`");
+ # XXX: we don't consider indexes etc. here and just copy the data!
+ sql_query("UPDATE `" . $table . "` SET `" . $col . "_new` = UNIX_TIMESTAMP(`" . $col . "`)");
+ sql_query("ALTER TABLE `" . $table . "` DROP `" . $col . "`, CHANGE `" . $col . "_new` `" . $col . "` INT NOT NULL");
+
+ global $applied;
+ $applied = true;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+function _rename_table($old, $new) {
+ $old = sql_escape($old);
+ $new = sql_escape($new);
+
+ if(sql_num_query("SHOW TABLES LIKE '" . $new . "'") === 0
+ && sql_num_query("SHOW TABLES LIKE '" . $old . "'") === 1) {
+ sql_query("RENAME TABLE `" . $old . "` TO `" . $new . "`");
+
+ global $applied;
+ $applied = true;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+function _add_index($table, $cols, $type = "INDEX") {
+ $table = sql_escape($table);
+ $cols = array_map('sql_escape', $cols);
+ $type = sql_escape($type);
+
+ if(sql_num_query("SHOW INDEX FROM `" . $table . "` WHERE `Key_name` = '" . $cols[0] . "'") == 0) {
+ sql_query("ALTER TABLE `" . $table . "` ADD " . $type . " (`" . implode($cols, '`,`') . "`)");
+
+ global $applied;
+ $applied = true;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+$updates = scandir(UPDATE_DIR);
+foreach($updates as $update) {
+ if(substr($update, -4) == '.php') {
+ $applied = false;
+ require_once( UPDATE_DIR . DIRECTORY_SEPARATOR . $update);
+ if($applied)
+ echo "Successfully applied " . $update . " (at least partially).\n";
+ }
+}
+?>