Exemplo n.º 1
1
 /**
  * Prepares component
  *
  * @param $database
  *
  * @throws \Phalcon\Exception
  */
 public static function setup($database)
 {
     if (!isset($database->adapter)) {
         throw new \Phalcon\Exception('Unspecified database Adapter in your configuration!');
     }
     $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $database->adapter;
     if (!class_exists($adapter)) {
         throw new \Phalcon\Exception('Invalid database Adapter!');
     }
     $configArray = $database->toArray();
     unset($configArray['adapter']);
     self::$_connection = new $adapter($configArray);
     self::$_databaseConfig = $database;
     if ($database->adapter == 'Mysql') {
         self::$_connection->query('SET FOREIGN_KEY_CHECKS=0');
     }
     if (Migrations::isConsole()) {
         $profiler = new Profiler();
         $eventsManager = new EventsManager();
         $eventsManager->attach('db', function ($event, $connection) use($profiler) {
             if ($event->getType() == 'beforeQuery') {
                 $profiler->startProfile($connection->getSQLStatement());
             }
             if ($event->getType() == 'afterQuery') {
                 $profiler->stopProfile();
             }
         });
         self::$_connection->setEventsManager($eventsManager);
     }
 }
Exemplo n.º 2
1
 /**
  * Run the command
  */
 public function run($parameters)
 {
     if ($this->isReceivedOption('table')) {
         $tableName = $this->getOption('table');
     } else {
         $tableName = 'all';
     }
     $path = '';
     if ($this->isReceivedOption('directory')) {
         $path = $this->getOption('directory') . '/';
     }
     if ($this->isReceivedOption('migrations')) {
         $migrationsDir = $path . $this->getOption('migrations');
     } else {
         $migrationsDir = $path . 'app/migrations';
     }
     $exportData = $this->getOption('data');
     $originalVersion = $this->getOption('version');
     $action = $this->getOption(array('action', 1));
     if ($action == 'generate') {
         Migrations::generate(array('directory' => $path, 'tableName' => $tableName, 'exportData' => $exportData, 'migrationsDir' => $migrationsDir, 'originalVersion' => $originalVersion, 'force' => $this->isReceivedOption('force')));
     } else {
         if ($action == 'run') {
             Migrations::run(array('directory' => $path, 'migrationsDir' => $migrationsDir, 'force' => $this->isReceivedOption('force')));
         }
     }
 }
 public function runAction()
 {
     if ($this->request->isPost()) {
         $force = $this->request->getPost('force', 'int');
         try {
             Migrations::run(array('config' => Tools::getConfig(), 'directory' => null, 'tableName' => 'all', 'migrationsDir' => $this->migrationsDir, 'force' => $force));
             $this->flash->success('The migration was executed successfully.');
         } catch (BuilderException $e) {
             $this->flash->error($e->getMessage());
         }
     }
     $this->_prepareVersions();
 }
 public function runAction()
 {
     if ($this->request->isPost()) {
         $version = '';
         $exportData = '';
         $force = $this->request->getPost('force', 'int');
         try {
             $migrationsDir = $this->_getMigrationsDir();
             \Phalcon\Migrations::run(array('directory' => null, 'tableName' => 'all', 'migrationsDir' => $migrationsDir, 'force' => $force));
             $this->flash->success("The migration was executed successfully");
         } catch (BuilderException $e) {
             $this->flash->error($e->getMessage());
         }
     }
     $this->_prepareVersions();
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  *
  * @param array $parameters
  * @return mixed
  */
 public function run(array $parameters)
 {
     if ($this->isReceivedOption('table')) {
         $tableName = $this->getOption('table');
     } else {
         $tableName = 'all';
     }
     $path = '';
     if ($this->isReceivedOption('directory')) {
         $path = $this->getOption('directory');
     }
     $path = realpath($path) . DIRECTORY_SEPARATOR;
     if ($this->isReceivedOption('config')) {
         $config = $this->loadConfig($path . $this->getOption('config'));
     } else {
         $config = $this->getConfig($path);
     }
     if ($this->isReceivedOption('migrations')) {
         $migrationsDir = $path . $this->getOption('migrations');
     } elseif (isset($config['application']['migrationsDir'])) {
         $migrationsDir = $config['application']['migrationsDir'];
         if (!$this->path->isAbsolutePath($migrationsDir)) {
             $migrationsDir = $path . $migrationsDir;
         }
     } else {
         if (file_exists($path . 'app')) {
             $migrationsDir = $path . 'app/migrations';
         } elseif (file_exists($path . 'apps')) {
             $migrationsDir = $path . 'apps/migrations';
         } else {
             $migrationsDir = $path . 'migrations';
         }
     }
     $exportData = $this->getOption('data');
     $originalVersion = $this->getOption('version');
     $action = $this->getOption(array('action', 1));
     $version = $this->getOption('version');
     if ($action == 'generate') {
         Migrations::generate(array('directory' => $path, 'tableName' => $tableName, 'exportData' => $exportData, 'migrationsDir' => $migrationsDir, 'originalVersion' => $originalVersion, 'force' => $this->isReceivedOption('force'), 'no-ai' => $this->isReceivedOption('no-auto-increment'), 'config' => $config));
     } else {
         if ($action == 'run') {
             Migrations::run(array('directory' => $path, 'tableName' => $tableName, 'migrationsDir' => $migrationsDir, 'force' => $this->isReceivedOption('force'), 'config' => $config, 'version' => $version));
         }
     }
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  *
  * @param array $parameters
  *
  * @return mixed
  */
 public function run(array $parameters)
 {
     $path = $this->isReceivedOption('directory') ? $this->getOption('directory') : '';
     $path = realpath($path) . DIRECTORY_SEPARATOR;
     if ($this->isReceivedOption('config')) {
         $config = $this->loadConfig($path . $this->getOption('config'));
     } else {
         $config = $this->getConfig($path);
     }
     if ($this->isReceivedOption('migrations')) {
         $migrationsDir = $path . $this->getOption('migrations');
     } elseif (isset($config['application']['migrationsDir'])) {
         $migrationsDir = $config['application']['migrationsDir'];
         if (!$this->path->isAbsolutePath($migrationsDir)) {
             $migrationsDir = $path . $migrationsDir;
         }
     } elseif (file_exists($path . 'app')) {
         $migrationsDir = $path . 'app/migrations';
     } elseif (file_exists($path . 'apps')) {
         $migrationsDir = $path . 'apps/migrations';
     } else {
         $migrationsDir = $path . 'migrations';
     }
     // keep migrations log in db
     // either "log-in-db" option or "logInDb" config variable from "application" block
     $migrationsInDb = false;
     if ($this->isReceivedOption('log-in-db')) {
         $migrationsInDb = true;
     } elseif (isset($config['application']['logInDb'])) {
         $migrationsInDb = $config['application']['logInDb'];
     }
     // migrations naming is timestamp-based rather than traditional, dotted versions
     // either "ts-based" option or "migrationsTsBased" config variable from "application" block
     $migrationsTsBased = false;
     if ($this->isReceivedOption('ts-based')) {
         $migrationsTsBased = true;
     } elseif (isset($config['application']['migrationsTsBased'])) {
         $migrationsTsBased = $config['application']['migrationsTsBased'];
     }
     $tableName = $this->isReceivedOption('table') ? $this->getOption('table') : 'all';
     $descr = $this->getOption('descr');
     $exportData = $this->getOption('data');
     $action = $this->getOption(['action', 1]);
     $version = $this->getOption('version');
     switch ($action) {
         case 'generate':
             Migrations::generate(['directory' => $path, 'tableName' => $tableName, 'exportData' => $exportData, 'migrationsDir' => $migrationsDir, 'version' => $version, 'force' => $this->isReceivedOption('force'), 'noAutoIncrement' => $this->isReceivedOption('no-auto-increment'), 'config' => $config, 'descr' => $descr]);
             break;
         case 'run':
             Migrations::run(['directory' => $path, 'tableName' => $tableName, 'migrationsDir' => $migrationsDir, 'force' => $this->isReceivedOption('force'), 'tsBased' => $migrationsTsBased, 'config' => $config, 'version' => $version, 'migrationsInDb' => $migrationsInDb]);
             break;
         case 'list':
             Migrations::listAll(['directory' => $path, 'tableName' => $tableName, 'migrationsDir' => $migrationsDir, 'force' => $this->isReceivedOption('force'), 'tsBased' => $migrationsTsBased, 'config' => $config, 'version' => $version, 'migrationsInDb' => $migrationsInDb]);
             break;
     }
 }