示例#1
0
文件: Migration.php 项目: rj28/test
 public static function gen()
 {
     self::test();
     list($old, $mg) = static::_readMeta();
     if (!is_array($old)) {
         $old = [];
     }
     $new = Table::getList();
     $migration = '';
     foreach ($old as $table) {
         if (empty($new[$table->name])) {
             $migration .= "DROP TABLE `{$table->name}`;\n";
         }
     }
     foreach ($new as $table) {
         if (empty($old[$table->name])) {
             $migration .= $table->getCreateTable() . "\n";
         } else {
             $migration .= $table->diff($old[$table->name]);
         }
     }
     if (!trim($migration)) {
         echo "Nothing to do.\n";
         return;
     }
     $fileName = static::_getNewMigrationFileName();
     file_put_contents(static::_dir() . $fileName, "<?php\n\n\$db->execute(\"\n\t" . str_replace("\n", "\n\t", $migration) . "\n\");");
     echo $migration . "\n";
     echo "Created file {$fileName}\n";
     $mg[] = $fileName;
     static::_writeMeta($new, $mg);
 }
示例#2
0
文件: Table.php 项目: rj28/test
 public function diff(Table $oldTable)
 {
     $ret = [];
     $old = $oldTable->columns;
     $new = $this->columns;
     if ($this->engine != $oldTable->engine) {
         $ret[] = "ALTER TABLE `{$this->name}` ENGINE = {$this->engine};";
     }
     foreach ($new as $name => $column) {
         if (isset($old[$name])) {
             $str = $column->getQueryAdd();
             if ($str != $old[$name]->getQueryAdd()) {
                 $ret[] = $column->getQueryChange();
             }
         } else {
             if (!isset($old[$name])) {
                 $ret[] = $column->getQueryAdd();
             }
         }
     }
     $old = $oldTable->getKeys();
     $new = $this->getKeys();
     foreach ($old as $key => $entries) {
         if (!isset($new[$key])) {
             $ret[] = "ALTER TABLE `{$this->name}` DROP INDEX `{$key}`;";
         }
     }
     foreach ($new as $key => $group) {
         if (isset($old[$key])) {
             $str = $group->getQueryAdd();
             if ($str != $old[$key]->getQueryAdd()) {
                 $ret[] = $group->getQueryDrop();
                 $ret[] = $group->getQueryAdd();
             }
         } else {
             $ret[] = $group->getQueryAdd();
         }
     }
     $ret = trim(implode("\n", $ret));
     return $ret ? $ret . "\n" : '';
 }