Skip to content

Commit

Permalink
Implement simple schema import.
Browse files Browse the repository at this point in the history
I've removed model + record imports as models are no longer used to do
imports, and I feel record imports were an infrequently used feature.
  • Loading branch information
markstory committed May 26, 2013
1 parent 31bdbbb commit 2cd529a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
17 changes: 6 additions & 11 deletions lib/Cake/Test/TestCase/TestSuite/TestFixtureTest.php
Expand Up @@ -273,15 +273,13 @@ public function testInitModelTablePrefix() {
* @return void
*/
public function testImport() {
$this->markTestSkipped('Skipped for now as table prefixes need to be re-worked.');
Configure::write('App.namespace', 'TestApp');
$Fixture = new ImportFixture();
$Fixture->fields = $Fixture->records = null;
$Fixture->import = [
'model' => 'Post',
$fixture = new ImportFixture();
$fixture->fields = $fixture->records = null;
$fixture->import = [
'table' => 'posts',
'connection' => 'test',
];
$Fixture->init();
$fixture->init();

$expected = [
'id',
Expand All @@ -292,10 +290,7 @@ public function testImport() {
'created',
'updated',
];
$this->assertEquals($expected, array_keys($Fixture->fields));

$keys = array_flip(ClassRegistry::keys());
$this->assertFalse(array_key_exists('post', $keys));
$this->assertEquals($expected, $fixture->schema()->columns());
}

/**
Expand Down
50 changes: 27 additions & 23 deletions lib/Cake/TestSuite/Fixture/TestFixture.php
Expand Up @@ -21,6 +21,7 @@
use Cake\Database\Schema\Table;
use Cake\Error;
use Cake\Log\Log;
use Cake\Model\ConnectionManager;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;

Expand Down Expand Up @@ -119,29 +120,8 @@ public function init() {
$this->_schemaFromFields();
}

if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
$import = array_merge(
array('connection' => 'default', 'records' => false),
is_array($this->import) ? $this->import : array('model' => $this->import)
);

$this->Schema->connection = $import['connection'];
if (isset($import['table'])) {
$model = new Model(null, $import['table'], $import['connection']);
$db = ConnectionManager::getDataSource($import['connection']);
$db->cacheSources = false;
$model->useDbConfig = $import['connection'];
$model->name = Inflector::camelize(Inflector::singularize($import['table']));
$model->table = $import['table'];
$model->tablePrefix = $db->config['prefix'];
$this->fields = $model->schema(true);
$this->primaryKey = $model->primaryKey;
ClassRegistry::flush();
}

if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
$this->table = str_replace($db->config['prefix'], '', $this->table);
}
if (!empty($this->import)) {
$this->_schemaFromImport();
}
}

Expand Down Expand Up @@ -174,6 +154,30 @@ protected function _schemaFromFields() {
}
}

/**
* Build fixture schema from a table in another datasource.
*
* @return void
*/
protected function _schemaFromImport() {
if (!is_array($this->import)) {
return;
}
$import = array_merge(
array('connection' => 'default', 'records' => false, 'table' => null),
$this->import
);

if (empty($import['table'])) {
throw new Error\Exception(__d('cake_dev', 'Cannot import from undefined table.'));
}

$db = ConnectionManager::getDataSource($import['connection']);
$schemaCollection = $db->schemaCollection();
$table = $schemaCollection->describe($import['table']);
$this->_schema = $table;
}

/**
* Get/Set the Cake\Database\Schema\Table instance used by this fixture.
*
Expand Down

0 comments on commit 2cd529a

Please sign in to comment.