/var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/migrations/src/Command
Edit: /var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/migrations/src/Command/BakeMigrationCommand.php (7545B)
on('Bake.initialize', function (Event $event) {
$event->getSubject()->loadHelper('Migrations.Migration');
});
$this->_name = $name;
parent::bake($name, $args, $io);
}
/**
* @inheritDoc
*/
public function template(): string
{
return 'Migrations.config/skeleton';
}
/**
* @inheritDoc
*/
public function templateData(Arguments $arguments): array
{
$className = $this->_name;
$namespace = Configure::read('App.namespace');
$pluginPath = '';
if ($this->plugin) {
$namespace = $this->_pluginNamespace($this->plugin);
$pluginPath = $this->plugin . '.';
}
$action = $this->detectAction($className);
if (empty($action)) {
return [
'plugin' => $this->plugin,
'pluginPath' => $pluginPath,
'namespace' => $namespace,
'tables' => [],
'action' => null,
'name' => $className,
];
}
$arguments = $arguments->getArguments();
unset($arguments[0]);
$columnParser = new ColumnParser();
$fields = $columnParser->parseFields($arguments);
$indexes = $columnParser->parseIndexes($arguments);
$primaryKey = $columnParser->parsePrimaryKey($arguments);
if (in_array($action[0], ['alter_table', 'add_field'], true) && !empty($primaryKey)) {
$this->io->abort('Adding a primary key to an already existing table is not supported.');
}
[$action, $table] = $action;
return [
'plugin' => $this->plugin,
'pluginPath' => $pluginPath,
'namespace' => $namespace,
'tables' => [$table],
'action' => $action,
'columns' => [
'fields' => $fields,
'indexes' => $indexes,
'primaryKey' => $primaryKey,
],
'name' => $className,
];
}
/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser(): ConsoleOptionParser
{
$parser = parent::getOptionParser();
$text = <<<'TEXT'
Create a blank or generated migration. Using the name of the migration
Operations and table names will be inferred.
Examples
bin/cake bake migration CreateUsers
This command will generate a migration that creates
a table named users.
bin/cake bake migration DropGroups
This command will generate a migration that drops
the groups table.
bin/cake bake migration AlterUsers
This command will generate a migration that alters the users table.
bin/cake bake migration AddFieldToUsers role:string
This command will generate a migration that adds a 'role' field
with a 'string' type to the users table. Migrations that operate
on columns can use the
Column Grammar to describe
the column in detail.
bin/cake bake migration AlterFieldOnUsers role
These commands will generate a migration that will alter the 'role'
field on the users table.
bin/cake bake migration RemoveFieldsFromUsers role
bin/cake bake migration RemoveRoleFromUsers
These commands will generate a migration that will remove the 'role'
field on the users table.
Column Grammar
When describing columns you can use the following syntax:
{name}:{primary}{type}{nullable}[{length}]:{index}
All sections other than name are optional.
* The types are the abstract database column types in CakePHP.
* The
? value indicates if a column is nullable.
e.x.
role:string?.
* Length option must be enclosed in
[], for example:
name:string[100].
* The
index attribute can define the column as having a unique
key with
unique or a primary key with
primary.
Examples
bin/cake bake migration AddOrgIdToProjects org_id:int
Create a migration that adds a column (
org_id INT) to the
projects
table.
bin/cake bake migration AddOrgIdToProjects org_id:int?
Create a migration that adds a nullable column (
org_id INT NULL) to the
projects
table.
bin/cake bake migration AddNameToProjects name:string[128]
Create a migration that adds (
name VARCHAR(128)) to the
projects
table.
bin/cake bake migration AddSlugToProjects name:string[128]:unique
Create a migration that adds (
name VARCHAR(128) and a
UNIQUE<.warning index)
to the projects table.
TEXT;
$parser->setDescription($text);
return $parser;
}
/**
* Detects the action and table from the name of a migration
*
* @param string $name Name of migration
* @return array
*/
public function detectAction($name)
{
if (preg_match('/^(Create|Drop)(.*)/', $name, $matches)) {
$action = strtolower($matches[1]) . '_table';
$table = Inflector::underscore($matches[2]);
} elseif (preg_match('/^(Add).+?(?:To)(.*)/', $name, $matches)) {
$action = 'add_field';
$table = Inflector::underscore($matches[2]);
} elseif (preg_match('/^(Remove).+?(?:From)(.*)/', $name, $matches)) {
$action = 'drop_field';
$table = Inflector::underscore($matches[2]);
} elseif (preg_match('/^(Alter).+?(?:On)(.*)/', $name, $matches)) {
$action = 'alter_field';
$table = Inflector::underscore($matches[2]);
} elseif (preg_match('/^(Alter)(.*)/', $name, $matches)) {
$action = 'alter_table';
$table = Inflector::underscore($matches[2]);
} else {
return [];
}
return [$action, $table];
}
}