Migration is designed to be used together with the "yii migrate" command. Each child class of Migration represents an individual database migration which is identified by the child class name. Within each migration, the Migration::up method should be overridden to contain the logic for "upgrading" the database; while the Migration::down method for the "downgrading" logic. The "yii migrate" command manages all available migrations in an application. If the database supports transactions, you may also override Migration::safeUp and Migration::safeDown so that if anything wrong happens during the upgrading or downgrading, the whole migration can be reverted in a whole. Migration provides a set of convenient methods for manipulating database data and schema. For example, the Migration::insert method can be used to easily insert a row of data into a database table; the Migration::createTable method can be used to create a database table. Compared with the same methods in Command, these methods will display extra information showing the method parameters and execution time, which may be useful when applying migrations. For more details and usage information on Migration, see the guide article on Migration.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Component, implements yii\db\MigrationInterface, use trait SchemaBuilderTrait
Exemplo n.º 1
0
 /**
  * Update ip-geo-base data
  *
  * @throws \yii\base\Exception
  */
 public function actionUpdate()
 {
     $migrate = new Migration();
     $migrate->dropForeignKey('fk-geobase_contact-geobase_city_id', 'geobase_contact');
     $ipGeoBase = new IpGeoBase();
     $ipGeoBase->updateDB();
     $migrate->addForeignKey('fk-geobase_contact-geobase_city_id', 'geobase_contact', 'geobase_city_id', 'geobase_city', 'id', 'CASCADE', 'CASCADE');
 }
Exemplo n.º 2
0
 public function install()
 {
     parent::install();
     $migration = new Migration();
     $migration->createTable($this->getTable(), ['id' => Schema::TYPE_PK, 'object_id' => Schema::TYPE_INTEGER, 'term_id' => Schema::TYPE_BIGINT, 'value' => Schema::TYPE_STRING]);
     if ($migration->db->driverName === 'mysql') {
         $migration->addForeignKey('fk_' . $this->getTable() . '_' . $this->getRefTableName(), $this->getTable(), 'object_id', $this->getRefTableName(), 'id', 'CASCADE');
         $migration->addForeignKey('fk_' . $this->getTable() . '_' . TaxonomyTerms::tableName(), $this->getTable(), 'term_id', TaxonomyTerms::tableName(), 'id', 'CASCADE');
     }
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if ($this->db->driverName === 'mysql') {
         $this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
     }
 }
Exemplo n.º 4
0
 /**
  * @inheritdoc
  */
 public function createTable($table, $columns, $options = null)
 {
     if ($options === null && $this->db->driverName === 'mysql') {
         $options = "CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB";
     }
     parent::createTable($table, $columns, $options);
 }
Exemplo n.º 5
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if (Yii::$app->db->driverName === 'mysql') {
         $this->tableOptions = 'ENGINE=InnoDB CHARACTER SET=utf8 COLLATE=utf8_unicode_ci';
     }
 }
 public function init()
 {
     parent::init();
     Yii::setAlias('@webroot', '@frontend/web');
     Yii::setAlias('@web', '/');
     Yii::$app->set('assetManager', $this->assetManager);
 }
Exemplo n.º 7
0
 /**
  * @inheritdoc
  */
 public function createTable($table, $columns, $options = null)
 {
     if ($options === null && $this->db->driverName === 'mysql') {
         // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
         $options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
     }
     parent::createTable($table, $columns, $options);
 }
Exemplo n.º 8
0
 /**
  * Initialize migrations.
  * Calls parent init method, then loads current authManager instance.
  * 
  * @return DbManager
  * @throws yii\base\InvalidConfigException
  */
 public function init()
 {
     parent::init();
     $this->authManager = Yii::$app->getAuthManager();
     if (!$this->authManager instanceof DbManager) {
         throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
     }
 }
Exemplo n.º 9
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if ($this->db->driverName === 'mysql') {
         // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
         $this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
     }
 }
Exemplo n.º 10
0
 public function setUp()
 {
     $this->migrateControllerClass = MigrateController::className();
     $this->migrationBaseClass = Migration::className();
     $this->mockApplication(['components' => ['db' => ['class' => 'yii\\db\\Connection', 'dsn' => 'sqlite::memory:']]]);
     $this->setUpMigrationPath();
     parent::setUp();
 }
