updating schema reading

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5579 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-08-25 03:59:21 +00:00
parent 2e60a4d2f1
commit 0533bbd92c
3 changed files with 148 additions and 56 deletions

View file

@ -159,19 +159,11 @@ class CakeSchema extends Object {
$options
));
$db =& ConnectionManager::getDataSource($connection);
$tables = $db->sources();
$currentTables = array_flip($db->sources());
if (empty($models)) {
$models = Configure::listObjects('model');
}
$Inflector = Inflector::getInstance();
$tablesWithoutModels = array_diff(array_map(array($Inflector, 'classify'), $tables), $models);
$modelsWithoutTables = array_diff($models, array_map(array($Inflector, 'classify'), $tables));
$models = am($tablesWithoutModels, array_diff($models, $modelsWithoutTables));
loadModel(null);
$tables = array();
foreach ($models as $model) {
if($model == 'ArosAco') {
@ -187,23 +179,38 @@ class CakeSchema extends Object {
if(class_exists(low($model))) {
$Object =& new $model();
$Object->setDataSource($connection);
if (is_object($Object)) {
if (is_object($Object) && isset($currentTables[$Object->table])) {
if(empty($tables[$Object->table])) {
$tables[$Object->table] = $this->__columns($Object);
$tables[$Object->table]['indexes'] = $db->index($Object);
unset($currentTables[$Object->table]);
}
if(!empty($Object->hasAndBelongsToMany)) {
foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) {
$class = $assocData['className'];
$tables[$Object->$Assoc->table] = $this->__columns($Object->$class);
$tables[$Object->$Assoc->table]['indexes'] = $db->index($Object->$class);
if (isset($assocData['with'])) {
$class = $assocData['with'];
} elseif ($assocData['_with']) {
$class = $assocData['_with'];
}
}
} else {
trigger_error('Schema generation error: model class ' . $class . ' not found', E_USER_WARNING);
if (is_object($Object->$class) && isset($currentTables[$Object->$class->table])) {
$tables[$Object->$class->table] = $this->__columns($Object->$class);
$tables[$Object->$class->table]['indexes'] = $db->index($Object->$class);
unset($currentTables[$Object->$class->table]);
}
}
}
}
}
}
if(!empty($currentTables)) {
foreach(array_flip($currentTables) as $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);
}
}
ksort($tables);
return compact('name', 'tables');
}
@ -249,7 +256,7 @@ class CakeSchema extends Object {
}
foreach ($tables as $table => $fields) {
if(!is_numeric($table)) {
if(!is_numeric($table) && $table !== 'missing') {
$out .= "\tvar \${$table} = array(\n";
if (is_array($fields)) {
$cols = array();
@ -317,6 +324,9 @@ class CakeSchema extends Object {
}
$tables = array();
foreach ($new as $table => $fields) {
if($table == 'missing') {
break;
}
if (!array_key_exists($table, $old)) {
$tables[$table]['add'] = $fields;
} else {

View file

@ -38,8 +38,20 @@ 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),
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
var $posts = array(
'id' => array('type'=>'integer', 'null' => false, 'default' => null, 'key' => 'primary', 'extra'=> 'auto_increment'),
'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),
@ -51,18 +63,6 @@ class MyAppSchema extends CakeSchema {
);
var $comments = array(
'id' => array('type'=>'integer', 'null' => false, 'default' => null, 'key' => 'primary', 'extra'=> 'auto_increment'),
'post_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
'user_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
'title' => array('type'=>'string', 'null' => false, 'length' => 100),
'comment' => array('type'=>'text', 'null' => false),
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
function setup($version) {
}
@ -73,24 +73,39 @@ 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),
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
var $posts = array(
'id' => array('type'=>'integer', 'null' => false, 'default' => null, 'key' => 'primary', 'extra'=> 'auto_increment'),
'author_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
'title' => array('type'=>'string', 'null' => false, 'default' => ''),
'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),
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
var $comments = array(
'id' => array('type'=>'integer', 'null' => false, 'default' => null, 'key' => 'primary', 'extra'=> 'auto_increment'),
'article_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
'user_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
'comment' => array('type'=>'text', 'null' => true),
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
'created' => array('type'=>'datetime', 'null' => true),
'updated' => array('type'=>'datetime', 'null' => true),
var $posts_tags = array(
'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
'tag_id' => array('type' => 'integer', 'null' => false, '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),
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
);
@ -109,8 +124,10 @@ class TestAppSchema extends CakeSchema {
*/
class SchemaPost extends CakeTestModel {
var $name = 'SchemaPost';
//var $useTable = 'posts';
var $useTable = 'posts';
var $hasMany = array('SchemaComment');
var $hasAndBelongsToMany = array('SchemaTag');
}
/**
* Short description for class.
@ -120,9 +137,20 @@ class SchemaPost extends CakeTestModel {
*/
class SchemaComment extends CakeTestModel {
var $name = 'SchemaComment';
//var $useTable = 'comments';
var $useTable = 'comments';
var $belongsTo = array('SchemaPost');
}
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model
*/
class SchemaTag extends CakeTestModel {
var $name = 'SchemaTag';
var $useTable = 'tags';
var $hasAndBelongsToMany = array('SchemaPost');
}
/**
* Short description for class.
*
@ -131,25 +159,29 @@ class SchemaComment extends CakeTestModel {
*/
class CakeSchemaTest extends CakeTestCase {
var $fixtures = array('core.post', 'core.comment', 'core.author');
var $fixtures = array('core.post', 'core.comment', 'core.author', 'core.tag', 'core.posts_tag');
function setUp() {
$this->Schema = new TestAppSchema();
}
function testSchemaGeneration() {
$read = $this->Schema->read(array('connection'=>'test_suite', 'name'=>'TestApp', 'models'=>array('post', 'comment')));
function testSchemaRead() {
$read = $this->Schema->read(array('connection'=>'test_suite', 'name'=>'TestApp', 'models'=>array('SchemaPost', 'SchemaComment', 'SchemaTag')));
unset($read['tables']['missing']);
$this->assertEqual($read['tables'], $this->Schema->tables);
}
$write = $this->Schema->write(array('name'=>'MyOtherApp', 'tables'=> $read['tables'], 'path'=> TMP . 'tests'));
function testSchemaWrite() {
$write = $this->Schema->write(array('name'=>'MyOtherApp', 'tables'=> $this->Schema->tables, 'path'=> TMP . 'tests'));
$file = file_get_contents(TMP . 'tests' . DS .'schema.php');
$this->assertEqual($write, $file);
require_once( TMP . 'tests' . DS .'schema.php');
$OtherSchema = new MyOtherAppSchema();
$this->assertEqual($read['tables'], $OtherSchema->tables);
$this->assertEqual($this->Schema->tables, $OtherSchema->tables);
}
function testSchemaComparison() {
@ -161,8 +193,8 @@ class CakeSchemaTest extends CakeTestCase {
'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, 'default'=>''), 'title'=>array('type'=> 'string', 'null'=> false, 'length'=> 100)),
'drop'=>array('article_id'=>array('type'=> 'integer', 'null'=> false, 'default'=>'')),
'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

@ -0,0 +1,50 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @subpackage cake.tests.fixtures
* @since CakePHP(tm) v 1.2.0.4667
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.fixtures
*/
class PostsTagFixture extends CakeTestFixture {
var $name = 'PostsTag';
var $fields = array(
'post_id' => array('type' => 'integer', 'null' => false),
'tag_id' => array('type' => 'integer', 'null' => false),
'indexes' => array('UNIQUE_TAG' => array('column'=> array('post_id', 'tag_id'), 'unique'=> 1))
);
var $records = array(
array('post_id' => 1, 'tag_id' => 1),
array('post_id' => 1, 'tag_id' => 2),
array('post_id' => 2, 'tag_id' => 1),
array('post_id' => 2, 'tag_id' => 3)
);
}
?>