/** * testSetGetVersion method * * @return void */ public function testSetGetVersion() { $this->Version = $this->getMock('MigrationVersion', array('getMapping'), array(array('connection' => 'test'))); // Checking current $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping())); $result = $this->Version->getVersion('inexistent_plugin'); $expected = 0; $this->assertEqual($result, $expected); // Setting as 1 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping())); $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1))); $setResult = $this->Version->setVersion(1, 'inexistent_plugin'); $this->assertTrue(!empty($setResult)); $result = $this->Version->getVersion('inexistent_plugin'); $expected = 1; $this->assertEqual($result, $expected); // Setting as 2 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1))); $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 2))); $setResult = $this->Version->setVersion(2, 'inexistent_plugin'); $this->assertTrue(!empty($setResult)); $result = $this->Version->getVersion('inexistent_plugin'); $expected = 2; $this->assertEqual($result, $expected); // Setting as 1 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 2))); $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1))); $setResult = $this->Version->setVersion(2, 'inexistent_plugin', false); $this->assertTrue(!empty($setResult)); $result = $this->Version->getVersion('inexistent_plugin'); $expected = 1; $this->assertEqual($result, $expected); }
/** * testRun method * * @return void */ public function testRun() { $back = $this->Version; $options = array('connection' => 'test'); $Version = $this->getMock('MigrationVersion', array('getMapping', 'getMigration'), array($options), 'TestMigrationVersionMockMigrationVersion', false); $this->Version = $Version; $this->Version->expects($this->any())->method('getMigration')->will($this->returnValue(new CakeMigration($options))); $this->Version->Version = ClassRegistry::init(array('class' => 'schema_migrations', 'ds' => 'test')); // direction => up $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping())); $this->assertEqual($Version->getVersion('mocks'), 0); $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array(1)); $this->assertEqual($Version->getVersion('mocks'), 1); // direction => down $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1))); $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array()); $this->assertEqual($Version->getVersion('mocks'), 0); // Set 1, 2, 3 versions applied $this->Version->setVersion(1, 'mocks'); $this->Version->setVersion(2, 'mocks'); $this->Version->setVersion(3, 'mocks'); // direction => up $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 3))); $this->assertEqual($Version->getVersion('mocks'), 3); $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 4)); $this->assertEqual($Version->getVersion('mocks'), 4); // direction => down $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 4))); $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); // version => 7 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 3))); $this->assertTrue($Version->run(array('version' => 7, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 7)); $this->assertEqual($Version->getVersion('mocks'), 7); // version => 3 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 7))); $this->assertTrue($Version->run(array('version' => 3, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); // version => 10 (top version) $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 3))); $this->assertTrue($Version->run(array('version' => 10, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 10)); $this->assertEqual($Version->getVersion('mocks'), 10); // version => 0 (run down all migrations) //$Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 10)); $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 10))); $this->assertTrue($Version->run(array('version' => 0, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array()); $this->assertEqual($Version->getVersion('mocks'), 0); // Changing values back $this->Version = $back; unset($back); }
/** * Generate a new migration file * * @return void * @access public */ public function generate() { while (true) { $name = $this->in(__d('migrations', 'Please enter the class name of the migration to generate: (example: AddTableUsers)', true)); if (!preg_match('/^[a-zA-Z0-9]+$/', $name)) { $this->out(''); $this->err(sprintf(__d('migrations', 'Migration name (%s) is invalid. It must only contain alphanumeric characters.', true), $name)); } else { break; } } $fromSchema = false; $this->Schema = $this->_getSchema(); $migration = array('up' => array(), 'down' => array()); $oldSchema = $this->_getSchema($this->type); if ($oldSchema !== false) { $response = $this->in(__d('migrations', 'Do you want compare the schema.php file to the database?', true), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->hr(); $this->out(__d('migrations', 'Comparing schema.php to the database...', true)); if ($this->type !== 'migrations') { unset($oldSchema->tables['schema_migrations']); } $newSchema = $this->_readSchema(); $comparison = $this->Schema->compare($oldSchema, $newSchema); $migration = $this->_fromComparison($migration, $comparison, $oldSchema->tables, $newSchema['tables']); $fromSchema = true; } } else { $response = $this->in(__d('migrations', 'Do you want generate a dump from current database?', true), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->hr(); $this->out(__d('migrations', 'Generating dump from current database...', true)); $dump = $this->_readSchema(); $dump = $dump['tables']; unset($dump['missing']); if (!empty($dump)) { $migration['up']['create_table'] = $dump; $migration['down']['drop_table'] = array_keys($dump); } $fromSchema = true; } } $this->out(__d('migrations', 'Generating Migration...', true)); $version = (int) gmdate('U'); //UTC timestamp $this->_writeMigration($name, $version, $migration); if ($fromSchema) { $this->Version->setVersion($version, $this->type); } $this->out(''); $this->out(__d('migrations', 'Done.', true)); }
/** * Finalizes the generated migration - offers to preview it, * prompts for a name, writes the file, and updates db version if needed. * * @param array &$migration Reference to variable of the same name in generate() method * @param array &$migrationName Reference to variable of the same name in generate() method * @param bool &$fromSchema Reference to variable of the same name in generate() method * @return void */ protected function _finalizeGeneratedMigration(&$migration, &$migrationName, &$fromSchema) { $response = $this->in(__d('migrations', 'Do you want to preview the file before generation?'), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->out($this->_generateMigration('Preview of migration', 'PreviewMigration', $migration)); } $name = $migrationName; if (empty($name)) { $name = $this->_promptForMigrationName(); } $this->out(__d('migrations', 'Generating Migration...')); $time = gmdate('U'); $this->_writeMigration($name, $time, $migration); if ($fromSchema) { $this->Version->setVersion($time, $this->type); } $this->out(''); $this->out(__d('migrations', 'Done.')); }
/** * Generate a new migration file * * @return void */ public function generate() { $fromSchema = false; $this->Schema = $this->_getSchema(); $migration = array('up' => array(), 'down' => array()); $oldSchema = $this->_getSchema($this->type); if ($oldSchema !== false) { $response = $this->in(__d('Migrations', 'Do you want compare the schema.php file to the database?'), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->hr(); $this->out(__d('Migrations', 'Comparing schema.php to the database...')); if ($this->type !== 'migrations') { unset($oldSchema->tables['schema_migrations']); } $newSchema = $this->_readSchema(); $comparison = $this->Schema->compare($oldSchema, $newSchema); $migration = $this->_fromComparison($migration, $comparison, $oldSchema->tables, $newSchema['tables']); $fromSchema = true; } } else { $response = $this->in(__d('Migrations', 'Do you want generate a dump from current database?'), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->hr(); $this->out(__d('Migrations', 'Generating dump from current database...')); $dump = $this->_readSchema(); $dump = $dump['tables']; unset($dump['missing']); if (!empty($dump)) { $migration['up']['create_table'] = $dump; $migration['down']['drop_table'] = array_keys($dump); } $fromSchema = true; } } $response = $this->in(__d('Migrations', 'Do you want to preview the file before generation?'), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->out($this->_generateMigration('', 'PreviewMigration', $migration)); } while (true) { $name = $this->in(__d('Migrations', 'Please enter the descriptive name of the migration to generate:')); if (!preg_match('/^([A-Za-z0-9_]+|\\s)+$/', $name) || is_numeric($name[0])) { $this->out(''); $this->err(__d('Migrations', 'Migration name (%s) is invalid. It must only contain alphanumeric characters and start with a letter.', $name)); } elseif (strlen($name) > 255) { $this->out(''); $this->err(__d('Migrations', 'Migration name (%s) is invalid. It cannot be longer than 255 characters.', $name)); } else { $name = str_replace(' ', '_', trim($name)); break; } } $this->out(__d('Migrations', 'Generating Migration...')); $time = gmdate('U'); $this->_writeMigration($name, $time, $migration); if ($fromSchema) { $this->Version->setVersion($time, $this->type); } $this->out(''); $this->out(__d('Migrations', 'Done.')); if ($fromSchema && isset($comparison)) { $response = $this->in(__d('Migrations', 'Do you want update the schema.php file?'), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->_updateSchema(); } } }
/** * Generate a new migration file * * @return void * @access public */ public function generate() { while (true) { $name = $this->in(__d('migrations', 'Please enter the descriptive name of the migration to generate:', true)); if (!preg_match('/^([a-z0-9_]+|\\s)+$/', $name)) { $this->out(''); $this->err(sprintf(__d('migrations', 'Migration name (%s) is invalid. It must only contain alphanumeric characters.', true), $name)); } else { $name = str_replace(' ', '_', trim($name)); break; } } $fromSchema = false; $this->Schema = $this->_getSchema(); $migration = array('up' => array(), 'down' => array()); $oldSchema = $this->_getSchema($this->type); if ($oldSchema !== false) { $response = $this->in(__d('migrations', 'Do you want compare the schema.php file to the database?', true), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->hr(); $this->out(__d('migrations', 'Comparing schema.php to the database...', true)); if ($this->type !== 'migrations') { unset($oldSchema->tables['schema_migrations']); } $newSchema = $this->_readSchema(); $comparison = $this->Schema->compare($oldSchema, $newSchema); $migration = $this->_fromComparison($migration, $comparison, $oldSchema->tables, $newSchema['tables']); $fromSchema = true; } } else { $response = $this->in(__d('migrations', 'Do you want generate a dump from current database?', true), array('y', 'n'), 'y'); if (strtolower($response) === 'y') { $this->hr(); $this->out(__d('migrations', 'Generating dump from current database...', true)); $dump = $this->_readSchema(); $dump = $dump['tables']; unset($dump['missing']); if (!empty($dump)) { $migration['up']['create_table'] = $dump; $migration['down']['drop_table'] = array_keys($dump); } $fromSchema = true; } } $this->out(__d('migrations', 'Generating Migration...', true)); $class = 'M' . str_replace('-', '', String::uuid()); $this->_writeMigration($name, $class, $migration); $version = 1; $map = array(); if (file_exists($this->path . 'map.php')) { include $this->path . 'map.php'; ksort($map); end($map); list($version) = each($map); $version++; } $map[$version] = array($name => $class); $this->out(__d('migrations', 'Mapping Migrations...', true)); $this->_writeMap($map); if ($fromSchema) { $this->Version->setVersion($version, $this->type); } $this->out(''); $this->out(__d('migrations', 'Done.', true)); }
/** * testRun method * * @return void */ function testRun() { $back = $this->Version; $options = array('connection' => 'test_suite'); $Version =& new TestMigrationVersionMockMigrationVersion($options); $this->Version = $Version; $this->Version->setReturnValue('getMigration', new CakeMigration($options)); $this->Version->Version =& ClassRegistry::init(array('class' => 'schema_migrations', 'ds' => 'test_suite')); // Variable used on setReturValueAt method $mappingCount = 0; // direction => up $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping()); $this->assertEqual($Version->getVersion('mocks'), 0); $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array(1)); $this->assertEqual($Version->getVersion('mocks'), 1); // direction => down $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 1)); $this->assertEqual($Version->getVersion('mocks'), 1); $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array()); $this->assertEqual($Version->getVersion('mocks'), 0); // Set 1, 2, 3 versions applied $this->Version->setVersion(1, 'mocks'); $this->Version->setVersion(2, 'mocks'); $this->Version->setVersion(3, 'mocks'); // direction => up $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 4)); $this->assertEqual($Version->getVersion('mocks'), 4); // direction => down $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 4)); $this->assertEqual($Version->getVersion('mocks'), 4); $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); // version => 7 $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); $this->assertTrue($Version->run(array('version' => 7, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 7)); $this->assertEqual($Version->getVersion('mocks'), 7); // version => 3 $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 7)); $this->assertEqual($Version->getVersion('mocks'), 7); $this->assertTrue($Version->run(array('version' => 3, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); // version => 10 (top version) $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); $this->assertTrue($Version->run(array('version' => 10, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 10)); $this->assertEqual($Version->getVersion('mocks'), 10); // version => 0 (run down all migrations) $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 10)); $this->assertEqual($Version->getVersion('mocks'), 10); $this->assertTrue($Version->run(array('version' => 0, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array()); $this->assertEqual($Version->getVersion('mocks'), 0); // Changing values back $this->Version = $back; unset($back); }