Example #1
0
require_once 'init.php';
//Avoid "Error: DateTime::__construct(): It is not safe to rely on the system's timezone settings" error
require_once 'config.inc.php';
date_default_timezone_set($THINKUP_CFG['timezone']);
// don't run via the web...
if (isset($_SERVER['SERVER_NAME'])) {
    die("This script should only be run via the command line.");
}
// help?
array_shift($argv);
if (isset($argv[0]) && preg_match('/^(\\-h|\\-\\-help)$/i', $argv[0])) {
    usage();
}
try {
    // do we need a migration?
    $db_version = UpgradeController::getCurrentDBVersion($cached = false);
    $config = Config::getInstance();
    $thinkup_db_version = $config->getValue('THINKUP_VERSION');
    $filename = false;
    if ($db_version == $thinkup_db_version) {
        error_log("\nYour ThinkUp database structure is up to date.\n");
        exit;
    } else {
        print "\nThinkup needs to be upgraded to version {$thinkup_db_version}, proceed => [y|n] ";
        $handle = fopen("php://stdin", "r");
        $line = fgets($handle);
        if (trim($line) != 'y') {
            exit;
        }
        print "\nWould you like to backup your data first? => [y|n] ";
        $handle = fopen("php://stdin", "r");
 /**
  * Determin if ThinkUp needs to show the upgrading page.
  * @param bool Is the current user an admin
  * @param str The calling classname
  * @return bool Whether or not we need to show the upgrade page
  */
 public static function isUpgrading($is_admin, $class_name)
 {
     $config = Config::getInstance();
     $status = false;
     $db_version = UpgradeController::getCurrentDBVersion($config->getValue('cache_pages'));
     if (version_compare($db_version, $config->getValue('THINKUP_VERSION'), '<')) {
         if ($class_name != 'UpgradeController') {
             $status = true;
         } else {
             if (!$is_admin && !isset($_GET['upgrade_token'])) {
                 $status = true;
             }
         }
         if ($status == true) {
             self::generateUpgradeToken();
         }
     }
     return $status;
 }
 public function testRunNewMigrationUpdateCompletedTable()
 {
     $this->simulateLogin('*****@*****.**', true);
     $controller = new UpgradeController(true);
     $this->newMigrationFiles('some_stuff');
     $this->newMigrationFiles('some_stuff', $old = true);
     // older already ran?
     $db_version = UpgradeController::getCurrentDBVersion($cached = false);
     $list = $controller->getMigrationList($db_version);
     $this->assertEqual(count($list), 1);
     $this->assertTrue($list[0]['new_migration']);
     $this->assertPattern("/^2011-09-21_some_stuff_v\\d+\\.\\d+\\.sql\$/", $list[0]['filename']);
     // create completion table
     $com_sql_file = THINKUP_ROOT_PATH . 'webapp/install/sql/completed_migrations.sql';
     //echo $com_sql_file;
     $com_sql = file_get_contents($com_sql_file);
     $this->pdo->query($com_sql);
     $this->pdo->query("alter table " . $this->table_prefix . "completed_migrations DROP column sql_ran");
     $_GET['migration_index'] = 1;
     $results = $controller->go();
     $obj = json_decode($results);
     $this->assertTrue($obj->processed);
     $sql = file_get_contents($this->test_migrations[0]);
     $sql = preg_replace('/\\-\\-.*/', '', $sql);
     $this->assertEqual($obj->sql, $sql);
     $stmt = $this->pdo->query("select * from " . $this->table_prefix . "completed_migrations");
     $data2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
     $this->assertEqual(count($data2), 3);
     $this->assertPattern('/CREATE TABLE `.*test1`/', $data2[0]['sql_ran']);
 }