Example #1
0
                echo 'Found ' . count($useless) . ' useless columns in existing table : ' . implode(', ', $useless) . "\n";
                foreach ($useless as $column) {
                    Database::removeTableColumn($table, $column);
                }
            }
            echo 'Check column format' . "\n";
            foreach ($required_columns as $column) {
                if (in_array($column, $missing)) {
                    continue;
                }
                // Already created with the right format
                $problems = Database::checkTableColumnFormat($table, $column, $datamap[$column], function ($message) {
                    echo "\t" . $message . "\n";
                });
                if ($problems) {
                    echo 'Column ' . $column . ' has bad format, updating it' . "\n";
                    Database::updateTableColumnFormat($table, $column, $datamap[$column], $problems);
                }
            }
        } else {
            echo 'Table is missing, create it' . "\n";
            Database::createTable($table, $datamap);
        }
        echo 'Done for table ' . $table . "\n";
    }
    echo 'Everything went well' . "\n";
    echo 'Database structure is up to date' . "\n";
} catch (Exception $e) {
    $uid = $e instanceof LoggingException ? $e->getUid() : 'no available uid';
    die('Encountered exception : ' . $e->getMessage() . ', see logs for details (uid: ' . $uid . ') ...');
}
Example #2
0
 /**
  * Update database
  */
 public static function updateStructure()
 {
     $class = static::getClassName();
     Logger::info('Updating ' . $class . ' database structure');
     $datamap = static::getDataMap();
     $table = static::getDBTable();
     // Check if table exists
     Logger::info('Look for table ' . $table);
     if (Database::tableExists($table)) {
         Logger::info('Table found, check columns');
         $existing_columns = Database::getTableColumns($table);
         Logger::info('Found ' . count($existing_columns) . ' columns in existing table : ' . implode(', ', $existing_columns));
         $required_columns = array_keys($datamap);
         Logger::info('Found ' . count($required_columns) . ' columns in required table : ' . implode(', ', $required_columns));
         $missing = array();
         foreach ($required_columns as $c) {
             if (!in_array($c, $existing_columns)) {
                 $missing[] = $c;
             }
         }
         if (count($missing)) {
             Logger::info('Found ' . count($missing) . ' missing columns in existing table : ' . implode(', ', $missing));
             foreach ($missing as $column) {
                 Database::createTableColumn($table, $column, $datamap[$column]);
             }
         }
         $useless = array();
         foreach ($existing_columns as $c) {
             if (!in_array($c, $required_columns)) {
                 $useless[] = $c;
             }
         }
         if (count($useless)) {
             Logger::info('Found ' . count($useless) . ' useless columns in existing table : ' . implode(', ', $useless));
             foreach ($useless as $column) {
                 Database::removeTableColumn($table, $column);
             }
         }
         Logger::info('Check column format');
         foreach ($required_columns as $column) {
             if (in_array($column, $missing)) {
                 continue;
             }
             // Already created with the right format
             $problems = Database::checkTableColumnFormat($table, $column, $datamap[$column], function ($message) {
                 Logger::info("\t" . $message);
             });
             if ($problems) {
                 Logger::info('Column ' . $column . ' has bad format, updating it');
                 Database::updateTableColumnFormat($table, $column, $datamap[$column], $problems);
             }
         }
     } else {
         Logger::info('Table is missing, create it');
         Database::createTable($table, $datamap);
     }
     Logger::info('Done for ' . $class);
 }