Exemplo n.º 11
0
 public function init()
 {
     parent::init();
     if ($this->db->driverName === 'mysql') {
         $this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
     } else {
         throw new InvalidConfigException($this->db->driverName . " is not support");
     }
 }
 /**
  * @inheritdoc
  */
 public function createTable($name, $columns, $options = null)
 {
     $baseTableName = $this->db->getSchema()->getRawTableName($name);
     if ($this->db->getSchema()->getTableSchema($baseTableName, true) !== null) {
         echo "    > table {$name} already exists ...\n";
         return;
     }
     parent::createTable($name, $columns, $options);
 }
Exemplo n.º 13
0
 public function init()
 {
     parent::init();
     if ($this->db->driverName === 'mysql') {
         //Mysql 表选项
         $engine = $this->useTransaction ? 'InnoDB' : 'MyISAM';
         $this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=' . $engine;
     }
 }
 public function init()
 {
     parent::init();
     /** @var Manager $attachment */
     $attachment = Manager::getInstance();
     if (!$attachment instanceof Manager) {
         throw new InvalidConfigException('Attachment Manager component not defined');
     }
     $this->attachmentTable = $attachment->attachmentFileTable;
 }
Exemplo n.º 15
0
 /**
  * {@inheritdoc}
  */
 public function init()
 {
     parent::init();
     switch (Yii::$app->db->driverName) {
         case 'mysql':
             $this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
             break;
         default:
             $this->tableOptions = null;
     }
 }
Exemplo n.º 16
0
 /**
  * Initializes the migration.
  * This method will set tableOptions to use InnoDB if the db driver is mysql.
  */
 public function init()
 {
     parent::init();
     if ($this->tableOptions === true) {
         if ($this->db->driverName === 'mysql') {
             $this->tableOptions = 'ENGINE=InnoDB';
         } else {
             $this->tableOptions = '';
         }
     }
 }
Exemplo n.º 17
0
 /**
  * @throws \yii\base\InvalidConfigException
  */
 public function init()
 {
     if (is_null($this->_tableName)) {
         throw new InvalidConfigException('$_tableName must be set!');
     }
     if ($this->db->driverName === 'mysql' && $this->_tableOptions !== false) {
         // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
         $this->_tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
     }
     parent::init();
 }
Exemplo n.º 18
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     switch (Yii::$app->db->driverName) {
         case 'mysql':
         case 'pgsql':
             $this->tableOptions = null;
             break;
         default:
             throw new \RuntimeException('Your database is not supported!');
     }
 }
Exemplo n.º 19
0
 /**
  * Builds and executes a SQL statement for creating a new index.
  * @param string $name the name of the index. The name will be properly quoted by the method.
  * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
  * @param string $column the column(s) that should be included in the index. If there are multiple columns, please separate them
  * by commas or use an array. The column names will be properly quoted by the method.
  * @param boolean $unique whether to add UNIQUE constraint on the created index.
  */
 public function createIndex($name, $table, $column, $unique = false)
 {
     if (in_array($this->db->getDriverName(), array('mysql', 'mysqli')) && $table != strtolower($table)) {
         echo "    > create (via alter table)" . ($unique ? ' unique' : '') . " index {$name} on {$table} (" . implode(',', (array) $column) . ") ...";
         $time = microtime(true);
         $sql = 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ADD ' . ($unique ? 'UNIQUE INDEX' : 'INDEX') . ' ' . $this->db->quoteTableName($name) . ' (' . $this->db->getQueryBuilder()->buildColumns($column) . ')';
         $this->db->createCommand($sql)->execute();
         echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
     } else {
         parent::createIndex($name, $table, $column, $unique);
     }
 }
Exemplo n.º 20
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     switch (\Yii::$app->db->driverName) {
         case 'mysql':
             $this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
             break;
         case 'pgsql':
             $this->tableOptions = null;
             break;
         default:
             throw new \RuntimeException('Your database is not supported!');
     }
 }
Exemplo n.º 21
0
 public function init()
 {
     parent::init();
     if ($this->file === null) {
         $reflection = new \ReflectionClass($this);
         $this->file = str_replace('.php', '.sql', $reflection->getFileName());
     } else {
         $reflection = new \ReflectionClass($this);
         $this->file = dirname($reflection->getFileName()) . DIRECTORY_SEPARATOR . $this->file;
     }
     if (!is_file($this->file)) {
         throw new Exception("File {$this->file} not found");
     }
 }
