Permalink
Browse files

Making generating schema for connections with prefixes work correctly…

…. The prefix needs to be removed so that when tables are created or updated they do not get the prefix added twice.

Moving tests around. Fixes #1180
  • Loading branch information...
markstory committed Oct 21, 2010
1 parent 4984172 commit 7c296211efdf18d8893fbc414baac71a3780c76c
Showing with 23 additions and 11 deletions.
  1. +9 −4 cake/libs/model/cake_schema.php
  2. +14 −7 cake/tests/cases/libs/model/cake_schema.test.php
@@ -253,13 +253,18 @@ function read($options = array()) {
}
if (is_object($Object) && $Object->useTable !== false) {
- $table = $db->fullTableName($Object, false);
- if (in_array($table, $currentTables)) {
- $key = array_search($table, $currentTables);
+ $fulltable = $table = $db->fullTableName($Object, false);
+ if ($prefix && strpos($table, $prefix) !== 0) {
+ continue;
+ }
+ $table = str_replace($prefix, '', $table);
+
+ if (in_array($fulltable, $currentTables)) {
+ $key = array_search($fulltable, $currentTables);
if (empty($tables[$table])) {
$tables[$table] = $this->__columns($Object);
$tables[$table]['indexes'] = $db->index($Object);
- $tables[$table]['tableParameters'] = $db->readTableParameters($table);
+ $tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
unset($currentTables[$key]);
}
if (!empty($Object->hasAndBelongsToMany)) {
@@ -559,13 +559,6 @@ function testSchemaRead() {
$this->Schema->tables['datatypes']['float_field']
);
- $db =& ConnectionManager::getDataSource('test_suite');
- $config = $db->config;
- $config['prefix'] = 'schema_test_prefix_';
- ConnectionManager::create('schema_prefix', $config);
- $read = $this->Schema->read(array('connection' => 'schema_prefix', 'models' => false));
- $this->assertTrue(empty($read['tables']));
-
$SchemaPost =& ClassRegistry::init('SchemaPost');
$SchemaPost->table = 'sts';
$SchemaPost->tablePrefix = 'po';
@@ -603,6 +596,20 @@ function testSchemaReadWithTablePrefix() {
}
+/**
+ * test reading schema with config prefix.
+ *
+ * @return void
+ */
+ function testSchemaReadWithConfigPrefix() {
+ $db =& ConnectionManager::getDataSource('test_suite');
+ $config = $db->config;
+ $config['prefix'] = 'schema_test_prefix_';
+ ConnectionManager::create('schema_prefix', $config);
+ $read = $this->Schema->read(array('connection' => 'schema_prefix', 'models' => false));
+ $this->assertTrue(empty($read['tables']));
+ }
+
/**
* test reading schema from plugins.
*

0 comments on commit 7c29621

Please sign in to comment.