From 01b52fec4af8aad7a5234c54547191c6fd41d288 Mon Sep 17 00:00:00 2001 From: gwoo Date: Sat, 15 Dec 2007 02:06:10 +0000 Subject: [PATCH] updating schema, fixes #3718, added $cache param to Configure::listObjects(), updating view tests git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6157 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/console/libs/schema.php | 117 +++++++++++----------- cake/libs/configure.php | 17 ++-- cake/libs/model/schema.php | 5 +- cake/tests/cases/libs/view/theme.test.php | 21 ++-- cake/tests/cases/libs/view/view.test.php | 5 +- 5 files changed, 85 insertions(+), 80 deletions(-) diff --git a/cake/console/libs/schema.php b/cake/console/libs/schema.php index ccf0ff2cd..800ad8e7c 100644 --- a/cake/console/libs/schema.php +++ b/cake/console/libs/schema.php @@ -239,10 +239,9 @@ class SchemaShell extends Shell { $this->__update($Schema, $table); break; default: - $this->err('command not found'); + $this->err(__('command not found', true)); exit(); } - } /** * Create database from Schema object @@ -251,48 +250,17 @@ class SchemaShell extends Shell { * @access private */ function __create($Schema, $table = null) { - $options = array(); - $table = null; - $event = array_keys($Schema->tables); - if ($table) { - $event = array($table); - } - $errors = array(); - - Configure::write('debug', 2); $db =& ConnectionManager::getDataSource($this->Schema->connection); - $db->fullDebug = true; + $drop = $db->dropSchema($Schema, $table); $this->out($drop); - if ('y' == $this->in('Are you sure you want to drop tables and create your database?', array('y', 'n'), 'n')) { + if ('y' == $this->in(__('Are you sure you want to drop and create the tables?', true), array('y', 'n'), 'n')) { $create = $db->createSchema($Schema, $table); $this->out('Updating Database...'); - $contents = array_map('trim', explode(";", $drop. $create)); - foreach($contents as $sql) { - if ($this->__dry === true) { - $this->out($sql); - } else { - if (!empty($sql)) { - if (!$this->Schema->before(array('created'=> $event))) { - return false; - } - if (!$db->_execute($sql)) { - $errors[] = $db->lastError(); - } - $this->Schema->after(array('created'=> $event, 'errors'=> $errors)); - } - } - } - if (!empty($errors)) { - $this->err($errors); - } elseif ($this->__dry !== true) { - $this->out(__('Database updated', true)); - exit(); - } + $this->__run($drop . $create, 'create'); } - $this->out(__('End', true)); - exit(); + $this->out(__('End create.', true)); } /** * Update database with Schema object @@ -301,43 +269,73 @@ class SchemaShell extends Shell { * @access private */ function __update($Schema, $table = null) { + $db =& ConnectionManager::getDataSource($this->Schema->connection); + $this->out('Comparing Database to Schema...'); $Old = $this->Schema->read(); $compare = $this->Schema->compare($Old, $Schema); - if (isset($compare[$table])) { - $compare = array($table => $compare[$table]); + $contents = array(); + + if (!$table) { + foreach ($compare as $table => $changes) { + $contents[$table] = $db->alterSchema(array($table => $changes), $table); + } + } elseif (isset($compare[$table])) { + $contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table); } + if (empty($contents)) { + $this->out(__('Schema is up to date.', true)); + exit(); + } + + $this->out("\n" . __('The following statements will run.', true)); + $this->out(array_map('trim', $contents)); + if ('y' == $this->in(__('Are you sure you want to alter the tables?', true), array('y', 'n'), 'n')) { + $this->out(''); + $this->out(__('Updating Database...', true)); + $this->__run($contents, 'update'); + } + + $this->out(__('End update.', true)); + } +/** + * runs sql from __create() or __update() + * + * @access private + */ + function __run($contents, $event) { Configure::write('debug', 2); $db =& ConnectionManager::getDataSource($this->Schema->connection); $db->fullDebug = true; - $contents = $db->alterSchema($compare, $table); - if (empty($contents)) { - $this->out(__('Schema is up to date.', true)); - exit(); - } elseif ($this->__dry === true || 'y' == $this->in('Would you like to see the changes?', array('y', 'n'), 'y')) { - $this->out($contents); - } - if ($this->__dry !== true) { - if ('y' == $this->in('Are you sure you want to update your database?', array('y', 'n'), 'n')) { - $this->out('Updating Database...'); - if (!$this->Schema->before($compare)) { + $errors = array(); + foreach($contents as $table => $sql) { + if (empty($sql)) { + $this->out(sprintf(__('%s is up to date.', true), $table)); + } else { + if ($this->__dry === true) { + $this->out(sprintf(__('Dry run for %s :', true), $table)); + $this->out($sql); + } else { + if (!$this->Schema->before(array($event => $table))) { return false; } - if ($db->_execute($contents)) { - $this->Schema->after($compare); - $this->out(__('Database updated', true)); - } else { - $this->err(__('Database could not be updated', true)); - $this->err($db->lastError()); + if (!$db->_execute($sql)) { + $error = $db->lastError(); } - exit(); + + $this->Schema->after(array($event => $table, 'errors'=> $errors)); + + if (isset($error)) { + $this->out($errors); + } elseif ($this->__dry !== true) { + $this->out(sprintf(__('%s updated.', true), $table)); + } + } } } - $this->out(__('End', true)); - exit(); } /** * Displays help contents @@ -366,6 +364,5 @@ class SchemaShell extends Shell { $this->out(""); exit(); } - } ?> \ No newline at end of file diff --git a/cake/libs/configure.php b/cake/libs/configure.php index a48a5c9b2..3e72c179d 100644 --- a/cake/libs/configure.php +++ b/cake/libs/configure.php @@ -134,7 +134,7 @@ class Configure extends Object { * @return Configure instance * @access public */ - function listObjects($type, $path = null) { + function listObjects($type, $path = null, $cache = true) { $_this =& Configure::getInstance(); $objects = array(); $extension = false; @@ -147,11 +147,11 @@ class Configure extends Object { $name = $type . str_replace(DS, '', $path); } - if (empty($_this->__objects)) { + if (empty($_this->__objects) && $cache === true) { $_this->__objects = Cache::read('object_map', '_cake_core_'); } - if (empty($_this->__objects) || !isset($_this->__objects[$type])) { + if (empty($_this->__objects) || !isset($_this->__objects[$type]) || $cache !== true) { $Inflector =& Inflector::getInstance(); $types = array( 'model' => array('suffix' => '.php', 'base' => 'AppModel'), @@ -178,7 +178,7 @@ class Configure extends Object { $items = array(); foreach ((array)$path as $dir) { - if($type === 'file' || $type === 'class' || strpos($dir, $type) !== false) { + if ($type === 'file' || $type === 'class' || strpos($dir, $type) !== false) { $items = $_this->__list($dir, $types[$type]['suffix'], $extension); $objects = array_merge($items, array_diff($objects, $items)); } @@ -187,9 +187,14 @@ class Configure extends Object { if ($type !== 'file') { $objects = array_map(array(&$Inflector, 'camelize'), $objects); } - $_this->__objects[$name] = $objects; - $_this->__cache = true; + if ($cache === true) { + $_this->__objects[$name] = $objects; + $_this->__cache = true; + } else { + return $objects; + } } + return $_this->__objects[$name]; } /** diff --git a/cake/libs/model/schema.php b/cake/libs/model/schema.php index 4be373d16..43a28f722 100644 --- a/cake/libs/model/schema.php +++ b/cake/libs/model/schema.php @@ -113,7 +113,7 @@ class CakeSchema extends Object { } } - if(file_exists($this->path . DS . $file) && is_file($this->path . DS . $file)) { + if (file_exists($this->path . DS . $file) && is_file($this->path . DS . $file)) { $this->file = $file; } } @@ -194,7 +194,8 @@ class CakeSchema extends Object { } if (!is_array($models) && $models !== false) { - $models = Configure::listObjects('model'); + $appPaths = array_diff(Configure::read('modelPaths'), Configure::corePaths('model')); + $models = Configure::listObjects('model', $appPaths, false); } if (is_array($models)) { diff --git a/cake/tests/cases/libs/view/theme.test.php b/cake/tests/cases/libs/view/theme.test.php index b6a2c427f..92b3b6c21 100644 --- a/cake/tests/cases/libs/view/theme.test.php +++ b/cake/tests/cases/libs/view/theme.test.php @@ -28,8 +28,8 @@ */ uses('controller' . DS . 'controller', 'view'.DS.'theme'); -class PostsController extends Controller { - var $name = 'Posts'; +class ThemePostsController extends Controller { + var $name = 'ThemePosts'; function index() { $this->set('testData', 'Some test data'); $test2 = 'more data'; @@ -38,7 +38,7 @@ class PostsController extends Controller { } } -class TestView extends ThemeView { +class TestThemeView extends ThemeView { function renderElement($name, $params = array()) { return $name; @@ -62,14 +62,15 @@ class TestView extends ThemeView { * @package cake.tests * @subpackage cake.tests.cases.libs */ -class ViewTest extends UnitTestCase { +class ThemeViewTest extends UnitTestCase { function setUp() { Router::reload(); $this->Controller = new Controller(); - $this->PostsController = new PostsController(); + $this->PostsController = new ThemePostsController(); + $this->PostsController->viewPath = 'posts'; $this->PostsController->index(); - $this->ThemeView = new View($this->PostsController); + $this->ThemeView = new ThemeView($this->PostsController); } function testPluginGetTemplate() { @@ -79,7 +80,7 @@ class ViewTest extends UnitTestCase { $this->Controller->action = 'index'; $this->Controller->theme = 'test_plugin_theme'; - $ThemeView = new TestView($this->Controller); + $ThemeView = new TestThemeView($this->Controller); Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)); Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)); @@ -99,7 +100,7 @@ class ViewTest extends UnitTestCase { $this->Controller->action = 'display'; $this->Controller->params['pass'] = array('home'); - $ThemeView = new TestView($this->Controller); + $ThemeView = new TestThemeView($this->Controller); $ThemeView->theme = 'test_theme'; Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)); @@ -135,7 +136,7 @@ class ViewTest extends UnitTestCase { $this->Controller->action = 'display'; $this->Controller->params['pass'] = array('home'); - $ThemeView = new TestView($this->Controller); + $ThemeView = new TestThemeView($this->Controller); $expected = 'missingView'; $result = $ThemeView->getViewFileName('does_not_exist'); @@ -149,7 +150,7 @@ class ViewTest extends UnitTestCase { $this->Controller->viewPath = 'posts'; $this->Controller->layout = 'whatever'; - $ThemeView = new TestView($this->Controller); + $ThemeView = new TestThemeView($this->Controller); $expected = 'missingLayout'; $result = $ThemeView->getLayoutFileName(); $this->assertEqual($result, $expected); diff --git a/cake/tests/cases/libs/view/view.test.php b/cake/tests/cases/libs/view/view.test.php index 823c37a7d..9fc2c8a8d 100644 --- a/cake/tests/cases/libs/view/view.test.php +++ b/cake/tests/cases/libs/view/view.test.php @@ -28,7 +28,7 @@ */ uses('controller' . DS . 'controller', 'view'.DS.'view'); -class PostsController extends Controller { +class ViewPostsController extends Controller { var $name = 'Posts'; function index() { $this->set('testData', 'Some test data'); @@ -67,7 +67,8 @@ class ViewTest extends UnitTestCase { function setUp() { Router::reload(); $this->Controller = new Controller(); - $this->PostsController = new PostsController(); + $this->PostsController = new ViewPostsController(); + $this->PostsController->viewPath = 'posts'; $this->PostsController->index(); $this->View = new View($this->PostsController); }