Permalink
Browse files

Support both old and new migration files and database entries

  • Loading branch information...
josegonzalez authored and lorenzo committed Apr 10, 2012
1 parent 0760680 commit f648a1f7263d2c91d476c224105b5300d432a1b9
Showing with 84 additions and 13 deletions.
  1. +9 −8 Console/Command/MigrationShell.php
  2. +75 −5 Lib/MigrationVersion.php
@@ -104,17 +104,17 @@ public function startup() {
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(
- 'The Migration shell.' .
+ 'The Migration shell.' .
'')
->addOption('plugin', array(
- 'short' => 'p',
+ 'short' => 'p',
'help' => __('Plugin name to be used')))
->addOption('force', array(
'short' => 'f',
'boolean' => true,
'help' => __('Force \'generate\' to compare all tables.')))
->addOption('connection', array(
- 'short' => 'c',
+ 'short' => 'c',
'default' => 'default',
'help' => __('Set db config <config>. Uses \'default\' if none is specified.')))
->addOption('no-auto-init', array(
@@ -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();
}
@@ -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
@@ -128,6 +128,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];
}
@@ -137,7 +141,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,
));
@@ -149,6 +158,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);
@@ -157,10 +174,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])) {
+ $mapping[$version]['migrated'] = $migrated[$bcMapping[$class]];
+ }
}
}
@@ -305,6 +329,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') {
@@ -325,6 +364,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;
+
+ }
+
}
/**

0 comments on commit f648a1f

Please sign in to comment.