Exemplo n.º 22
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     switch (Yii::$app->db->driverName) {
         case 'mysql':
             $this->tableOptions = 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
             $this->blobType = 'LONGBLOB';
             break;
         case 'pgsql':
             $this->blobType = 'BYTEA';
             break;
         default:
             throw new \RuntimeException('Your database is not supported!');
     }
 }
 public function init()
 {
     parent::init();
     if (isset(Yii::$app->params['superadmin.role.name'])) {
         $this->roleName = Yii::$app->params['superadmin.role.name'];
     }
     if (isset(Yii::$app->params['superadmin.permission.name'])) {
         $this->permissionName = Yii::$app->params['superadmin.permission.name'];
     }
     if (isset(Yii::$app->params['superadmin.user.login'])) {
         $this->userLogin = Yii::$app->params['superadmin.user.login'];
     }
     if (isset(Yii::$app->params['superadmin.user.password'])) {
         $this->userPassword = Yii::$app->params['superadmin.user.password'];
     } else {
         throw new InvalidConfigException('You should configure "superadmin.user.password" param in common/config/yiip/yii2-module-rbac/params.php to use superadmin role.');
     }
 }
Exemplo n.º 24
0
 /**
  * Initializes the migration.
  * This method will set [[db]] to be the 'db' application component, if it is `null`.
  */
 public function init()
 {
     parent::init();
     if ($this->db->driverName === 'mysql') {
         switch ($this->encoding) {
             case 'utf8mb4':
                 $size = 191;
                 break;
             default:
                 $size = 255;
         }
         /* @var string MAX_CHAR_LENGTH the number of maximum allowed varchar size. */
         defined('MAX_CHAR_LENGTH') or define('MAX_CHAR_LENGTH', $size);
         $this->tableOptions = 'CHARACTER SET ';
         $this->tableOptions .= $this->encoding;
         $this->tableOptions .= ' COLLATE ';
         $this->tableOptions .= $this->collation;
         $this->tableOptions .= ' ENGINE=';
         $this->tableOptions .= $this->engine;
     }
 }
 /**
  * Create table in database
  */
 public function runCreateTable()
 {
     $this->dropTable();
     if ($this->hideMigrationOutput) {
         ob_start();
     }
     /** @var Connection $_conn */
     $_conn = Yii::$app->{$this->db};
     if (!$_conn->schema->getTableSchema($this->tableName)) {
         $this->migrationClass->createTable($this->tableNameRaw, $this->columns);
         if (is_array($this->primaryKeys) && sizeof($this->primaryKeys)) {
             try {
                 $this->migrationClass->addPrimaryKey("{$this->tableNameRaw}_pk", $this->tableNameRaw, $this->primaryKeys);
             } catch (\yii\db\Exception $exp) {
             }
         }
     }
     if ($this->hideMigrationOutput) {
         ob_clean();
         ob_flush();
     }
 }
 public function init()
 {
     require_once Yii::getAlias($this->originalMigrationPath) . DIRECTORY_SEPARATOR . $this->originalMigrationName . '.php';
     $this->originalMigration = new $this->originalMigrationName();
     parent::init();
 }
Exemplo n.º 27
0
 /**
  * Drop multiple columns to a table
  * @param string $table
  * @param array $columns ["column_name"=>type]
  */
 public function dropColumns($table, $columns)
 {
     foreach ($columns as $column => $type) {
         parent::dropColumn($table, $column);
     }
 }
 public function init()
 {
     $this->fifteenMinutesAgo = strtotime('-15 minutes');
     parent::init();
 }
 public function init()
 {
     $this->db = 'dbForms';
     parent::init();
 }
Exemplo n.º 30
0
 function actionTableusercompany()
 {
     $id_company = "FIELD1";
     $company_name = "FIELD2";
     $company_logo = "FIELD3";
     $company_color = "FIELD4";
     $company_image = "FIELD5";
     $migration = new Migration();
     $query = $migration->createTable('TABLE34', [$id_company => 'pk', $company_name => 'string', $company_logo => 'string', $company_color => 'string', $company_image => 'string']);
 }