Example #1
0
 public function run($options = array())
 {
     Yentu::greet();
     $this->db = DatabaseManipulator::create();
     $this->initializeCodeWriter();
     $files = scandir(Yentu::getPath("migrations"));
     if (count($files) > 2) {
         throw new \yentu\exceptions\CommandException("Cannot run imports. Your migrations directory is not empty");
     }
     $description = $this->db->getDescription();
     $this->code->add('begin()');
     if (isset($description['schemata'])) {
         $this->importSchemata($description['schemata']);
     }
     if (isset($description['tables'])) {
         $this->importTables($description['tables']);
     }
     if (isset($description['views'])) {
         $this->importViews($description['views']);
     }
     $this->importForeignKeys();
     $this->code->add('->end();');
     $this->newVersion = date('YmdHis', time());
     $path = Yentu::getPath("migrations/{$this->newVersion}_import.php");
     file_put_contents($path, $this->code);
     \clearice\ClearIce::output("Created `{$path}`\n");
     if (!$this->db->getAssertor()->doesTableExist('yentu_history')) {
         $this->db->createHistory();
     }
     $this->db->setVersion($this->newVersion);
     $this->db->disconnect();
     return $description;
 }
Example #2
0
 public function run($options = array())
 {
     Yentu::greet();
     $db = DatabaseManipulator::create();
     DatabaseItem::setDriver($db);
     ChangeReverser::setDriver($db);
     $previousMigration = '';
     if (isset($options['default-schema'])) {
         $this->schemaCondition = "default_schema = ?";
         $this->schemaConditionData[] = $options['default-schema'];
     }
     if (empty($options)) {
         $session = $db->getLastSession();
         $operations = $db->query("SELECT id, method, arguments, migration, default_schema FROM yentu_history WHERE {$this->schemaCondition} session = ? ORDER BY id DESC", $this->schemaConditionData + [$session]);
     } else {
         $operations = [];
         foreach ($options['stand_alones'] as $set) {
             $operations += $this->getOperations($db, $set);
         }
     }
     foreach ($operations as $operation) {
         if ($previousMigration !== $operation['migration']) {
             ClearIce::output("Rolling back '{$operation['migration']}' migration" . ($operation['default_schema'] != '' ? " on `{$operation['default_schema']}` schema." : ".") . "\n");
             $previousMigration = $operation['migration'];
         }
         ChangeReverser::call($operation['method'], json_decode($operation['arguments'], true));
         $db->query('DELETE FROM yentu_history WHERE id = ?', array($operation['id']));
     }
 }
Example #3
0
 public function run($options = array())
 {
     Yentu::greet();
     $driver = \yentu\DatabaseManipulator::create();
     $version = $driver->getVersion();
     if ($version == null) {
         ClearIce::output("\nYou have not applied any migrations\n");
         return;
     }
     $migrationInfo = $this->getMigrationInfo();
     ClearIce::output("\n" . ($migrationInfo['counter']['previous'] == 0 ? 'No' : $migrationInfo['counter']['previous']) . " migration(s) have been applied so far.\n");
     $this->displayMigrations($migrationInfo['run']['previous']);
     ClearIce::output("\nLast migration applied:\n    {$migrationInfo['current']}\n");
     if ($migrationInfo['counter']['yet'] > 0) {
         ClearIce::output("\n{$migrationInfo['counter']['yet']} migration(s) that could be applied.\n");
         $this->displayMigrations($migrationInfo['run']['yet']);
     } else {
         ClearIce::output("\nThere are no pending migrations.\n");
     }
 }
Example #4
0
 /**
  * Returns an array of all migrations that have been run on the database.
  * The information returned includes the timestamp, the name of the migration
  * and the default schema on which it was run.
  * @return array
  */
 public static function getRunMirations()
 {
     $db = DatabaseManipulator::create();
     $runMigrations = $db->query("SELECT DISTINCT version, migration, default_schema FROM yentu_history ORDER BY version");
     $migrations = array();
     foreach ($runMigrations as $migration) {
         $migrations[$migration['version']] = array('timestamp' => $migration['version'], 'migration' => $migration['migration'], 'default_schema' => $migration['default_schema']);
     }
     return $migrations;
 }
Example #5
0
 public function run($options = array())
 {
     global $migrateCommand;
     global $migrateVariables;
     self::fillOptions($options);
     $migrateCommand = $this;
     if ($options['dump-queries'] !== true) {
         Yentu::greet();
     }
     $this->driver = ChangeLogger::wrap(DatabaseManipulator::create());
     $this->driver->setDumpQueriesOnly($options['dump-queries']);
     $this->driver->setDryRun($options['dry']);
     $totalOperations = 0;
     $filter = self::FILTER_UNRUN;
     $this->setupOptions($options, $filter);
     DatabaseItem::setDriver($this->driver);
     \yentu\Timer::start();
     $migrationPaths = Yentu::getMigrationPathsInfo();
     foreach ($migrationPaths as $path) {
         $this->setDefaultSchema($path);
         $migrateVariables = $path['variables'];
         $migrations = $this->filter(Yentu::getMigrations($path['home']), $filter);
         $this->announceMigration($migrations, $path);
         $this->currentPath = $path;
         foreach ($migrations as $migration) {
             $this->countOperations("{$path['home']}/{$migration['file']}");
             $this->driver->setVersion($migration['timestamp']);
             $this->driver->setMigration($migration['migration']);
             ClearIce::output("\nApplying '{$migration['migration']}' migration\n");
             require "{$path['home']}/{$migration['file']}";
             DatabaseItem::purge();
             ClearIce::output("\n");
             $totalOperations += $this->driver->resetOperations();
         }
     }
     if ($this->driver->getChanges()) {
         $elapsed = \yentu\Timer::stop();
         ClearIce::output("\nMigration took " . \yentu\Timer::pretty($elapsed) . "\n");
         ClearIce::output($this->driver->getChanges() . " operations performed\n");
         ClearIce::output($totalOperations - $this->driver->getChanges() . " operations skipped\n");
     }
     $this->driver->disconnect();
 }
Example #6
0
 public function run($options = array())
 {
     Yentu::greet();
     if (file_exists(Yentu::getPath(''))) {
         throw new CommandException("Could not initialize yentu. Your project has already been initialized with yentu.");
     } else {
         if (!is_writable(dirname(Yentu::getPath('')))) {
             throw new CommandException("Your current directory is not writable.");
         }
     }
     $params = $this->getParams($options);
     if (count($params) == 0 && defined('STDOUT')) {
         global $argv;
         throw new CommandException("You didn't provide any parameters for initialization. Please execute " . "`{$argv[0]} init -i` to initialize yentu interractively. " . "You can also try `{$argv[0]} init --help` for more information.");
     }
     $this->createConfigFile($params);
     Config::readPath(Yentu::getPath('config'), 'yentu');
     $db = \yentu\DatabaseManipulator::create($params);
     if ($db->getAssertor()->doesTableExist('yentu_history')) {
         throw new CommandException("Could not initialize yentu. Your database has already been initialized with yentu.");
     }
     $db->createHistory();
     $db->disconnect();
     ClearIce::output("Yentu successfully initialized.\n");
 }