Skip to content

Commit

Permalink
Merge pull request #269 from CakeDC/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
steinkel authored Sep 28, 2016
2 parents 03256d8 + 26347a6 commit cbe3137
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 55 deletions.
20 changes: 9 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php

php:
- 5.3
- 5.4
- 5.5

- 5.6
- 7.0
env:
global:
- PLUGIN_NAME=Migrations
Expand All @@ -13,20 +12,19 @@ env:
- REQUIRE="phpunit/phpunit:3.7.31"

matrix:
- DB=mysql CAKE_VERSION=2.6
- DB=mysql CAKE_VERSION=2.7

matrix:
include:
- php: 5.3
- php: 5.5
env:
- CAKE_VERSION=2.6
- php: 5.4
- CAKE_VERSION=2.7
- php: 5.6
env:
- CAKE_VERSION=2.6
- php: 5.5
- CAKE_VERSION=2.7
- php: 7.0
env:
- CAKE_VERSION=2.6

- CAKE_VERSION=2.8
before_script:
- git clone https://github.com/steinkel/travis.git --depth 1 ../travis
- ../travis/before_script.sh
Expand Down
91 changes: 69 additions & 22 deletions Console/Command/MigrationShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
App::uses('ConnectionManager', 'Model');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
App::uses('CakeTime', 'Utility');

/**
* Migration shell.
Expand Down Expand Up @@ -61,6 +62,13 @@ class MigrationShell extends AppShell {
*/
public $Version;

/**
* Skip a version or it can skip many version using comma as separate.
*
* @var array
*/
public $skip = array();

