Permalink
Browse files

Finishing implementation, renamed version to class on the schema.

Still need to fix the tests.
  • Loading branch information...
1 parent d58578b commit 0a57b2345accea1e123bbfd814bfe56a6e824f1a @renan renan committed Nov 18, 2011
@@ -20,7 +20,7 @@ class M4af6e0f0a1284147a0b100ca58157726 extends CakeMigration {
'create_table' => array(
'schema_migrations' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
- 'version' => array('type' => 'integer', 'null' => false, 'default' => NULL),
+ 'class' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 33),
'type' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 50),
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'indexes' => array(
@@ -19,28 +19,20 @@ class M4ec50d1f7a284842b1b770fdcbdd56cb extends CakeMigration {
'up' => array(
'alter_field' => array(
'schema_migrations' => array(
- 'version' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 33)
+ 'version' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 33, 'name' => 'class')
)
)
),
'down' => array(
'alter_field' => array(
'schema_migrations' => array(
- 'version' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 11)
+ 'class' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 11, 'name' => 'version')
)
)
)
);
/**
- * MigrationVersion instance
- *
- * @var MigrationVersion
- * @access public
- */
- public $Version;
-
-/**
* Records to be migrated
*
* @var array
@@ -68,12 +60,6 @@ public function before($direction) {
throw new InternalErrorException(__d('migrations', 'Sorry, I can\'t downgrade. Why would you want that anyways?'));
}
- $this->Version = new MigrationVersion(array(
- 'connection' => $this->connection,
- 'autoinit' => false
- ));
- $this->Version->Version = $this->generateModel('Version', 'schema_migrations');
-
$this->records = $this->Version->Version->find('all');
$this->needsUpgrade();
@@ -105,7 +91,7 @@ public function needsUpgrade() {
$schema = $this->Version->Version->schema();
// Needs upgrade
- if ($schema['version']['type'] === 'integer') {
+ if (isset($schema['version'])) {
return;
}
@@ -147,8 +133,8 @@ public function checkPlugins() {
*/
public function checkRecords() {
foreach ($this->records as $record) {
- $type = $record['Version']['type'];
- $version = $record['Version']['version'];
+ $type = $record[$this->Version->Version->alias]['type'];
+ $version = $record[$this->Version->Version->alias]['version'];
if (!isset($this->mappings[$type])) {
$this->mappings[$type] = $this->Version->getMapping($type);
@@ -175,15 +161,22 @@ public function checkRecords() {
* @return void
*/
public function upgradeRecords() {
+ // Refresh the model
+ $options = array(
+ 'class' => 'Migrations.SchemaMigration',
+ 'ds' => $this->connection);
+ $this->Version->Version =& ClassRegistry::init($options);
+
+ // Upgrade them
foreach ($this->records as $record) {
- $type = $record['Version']['type'];
- $version = $record['Version']['version'];
+ $type = $record[$this->Version->Version->alias]['type'];
+ $version = $record[$this->Version->Version->alias]['version'];
$mapping = $this->mappings[$type];
$migration = $mapping[$version];
- $this->Version->Version->id = $record['Version']['id'];
- $this->Version->Version->saveField('version', $migration['class']);
+ $this->Version->Version->id = $record[$this->Version->Version->alias]['id'];
+ $this->Version->Version->saveField('class', $migration['class']);
}
}
}
View
@@ -77,6 +77,13 @@ class CakeMigration extends Object {
public $db = null;
/**
+ * MigrationVersion instance
+ *
+ * @var MigrationVersion
+ */
+ public $Version = null;
+
+/**
* CakeSchema instance
*
* @var CakeSchema
@@ -422,7 +429,11 @@ protected function _invokeCallbacks($callback, $type, $data = array()) {
* @return void
*/
protected function _clearCache() {
- Cache::clear(false, '_cake_model_');
+ DboSource::$methodCache = array();
+ $keys = Cache::configured();
+ foreach ($keys as $key) {
+ Cache::clear(false, $key);
+ }
ClassRegistry::flush();
}
View
@@ -68,18 +68,15 @@ public function __construct($options = array()) {
* @return integer Last version migrated
*/
public function getVersion($type) {
- $version = $this->Version->find('first', array(
- 'fields' => array('version'),
- 'conditions' => array($this->Version->alias . '.type' => $type),
- 'order' => array($this->Version->alias . '.version' => 'DESC'),
- 'recursive' => -1,
- ));
+ $mapping = $this->getMapping($type);
+ krsort($mapping);
- if (empty($version)) {
- return 0;
- } else {
- return $version[$this->Version->alias]['version'];
+ foreach ($mapping as $version => $info) {
+ if ($info['migrated'] !== null) {
+ return $version;
+ }
}
+ return 0;
}
/**
@@ -92,14 +89,21 @@ public function getVersion($type) {
* @return boolean
*/
public function setVersion($version, $type, $migrated = true) {
+ $mapping = $this->getMapping($type);
+
+ // For BC, 002 was not applied yet.
+ $bc = ($this->Version->schema('class') === null);
+ $field = $bc ? 'version' : 'class';
+ $value = $bc ? $version : $mapping[$version]['class'];
+
if ($migrated) {
$this->Version->create();
return $this->Version->save(array(
- 'version' => $version, 'type' => $type
+ $field => $value, 'type' => $type
));
} else {
$conditions = array(
- $this->Version->alias . '.version' => $version,
+ $this->Version->alias . '.' . $field => $value,
$this->Version->alias . '.type' => $type
);
return $this->Version->deleteAll($conditions);
@@ -122,12 +126,17 @@ public function getMapping($type) {
}
$migrated = $this->Version->find('all', array(
- 'fields' => array('version', 'created'),
'conditions' => array($this->Version->alias . '.type' => $type),
- 'order' => array($this->Version->alias . '.version' => 'ASC'),
'recursive' => -1,
));
- $migrated = Set::combine($migrated, '/' . $this->Version->alias . '/version', '/' . $this->Version->alias . '/created');
+
+ // For BC, 002 was not applied yet.
+ $bc = ($this->Version->schema('class') === null);
+ if ($bc) {
+ $migrated = Set::combine($migrated, '/' . $this->Version->alias . '/version', '/' . $this->Version->alias . '/created');
+ } else {
+ $migrated = Set::combine($migrated, '/' . $this->Version->alias . '/class', '/' . $this->Version->alias . '/created');
+ }
ksort($mapping);
foreach ($mapping as $version => $migration) {
@@ -137,8 +146,10 @@ public function getMapping($type) {
'version' => $version, 'name' => $name, 'class' => $class,
'type' => $type, 'migrated' => null
);
- if (isset($migrated[$version])) {
+ if ($bc && isset($migrated[$version])) {
$mapping[$version]['migrated'] = $migrated[$version];
+ } else if (!$bc && isset($migrated[$class])) {
+ $mapping[$version]['migrated'] = $migrated[$class];
}
}
@@ -210,6 +221,7 @@ public function run($options) {
|| ($direction == 'down' && $info['migrated'] !== null)) {
$migration = $this->getMigration($info['name'], $info['class'], $info['type'], $options);
+ $migration->Version = $this;
$migration->info = $info;
$migration->run($direction);

0 comments on commit 0a57b23

Please sign in to comment.