* @since 2.0 */ class MigrateController extends BaseMigrateController { /** * @var string the name of the table for keeping applied migration information. */ public $migrationTable = '{{%migration}}'; /** * @inheritdoc */ public $templateFile = '@yii/views/migration.php'; /** * @var Connection|string the DB connection object or the application * component ID of the DB connection. */ public $db = 'db'; /** * @inheritdoc */ public function options($actionId) { return array_merge( parent::options($actionId), ['migrationTable', 'db'] // global for all actions ); } /** * This method is invoked right before an action is to be executed (after all possible filters.) * It checks the existence of the [[migrationPath]]. * @param \yii\base\Action $action the action to be executed. * @throws Exception if db component isn't configured * @return boolean whether the action should continue to be executed. */ public function beforeAction($action) { if (parent::beforeAction($action)) { if ($action->id !== 'create') { if (is_string($this->db)) { $this->db = Yii::$app->get($this->db); } if (!$this->db instanceof Connection) { throw new Exception("The 'db' option must refer to the application component ID of a DB connection."); } } return true; } else { return false; } } /** * Creates a new migration instance. * @param string $class the migration class name * @return \yii\db\Migration the migration instance */ protected function createMigration($class) { $file = $this->migrationPath . DIRECTORY_SEPARATOR . $class . '.php'; require_once($file); return new $class(['db' => $this->db]); } /** * @inheritdoc */ protected function getMigrationHistory($limit) { if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) { $this->createMigrationHistoryTable(); } $query = new Query; $rows = $query->select(['version', 'apply_time']) ->from($this->migrationTable) ->orderBy('version DESC') ->limit($limit) ->createCommand($this->db) ->queryAll(); $history = ArrayHelper::map($rows, 'version', 'apply_time'); unset($history[self::BASE_MIGRATION]); return $history; } /** * Creates the migration history table. */ protected function createMigrationHistoryTable() { $tableName = $this->db->schema->getRawTableName($this->migrationTable); echo "Creating migration history table \"$tableName\"..."; $this->db->createCommand()->createTable($this->migrationTable, [ 'version' => 'varchar(180) NOT NULL PRIMARY KEY', 'apply_time' => 'integer', ])->execute(); $this->db->createCommand()->insert($this->migrationTable, [ 'version' => self::BASE_MIGRATION, 'apply_time' => time(), ])->execute(); echo "done.\n"; } /** * @inheritdoc */ protected function addMigrationHistory($version) { $command = $this->db->createCommand(); $command->insert($this->migrationTable, [ 'version' => $version, 'apply_time' => time(), ])->execute(); } /** * @inheritdoc */ protected function removeMigrationHistory($version) { $command = $this->db->createCommand(); $command->delete($this->migrationTable, [ 'version' => $version, ])->execute(); } }