Skip to content

Commit dfbed18

Browse files
committed
New name param.
1 parent 1dd113d commit dfbed18

1 file changed

Lines changed: 59 additions & 12 deletions

File tree

Console/Command/MigrationShell.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class MigrationShell extends AppShell {
4848
*/
4949
public $path;
5050

51+
/**
52+
* The migration script name
53+
*
54+
* @var string
55+
*/
56+
public $migrationName = '';
57+
5158
/**
5259
* Type of migration, can be 'app' or a plugin name
5360
*
@@ -91,6 +98,10 @@ public function startup() {
9198

9299
$this->migrationConnection = $this->_startMigrationConnection();
93100

101+
if (!empty($this->params['name'])) {
102+
$this->migrationName = $this->params['name'];
103+
}
104+
94105
if (!empty($this->params['plugin'])) {
95106
$this->type = $this->params['plugin'];
96107
}
@@ -168,9 +179,11 @@ public function getOptionParser() {
168179
return $parser->description(
169180
'The Migration shell.' .
170181
'')
182+
->addOption('name', array(
183+
'help' => __d('migrations', 'The migration script name.')))
171184
->addOption('plugin', array(
172185
'short' => 'p',
173-
'help' => __d('migrations', 'Plugin name to be used')))
186+
'help' => __d('migrations', 'Plugin name to be used.')))
174187
->addOption('precheck', array(
175188
'short' => 'm',
176189
'default' => 'Migrations.PrecheckException',
@@ -215,6 +228,12 @@ public function getOptionParser() {
215228
'boolean' => false,
216229
'default' => false,
217230
'help' => __d('migrations', 'CamelCased Classname without the `Schema` suffix to use when reading or generating schema files. See `Console/cake schema generate --help`.')))
231+
->addOption('preview', array(
232+
'boolean' => true,
233+
'help' => __d('migrations', 'Enables the migration file preview.')))
234+
->addOption('no-preview', array(
235+
'boolean' => true,
236+
'help' => __d('migrations', 'Disables the migration file preview.')))
218237
->addSubcommand('status', array(
219238
'help' => __d('migrations', 'Displays a status of all plugin and app migrations.')))
220239
->addSubcommand('run', array(
@@ -447,7 +466,8 @@ public function generate() {
447466
$fromSchema = false;
448467
$this->Schema = $this->_getSchema();
449468
$migration = array('up' => array(), 'down' => array());
450-
$migrationName = '';
469+
$migrationName = $this->migrationName;
470+
$this->out($migrationName);
451471
$comparison = array();
452472

453473
if (!empty($this->args)) {
@@ -648,6 +668,11 @@ protected function _finalizeGeneratedMigration(&$migration, &$migrationName, &$f
648668
$name = $migrationName;
649669
if (empty($name)) {
650670
$name = $this->_promptForMigrationName();
671+
} else {
672+
$validMigrationName = $this->_validateMigrationName($name);
673+
if ($validMigrationName === true) {
674+
$name = $this->_buildMigrationName($name);
675+
}
651676
}
652677

653678
$this->out(__d('migrations', 'Generating Migration...'));
@@ -670,20 +695,42 @@ protected function _finalizeGeneratedMigration(&$migration, &$migrationName, &$f
670695
protected function _promptForMigrationName() {
671696
while (true) {
672697
$name = $this->in(__d('migrations', 'Please enter the descriptive name of the migration to generate:'));
673-
if (!preg_match('/^([A-Za-z0-9_]+|\s)+$/', $name) || is_numeric($name[0])) {
674-
$this->out('');
675-
$this->err(__d('migrations', 'Migration name (%s) is invalid. It must only contain alphanumeric characters and start with a letter.', $name));
676-
} elseif (strlen($name) > 255) {
677-
$this->out('');
678-
$this->err(__d('migrations', 'Migration name (%s) is invalid. It cannot be longer than 255 characters.', $name));
679-
} else {
680-
$name = str_replace(' ', '_', trim($name));
681-
break;
682-
}
698+
$validMigrationName = $this->_validateMigrationName($name);
699+
if ($validMigrationName === true) {
700+
$name = $this->_buildMigrationName($name);
701+
break;
702+
}
683703
}
684704
return $name;
685705
}
686706

707+
/**
708+
* Validates the presented migration name.
709+
*
710+
* @param string $name
711+
* @return bool
712+
*/
713+
protected function _validateMigrationName($name) {
714+
if (!preg_match('/^([A-Za-z0-9_]+|\s)+$/', $name) || is_numeric($name[0])) {
715+
$this->out('');
716+
$this->err(__d('migrations', 'Migration name (%s) is invalid. It must only contain alphanumeric characters and start with a letter.', $name));
717+
} elseif (strlen($name) > 255) {
718+
$this->out('');
719+
$this->err(__d('migrations', 'Migration name (%s) is invalid. It cannot be longer than 255 characters.', $name));
720+
}
721+
return true;
722+
}
723+
724+
/**
725+
* Build the migration name.
726+
*
727+
* @param string $name
728+
* @return mixed
729+
*/
730+
protected function _buildMigrationName($name) {
731+
return str_replace(' ', '_', trim($name));
732+
}
733+
687734
/**
688735
* Displays a status of all plugin and app migrations
689736
*

0 commit comments

Comments
 (0)