public function fire() { $filename = date('Y-m-d-His-') . $this->input->getArgument('name') . '.php'; $path = migrationPath($filename); // @codeCoverageIgnoreStart if (is_file($migrationCandidates = $_SERVER['PHWOOLCON_ROOT_PATH'] . '/vendor/phwoolcon/migrations.php')) { $candidates = (include $migrationCandidates); $chooseTarget = $this->input->getOption('choose-target'); if (!$chooseTarget && isset($candidates['selected']) && isset($candidates['candidates'][$candidates['selected']])) { $path = $candidates['candidates'][$candidates['selected']] . '/' . $filename; } elseif (count($candidates['candidates']) == 1) { $path = reset($candidates['candidates']) . '/' . $filename; } elseif (count($candidates['candidates']) > 1) { $targets = array_merge([''], array_keys($candidates['candidates'])); unset($targets[0]); $choose = $this->interactive->choice('Please choose migration target: ', $targets, reset($targets)); $candidates['selected'] = $choose; fileSaveArray($migrationCandidates, $candidates); $path = $candidates['candidates'][$choose] . '/' . $filename; } } // @codeCoverageIgnoreEnd file_put_contents($path, $this->template()); $this->output->writeln("<info>Created Migration:</info> {$filename}"); }
public function testMigrateCreate() { $migrationFile = $this->createMigrateFile(); /* @var \Phwoolcon\Cli\Command\MigrateCreate $command */ $command = $this->cli->get('migrate:create'); $this->assertNotEmpty($migrationFile, 'Unable to create migration file'); $this->assertFileExists($savedFile = migrationPath($migrationFile), 'Unable to write migration file'); $this->assertEquals($command->template(), file_get_contents($savedFile), 'Bad content written to migration file'); }
protected function listToBeMigrated() { $found = false; foreach (glob(migrationPath('*.php')) as $file) { $filename = basename($file); if ($this->migrationExecuted($filename)) { continue; } $found or $this->comment('Following migrations are ready for install:'); $found = true; $this->info($filename); } $found or $this->info('Nothing to be migrated.'); return $this; }
protected function revertMigration($filename) { $db = $this->db; $db->begin(); $file = migrationPath($filename); try { $this->logAndShowInfo(sprintf('Start reverting migration "%s"', $filename)); $migration = (include $file); if (isset($migration['down']) && is_callable($migration['down'])) { call_user_func($migration['down'], $db, $this); } $this->migrationExecuted($filename, false); $db->commit(); Db::clearMetadata(); $this->logAndShowInfo(sprintf('Finish reverting migration "%s"', $filename)); } catch (Exception $e) { $db->rollback(); Log::exception($e); $this->error(sprintf('Error when reverting migration "%s"', $filename)); $this->error($e->getMessage()); } // @codeCoverageIgnoreEnd }
protected function runMigration() { $db = $this->db; $migrated = false; foreach (glob(migrationPath('*.php')) as $file) { $filename = basename($file); if ($this->migrationExecuted($filename)) { continue; } $migrated = true; $this->logAndShowInfo(sprintf('Start migration "%s"', $filename)); $db->begin(); try { $migration = (include $file); if (isset($migration['up']) && is_callable($migration['up'])) { call_user_func($migration['up'], $db, $this); } $this->migrationExecuted($filename, true); $db->commit(); } catch (Exception $e) { $db->rollback(); Log::exception($e); $this->error(sprintf('Error in migration "%s"', $filename)); $this->error($e->getMessage()); return; } // @codeCoverageIgnoreEnd $this->logAndShowInfo(sprintf('Finish migration "%s"', $filename)); } if ($migrated) { Db::clearMetadata(); } else { $this->info('Nothing to be migrated.'); } // @codeCoverageIgnoreEnd }