Skip to content
This repository
Browse code

Enabling plugin.name schema file creation and addition.

Making CakeSchema::load() use plugin parameter, tests added.
Plugin schema file added to test_app.
  • Loading branch information...
commit 05f6099def804cdd0b60827bdab2d01d18d02fa1 1 parent c2a53d3
Mark Story authored October 04, 2009
13  cake/console/libs/schema.php
@@ -63,10 +63,12 @@ function startup() {
63 63
 			$name = $this->params['name'];
64 64
 			$this->params['file'] = Inflector::underscore($name);
65 65
 		}
66  
-		$path = $this->_getPath();
67 66
 		if (empty($this->params['file'])) {
68 67
 			$this->params['file'] = 'schema.php';
69 68
 		}
  69
+		if (!empty($this->params['path'])) {
  70
+			$path = $this->params['path'];
  71
+		}
70 72
 		if (strpos($this->params['file'], '.php') === false) {
71 73
 			$this->params['file'] .= '.php';
72 74
 		}
@@ -263,20 +265,23 @@ function update() {
263 265
  * @return void
264 266
  **/
265 267
 	function _loadSchema() {
266  
-		$name = null;
  268
+		$name = $plugin = null;
267 269
 		if (isset($this->args[0])) {
268 270
 			$name = $this->args[0];
269 271
 		}
270 272
 		if (isset($this->params['name'])) {
271 273
 			$name = $this->params['name'];
272 274
 		}
  275
+		if (strpos($name, '.') !== false) {
  276
+			list($plugin, $name) = explode('.', $name);
  277
+		}
273 278
 
274 279
 		if (isset($this->params['dry'])) {
275 280
 			$this->__dry = true;
276 281
 			$this->out(__('Performing a dry run.', true));
277 282
 		}
278 283
 
279  
-		$options = array('name' => $name);
  284
+		$options = array('name' => $name, 'plugin' => $plugin);
280 285
 		if (isset($this->params['s'])) {
281 286
 			$fileName = rtrim($this->Schema->file, '.php');
282 287
 			$options['file'] = $fileName . '_' . $this->params['s'] . '.php';
@@ -285,7 +290,7 @@ function _loadSchema() {
285 290
 		$Schema =& $this->Schema->load($options);
286 291
 
287 292
 		if (!$Schema) {
288  
-			$this->err(sprintf(__('%s could not be loaded', true), $this->Schema->file));
  293
+			$this->err(sprintf(__('%s could not be loaded', true), $this->Schema->path . DS . $this->Schema->file));
289 294
 			$this->_stop();
290 295
 		}
291 296
 		$table = null;
3  cake/libs/model/cake_schema.php
@@ -133,9 +133,10 @@ function _build($data) {
133 133
 				}
134 134
 			}
135 135
 		}
136  
-
137 136
 		if (file_exists($this->path . DS . $file) && is_file($this->path . DS . $file)) {
138 137
 			$this->file = $file;
  138
+		} elseif (!empty($this->plugin)) {
  139
+			$this->path = App::pluginPath($this->plugin) . 'config' . DS . 'schema';
139 140
 		}
140 141
 	}
141 142
 
32  cake/tests/cases/console/libs/schema.test.php
@@ -366,19 +366,35 @@ function testPluginParam() {
366 366
 			'connection' => 'test_suite'
367 367
 		);
368 368
 		$this->Shell->startup();
369  
-		$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'config' . DS . 'schema' . DS;
  369
+		$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'config' . DS . 'schema';
370 370
 		$this->assertEqual($this->Shell->Schema->path, $expected);
  371
+		
  372
+		App::build();
  373
+	}
371 374
 
372  
-		unset($this->Shell->Schema);
  375
+/**
  376
+ * test that using Plugin.name with write.
  377
+ *
  378
+ * @return void
  379
+ **/
  380
