Skip to content

Commit

Permalink
Making schema dump work with plugins.
Browse files Browse the repository at this point in the history
Adding tests.
Making output of dump file more flexible.
  • Loading branch information
markstory committed Oct 4, 2009
1 parent 75f2106 commit 2dc0fbf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
28 changes: 16 additions & 12 deletions cake/console/libs/schema.php
Expand Up @@ -192,8 +192,10 @@ function generate() {

/**
* Dump Schema object to sql file
* if first arg == write, file will be written to sql file
* or it will output sql
* Use the `write` param to enable and control SQL file output location.
* Simply using -write will write the sql file to the same dir as the schema file.
* If -write contains a full path name the file will be saved there. If -write only
* contains no DS, that will be used as the file name, in the same dir as the schema file.
*
* @access public
*/
Expand All @@ -204,11 +206,11 @@ function dump() {
$this->err(__('Schema could not be loaded', true));
$this->_stop();
}
if (!empty($this->args[0])) {
if ($this->args[0] == 'write') {
if (isset($this->params['write'])) {
if ($this->params['write'] == 1) {
$write = Inflector::underscore($this->Schema->name);
} else {
$write = $this->args[0];
$write = $this->params['write'];
}
}
$db =& ConnectionManager::getDataSource($this->Schema->connection);
Expand All @@ -219,7 +221,12 @@ function dump() {
if (strpos($write, '.sql') === false) {
$write .= '.sql';
}
$File = new File($this->Schema->path . DS . $write, true);
if (strpos($write, DS) !== false) {
$File =& new File($write, true);
} else {
$File =& new File($this->Schema->path . DS . $write, true);
}

if ($File->write($contents)) {
$this->out(sprintf(__('SQL dump file created in %s', true), $File->pwd()));
$this->_stop();
Expand Down Expand Up @@ -259,16 +266,13 @@ function update() {
**/
function _loadSchema() {
$name = $plugin = null;
if (isset($this->args[0])) {
$name = $this->args[0];
}
if (isset($this->params['name'])) {
$name = $this->params['name'];
}
if (strpos($name, '.') !== false) {
list($plugin, $name) = explode('.', $name);
if (isset($this->params['plugin'])) {
$plugin = $this->params['plugin'];
}

if (isset($this->params['dry'])) {
$this->__dry = true;
$this->out(__('Performing a dry run.', true));
Expand Down
49 changes: 37 additions & 12 deletions cake/tests/cases/console/libs/schema.test.php
Expand Up @@ -217,15 +217,12 @@ function testViewWithPlugins() {
* @return void
**/
function testDumpWithFileWriting() {
$file =& new File(APP . 'config' . DS . 'schema' . DS . 'i18n.php');
$contents = $file->read();
$file =& new File(TMP . 'tests' . DS . 'i18n.php');
$file->write($contents);

$this->Shell->params = array('name' => 'i18n');
$this->Shell->args = array('write');
$this->Shell->params = array(
'name' => 'i18n',
'write' => TMP . 'tests' . DS . 'i18n.sql'
);
$this->Shell->expectOnce('_stop');
$this->Shell->startup();
$this->Shell->Schema->path = TMP . 'tests';
$this->Shell->dump();

$sql =& new File(TMP . 'tests' . DS . 'i18n.sql');
Expand All @@ -240,7 +237,35 @@ function testDumpWithFileWriting() {
$this->assertPattern('/content/', $contents);

$sql->delete();
}

/**
* test that dump() can find and work with plugin schema files.
*
* @return void
**/
function testDumpFileWritingWithPlugins() {
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
));
$this->Shell->args = array('TestPlugin.TestPluginApp');
$this->Shell->params = array(
'connection' => 'test_suite',
'write' => TMP . 'tests' . DS . 'dump_test.sql'
);
$this->Shell->startup();
$this->Shell->expectOnce('_stop');
$this->Shell->dump();

$file =& new File(TMP . 'tests' . DS . 'dump_test.sql');
$contents = $file->read();

$this->assertPattern('/CREATE TABLE `acos`/', $contents);
$this->assertPattern('/id/', $contents);
$this->assertPattern('/model/', $contents);

$file->delete();
App::build();
}

/**
Expand Down Expand Up @@ -310,7 +335,7 @@ function testGenerateOverwrite() {
*
* @return void
**/
function testRunCreateNoArgs() {
function testCreateNoArgs() {
$this->Shell->params = array(
'connection' => 'test_suite',
'path' => APP . 'config' . DS . 'sql'
Expand All @@ -333,7 +358,7 @@ function testRunCreateNoArgs() {
*
* @return void
**/
function testRunCreateWithTableArgs() {
function testCreateWithTableArgs() {
$this->Shell->params = array(
'connection' => 'test_suite',
'name' => 'DbAcl',
Expand All @@ -358,7 +383,7 @@ function testRunCreateWithTableArgs() {
*
* @return void
**/
function testRunUpdateWithTable() {
function testUpdateWithTable() {
$this->Shell->params = array(
'connection' => 'test_suite',
'f' => true
Expand Down Expand Up @@ -401,7 +426,7 @@ function testPluginParam() {
*
* @return void
**/
function testPluginDotSyntax() {
function testPluginDotSyntaxWithCreate() {
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
));
Expand Down

0 comments on commit 2dc0fbf

Please sign in to comment.