Permalink
Browse files

Merge branch 'master' into develop

Conflicts:
	Console/Command/MigrationShell.php
	View/Elements/migrations_panel.ctp
  • Loading branch information...
elitalon committed Apr 14, 2012
2 parents eaa1640 + 05b59b4 commit f37058b7eb769a3f7a302999324f45f37822fb19
@@ -104,7 +104,7 @@ public function startup() {
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(
- 'The Migration shell.' .
+ 'The Migration shell.' .
'')
->addOption('plugin', array(
'short' => 'p',
@@ -114,7 +114,7 @@ public function getOptionParser() {
'boolean' => true,
'help' => __d('Migrations', 'Force \'generate\' to compare all tables.')))
->addOption('connection', array(
- 'short' => 'c',
+ 'short' => 'c',
'default' => 'default',
'help' => __d('Migrations', 'Set db config <config>. Uses \'default\' if none is specified.')))
->addOption('no-auto-init', array(
@@ -158,7 +158,7 @@ public function run() {
if ($mapping === false) {
$this->out(__d('Migrations', 'No migrations available.'));
- return $this->_stop();
+ return $this->_stop(1);
}
$latestVersion = $this->Version->getVersion($this->type);
@@ -201,14 +201,14 @@ protected function _execute($options, $once) {
$this->out(' ' . sprintf(__d('Migrations', 'Error: %s'), $e->getMessage()));
$this->hr();
-
+
$response = $this->in(__d('Migrations', 'Do you want to mark the migration as successful?. [y]es or [a]bort.'), array('y', 'a'));
-
+
if (strtolower($response) === 'y') {
$this->Version->setVersion($e->Migration->info['version'], $this->type, $options['direction'] === 'up');
if (!$once) {
return $this->run();
- }
+ }
} else if (strtolower($response) === 'a') {
return $this->_stop();
}
@@ -227,7 +227,7 @@ protected function _singleStepOptions($mapping, $latestVersion) {
}
if (!isset($mapping[$latestVersion])) {
$this->out(__d('Migrations', 'Not a valid migration version.'));
- return $this->_stop();
+ return $this->_stop(2);
}
$options['version'] = $mapping[$latestVersion]['version'];
return $options;
@@ -297,7 +297,7 @@ public function generate() {
$newSchema = $this->_readSchema();
$comparison = $this->Schema->compare($oldSchema, $newSchema);
$migration = $this->_fromComparison($migration, $comparison, $oldSchema->tables, $newSchema['tables']);
-
+
$fromSchema = true;
}
} else {
@@ -380,6 +380,7 @@ public function status() {
if ($mapping === false) {
continue;
}
+
$version = $this->Version->getVersion($type);
$latest = end($mapping);
if ($outdated && $latest['version'] == $version) {
@@ -652,7 +653,7 @@ private function __values($values) {
if (is_array($value)) {
$_values[] = "'" . $key . "' => array('" . implode("', '", $value) . "')";
} else if (!is_numeric($key)) {
- $value = var_export($value, true);
+ $value = var_export($value, true);
$_values[] = "'" . $key . "' => " . $value;
}
}
View
@@ -59,11 +59,10 @@ public function __construct($options = array()) {
$this->connection = $options['connection'];
}
- $options = array(
+ $this->Version = ClassRegistry::init(array(
'class' => 'Migrations.SchemaMigration',
'ds' => $this->connection
- );
- $this->Version = ClassRegistry::init($options);
+ ));
if (!isset($options['autoinit']) || $options['autoinit'] !== false) {
$this->__initMigrations();
}
@@ -97,7 +96,11 @@ public function getVersion($type) {
* @return boolean
*/
public function setVersion($version, $type, $migrated = true) {
+ if ($type !== 'app') {
+ $type = Inflector::camelize($type);
+ }
$mapping = $this->getMapping($type);
+
// For BC, 002 was not applied yet.
$bc = ($this->Version->schema('class') === null);
$field = $bc ? 'version' : 'class';
@@ -129,6 +132,10 @@ public function setVersion($version, $type, $migrated = true) {
* @return mixed False in case of no file found or empty mapping, array with mapping
*/
public function getMapping($type, $cache = true) {
+ if ($type !== 'app') {
+ $type = Inflector::camelize($type);
+ }
+
if ($cache && !empty($this->__mapping[$type])) {
return $this->__mapping[$type];
}
@@ -138,7 +145,12 @@ public function getMapping($type, $cache = true) {
}
$migrated = $this->Version->find('all', array(
- 'conditions' => array($this->Version->alias . '.type' => $type),
+ 'conditions' => array(
+ 'OR' => array(
+ array($this->Version->alias . '.type' => Inflector::underscore($type)),
+ array($this->Version->alias . '.type' => $type),
+ )
+ ),
'recursive' => -1,
));
@@ -150,6 +162,14 @@ public function getMapping($type, $cache = true) {
$migrated = Set::combine($migrated, '/' . $this->Version->alias . '/class', '/' . $this->Version->alias . '/created');
}
+ $bcMapping = array();
+ if ($type == 'Migrations') {
+ $bcMapping = array(
+ 'InitMigrations' => 'M4af6e0f0a1284147a0b100ca58157726',
+ 'ConvertVersionToClassNames' => 'M4ec50d1f7a284842b1b770fdcbdd56cb',
+ );
+ }
+
ksort($mapping);
foreach ($mapping as $version => $migration) {
list($name, $class) = each($migration);
@@ -158,10 +178,17 @@ public function getMapping($type, $cache = true) {
'version' => $version, 'name' => $name, 'class' => $class,
'type' => $type, 'migrated' => null
);
- if ($bc && isset($migrated[$version])) {
- $mapping[$version]['migrated'] = $migrated[$version];
- } else if (!$bc && isset($migrated[$class])) {
- $mapping[$version]['migrated'] = $migrated[$class];
+
+ if ($bc) {
+ if (isset($migrated[$version])) {
+ $mapping[$version]['migrated'] = $migrated[$version];
+ }
+ } else {
+ if (isset($migrated[$class])) {
+ $mapping[$version]['migrated'] = $migrated[$class];
+ } elseif (isset($bcMapping[$class]) && !empty($migrated[$bcMapping[$class]])) {
+ $mapping[$version]['migrated'] = $migrated[$bcMapping[$class]];
+ }
}
}
@@ -306,6 +333,21 @@ private function __loadFile($name, $type) {
* @return array containing a list of migration versions ordered by filename
*/
protected function _enumerateMigrations($type) {
+ $map = $this->_enumerateNewMigrations($type);
+ $oldMap = $this->_enumerateOldMigrations($type);
+ foreach ($oldMap as $k => $v) {
+ $map[$k] = $oldMap[$k];
+ }
+ return $map;
+ }
+
+/**
+ * Returns a map of all available migrations for a type (app or plugin) using inflection
+ *
+ * @param string $type Can be 'app' or a plugin name
+ * @return array containing a list of migration versions ordered by filename
+ */
+ protected function _enumerateNewMigrations($type) {
$mapping = array();
$path = APP . 'Config' . DS . 'Migration' . DS;
if ($type != 'app') {
@@ -326,6 +368,37 @@ protected function _enumerateMigrations($type) {
return $mapping;
}
+/**
+ * Returns a map of all available migrations for a type (app or plugin) using regular expressions
+ *
+ * @param string $type Can be 'app' or a plugin name
+ * @return array containing a list of migration versions ordered by filename
+ */
+ protected function _enumerateOldMigrations($type) {
+ $mapping = array();
+ $path = APP . 'Config' . DS . 'Migration' . DS;
+ if ($type != 'app') {
+ $path = CakePlugin::path(Inflector::camelize($type)) . 'Config' . DS . 'Migration' . DS;
+ }
+ if (!file_exists($path)) {
+ return $mapping;
+ }
+ $folder = new Folder($path);
+ foreach ($folder->find('.*?\.php', true) as $file) {
+ $parts = explode('_', $file);
+ $version = array_shift($parts);
+ $className = implode('_', $parts);
+ if ($version > 0 && strlen($className) > 0) {
+ $contents = file_get_contents($path . $file);
+ if (preg_match("/class\s([\w]+)\sextends/", $contents, $matches)) {
+ $mapping[(int)$version] = array(substr($file, 0, -4) => $matches[1]);
+ }
+ }
+ }
+ return $mapping;
+
+ }
+
}
/**
@@ -723,10 +723,7 @@ public function testGenerateComparison() {
$this->Shell->expects($this->at(4))->method('in')->will($this->returnValue('y'));
$this->Shell->expects($this->at(5))->method('dispatchShell')->with('schema generate --connection test --force');
- $mapping = array(
- gmdate('U') => array('class' => 'M4af9d15154844819b7a0007058157726')
- );
- $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnValue($mapping));
+ $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnCallback(array($this, 'returnMapping')));
$this->assertEmpty(glob(TMP . 'tests' . DS . '*drop_slug_field.php'));
$this->Shell->params['force'] = true;
@@ -757,6 +754,12 @@ public function testGenerateComparison() {
$this->assertPattern(str_replace("\r\n", "\n", $pattern), $result);
}
+ public function returnMapping() {
+ return array(
+ gmdate('U') => array('class' => 'M4af9d15154844819b7a0007058157726')
+ );
+ }
+
/**
* testGenerateDump method
*
@@ -770,7 +773,7 @@ public function testGenerateDump() {
$mapping = array(
gmdate('U') => array('class' => 'M4af9d15154844819b7a0007058157726')
);
- $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnValue($mapping));
+ $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnCallback(array($this, 'returnMapping')));
$this->assertEmpty(glob(TMP . 'tests' . DS . '*schema_dump.php'));
$this->Shell->type = 'TestMigrationPlugin2';
@@ -783,6 +786,7 @@ public function testGenerateDump() {
foreach ($files as $f) {
unlink($f);
}
+
$expected = file_get_contents(CakePlugin::path('Migrations') . '/Test/Fixture/test_migration.txt');
$this->assertEquals($expected, $result);
}
@@ -72,8 +72,15 @@ public function testGetMapping() {
1 => array(
'version' => 1,
'name' => '001_schema_dump',
- 'class' => 'SchemaDump',
- 'type' => 'test_migration_plugin',
+ 'class' => 'M4af6d40056b04408808500cb58157726',
+ 'type' => 'TestMigrationPlugin',
+ 'migrated' => null
+ ),
+ 2 => array(
+ 'version' => 2,
+ 'name' => '002_another_migration_plugin_test_migration',
+ 'class' => 'AnotherMigrationPluginTestMigration',
+ 'type' => 'TestMigrationPlugin',
'migrated' => null
)
);
@@ -85,14 +92,14 @@ public function testGetMapping() {
'version' => 1,
'name' => '001_init_migrations',
'class' => 'InitMigrations',
- 'type' => 'migrations',
+ 'type' => 'Migrations',
'migrated' => '2009-11-10 00:55:34'
),
2 => array(
'version' => 2,
'name' => '002_convert_version_to_class_names',
'class' => 'ConvertVersionToClassNames',
- 'type' => 'migrations',
+ 'type' => 'Migrations',
'migrated' => '2011-11-18 13:53:32'
)
);
@@ -29,8 +29,8 @@
),
'users' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
- 'user' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
- 'password' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'user' => array('type' => 'string', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'password' => array('type' => 'string', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
'updated' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
'indexes' => array(
@@ -0,0 +1,45 @@
+<?php
+class AnotherMigrationPluginTestMigration extends CakeMigration {
+
+/**
+ * Migration description
+ *
+ * @var string
+ * @access public
+ */
+ public $description = 'Version 002 (another test) of TestMigrationPlugin';
+
+/**
+ * Actions to be performed
+ *
+ * @var array $migration
+ * @access public
+ */
+ public $migration = array(
+ 'up' => array(),
+ 'down' => array()
+ );
+
+/**
+ * Before migration callback
+ *
+ * @param string $direction, up or down direction of migration process
+ * @return boolean Should process continue
+ * @access public
+ */
+ public function before($direction) {
+ return true;
+ }
+
+/**
+ * After migration callback
+ *
+ * @param string $direction, up or down direction of migration process
+ * @return boolean Should process continue
+ * @access public
+ */
+ public function after($direction) {
+ return true;
+ }
+}
+?>
@@ -10,7 +10,7 @@
*/
?>
-<h2><?php __d('Migrations', 'Migration Status') ?></h2>
+<h2><?php echo __d('Migrations', 'Migration Status') ?></h2>
<div class="code-table">
<a id="hide-migrations" href="#migrations">Toggle Applied migrations</a>
Oops, something went wrong.

0 comments on commit f37058b

Please sign in to comment.