Exemple #1
0
 public function migrate()
 {
     echo "\n" . $this->version . "\n";
     $dir = Loader::dirForVersion($this->version);
     $files = glob($dir . "*.php");
     $tableClasses = array_map(function ($element) {
         return basename($element, ".php");
     }, $files);
     // Load tables
     foreach ($tableClasses as $table) {
         Loader::loadModelVersion($table, $this->version);
         $class = Loader::classNameForVersion($table, $this->version);
         $this->tables[] = new $class();
     }
     $current = Connection::getTableNames();
     $removed = array_diff($current, $tableClasses);
     //we don't want the db_migration table removed
     if (($key = array_search('db_migration', $removed)) !== false) {
         unset($removed[$key]);
     }
     Connection::begin();
     // pre action for every table
     foreach ($this->tables as $table) {
         if (method_exists($table, 'pre')) {
             $table->pre();
         }
     }
     $this->dropReferences();
     $this->dropIndexes();
     $this->doTables();
     $this->createIndexes();
     $this->createReferences();
     echo "Dropping tables\n";
     Connection::dropTables($removed);
     // post action for every table
     foreach ($this->tables as $table) {
         if (method_exists($table, 'post')) {
             $table->post();
         }
     }
     Connection::commit();
     echo "\n";
 }