+	function testPluginDotSyntax() {
  381
+		App::build(array(
  382
+			'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  383
+		));
373 384
 		$this->Shell->params = array(
374  
-			'plugin' => 'TestPlugin',
375  
-			'connection' => 'test_suite',
376  
-			'path' => '/some/path'
  385
+			'connection' => 'test_suite'
377 386
 		);
  387
+		$this->Shell->args = array('TestPlugin.TestPluginApp');
378 388
 		$this->Shell->startup();
379  
-		$expected = '/some/path';
380  
-		$this->assertEqual($this->Shell->Schema->path, $expected);
381  
-	}
  389
+		$this->Shell->setReturnValue('in', 'y');
  390
+		$this->Shell->create();
382 391
 
  392
+		$db =& ConnectionManager::getDataSource('test_suite');
  393
+		$sources = $db->listSources();
  394
+		$this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
  395
+
  396
+		$db->execute('DROP TABLE ' . $db->config['prefix'] . 'acos');
  397
+		App::build();
  398
+	}
383 399
 }
384 400
 ?>
18  cake/tests/cases/libs/model/cake_schema.test.php
@@ -574,12 +574,28 @@ function testSchemaComparison() {
574 574
  * @return void
575 575
  */
576 576
 	function testSchemaLoading() {
577  
-		$Other = $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests'));
  577
+		$Other =& $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests'));
578 578
 		$this->assertEqual($Other->name, 'MyOtherApp');
579 579
 		$this->assertEqual($Other->tables, $this->Schema->tables);
580 580
 	}
581 581
 
582 582
 /**
  583
+ * test loading schema files inside of plugins.
  584
+ *
  585
+ * @return void
  586
+ **/
  587
+	function testSchemaLoadingFromPlugin() {
  588
+		App::build(array(
  589
+			'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  590
+		));
  591
+		$Other =& $this->Schema->load(array('name' => 'TestPluginApp', 'plugin' => 'TestPlugin'));
  592
+		$this->assertEqual($Other->name, 'TestPluginApp');
  593
+		$this->assertEqual(array_keys($Other->tables), array('acos'));
  594
+
  595
+		App::build();
  596
+	}
  597
+
  598
+/**
583 599
  * testSchemaCreateTable method
584 600
  *
585 601
  * @access public
39  cake/tests/test_app/plugins/test_plugin/config/schema/schema.php
... ...
@@ -0,0 +1,39 @@
  1
+<?php
  2
+/**
  3
+ * TestAppSchema file
  4
+ *
  5
+ * Use for testing the loading of schema files from plugins.
  6
+ *
  7
+ * PHP versions 4 and 5
  8
+ *
  9
+ * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
  10
+ * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  11
+ *
  12
+ * Licensed under The MIT License
  13
+ * Redistributions of files must retain the above copyright notice.
  14
+ *
  15
+ * @filesource
  16
+ * @copyright     Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  17
+ * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  18
+ * @package       cake
  19
+ * @subpackage    cake.app.config.sql
  20
+ * @since         CakePHP(tm) v 1.3
  21
+ * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
  22
+ */
  23
+class TestPluginAppSchema extends CakeSchema {
  24
+
  25
+	var $name = 'TestPluginApp';
  26
+
  27
+	var $acos = array(
  28
+		'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'length' => 10, 'key' => 'primary'),
  29
+		'parent_id' => array('type'=>'integer', 'null' => true, 'default' => NULL, 'length' => 10),
  30
+		'model' => array('type'=>'string', 'null' => true),
  31
+		'foreign_key' => array('type'=>'integer', 'null' => true, 'default' => NULL, 'length' => 10),
  32
+		'alias' => array('type'=>'string', 'null' => true),
  33
+		'lft' => array('type'=>'integer', 'null' => true, 'default' => NULL, 'length' => 10),
  34
+		'rght' => array('type'=>'integer', 'null' => true, 'default' => NULL, 'length' => 10),
  35
+		'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
  36
+	);
  37
+
  38
+}
  39
+?>

0 notes on commit 05f6099

Please sign in to comment.
Something went wrong with that request. Please try again.