allow baking models with missing tables - even for rdbms

test updated to reflect expected new expected behavior. If you bake a
model with the table missing, if forced to run skip any part of the
process that would result in an exception.
This commit is contained in:
AD7six 2011-11-20 01:11:33 +01:00
parent ac8e1e8304
commit 8e38f666b7
2 changed files with 52 additions and 20 deletions

View file

@ -188,7 +188,7 @@ class ModelTask extends BakeTask {
$db = ConnectionManager::getDataSource($this->connection); $db = ConnectionManager::getDataSource($this->connection);
$fullTableName = $db->fullTableName($useTable); $fullTableName = $db->fullTableName($useTable);
if (!in_array($useTable, $this->_tables)) { if (!in_array($useTable, $this->_tables)) {
$prompt = __d('cake_console', "The table $useTable doesn't exist or could not be automatically detected\ncontinue anyway?"); $prompt = __d('cake_console', "The table %s doesn't exist or could not be automatically detected\ncontinue anyway?", $useTable);
$continue = $this->in($prompt, array('y', 'n')); $continue = $this->in($prompt, array('y', 'n'));
if (strtolower($continue) == 'n') { if (strtolower($continue) == 'n') {
return false; return false;
@ -196,26 +196,35 @@ class ModelTask extends BakeTask {
} }
$tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection)); $tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection));
$fields = $tempModel->schema(true);
$knownToExist = false;
try {
$fields = $tempModel->schema(true);
$knownToExist = true;
} catch (Exception $e) {
$fields = array($tempModel->primaryKey);
}
if (!array_key_exists('id', $fields)) { if (!array_key_exists('id', $fields)) {
$primaryKey = $this->findPrimaryKey($fields); $primaryKey = $this->findPrimaryKey($fields);
} }
$displayField = $tempModel->hasField(array('name', 'title')); if ($knownToExist) {
if (!$displayField) { $displayField = $tempModel->hasField(array('name', 'title'));
$displayField = $this->findDisplayField($tempModel->schema()); if (!$displayField) {
} $displayField = $this->findDisplayField($tempModel->schema());
}
$prompt = __d('cake_console', "Would you like to supply validation criteria \nfor the fields in your model?"); $prompt = __d('cake_console', "Would you like to supply validation criteria \nfor the fields in your model?");
$wannaDoValidation = $this->in($prompt, array('y','n'), 'y'); $wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
if (array_search($useTable, $this->_tables) !== false && strtolower($wannaDoValidation) == 'y') { if (array_search($useTable, $this->_tables) !== false && strtolower($wannaDoValidation) == 'y') {
$validate = $this->doValidation($tempModel); $validate = $this->doValidation($tempModel);
} }
$prompt = __d('cake_console', "Would you like to define model associations\n(hasMany, hasOne, belongsTo, etc.)?"); $prompt = __d('cake_console', "Would you like to define model associations\n(hasMany, hasOne, belongsTo, etc.)?");
$wannaDoAssoc = $this->in($prompt, array('y','n'), 'y'); $wannaDoAssoc = $this->in($prompt, array('y','n'), 'y');
if (strtolower($wannaDoAssoc) == 'y') { if (strtolower($wannaDoAssoc) == 'y') {
$associations = $this->doAssociations($tempModel); $associations = $this->doAssociations($tempModel);
}
} }
$this->out(); $this->out();

View file

@ -30,7 +30,7 @@ App::uses('ModelTask', 'Console/Command/Task');
/** /**
* ModelTaskTest class * ModelTaskTest class
* *
* @package Cake.Test.Case.Console.Command.Task * @package Cake.Test.Case.Console.Command.Task
*/ */
class ModelTaskTest extends CakeTestCase { class ModelTaskTest extends CakeTestCase {
@ -965,12 +965,35 @@ STRINGEND;
$this->Task->connection = 'test'; $this->Task->connection = 'test';
$this->Task->path = '/my/path/'; $this->Task->path = '/my/path/';
$this->Task->expects($this->once())->method('_stop');
$this->Task->expects($this->once())->method('err');
$this->Task->expects($this->any())->method('in') $this->Task->expects($this->any())->method('in')
->will($this->onConsecutiveCalls('Foobar', 'y')); ->will($this->onConsecutiveCalls(
'Foobar', // Or type in the name of the model
'y', // Do you want to use this table
'n' // Doesn't exist, continue anyway?
));
$this->Task->execute(); $this->Task->execute();
} }
/**
* test using bake interactively with a table that does not exist.
*
* @return void
*/
public function testForcedExecuteWithNonExistantTableName() {
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->expects($this->any())->method('in')
->will($this->onConsecutiveCalls(
'Foobar', // Or type in the name of the model
'y', // Do you want to use this table
'y', // Doesn't exist, continue anyway?
'id', // Primary key
'y' // Looks good?
));
$this->Task->execute();
}
} }