]> git.wh0rd.org Git - tt-rss.git/commitdiff
add update.php -indexes to recreate tt-rss indexes from schema file
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Thu, 9 Aug 2012 10:20:27 +0000 (14:20 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Thu, 9 Aug 2012 10:20:27 +0000 (14:20 +0400)
include/functions.php
update.php

index 3da777135c4b4d79974bb6ac7b72c6485772680f..4ba60e84414c7e510e698cc8fcc55b67d4189d48 100644 (file)
                        return $output;
                }
        }
+
+       function read_stdin() {
+               $fp = fopen("php://stdin", "r");
+
+               if ($fp) {
+                       $line = trim(fgets($fp));
+                       fclose($fp);
+                       return $line;
+               }
+
+               return null;
+       }
 ?>
index fa322d3dc5c0a40abdb0caf76747d1bf39067007..4f8dcdbee75d0659fe57cd8aedebe401a0beb1e4 100755 (executable)
@@ -29,6 +29,7 @@
                print "  -get-feeds          - receive popular feeds from linked instances\n";
                print "  -import USER FILE   - import articles from XML\n";
                print "  -quiet              - don't show messages\n";
+               print " -indexes            - recreate missing schema indexes\n";
                print "  -help               - show this help\n";
                return;
        }
 
        }
 
+       if (in_array("-indexes", $op)) {
+               _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
+               _debug("Type 'yes' to continue.");
+
+               if (read_stdin() != 'yes')
+                       exit;
+
+               _debug("clearing existing indexes...");
+
+               if (DB_TYPE == "pgsql") {
+                       $result = db_query($link, "SELECT relname FROM
+                               pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
+                                       AND relname NOT LIKE '%_pkey'
+                               AND relkind = 'i'");
+               } else {
+                       $result = db_query($link, "SELECT index_name,table_name FROM
+                               information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
+               }
+
+               while ($line = db_fetch_assoc($result)) {
+                       if (DB_TYPE == "pgsql") {
+                               $statement = "DROP INDEX " . $line["relname"];
+                               _debug($statement);
+                       } else {
+                               $statement = "ALTER TABLE ".
+                                       $line['table_name']." DROP INDEX ".$line['index_name'];
+                               _debug($statement);
+                       }
+                       db_query($link, $statement, false);
+               }
+
+               _debug("reading indexes from schema for: " . DB_TYPE);
+
+               $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
+               if ($fp) {
+                       while ($line = fgets($fp)) {
+                               $matches = array();
+
+                               if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
+                                       $index = $matches[1];
+                                       $table = $matches[2];
+
+                                       $statement = "CREATE INDEX $index ON $table";
+
+                                       _debug($statement);
+                                       db_query($link, $statement);
+                               }
+                       }
+                       fclose($fp);
+               } else {
+                       _debug("unable to open schema file.");
+               }
+               _debug("all done.");
+       }
+
        db_close($link);
 
        if ($lock_handle != false) {