/**
* Messages used to display action being performed
*
Expand All @@ -87,12 +95,18 @@ public function startup() {
$this->type = $this->params['plugin'];
}

if (!empty($this->params['skip'])) {
$this->skip = $this->params['skip'];
}

$this->path = $this->_getPath() . 'Config' . DS . 'Migration' . DS;

$options = array(
'precheck' => $this->params['precheck'],
'autoinit' => !$this->params['no-auto-init'],
'dry' => $this->params['dry']);
'dry' => $this->params['dry'],
'skip' => isset($this->params['skip']) ? $this->params['skip'] : null,
'jumpTo' => isset($this->params['jump-to']) ? $this->params['jump-to'] : null);

if (!empty($this->connection)) {
$options['connection'] = $this->connection;
Expand All @@ -101,7 +115,6 @@ public function startup() {
if (!empty($this->migrationConnection)) {
$options['migrationConnection'] = $this->migrationConnection;
}

$this->Version = new MigrationVersion($options);

$this->_messages = array(
Expand Down Expand Up @@ -148,7 +161,7 @@ protected function _startMigrationConnection() {
/**
* Get the option parser.
*
* @return void
* @return string
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
Expand All @@ -166,6 +179,15 @@ public function getOptionParser() {
'short' => 'f',
'boolean' => true,
'help' => __d('migrations', 'Force \'generate\' to compare all tables.')))
->addOption('skip', array(
'help' => __('Skip certain migration.')))
->addOption('jump-to', array(
'short' => 'j',
'help' => __('Jump to a certain migration and mark the preceding migrations as executed.')))
->addOption('compare', array(
'short' => 'm',
'boolean' => true,
'help' => __d('migrations', 'Force the comparison between schema file and database')))
->addOption('overwrite', array(
'short' => 'o',
'boolean' => true,
Expand Down Expand Up @@ -225,7 +247,7 @@ public function run() {

if ($mapping === false) {
$this->out(__d('migrations', 'No migrations available.'));
return $this->_stop(1);
return $this->_stop();
}
$latestVersion = $this->Version->getVersion($this->type);

Expand All @@ -237,7 +259,8 @@ public function run() {
'precheck' => isset($this->params['precheck']) ? $this->params['precheck'] : null,
'type' => $this->type,
'dry' => $this->params['dry'],
'callback' => &$this);
'callback' => &$this,
'skip' => $this->skip);

$once = false; //In case of exception run shell again (all, reset, migration number)
if (isset($this->args[0]) && in_array($this->args[0], array('up', 'down'))) {
Expand Down Expand Up @@ -269,7 +292,6 @@ public function run() {
$this->out(__d('migrations', 'Migration will run dry, no database changes will be made'));
$this->out('');
}

$result = $this->_execute($options, $once);
if ($result !== true) {
$this->out($result);
Expand Down Expand Up @@ -429,14 +451,19 @@ public function generate() {
} else {
$oldSchema = $this->_getSchema($this->type);
if ($oldSchema !== false) {
$response = $this->in(__d('migrations', 'Do you want to compare the schema.php file to the database?'), array('y', 'n'), 'y');
$response = isset($this->params['compare']) && $this->params['compare'] === true ? 'y' : false;

if ($response === false) {
$response = $this->in(__d('migrations', 'Do you want to compare the schema.php file to the database?'), array('y', 'n'), 'y');
}

if (strtolower($response) === 'y') {
$this->_generateFromComparison($migration, $oldSchema, $comparison);
$this->_migrationChanges($migration);
$fromSchema = true;
} else {
$response = $this->in(__d('migrations', 'Do you want to compare the database to the schema.php file?'), array('y', 'n'), 'y');
if(strtolower($response) === 'y') {
if (strtolower($response) === 'y') {
$this->_generateFromInverseComparison($migration, $oldSchema, $comparison);
$this->_migrationChanges($migration);
$fromSchema = false;
Expand Down Expand Up @@ -465,6 +492,12 @@ public function generate() {
}
}

/**
* _migrationsChanges method
*
* @param array $migration list of migrations
* @return bool
*/
protected function _migrationChanges($migration) {
if (empty($migration)) {
$this->hr();
Expand Down Expand Up @@ -675,13 +708,13 @@ public function status() {
$this->out(__d('migrations', 'Current version:'));
if ($version != 0) {
$info = $mapping[$version];
$this->out(' #' . number_format($info['version'] / 100, 2, '', '') . ' ' . $info['name']);
$this->out(' #' . sprintf("%'.03d", $info['version']) . ' ' . $info['name']);
} else {
$this->out(' ' . __d('migrations', 'None applied.'));
}

$this->out(__d('migrations', 'Latest version:'));
$this->out(' #' . number_format($latest['version'] / 100, 2, '', '') . ' ' . $latest['name']);
$this->out(' #' . sprintf("%'.03d", $latest['version']) . ' ' . $latest['name']);
$this->hr();
} catch (MigrationVersionException $e) {
continue;
Expand All @@ -705,17 +738,17 @@ protected function _showInfo($mapping, $type = null) {
if ($version != 0) {
$info = $mapping[$version];
$this->out(__d('migrations', 'Current migration version:'));
$this->out(' #' . number_format($version / 100, 2, '', '') . ' ' . $info['name']);
$this->out(' #' . sprintf("%'.03d", $version) . ' ' . $info['name']);
$this->hr();
}

$this->out(__d('migrations', 'Available migrations:'));
foreach ($mapping as $version => $info) {
$this->out(' [' . number_format($version / 100, 2, '', '') . '] ' . $info['name']);
$this->out(' [' . sprintf("%'.03d", $version) . '] ' . $info['name']);

$this->out(' ', false);
if ($info['migrated'] !== null) {
$this->out(__d('migrations', 'applied') . ' ' . date('r', strtotime($info['migrated'])));
$this->out(__d('migrations', 'applied') . ' ' . CakeTime::nice(strtotime($info['migrated'])));
} else {
$this->out(__d('migrations', 'not applied'));
}
Expand Down Expand Up @@ -822,14 +855,14 @@ protected function _getSchema($type = null) {
}

$folder = new Folder($this->_getPath($type) . 'Config' . DS . 'Schema');
$schema_files = $folder->find('.*schema.*.php');
$schemaFiles = $folder->find('.*schema.*.php');

if (count($schema_files) === 0) {
if (count($schemaFiles) === 0) {
return false;
}

$name = $this->_getSchemaClassName($type);
$file = $this->_findSchemaFile($folder, $schema_files, $name);
$file = $this->_findSchemaFile($folder, $schemaFiles, $name);

if ($type === 'app' && empty($file)) {
$appDir = preg_replace('/[^a-zA-Z0-9]/', '', APP_DIR);
Expand All @@ -848,13 +881,13 @@ protected function _getSchema($type = null) {
* Finds schema file
*
* @param Folder $folder Folder object with schema folder path.
* @param string $schema_files Schema files inside schema folder.
* @param string $schemaFiles Schema files inside schema folder.
* @param string $name Schema-class name.
* @return mixed null in case of no file found, schema file.
*/
protected function _findSchemaFile($folder, $schema_files, $name) {
foreach ($schema_files as $schema_file) {
$file = new File($folder->pwd() . DS . $schema_file);
protected function _findSchemaFile($folder, $schemaFiles, $name) {
foreach ($schemaFiles as $schemaFile) {
$file = new File($folder->pwd() . DS . $schemaFile);
$content = $file->read();
if (strpos($content, $name) !== false) {
return $file->path;
Expand Down Expand Up @@ -1155,7 +1188,12 @@ protected function _values($values) {
if (is_array($values)) {
foreach ($values as $key => $value) {
if (is_array($value)) {
$_values[] = "'" . $key . "' => array('" . implode("', '", $value) . "')";
if (array_keys($value) !== range(0, count($value) - 1)) {
$set = implode("', '", $this->_values($value));
} else {
$set = "'" . implode("', '", $value) . "'";
}
$_values[] = "'" . $key . "' => array(" . $set . ")";
} elseif (!is_numeric($key)) {
$value = var_export($value, true);
$_values[] = "'" . $key . "' => " . $value;
Expand Down Expand Up @@ -1201,12 +1239,21 @@ protected function _getPath($type = null) {
/**
* Callback used to display what migration is being runned
*
* Additionally, shows the generation date of the migration,
* if the version is greater than '2000-01-01'.
*
* @param CakeMigration &$Migration Migration being performed
* @param string $direction Direction being runned
* @return void
*/
public function beforeMigration(&$Migration, $direction) {
$this->out(' [' . number_format($Migration->info['version'] / 100, 2, '', '') . '] ' . $Migration->info['name']);
$version = $Migration->info['version'];
$generationDate = '';
if ($version > 946684800) {
$generationDate = ' (' . CakeTime::format($version, '%Y-%m-%d %H:%M:%S') . ')';
}
$this->out(' [' . sprintf("%'.03d", $version) . '] ' . $Migration->info['name'] . $generationDate
);
}

/**
Expand Down
30 changes: 29 additions & 1 deletion Docs/Documentation/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,32 @@ Will set the datasource and will run the migrations in this datasource.

```
cake Migrations.migration run --connection my_data_source
```
```

Skipping certain migrations
--------------------------------------------------

Will skip one migration.

```
cake Migrations.migration run all --skip 1458963215_articles_table
```

You can skip many migrations using comma to separate, for example.

```
cake Migrations.migration run all --skip 1458963215_articles_table,1457412585_users_table
```

Remember this migrations will be set as executed.

Jumping to certain migrations
--------------------------------------------------

If you want to jump to a certain migration, you can use ```--jump-to``` or ```-j``` + migration name as in the example below.

```
cake Migrations.migration run all -j 1458963215_articles_table
```

Remember all migrations before this will be set as executed.
Loading

0 comments on commit cbe3137

Please sign in to comment.