updating schema and tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5605 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-08-29 08:32:18 +00:00
parent 9d2a62752d
commit ae4ebec603
3 changed files with 92 additions and 77 deletions

View file

@ -160,6 +160,10 @@ class CakeSchema extends Object {
));
$db =& ConnectionManager::getDataSource($connection);
$currentTables = array_flip($db->sources());
$prefix = null;
if(isset($db->config['prefix'])) {
$prefix = $db->config['prefix'];
}
if (empty($models)) {
$models = Configure::listObjects('model');
}
@ -179,11 +183,14 @@ class CakeSchema extends Object {
if(class_exists(low($model))) {
$Object =& new $model();
$Object->setDataSource($connection);
if (is_object($Object) && isset($currentTables[$Object->table])) {
$table = $db->fullTableName($Object, false);
if (is_object($Object)) {
$table = $db->fullTableName($Object, false);
if(isset($currentTables[$table])) {
if(empty($tables[$Object->table])) {
$tables[$Object->table] = $this->__columns($Object);
$tables[$Object->table]['indexes'] = $db->index($Object);
unset($currentTables[$Object->table]);
unset($currentTables[$table]);
}
if(!empty($Object->hasAndBelongsToMany)) {
foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) {
@ -192,10 +199,14 @@ class CakeSchema extends Object {
} elseif ($assocData['_with']) {
$class = $assocData['_with'];
}
if (is_object($Object->$class) && isset($currentTables[$Object->$class->table])) {
if (is_object($Object->$class)) {
$table = $db->fullTableName($Object->$class, false);
if(isset($currentTables[$table])) {
$tables[$Object->$class->table] = $this->__columns($Object->$class);
$tables[$Object->$class->table]['indexes'] = $db->index($Object->$class);
unset($currentTables[$Object->$class->table]);
unset($currentTables[$table]);
}
}
}
}
}
@ -205,6 +216,9 @@ class CakeSchema extends Object {
if(!empty($currentTables)) {
foreach(array_flip($currentTables) as $table) {
if($prefix) {
$table = str_replace($prefix, '', $table);
}
$Object = new AppModel(array('name'=> Inflector::classify($table), 'table'=> $table, 'ds'=> $connection));
$tables['missing'][$table] = $this->__columns($Object);
$tables['missing'][$table]['indexes'] = $db->index($Object);
@ -341,7 +355,7 @@ class CakeSchema extends Object {
}
foreach ($fields as $field => $value) {
if (isset($old[$table][$field])) {
$diff = array_diff($value, $old[$table][$field]);
$diff = array_diff_assoc($value, $old[$table][$field]);
if (!empty($diff)) {
$tables[$table]['change'][$field] = am($old[$table][$field], $diff);
}
@ -419,7 +433,7 @@ class CakeSchema extends Object {
unset($value['limit']);
}
if (empty($value['default'])) {
if (isset($value['default']) && $value['default'] != 0) {
unset($value['default']);
}
if (empty($value['length'])) {

View file

@ -39,28 +39,27 @@ class MyAppSchema extends CakeSchema {
var $connection = 'test_suite';
var $comments = array(
'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
'post_id' => array('type'=>'integer', 'null' => false),
'user_id' => array('type'=>'integer', 'null' => false),
'title' => array('type'=>'string', 'null' => false, 'length' => 100),
'comment' => array('type'=>'text', 'null' => false),
'id' => array('type'=>'integer', 'null' => false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
'post_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
'user_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
'title' => array('type'=>'string', 'null' => false, 'length'=> 100),
'comment' => array('type'=>'text', 'null' => false, 'default'=> null),
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
'created' => array('type'=>'datetime', 'null' => true, 'default'=> null),
'updated' => array('type'=>'datetime', 'null' => true, 'default'=> null),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
var $posts = array(
'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
'author_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
'title' => array('type'=>'string', 'null' => false, 'default' => 'Title'),
'summary' => array('type'=>'text', 'null' => true),
'body' => array('type'=>'text', 'null' => true),
'id' => array('type'=>'integer', 'null' => false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
'author_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
'title' => array('type'=>'string', 'null' => false, 'default'=> 'Title'),
'body' => array('type'=>'text', 'null' => true, 'default'=> null),
'summary'=>array('type'=> 'text', 'null'=> true),
'published' => array('type'=>'string', 'null' => true, 'default' => 'Y', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
'created' => array('type'=>'datetime', 'null' => true, 'default'=> null),
'updated' => array('type'=>'datetime', 'null' => true, 'default'=> null),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
function setup($version) {
@ -74,38 +73,38 @@ class TestAppSchema extends CakeSchema {
var $name = 'MyApp';
var $comments = array(
'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
'article_id' => array('type'=>'integer', 'null' => false),
'user_id' => array('type'=>'integer', 'null' => false),
'comment' => array('type'=>'text', 'null' => true),
'id' => array('type'=>'integer', 'null' => false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
'article_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
'user_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
'comment' => array('type'=>'text', 'null' => true, 'default'=> null),
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
'created' => array('type'=>'datetime', 'null' => true, 'default'=> null),
'updated' => array('type'=>'datetime', 'null' => true, 'default'=> null),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
var $posts = array(
'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
'author_id' => array('type'=>'integer', 'null' => false),
'title' => array('type'=>'string', 'null' => false),
'body' => array('type'=>'text', 'null' => true),
'id' => array('type'=>'integer', 'null' => false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
'author_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
'title' => array('type'=>'string', 'null' => false, 'default'=> null),
'body' => array('type'=>'text', 'null' => true, 'default'=> null),
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
'created' => array('type'=>'datetime', 'null' => true, 'default'=> null),
'updated' => array('type'=>'datetime', 'null' => true, 'default'=> null),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
var $posts_tags = array(
'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
'tag_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
'post_id' => array('type' => 'integer', 'null' => false, 'default'=> 0, 'key' => 'primary'),
'tag_id' => array('type' => 'integer', 'null' => false, 'default'=> 0, 'key' => 'primary'),
'indexes' => array('UNIQUE_TAG' => array('column'=> array('post_id', 'tag_id'), 'unique'=>1))
);
var $tags = array(
'id' => array('type' => 'integer', 'null'=> false, 'key' => 'primary', 'extra'=> 'auto_increment'),
'tag' => array('type' => 'string', 'null' => false),
'created' => array('type' => 'datetime', 'null' => true),
'updated' => array('type' => 'datetime', 'null' => true),
'id' => array('type' => 'integer', 'null'=> false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
'tag' => array('type' => 'string', 'null' => false, 'default'=> null),
'created' => array('type' => 'datetime', 'null' => true, 'default'=> null),
'updated' => array('type' => 'datetime', 'null' => true, 'default'=> null),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
@ -151,6 +150,10 @@ class SchemaTag extends CakeTestModel {
var $useTable = 'tags';
var $hasAndBelongsToMany = array('SchemaPost');
}
class PostsTag extends CakeTestModel {
var $name = 'PostsTag';
var $useTable = 'posts_tags';
}
/**
* Short description for class.
*
@ -159,7 +162,7 @@ class SchemaTag extends CakeTestModel {
*/
class CakeSchemaTest extends CakeTestCase {
var $fixtures = array('core.post', 'core.comment', 'core.author', 'core.tag', 'core.posts_tag');
var $fixtures = array('core.post', 'core.tag', 'core.posts_tag', 'core.comment');
function setUp() {
$this->Schema = new TestAppSchema();
@ -188,15 +191,15 @@ class CakeSchemaTest extends CakeTestCase {
$New = new MyAppSchema();
$compare = $New->compare($this->Schema);
$expected = array(
'comments'=> array(
'add'=>array('post_id'=>array('type'=> 'integer', 'null'=> false, 'default'=> 0), 'title'=>array('type'=> 'string', 'null'=> false, 'length'=> 100)),
'drop'=>array('article_id'=>array('type'=> 'integer', 'null'=> false, 'default'=> 0)),
'change'=>array('comment'=>array('type'=>'text', 'null'=> false, 'default'=> null))
),
'posts'=> array(
'add'=> array('summary'=>array('type'=> 'text', 'null'=> 1)),
'change'=> array('title'=>array('type'=>'string', 'null'=> false, 'default'=> 'Title'), 'published'=>array('type'=>'string', 'null'=> true, 'default'=>'Y', 'length'=> '1')),
),
'comments'=> array(
'add'=>array('post_id'=>array('type'=> 'integer', 'null'=> false), 'title'=>array('type'=> 'string', 'null'=> false, 'length'=> 100)),
'drop'=>array('article_id'=>array('type'=> 'integer', 'null'=> false)),
'change'=>array('comment'=>array('type'=>'text', 'null'=> false))
),
);

View file

@ -418,13 +418,9 @@ class CakeTestCase extends UnitTestCase {
$db =& ConnectionManager::getDataSource('default');
}
// Add test prefix
$config = $db->config;
$config['prefix'] .= 'test_suite_';
// Set up db connection
ConnectionManager::create('test_suite', $config);
$db->config['prefix'] = 'test_suite_';
ConnectionManager::create('test_suite', $db->config);
// Get db connection
$this->db =& ConnectionManager::getDataSource('test_suite');
$this->db->cacheSources = false;
@ -457,11 +453,13 @@ class CakeTestCase extends UnitTestCase {
} elseif (strpos($fixture, 'app.') === 0) {
$fixture = substr($fixture, strlen('app.'));
$fixturePaths = array(
APP . 'tests' . DS . 'fixtures'
TESTS . DS . 'fixtures',
VENDORS . 'tests' . DS . 'fixtures'
);
} else {
$fixturePaths = array(
TESTS . 'fixtures',
VENDORS . 'tests' . DS . 'fixtures',
CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
);
}