示例#1
0
 function getDiff()
 {
     $diffSequence = [];
     // Tables
     $tableData = new TableData($this->manager);
     $sourceTables = $this->manager->getTables('source');
     $targetTables = $this->manager->getTables('target');
     $commonTables = array_intersect($sourceTables, $targetTables);
     foreach ($commonTables as $table) {
         try {
             $diffs = $tableData->getDiff($table);
             $diffSequence = array_merge($diffSequence, $diffs);
         } catch (DataException $e) {
             Logger::error($e->getMessage());
         }
     }
     $addedTables = array_diff($sourceTables, $targetTables);
     foreach ($addedTables as $table) {
         $diffs = $tableData->getNewData($table);
         $diffSequence = array_merge($diffSequence, $diffs);
     }
     $deletedTables = array_diff($targetTables, $sourceTables);
     foreach ($deletedTables as $table) {
         $diffs = $tableData->getOldData($table);
         $diffSequence = array_merge($diffSequence, $diffs);
     }
     return $diffSequence;
 }
示例#2
0
 public function getDiff($table, $key)
 {
     Logger::info("Now calculating data diff for table `{$table}`");
     $diffSequence1 = $this->getOldNewDiff($table, $key);
     $diffSequence2 = $this->getChangeDiff($table, $key);
     $diffSequence = array_merge($diffSequence1, $diffSequence2);
     return $diffSequence;
 }
示例#3
0
 public function output()
 {
     $content = $this->getComments();
     $content .= $this->getContent();
     if (is_null($this->params->output)) {
         Logger::info("Writing migration file to " . getcwd() . "/migration.sql");
         file_put_contents('migration.sql', $content);
     } else {
         Logger::info("Writing migration file to " . $this->params->output);
         return file_put_contents($this->params->output, $content);
     }
 }
示例#4
0
 public function getOldData($table)
 {
     Logger::info("Now getting old data from table `{$table}`");
     $diffSequence = [];
     $iterator = $this->getIterator('target', $table);
     $key = $this->manager->getKey('target', $table);
     while ($iterator->hasNext()) {
         $data = $iterator->next(ArrayDiff::$size);
         foreach ($data as $entry) {
             $diffSequence[] = new DeleteData($table, ['keys' => array_only($entry, $key), 'diff' => new \Diff\DiffOp\DiffOpRemove($entry)]);
         }
     }
     return $diffSequence;
 }
示例#5
0
 public function getDiff($table, $key)
 {
     Logger::info("Now calculating data diff for table `{$table}`");
     $diffs = $this->getDataDiff($table, $key);
     $diffSequence = [];
     foreach ($diffs as $name => $diff) {
         if ($diff['diff'] instanceof \Diff\DiffOp\DiffOpRemove) {
             $diffSequence[] = new DeleteData($table, $diff);
         } else {
             if (is_array($diff['diff'])) {
                 $diffSequence[] = new UpdateData($table, $diff);
             } else {
                 if ($diff['diff'] instanceof \Diff\DiffOp\DiffOpAdd) {
                     $diffSequence[] = new InsertData($table, $diff);
                 }
             }
         }
     }
     return $diffSequence;
 }
示例#6
0
 public function get()
 {
     $params = new DefaultParams();
     $cli = new CLIGetter();
     $paramsCLI = $cli->getParams();
     if (!isset($paramsCLI->debug)) {
         error_reporting(E_ERROR);
     }
     if (!isset($paramsCLI->silent)) {
         Logger::$silent = false;
     }
     $fs = new FSGetter($paramsCLI);
     $paramsFS = $fs->getParams();
     $params = $this->merge($params, $paramsFS);
     $params = $this->merge($params, $paramsCLI);
     if (empty($params->server1)) {
         throw new CLIException("A server is required");
     }
     return $params;
 }
