Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
16 KiB

11 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\console\controllers;
use Yii;
use yii\db\Connection;
use yii\db\Query;
use yii\di\Instance;
11 years ago
use yii\helpers\ArrayHelper;
use yii\helpers\Console;
11 years ago
/**
* Manages application migrations.
11 years ago
*
* A migration means a set of persistent changes to the application environment
* that is shared among different developers. For example, in an application
* backed by a database, a migration may refer to a set of changes to
* the database, such as creating a new table, adding a new table column.
*
* This command provides support for tracking the migration history, upgrading
* or downloading with migrations, and creating new migration skeletons.
*
* The migration history is stored in a database table named
* as [[migrationTable]]. The table will be automatically created the first time
* this command is executed, if it does not exist. You may also manually
* create it as follows:
*
* ```sql
* CREATE TABLE migration (
11 years ago
* version varchar(180) PRIMARY KEY,
11 years ago
* apply_time integer
* )
* ```
11 years ago
*
* Below are some common usages of this command:
*
* ```
11 years ago
* # creates a new migration named 'create_user_table'
* yii migrate/create create_user_table
*
* # applies ALL new migrations
* yii migrate
*
* # reverts the last applied migration
* yii migrate/down
* ```
11 years ago
*
* Since 2.0.10 you can use namespaced migrations. In order to enable this feature you should configure [[migrationNamespaces]]
* property for the controller at application configuration:
*
* ```php
* return [
* 'controllerMap' => [
* 'migrate' => [
* 'class' => 'yii\console\controllers\MigrateController',
* 'migrationNamespaces' => [
* 'app\migrations',
* 'some\extension\migrations',
* ],
* //'migrationPath' => null, // allows to disable not namespaced migration completely
* ],
* ],
* ];
* ```
*
11 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class MigrateController extends BaseMigrateController
11 years ago
{
/**
* @var string the name of the table for keeping applied migration information.
*/
public $migrationTable = '{{%migration}}';
/**
* @inheritdoc
*/
public $templateFile = '@yii/views/migration.php';
/**
* @var array a set of template paths for generating migration code automatically.
*
* The key is the template type, the value is a path or the alias. Supported types are:
* - `create_table`: table creating template
* - `drop_table`: table dropping template
* - `add_column`: adding new column template
* - `drop_column`: dropping column template
* - `create_junction`: create junction template
*
* @since 2.0.7
*/
public $generatorTemplateFiles = [
'create_table' => '@yii/views/createTableMigration.php',
'drop_table' => '@yii/views/dropTableMigration.php',
'add_column' => '@yii/views/addColumnMigration.php',
'drop_column' => '@yii/views/dropColumnMigration.php',
'create_junction' => '@yii/views/createTableMigration.php',
];
/**
* @var bool indicates whether the table names generated should consider
* the `tablePrefix` setting of the DB connection. For example, if the table
* name is `post` the generator wil return `{{%post}}`.
* @since 2.0.8
*/
public $useTablePrefix = false;
/**
* @var array column definition strings used for creating migration code.
*
* The format of each definition is `COLUMN_NAME:COLUMN_TYPE:COLUMN_DECORATOR`. Delimiter is `,`.
* For example, `--fields="name:string(12):notNull:unique"`
* produces a string column of size 12 which is not null and unique values.
*
* Note: primary key is added automatically and is named id by default.
* If you want to use another name you may specify it explicitly like
* `--fields="id_key:primaryKey,name:string(12):notNull:unique"`
* @since 2.0.7
*/
public $fields = [];
/**
* @var Connection|array|string the DB connection object or the application component ID of the DB connection to use
* when applying migrations. Starting from version 2.0.3, this can also be a configuration array
* for creating the object.
*/
public $db = 'db';
/**
* @inheritdoc
*/
10 years ago
public function options($actionID)
{
return array_merge(
10 years ago
parent::options($actionID),
['migrationTable', 'db'], // global for all actions
$actionID === 'create'
? ['templateFile', 'fields', 'useTablePrefix']
: []
);
}
/**
* @inheritdoc
* @since 2.0.8
*/
public function optionAliases()
{
return array_merge(parent::optionAliases(), [
'f' => 'fields',
'p' => 'migrationPath',
't' => 'migrationTable',
'F' => 'templateFile',
'P' => 'useTablePrefix',
]);
}
/**
* 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.
* @return bool whether the action should continue to be executed.
*/
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if ($action->id !== 'create') {
$this->db = Instance::ensure($this->db, Connection::className());
}
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)
{
$this->includeMigrationFile($class);
return new $class(['db' => $this->db]);
}
/**
* @inheritdoc
*/
protected function getMigrationHistory($limit)
{
8 years ago
if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) {
$this->createMigrationHistoryTable();
}
$query = (new Query())
->select(['version', 'apply_time'])
->from($this->migrationTable)
->orderBy(['apply_time' => SORT_DESC, 'version' => SORT_DESC]);
if (empty($this->migrationNamespaces)) {
$query->limit($limit);
$rows = $query->all($this->db);
$history = ArrayHelper::map($rows, 'version', 'apply_time');
unset($history[self::BASE_MIGRATION]);
return $history;
}
$rows = $query->all($this->db);
$history = [];
foreach ($rows as $key => $row) {
8 years ago
if ($row['version'] === self::BASE_MIGRATION) {
continue;
8 years ago
}
if (preg_match('/m?(\d{6}_?\d{6})(\D.*)?$/is', $row['version'], $matches)) {
$time = str_replace('_', '', $matches[1]);
$row['canonicalVersion'] = $time;
} else {
$row['canonicalVersion'] = $row['version'];
}
Added php-cs-fixer coding standards validation to Travis CI (#14100) * php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
7 years ago
$row['apply_time'] = (int) $row['apply_time'];
$history[] = $row;
}
usort($history, function ($a, $b) {
if ($a['apply_time'] === $b['apply_time']) {
if (($compareResult = strcasecmp($b['canonicalVersion'], $a['canonicalVersion'])) !== 0) {
return $compareResult;
}
return strcasecmp($b['version'], $a['version']);
}
return ($a['apply_time'] > $b['apply_time']) ? -1 : +1;
});
$history = array_slice($history, 0, $limit);
$history = ArrayHelper::map($history, 'version', 'apply_time');
return $history;
}
/**
* Creates the migration history table.
*/
protected function createMigrationHistoryTable()
{
$tableName = $this->db->schema->getRawTableName($this->migrationTable);
$this->stdout("Creating migration history table \"$tableName\"...", Console::FG_YELLOW);
$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();
$this->stdout("Done.\n", Console::FG_GREEN);
}
/**
* @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();
}
/**
* @inheritdoc
* @since 2.0.8
*/
protected function generateMigrationSourceCode($params)
{
$parsedFields = $this->parseFields();
$fields = $parsedFields['fields'];
$foreignKeys = $parsedFields['foreignKeys'];
$name = $params['name'];
$templateFile = $this->templateFile;
$table = null;
if (preg_match('/^create_junction(?:_table_for_|_for_|_)(.+)_and_(.+)_tables?$/', $name, $matches)) {
$templateFile = $this->generatorTemplateFiles['create_junction'];
$firstTable = $matches[1];
$secondTable = $matches[2];
$fields = array_merge(
[
[
'property' => $firstTable . '_id',
'decorators' => 'integer()',
],
[
'property' => $secondTable . '_id',
'decorators' => 'integer()',
],
],
$fields,
[
[
'property' => 'PRIMARY KEY(' .
$firstTable . '_id, ' .
$secondTable . '_id)',
],
]
);
$foreignKeys[$firstTable . '_id']['table'] = $firstTable;
$foreignKeys[$secondTable . '_id']['table'] = $secondTable;
$foreignKeys[$firstTable . '_id']['column'] = null;
$foreignKeys[$secondTable . '_id']['column'] = null;
$table = $firstTable . '_' . $secondTable;
} elseif (preg_match('/^add_(.+)_columns?_to_(.+)_table$/', $name, $matches)) {
$templateFile = $this->generatorTemplateFiles['add_column'];
$table = $matches[2];
} elseif (preg_match('/^drop_(.+)_columns?_from_(.+)_table$/', $name, $matches)) {
$templateFile = $this->generatorTemplateFiles['drop_column'];
$table = $matches[2];
} elseif (preg_match('/^create_(.+)_table$/', $name, $matches)) {
$this->addDefaultPrimaryKey($fields);
$templateFile = $this->generatorTemplateFiles['create_table'];
$table = $matches[1];
} elseif (preg_match('/^drop_(.+)_table$/', $name, $matches)) {
$this->addDefaultPrimaryKey($fields);
$templateFile = $this->generatorTemplateFiles['drop_table'];
$table = $matches[1];
}
foreach ($foreignKeys as $column => $foreignKey) {
$relatedColumn = $foreignKey['column'];
$relatedTable = $foreignKey['table'];
// Since 2.0.11 if related column name is not specified,
// we're trying to get it from table schema
// @see https://github.com/yiisoft/yii2/issues/12748
if ($relatedColumn === null) {
$relatedColumn = 'id';
try {
$this->db = Instance::ensure($this->db, Connection::className());
$relatedTableSchema = $this->db->getTableSchema($relatedTable);
if ($relatedTableSchema !== null) {
$primaryKeyCount = count($relatedTableSchema->primaryKey);
if ($primaryKeyCount === 1) {
$relatedColumn = $relatedTableSchema->primaryKey[0];
} elseif ($primaryKeyCount > 1) {
$this->stdout("Related table for field \"{$column}\" exists, but primary key is composite. Default name \"id\" will be used for related field\n", Console::FG_YELLOW);
} elseif ($primaryKeyCount === 0) {
$this->stdout("Related table for field \"{$column}\" exists, but does not have a primary key. Default name \"id\" will be used for related field.\n", Console::FG_YELLOW);
}
}
} catch (\ReflectionException $e) {
$this->stdout("Cannot initialize database component to try reading referenced table schema for field \"{$column}\". Default name \"id\" will be used for related field.\n", Console::FG_YELLOW);
}
}
$foreignKeys[$column] = [
'idx' => $this->generateTableName("idx-$table-$column"),
'fk' => $this->generateTableName("fk-$table-$column"),
'relatedTable' => $this->generateTableName($relatedTable),
'relatedColumn' => $relatedColumn,
];
}
return $this->renderFile(Yii::getAlias($templateFile), array_merge($params, [
'table' => $this->generateTableName($table),
'fields' => $fields,
'foreignKeys' => $foreignKeys,
]));
}
/**
* If `useTablePrefix` equals true, then the table name will contain the
* prefix format.
*
* @param string $tableName the table name to generate.
* @return string
* @since 2.0.8
*/
protected function generateTableName($tableName)
{
if (!$this->useTablePrefix) {
return $tableName;
}
return '{{%' . $tableName . '}}';
}
/**
* Parse the command line migration fields
* @return array parse result with following fields:
*
* - fields: array, parsed fields
* - foreignKeys: array, detected foreign keys
*
* @since 2.0.7
*/
protected function parseFields()
{
$fields = [];
$foreignKeys = [];
foreach ($this->fields as $index => $field) {
$chunks = preg_split('/\s?:\s?/', $field, null);
$property = array_shift($chunks);
foreach ($chunks as $i => &$chunk) {
if (strpos($chunk, 'foreignKey') === 0) {
preg_match('/foreignKey\((\w*)\s?(\w*)\)/', $chunk, $matches);
$foreignKeys[$property] = [
'table' => isset($matches[1])
? $matches[1]
: preg_replace('/_id$/', '', $property),
'column' => !empty($matches[2])
? $matches[2]
: null,
];
unset($chunks[$i]);
continue;
}
if (!preg_match('/^(.+?)\(([^(]+)\)$/', $chunk)) {
$chunk .= '()';
}
}
$fields[] = [
'property' => $property,
'decorators' => implode('->', $chunks),
];
}
return [
'fields' => $fields,
'foreignKeys' => $foreignKeys,
];
}
/**
* Adds default primary key to fields list if there's no primary key specified
* @param array $fields parsed fields
* @since 2.0.7
*/
protected function addDefaultPrimaryKey(&$fields)
{
foreach ($fields as $field) {
if (false !== strripos($field['decorators'], 'primarykey()')) {
return;
}
}
array_unshift($fields, ['property' => 'id', 'decorators' => 'primaryKey()']);
}
11 years ago
}