示例#7
0
 public function run()
 {
     // Increase memory limit
     ini_set('memory_limit', '512M');
     try {
         // Params
         $paramsFactory = new ParamsFactory();
         $params = $paramsFactory->get();
         // Diff
         $diffCalculator = new DiffCalculator();
         $diff = $diffCalculator->getDiff($params);
         // Empty diff
         if (empty($diff['schema']) && empty($diff['data'])) {
             Logger::info("Identical resources");
         } else {
             // SQL
             $sqlGenerator = new SQLGenerator($diff);
             $up = '';
             $down = '';
             if ($params->include !== 'down') {
                 $up = $sqlGenerator->getUp();
             }
             if ($params->include !== 'up') {
                 $down = $sqlGenerator->getDown();
             }
             // Generate
             $templater = new Templater($params, $up, $down);
             $templater->output();
         }
         Logger::success("Completed");
     } catch (\Exception $e) {
         if ($e instanceof BaseException) {
             Logger::error($e->getMessage(), true);
         } else {
             Logger::error("Unexpected error: ");
             throw $e;
         }
     }
 }
示例#8
0
 public function getDown()
 {
     Logger::info("Now generating DOWN migration");
     $diff = $this->diffSorter->sort($this->diff, 'down');
     return MigrationGenerator::generate($diff, 'getDown');
 }
示例#9
0
 public function getDiff($table)
 {
     Logger::info("Now calculating schema diff for table `{$table}`");
     $diffSequence = [];
     $sourceSchema = $this->getSchema('source', $table);
     $targetSchema = $this->getSchema('target', $table);
     // Engine
     $sourceEngine = $sourceSchema['engine'];
     $targetEngine = $targetSchema['engine'];
     if ($sourceEngine != $targetEngine) {
         $diffSequence[] = new AlterTableEngine($table, $sourceEngine, $targetEngine);
     }
     // Collation
     $sourceCollation = $sourceSchema['collation'];
     $targetCollation = $targetSchema['collation'];
     if ($sourceCollation != $targetCollation) {
         $diffSequence[] = new AlterTableCollation($table, $sourceCollation, $targetCollation);
     }
     // Columns
     $sourceColumns = $sourceSchema['columns'];
     $targetColumns = $targetSchema['columns'];
     $differ = new MapDiffer();
     $diffs = $differ->doDiff($targetColumns, $sourceColumns);
     foreach ($diffs as $column => $diff) {
         if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
             $diffSequence[] = new AlterTableDropColumn($table, $column, $diff);
         } else {
             if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
                 $diffSequence[] = new AlterTableChangeColumn($table, $column, $diff);
             } else {
                 if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
                     $diffSequence[] = new AlterTableAddColumn($table, $column, $diff);
                 }
             }
         }
     }
     // Keys
     $sourceKeys = $sourceSchema['keys'];
     $targetKeys = $targetSchema['keys'];
     $differ = new MapDiffer();
     $diffs = $differ->doDiff($targetKeys, $sourceKeys);
     foreach ($diffs as $key => $diff) {
         if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
             $diffSequence[] = new AlterTableDropKey($table, $key, $diff);
         } else {
             if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
                 $diffSequence[] = new AlterTableChangeKey($table, $key, $diff);
             } else {
                 if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
                     $diffSequence[] = new AlterTableAddKey($table, $key, $diff);
                 }
             }
         }
     }
     // Constraints
     $sourceConstraints = $sourceSchema['constraints'];
     $targetConstraints = $targetSchema['constraints'];
     $differ = new MapDiffer();
     $diffs = $differ->doDiff($targetConstraints, $sourceConstraints);
     foreach ($diffs as $name => $diff) {
         if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
             $diffSequence[] = new AlterTableDropConstraint($table, $name, $diff);
         } else {
             if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
                 $diffSequence[] = new AlterTableChangeConstraint($table, $name, $diff);
             } else {
                 if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
                     $diffSequence[] = new AlterTableAddConstraint($table, $name, $diff);
                 }
             }
         }
     }
     return $diffSequence;
 }