mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-05 02:52:41 +00:00
Merge branch '1.3-console' into 1.3
This commit is contained in:
commit
a3f38c77dd
13 changed files with 719 additions and 237 deletions
|
@ -58,12 +58,21 @@ class SchemaShell extends Shell {
|
|||
* @access public
|
||||
*/
|
||||
function startup() {
|
||||
$name = $file = $path = $connection = null;
|
||||
$name = $file = $path = $connection = $plugin = null;
|
||||
if (!empty($this->params['name'])) {
|
||||
$name = $this->params['name'];
|
||||
} elseif (!empty($this->args[0])) {
|
||||
$name = $this->params['name'] = $this->args[0];
|
||||
}
|
||||
if (strpos($name, '.')) {
|
||||
list($this->params['plugin'], $this->params['name']) = explode('.', $name);
|
||||
$name = $this->params['name'];
|
||||
}
|
||||
|
||||
if ($name) {
|
||||
$this->params['file'] = Inflector::underscore($name);
|
||||
}
|
||||
$path = $this->_getPath();
|
||||
|
||||
if (empty($this->params['file'])) {
|
||||
$this->params['file'] = 'schema.php';
|
||||
}
|
||||
|
@ -72,28 +81,17 @@ class SchemaShell extends Shell {
|
|||
}
|
||||
$file = $this->params['file'];
|
||||
|
||||
if (!empty($this->params['path'])) {
|
||||
$path = $this->params['path'];
|
||||
}
|
||||
|
||||
if (!empty($this->params['connection'])) {
|
||||
$connection = $this->params['connection'];
|
||||
}
|
||||
|
||||
$this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the correct path for the params. Uses path, and plugin to find the correct path.
|
||||
* path param takes precedence over any plugins specified.
|
||||
*
|
||||
* @return mixed string to correct path or null.
|
||||
**/
|
||||
function _getPath() {
|
||||
if (!empty($this->params['path'])) {
|
||||
return $this->params['path'];
|
||||
}
|
||||
if (!empty($this->params['plugin'])) {
|
||||
$pluginPath = $this->_pluginPath($this->params['plugin']);
|
||||
return $pluginPath . 'config' . DS . 'schema' . DS;
|
||||
$plugin = $this->params['plugin'];
|
||||
}
|
||||
return null;
|
||||
$this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection', 'plugin'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -194,8 +192,10 @@ class SchemaShell extends Shell {
|
|||
|
||||
/**
|
||||
* Dump Schema object to sql file
|
||||
* if first arg == write, file will be written to sql file
|
||||
* or it will output sql
|
||||
* Use the `write` param to enable and control SQL file output location.
|
||||
* Simply using -write will write the sql file to the same dir as the schema file.
|
||||
* If -write contains a full path name the file will be saved there. If -write only
|
||||
* contains no DS, that will be used as the file name, in the same dir as the schema file.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
|
@ -206,11 +206,11 @@ class SchemaShell extends Shell {
|
|||
$this->err(__('Schema could not be loaded', true));
|
||||
$this->_stop();
|
||||
}
|
||||
if (!empty($this->args[0])) {
|
||||
if ($this->args[0] == 'write') {
|
||||
if (isset($this->params['write'])) {
|
||||
if ($this->params['write'] == 1) {
|
||||
$write = Inflector::underscore($this->Schema->name);
|
||||
} else {
|
||||
$write = $this->args[0];
|
||||
$write = $this->params['write'];
|
||||
}
|
||||
}
|
||||
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
||||
|
@ -221,7 +221,12 @@ class SchemaShell extends Shell {
|
|||
if (strpos($write, '.sql') === false) {
|
||||
$write .= '.sql';
|
||||
}
|
||||
$File = new File($this->Schema->path . DS . $write, true);
|
||||
if (strpos($write, DS) !== false) {
|
||||
$File =& new File($write, true);
|
||||
} else {
|
||||
$File =& new File($this->Schema->path . DS . $write, true);
|
||||
}
|
||||
|
||||
if ($File->write($contents)) {
|
||||
$this->out(sprintf(__('SQL dump file created in %s', true), $File->pwd()));
|
||||
$this->_stop();
|
||||
|
@ -235,62 +240,61 @@ class SchemaShell extends Shell {
|
|||
}
|
||||
|
||||
/**
|
||||
* Run database commands: create, update
|
||||
* Run database create commands. Alias for run create.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function run() {
|
||||
if (!isset($this->args[0])) {
|
||||
$this->err(__('Command not found', true));
|
||||
$this->_stop();
|
||||
}
|
||||
* @return void
|
||||
**/
|
||||
function create() {
|
||||
list($Schema, $table) = $this->_loadSchema();
|
||||
$this->__create($Schema, $table);
|
||||
}
|
||||
|
||||
$command = $this->args[0];
|
||||
/**
|
||||
* Run database create commands. Alias for run create.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function update() {
|
||||
list($Schema, $table) = $this->_loadSchema();
|
||||
$this->__update($Schema, $table);
|
||||
}
|
||||
|
||||
$this->Dispatch->shiftArgs();
|
||||
|
||||
$name = null;
|
||||
if (isset($this->args[0])) {
|
||||
$name = $this->args[0];
|
||||
}
|
||||
/**
|
||||
* Prepares the Schema objects for database operations.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function _loadSchema() {
|
||||
$name = $plugin = null;
|
||||
if (isset($this->params['name'])) {
|
||||
$name = $this->params['name'];
|
||||
}
|
||||
|
||||
if (isset($this->params['plugin'])) {
|
||||
$plugin = $this->params['plugin'];
|
||||
}
|
||||
|
||||
if (isset($this->params['dry'])) {
|
||||
$this->__dry = true;
|
||||
$this->out(__('Performing a dry run.', true));
|
||||
}
|
||||
|
||||
$options = array('name' => $name);
|
||||
$options = array('name' => $name, 'plugin' => $plugin);
|
||||
if (isset($this->params['s'])) {
|
||||
$fileName = rtrim($this->Schema->file, '.php');
|
||||
$options['file'] = $fileName . '_' . $this->params['s'] . '.php';
|
||||
}
|
||||
|
||||
$Schema = $this->Schema->load($options);
|
||||
$Schema =& $this->Schema->load($options);
|
||||
|
||||
if (!$Schema) {
|
||||
$this->err(sprintf(__('%s could not be loaded', true), $this->Schema->file));
|
||||
$this->err(sprintf(__('%s could not be loaded', true), $this->Schema->path . DS . $this->Schema->file));
|
||||
$this->_stop();
|
||||
}
|
||||
|
||||
$table = null;
|
||||
if (isset($this->args[1])) {
|
||||
$table = $this->args[1];
|
||||
}
|
||||
|
||||
switch ($command) {
|
||||
case 'create':
|
||||
$this->__create($Schema, $table);
|
||||
break;
|
||||
case 'update':
|
||||
$this->__update($Schema, $table);
|
||||
break;
|
||||
default:
|
||||
$this->err(__('Command not found', true));
|
||||
$this->_stop();
|
||||
}
|
||||
return array(&$Schema, $table);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -333,7 +337,6 @@ class SchemaShell extends Shell {
|
|||
$this->out(__('Creating table(s).', true));
|
||||
$this->__run($create, 'create', $Schema);
|
||||
}
|
||||
|
||||
$this->out(__('End create.', true));
|
||||
}
|
||||
|
||||
|
@ -392,7 +395,6 @@ class SchemaShell extends Shell {
|
|||
}
|
||||
Configure::write('debug', 2);
|
||||
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
||||
$db->fullDebug = true;
|
||||
|
||||
foreach ($contents as $table => $sql) {
|
||||
if (empty($sql)) {
|
||||
|
@ -428,27 +430,74 @@ class SchemaShell extends Shell {
|
|||
* @access public
|
||||
*/
|
||||
function help() {
|
||||
$this->out("The Schema Shell generates a schema object from");
|
||||
$this->out("the database and updates the database from the schema.");
|
||||
$this->hr();
|
||||
$this->out("Usage: cake schema <command> <arg1> <arg2>...");
|
||||
$this->hr();
|
||||
$this->out('Params:');
|
||||
$this->out("\n\t-connection <config>\n\t\tset db config <config>. uses 'default' if none is specified");
|
||||
$this->out("\n\t-path <dir>\n\t\tpath <dir> to read and write schema.php.\n\t\tdefault path: ". $this->Schema->path);
|
||||
$this->out("\n\t-name <name>\n\t\tclassname to use.");
|
||||
$this->out("\n\t-file <name>\n\t\tfile <name> to read and write.\n\t\tdefault file: ". $this->Schema->file);
|
||||
$this->out("\n\t-s <number>\n\t\tsnapshot <number> to use for run.");
|
||||
$this->out("\n\t-dry\n\t\tPerform a dry run on 'run' commands.\n\t\tQueries will be output to window instead of executed.");
|
||||
$this->out("\n\t-f\n\t\tforce 'generate' to create a new schema.");
|
||||
$this->out('Commands:');
|
||||
$this->out("\n\tschema help\n\t\tshows this help message.");
|
||||
$this->out("\n\tschema view\n\t\tread and output contents of schema file");
|
||||
$this->out("\n\tschema generate\n\t\treads from 'connection' writes to 'path'\n\t\tTo force generation of all tables into the schema, use the -f param.\n\t\tUse 'schema generate snapshot <number>' to generate snapshots\n\t\twhich you can use with the -s parameter in the other operations.");
|
||||
$this->out("\n\tschema dump <filename>\n\t\tDump database sql based on schema file to <filename>. \n\t\tIf <filename> is write, schema dump will be written to a file\n\t\tthat has the same name as the app directory.");
|
||||
$this->out("\n\tschema run create <schema> <table>\n\t\tDrop and create tables based on schema file\n\t\toptional <schema> arg for selecting schema name\n\t\toptional <table> arg for creating only one table\n\t\tpass the -s param with a number to use a snapshot\n\t\tTo see the changes, perform a dry run with the -dry param");
|
||||
$this->out("\n\tschema run update <schema> <table>\n\t\talter tables based on schema file\n\t\toptional <schema> arg for selecting schema name.\n\t\toptional <table> arg for altering only one table.\n\t\tTo use a snapshot, pass the -s param with the snapshot number\n\t\tTo see the changes, perform a dry run with the -dry param");
|
||||
$this->out();
|
||||
$help = <<<TEXT
|
||||
The Schema Shell generates a schema object from
|
||||
the database and updates the database from the schema.
|
||||
---------------------------------------------------------------
|
||||
Usage: cake schema <command> <arg1> <arg2>...
|
||||
---------------------------------------------------------------
|
||||
Params:
|
||||
-connection <config>
|
||||
set db config <config>. uses 'default' if none is specified
|
||||
|
||||
-path <dir>
|
||||
path <dir> to read and write schema.php.
|
||||
default path: {$this->Schema->path}
|
||||
|
||||
-name <name>
|
||||
Classname to use. If <name> is Plugin.className, it will
|
||||
set the plugin and name params.
|
||||
|
||||
-file <name>
|
||||
file <name> to read and write.
|
||||
default file: {$this->Schema->file}
|
||||
|
||||
-s <number>
|
||||
snapshot <number> to use for run.
|
||||
|
||||
-dry
|
||||
Perform a dry run on create + update commands.
|
||||
Queries will be output to window instead of executed.
|
||||
|
||||
-f
|
||||
force 'generate' to create a new schema.
|
||||
|
||||
-plugin
|
||||
Indicate the plugin to use.
|
||||
|
||||
Commands:
|
||||
|
||||
schema help
|
||||
shows this help message.
|
||||
|
||||
schema view <name>
|
||||
read and output contents of schema file.
|
||||
|
||||
schema generate
|
||||
reads from 'connection' writes to 'path'
|
||||
To force generation of all tables into the schema, use the -f param.
|
||||
Use 'schema generate snapshot <number>' to generate snapshots
|
||||
which you can use with the -s parameter in the other operations.
|
||||
|
||||
schema dump <name>
|
||||
Dump database sql based on schema file to stdout.
|
||||
If you use the `-write` param is used a .sql will be generated.
|
||||
If `-write` is a filename, then that file name will be generate.
|
||||
If `-write` is a full path, the schema will be written there.
|
||||
|
||||
schema create <name> <table>
|
||||
Drop and create tables based on schema file
|
||||
optional <table> argument can be used to create only a single
|
||||
table in the schema. Pass the -s param with a number to use a snapshot.
|
||||
Use the `-dry` param to preview the changes.
|
||||
|
||||
schema update <name> <table>
|
||||
Alter the tables based on schema file. Optional <table>
|
||||
parameter will only update one table.
|
||||
To use a snapshot pass the `-s` param with the snapshot number.
|
||||
To preview the changes that will be done use `-dry`.
|
||||
TEXT;
|
||||
$this->out($help);
|
||||
$this->_stop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -456,6 +456,12 @@ class ControllerTask extends Shell {
|
|||
$this->hr();
|
||||
$this->out("Usage: cake bake controller <arg1> <arg2>...");
|
||||
$this->hr();
|
||||
$this->out('Arguments:');
|
||||
$this->out();
|
||||
$this->out("<name>");
|
||||
$this->out("\tName of the controller to bake. Can use Plugin.name");
|
||||
$this->out("\tas a shortcut for plugin baking.");
|
||||
$this->out();
|
||||
$this->out('Commands:');
|
||||
$this->out();
|
||||
$this->out("controller <name>");
|
||||
|
|
|
@ -256,36 +256,7 @@ class FixtureTask extends Shell {
|
|||
* @return string fields definitions
|
||||
**/
|
||||
function _generateSchema($tableInfo) {
|
||||
$cols = array();
|
||||
$out = "array(\n";
|
||||
foreach ($tableInfo as $field => $fieldInfo) {
|
||||
if (is_array($fieldInfo)) {
|
||||
if (!in_array($field, array('indexes', 'tableParameters'))) {
|
||||
$col = "\t\t'{$field}' => array('type'=>'" . $fieldInfo['type'] . "', ";
|
||||
$col .= join(', ', $this->_Schema->__values($fieldInfo));
|
||||
} elseif ($field == 'indexes') {
|
||||
$col = "\t\t'indexes' => array(";
|
||||
$props = array();
|
||||
foreach ((array)$fieldInfo as $key => $index) {
|
||||
$props[] = "'{$key}' => array(".join(', ', $this->_Schema->__values($index)).")";
|
||||
}
|
||||
$col .= join(', ', $props);
|
||||
} elseif ($field == 'tableParameters') {
|
||||
//@todo add charset, collate and engine here
|
||||
$col = "\t\t'tableParameters' => array(";
|
||||
$props = array();
|
||||
foreach ((array)$fieldInfo as $key => $param) {
|
||||
$props[] = "'{$key}' => '$param'";
|
||||
}
|
||||
$col .= join(', ', $props);
|
||||
}
|
||||
$col .= ")";
|
||||
$cols[] = $col;
|
||||
}
|
||||
}
|
||||
$out .= join(",\n", $cols);
|
||||
$out .= "\n\t)";
|
||||
return $out;
|
||||
return $this->_Schema->generateTable('table', $tableInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -420,6 +391,12 @@ class FixtureTask extends Shell {
|
|||
$this->hr();
|
||||
$this->out("Usage: cake bake fixture <arg1> <params>");
|
||||
$this->hr();
|
||||
$this->out('Arguments:');
|
||||
$this->out();
|
||||
$this->out("<name>");
|
||||
$this->out("\tName of the fixture to bake. Can use Plugin.name");
|
||||
$this->out("\tas a shortcut for plugin baking.");
|
||||
$this->out();
|
||||
$this->out('Commands:');
|
||||
$this->out("\nfixture <name>\n\tbakes fixture with specified name.");
|
||||
$this->out("\nfixture all\n\tbakes all fixtures.");
|
||||
|
|
|
@ -883,6 +883,12 @@ class ModelTask extends Shell {
|
|||
$this->hr();
|
||||
$this->out("Usage: cake bake model <arg1>");
|
||||
$this->hr();
|
||||
$this->out('Arguments:');
|
||||
$this->out();
|
||||
$this->out("<name>");
|
||||
$this->out("\tName of the model to bake. Can use Plugin.name");
|
||||
$this->out("\tas a shortcut for plugin baking.");
|
||||
$this->out();
|
||||
$this->out('Commands:');
|
||||
$this->out();
|
||||
$this->out("model");
|
||||
|
|
|
@ -426,6 +426,12 @@ class ViewTask extends Shell {
|
|||
$this->hr();
|
||||
$this->out("Usage: cake bake view <arg1> <arg2>...");
|
||||
$this->hr();
|
||||
$this->out('Arguments:');
|
||||
$this->out();
|
||||
$this->out("<controller>");
|
||||
$this->out("\tName of the controller views to bake. Can use Plugin.name");
|
||||
$this->out("\tas a shortcut for plugin baking.");
|
||||
$this->out();
|
||||
$this->out('Commands:');
|
||||
$this->out();
|
||||
$this->out("view <controller>");
|
||||
|
|
|
@ -668,6 +668,23 @@ class App extends Object {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path that a plugin is on. Searches through the defined plugin paths.
|
||||
*
|
||||
* @param string $plugin CamelCased plugin name to find the path of.
|
||||
* @return string full path to the plugin.
|
||||
**/
|
||||
function pluginPath($plugin) {
|
||||
$_this =& App::getInstance();
|
||||
$pluginDir = Inflector::underscore($plugin);
|
||||
foreach ($_this->plugins as $path) {
|
||||
if (is_dir($path . $pluginDir)) {
|
||||
return $path . $pluginDir . DS ;
|
||||
}
|
||||
}
|
||||
return $_this->plugins[0] . $pluginDir . DS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a key/value list of all paths where core libs are found.
|
||||
* Passing $type only returns the values for a given value of $key.
|
||||
|
|
|
@ -1,29 +1,24 @@
|
|||
<?php
|
||||
/* SVN FILE: $Id$ */
|
||||
|
||||
/**
|
||||
* Schema database management for CakePHP.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
||||
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @filesource
|
||||
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.model
|
||||
* @since CakePHP(tm) v 1.2.0.5550
|
||||
* @version $Revision$
|
||||
* @modifiedby $LastChangedBy$
|
||||
* @lastmodified $Date$
|
||||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
||||
*/
|
||||
App::import('Model', 'ConnectionManager');
|
||||
App::import('Core', array('Model', 'ConnectionManager'));
|
||||
|
||||
/**
|
||||
* Base Class for Schema management
|
||||
|
@ -65,6 +60,13 @@ class CakeSchema extends Object {
|
|||
*/
|
||||
var $connection = 'default';
|
||||
|
||||
/**
|
||||
* plugin name.
|
||||
*
|
||||
* @var string
|
||||
**/
|
||||
var $plugin = null;
|
||||
|
||||
/**
|
||||
* Set of tables
|
||||
*
|
||||
|
@ -84,6 +86,9 @@ class CakeSchema extends Object {
|
|||
if (empty($options['name'])) {
|
||||
$this->name = preg_replace('/schema$/i', '', get_class($this));
|
||||
}
|
||||
if (!empty($options['plugin'])) {
|
||||
$this->plugin = $options['plugin'];
|
||||
}
|
||||
|
||||
if (strtolower($this->name) === 'cake') {
|
||||
$this->name = Inflector::camelize(Inflector::slug(Configure::read('App.dir')));
|
||||
|
@ -112,7 +117,7 @@ class CakeSchema extends Object {
|
|||
$file = null;
|
||||
foreach ($data as $key => $val) {
|
||||
if (!empty($val)) {
|
||||
if (!in_array($key, array('name', 'path', 'file', 'connection', 'tables', '_log'))) {
|
||||
if (!in_array($key, array('plugin', 'name', 'path', 'file', 'connection', 'tables', '_log'))) {
|
||||
$this->tables[$key] = $val;
|
||||
unset($this->{$key});
|
||||
} elseif ($key !== 'tables') {
|
||||
|
@ -123,9 +128,10 @@ class CakeSchema extends Object {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (file_exists($this->path . DS . $file) && is_file($this->path . DS . $file)) {
|
||||
$this->file = $file;
|
||||
} elseif (!empty($this->plugin)) {
|
||||
$this->path = App::pluginPath($this->plugin) . 'config' . DS . 'schema';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -207,6 +213,9 @@ class CakeSchema extends Object {
|
|||
$db =& ConnectionManager::getDataSource($connection);
|
||||
|
||||
App::import('Model', 'AppModel');
|
||||
if (isset($this->plugin)) {
|
||||
App::import('Model', Inflector::camelize($this->plugin) . 'AppModel');
|
||||
}
|
||||
|
||||
$tables = array();
|
||||
$currentTables = $db->listSources();
|
||||
|
@ -217,11 +226,18 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
if (!is_array($models) && $models !== false) {
|
||||
$models = App::objects('model');
|
||||
if (isset($this->plugin)) {
|
||||
$models = App::objects('model', App::pluginPath($this->plugin) . 'models' . DS, false);
|
||||
} else {
|
||||
$models = App::objects('model');
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($models)) {
|
||||
foreach ($models as $model) {
|
||||
if (isset($this->plugin)) {
|
||||
$model = $this->plugin . '.' . $model;
|
||||
}
|
||||
if (PHP5) {
|
||||
$Object = ClassRegistry::init(array('class' => $model, 'ds' => $connection));
|
||||
} else {
|
||||
|
@ -230,7 +246,7 @@ class CakeSchema extends Object {
|
|||
|
||||
if (is_object($Object) && $Object->useTable !== false) {
|
||||
$Object->setDataSource($connection);
|
||||
$table = $db->fullTableName($Object, false);
|
||||
$table = $db->fullTableName($Object->useTable, false);
|
||||
|
||||
if (in_array($table, $currentTables)) {
|
||||
$key = array_search($table, $currentTables);
|
||||
|
@ -345,35 +361,10 @@ class CakeSchema extends Object {
|
|||
|
||||
foreach ($tables as $table => $fields) {
|
||||
if (!is_numeric($table) && $table !== 'missing') {
|
||||
$out .= "\tvar \${$table} = array(\n";
|
||||
if (is_array($fields)) {
|
||||
$cols = array();
|
||||
foreach ($fields as $field => $value) {
|
||||
if ($field != 'indexes') {
|
||||
if (is_string($value)) {
|
||||
$type = $value;
|
||||
$value = array('type'=> $type);
|
||||
}
|
||||
$col = "\t\t'{$field}' => array('type' => '" . $value['type'] . "', ";
|
||||
unset($value['type']);
|
||||
$col .= join(', ', $this->__values($value));
|
||||
} else {
|
||||
$col = "\t\t'indexes' => array(";
|
||||
$props = array();
|
||||
foreach ((array)$value as $key => $index) {
|
||||
$props[] = "'{$key}' => array(" . join(', ', $this->__values($index)) . ")";
|
||||
}
|
||||
$col .= join(', ', $props);
|
||||
}
|
||||
$col .= ")";
|
||||
$cols[] = $col;
|
||||
}
|
||||
$out .= join(",\n", $cols);
|
||||
}
|
||||
$out .= "\n\t);\n";
|
||||
$out .= $this->generateTable($table, $fields);
|
||||
}
|
||||
}
|
||||
$out .="}\n";
|
||||
$out .= "}\n";
|
||||
|
||||
$File =& new File($path . DS . $file, true);
|
||||
$header = '$Id';
|
||||
|
@ -385,6 +376,52 @@ class CakeSchema extends Object {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the code for a table. Takes a table name and $fields array
|
||||
* Returns a completed variable declaration to be used in schema classes
|
||||
*
|
||||
* @param string $table Table name you want returned.
|
||||
* @param array $fields Array of field information to generate the table with.
|
||||
* @return string Variable declaration for a schema class
|
||||
**/
|
||||
function generateTable($table, $fields) {
|
||||
$out = "\tvar \${$table} = array(\n";
|
||||
if (is_array($fields)) {
|
||||
$cols = array();
|
||||
foreach ($fields as $field => $value) {
|
||||
if ($field != 'indexes' && $field != 'tableParameters') {
|
||||
if (is_string($value)) {
|
||||
$type = $value;
|
||||
$value = array('type'=> $type);
|
||||
}
|
||||
$col = "\t\t'{$field}' => array('type' => '" . $value['type'] . "', ";
|
||||
unset($value['type']);
|
||||
$col .= join(', ', $this->__values($value));
|
||||
} elseif ($field == 'indexes') {
|
||||
$col = "\t\t'indexes' => array(";
|
||||
$props = array();
|
||||
foreach ((array)$value as $key => $index) {
|
||||
$props[] = "'{$key}' => array(" . join(', ', $this->__values($index)) . ")";
|
||||
}
|
||||
$col .= join(', ', $props);
|
||||
} elseif ($field == 'tableParameters') {
|
||||
//@todo add charset, collate and engine here
|
||||
$col = "\t\t'tableParameters' => array(";
|
||||
$props = array();
|
||||
foreach ((array)$value as $key => $param) {
|
||||
$props[] = "'{$key}' => '$param'";
|
||||
}
|
||||
$col .= join(', ', $props);
|
||||
}
|
||||
$col .= ")";
|
||||
$cols[] = $col;
|
||||
}
|
||||
$out .= join(",\n", $cols);
|
||||
}
|
||||
$out .= "\n\t);\n";
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two sets of schemas
|
||||
*
|
||||
|
@ -395,7 +432,7 @@ class CakeSchema extends Object {
|
|||
*/
|
||||
function compare($old, $new = null) {
|
||||
if (empty($new)) {
|
||||
$new = $this;
|
||||
$new =& $this;
|
||||
}
|
||||
if (is_array($new)) {
|
||||
if (isset($new['tables'])) {
|
||||
|
@ -432,7 +469,7 @@ class CakeSchema extends Object {
|
|||
foreach ($fields as $field => $value) {
|
||||
if (isset($old[$table][$field])) {
|
||||
$diff = array_diff_assoc($value, $old[$table][$field]);
|
||||
if (!empty($diff) && $field !== 'indexes') {
|
||||
if (!empty($diff) && $field !== 'indexes' && $field !== 'tableParameters') {
|
||||
$tables[$table]['change'][$field] = array_merge($old[$table][$field], $diff);
|
||||
}
|
||||
}
|
||||
|
@ -449,11 +486,19 @@ class CakeSchema extends Object {
|
|||
|
||||
if (isset($old[$table]['indexes']) && isset($new[$table]['indexes'])) {
|
||||
$diff = $this->_compareIndexes($new[$table]['indexes'], $old[$table]['indexes']);
|
||||
if ($diff) {
|
||||
if ($diff && isset($diff['drop'])) {
|
||||
$tables[$table]['drop']['indexes'] = $diff['drop'];
|
||||
}
|
||||
if ($diff && isset($diff['add'])) {
|
||||
$tables[$table]['add']['indexes'] = $diff['add'];
|
||||
}
|
||||
}
|
||||
if (isset($old[$table]['tableParameters']) && isset($new[$table]['tableParameters'])) {
|
||||
$diff = $this->_compareTableParameters($new[$table]['tableParameters'], $old[$table]['tableParameters']);
|
||||
if ($diff) {
|
||||
$tables[$table]['change']['tableParameters'] = $diff;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $tables;
|
||||
}
|
||||
|
@ -523,6 +568,21 @@ class CakeSchema extends Object {
|
|||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two schema files table Parameters
|
||||
*
|
||||
* @param array $new New indexes
|
||||
* @param array $old Old indexes
|
||||
* @return mixed False on failure, or an array of parameters to add & drop.
|
||||
**/
|
||||
function _compareTableParameters($new, $old) {
|
||||
if (!is_array($new) || !is_array($old)) {
|
||||
return false;
|
||||
}
|
||||
$change = array_diff_assoc($new, $old);
|
||||
return $change;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two schema indexes
|
||||
*
|
||||
|
|
|
@ -248,7 +248,7 @@ class DboMysqlBase extends DboSource {
|
|||
$out = '';
|
||||
$colList = array();
|
||||
foreach ($compare as $curTable => $types) {
|
||||
$indexes = array();
|
||||
$indexes = $tableParameters = array();
|
||||
if (!$table || $table == $curTable) {
|
||||
$out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
|
||||
foreach ($types as $type => $column) {
|
||||
|
@ -256,13 +256,17 @@ class DboMysqlBase extends DboSource {
|
|||
$indexes[$type] = $column['indexes'];
|
||||
unset($column['indexes']);
|
||||
}
|
||||
if (isset($column['tableParameters'])) {
|
||||
$tableParameters[$type] = $column['tableParameters'];
|
||||
unset($column['tableParameters']);
|
||||
}
|
||||
switch ($type) {
|
||||
case 'add':
|
||||
foreach ($column as $field => $col) {
|
||||
$col['name'] = $field;
|
||||
$alter = 'ADD '.$this->buildColumn($col);
|
||||
$alter = 'ADD ' . $this->buildColumn($col);
|
||||
if (isset($col['after'])) {
|
||||
$alter .= ' AFTER '. $this->name($col['after']);
|
||||
$alter .= ' AFTER ' . $this->name($col['after']);
|
||||
}
|
||||
$colList[] = $alter;
|
||||
}
|
||||
|
@ -270,7 +274,7 @@ class DboMysqlBase extends DboSource {
|
|||
case 'drop':
|
||||
foreach ($column as $field => $col) {
|
||||
$col['name'] = $field;
|
||||
$colList[] = 'DROP '.$this->name($field);
|
||||
$colList[] = 'DROP ' . $this->name($field);
|
||||
}
|
||||
break;
|
||||
case 'change':
|
||||
|
@ -278,12 +282,13 @@ class DboMysqlBase extends DboSource {
|
|||
if (!isset($col['name'])) {
|
||||
$col['name'] = $field;
|
||||
}
|
||||
$colList[] = 'CHANGE '. $this->name($field).' '.$this->buildColumn($col);
|
||||
$colList[] = 'CHANGE ' . $this->name($field) . ' ' . $this->buildColumn($col);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
$colList = array_merge($colList, $this->_alterIndexes($curTable, $indexes));
|
||||
$colList = array_merge($colList, $this->_alterTableParameters($curTable, $tableParameters));
|
||||
$out .= "\t" . join(",\n\t", $colList) . ";\n\n";
|
||||
}
|
||||
}
|
||||
|
@ -312,6 +317,21 @@ class DboMysqlBase extends DboSource {
|
|||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate MySQL table parameter alteration statementes for a table.
|
||||
*
|
||||
* @param string $table Table to alter parameters for.
|
||||
* @param array $parameters Parameters to add & drop.
|
||||
* @return array Array of table property alteration statementes.
|
||||
* @todo Implement this method.
|
||||
**/
|
||||
function _alterTableParameters($table, $parameters) {
|
||||
if (isset($parameters['change'])) {
|
||||
return $this->buildTableParameters($parameters['change']);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate MySQL index alteration statements for a table.
|
||||
*
|
||||
|
|
|
@ -116,7 +116,7 @@ class SchemaShellTestSchema extends CakeSchema {
|
|||
*/
|
||||
class SchemaShellTest extends CakeTestCase {
|
||||
|
||||
var $fixtures = array('core.article', 'core.user');
|
||||
var $fixtures = array('core.article', 'core.user', 'core.post', 'core.auth_user');
|
||||
/**
|
||||
* startTest method
|
||||
*
|
||||
|
@ -184,24 +184,45 @@ class SchemaShellTest extends CakeTestCase {
|
|||
$this->Shell->Schema->path = APP . 'config' . DS . 'schema';
|
||||
$this->Shell->params['file'] = 'i18n.php';
|
||||
$this->Shell->expectOnce('_stop');
|
||||
$this->Shell->expectOnce('out');
|
||||
$this->Shell->view();
|
||||
}
|
||||
|
||||
/**
|
||||
* test that view() can find plugin schema files.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testViewWithPlugins() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
$this->Shell->args = array('TestPlugin.schema');
|
||||
$this->Shell->startup();
|
||||
$this->Shell->expectCallCount('_stop', 2);
|
||||
$this->Shell->expectCallCount('out', 2);
|
||||
$this->Shell->view();
|
||||
|
||||
$this->Shell->args = array();
|
||||
$this->Shell->params = array('plugin' => 'TestPlugin');
|
||||
$this->Shell->startup();
|
||||
$this->Shell->view();
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* test dump() with sql file generation
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testDumpWithFileWriting() {
|
||||
$file =& new File(APP . 'config' . DS . 'schema' . DS . 'i18n.php');
|
||||
$contents = $file->read();
|
||||
$file =& new File(TMP . 'tests' . DS . 'i18n.php');
|
||||
$file->write($contents);
|
||||
|
||||
$this->Shell->params = array('name' => 'i18n');
|
||||
$this->Shell->args = array('write');
|
||||
$this->Shell->params = array(
|
||||
'name' => 'i18n',
|
||||
'write' => TMP . 'tests' . DS . 'i18n.sql'
|
||||
);
|
||||
$this->Shell->expectOnce('_stop');
|
||||
$this->Shell->startup();
|
||||
$this->Shell->Schema->path = TMP . 'tests';
|
||||
$this->Shell->dump();
|
||||
|
||||
$sql =& new File(TMP . 'tests' . DS . 'i18n.sql');
|
||||
|
@ -216,7 +237,35 @@ class SchemaShellTest extends CakeTestCase {
|
|||
$this->assertPattern('/content/', $contents);
|
||||
|
||||
$sql->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* test that dump() can find and work with plugin schema files.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testDumpFileWritingWithPlugins() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
$this->Shell->args = array('TestPlugin.TestPluginApp');
|
||||
$this->Shell->params = array(
|
||||
'connection' => 'test_suite',
|
||||
'write' => TMP . 'tests' . DS . 'dump_test.sql'
|
||||
);
|
||||
$this->Shell->startup();
|
||||
$this->Shell->expectOnce('_stop');
|
||||
$this->Shell->dump();
|
||||
|
||||
$file =& new File(TMP . 'tests' . DS . 'dump_test.sql');
|
||||
$contents = $file->read();
|
||||
|
||||
$this->assertPattern('/CREATE TABLE `acos`/', $contents);
|
||||
$this->assertPattern('/id/', $contents);
|
||||
$this->assertPattern('/model/', $contents);
|
||||
|
||||
$file->delete();
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -281,21 +330,50 @@ class SchemaShellTest extends CakeTestCase {
|
|||
unlink(TMP . 'schema.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* test that generate() can read plugin dirs and generate schema files for the models
|
||||
* in a plugin.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testGenerateWithPlugins() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
$this->Shell->params = array(
|
||||
'plugin' => 'TestPlugin',
|
||||
'connection' => 'test_suite'
|
||||
);
|
||||
$this->Shell->startup();
|
||||
$this->Shell->Schema->path = TMP . 'tests' . DS;
|
||||
|
||||
$this->Shell->generate();
|
||||
$file =& new File(TMP . 'tests' . DS . 'schema.php');
|
||||
$contents = $file->read();
|
||||
|
||||
$this->assertPattern('/var \$posts/', $contents);
|
||||
$this->assertPattern('/var \$auth_users/', $contents);
|
||||
$this->assertNoPattern('/var \$users/', $contents);
|
||||
$this->assertNoPattern('/var \$articles/', $contents);
|
||||
|
||||
$file->delete();
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test schema run create with no table args.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testRunCreateNoArgs() {
|
||||
function testCreateNoArgs() {
|
||||
$this->Shell->params = array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'i18n',
|
||||
'path' => APP . 'config' . DS . 'sql'
|
||||
);
|
||||
$this->Shell->args = array('create');
|
||||
$this->Shell->args = array('i18n');
|
||||
$this->Shell->startup();
|
||||
$this->Shell->setReturnValue('in', 'y');
|
||||
$this->Shell->run();
|
||||
$this->Shell->create();
|
||||
|
||||
$db =& ConnectionManager::getDataSource('test_suite');
|
||||
$sources = $db->listSources();
|
||||
|
@ -310,16 +388,16 @@ class SchemaShellTest extends CakeTestCase {
|
|||
*
|
||||
* @return void
|
||||
**/
|
||||
function testRunCreateWithTableArgs() {
|
||||
function testCreateWithTableArgs() {
|
||||
$this->Shell->params = array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'DbAcl',
|
||||
'path' => APP . 'config' . DS . 'schema'
|
||||
);
|
||||
$this->Shell->args = array('create', 'acos');
|
||||
$this->Shell->args = array('DbAcl', 'acos');
|
||||
$this->Shell->startup();
|
||||
$this->Shell->setReturnValue('in', 'y');
|
||||
$this->Shell->run();
|
||||
$this->Shell->create();
|
||||
|
||||
$db =& ConnectionManager::getDataSource('test_suite');
|
||||
$sources = $db->listSources();
|
||||
|
@ -335,16 +413,15 @@ class SchemaShellTest extends CakeTestCase {
|
|||
*
|
||||
* @return void
|
||||
**/
|
||||
function testRunUpdateWithTable() {
|
||||
function testUpdateWithTable() {
|
||||
$this->Shell->params = array(
|
||||
'name' => 'SchemaShellTest',
|
||||
'connection' => 'test_suite',
|
||||
'f' => true
|
||||
);
|
||||
$this->Shell->args = array('update', 'articles');
|
||||
$this->Shell->args = array('SchemaShellTest', 'articles');
|
||||
$this->Shell->startup();
|
||||
$this->Shell->setReturnValue('in', 'y');
|
||||
$this->Shell->run();
|
||||
$this->Shell->update();
|
||||
|
||||
$article =& new Model(array('name' => 'Article', 'ds' => 'test_suite'));
|
||||
$fields = $article->schema();
|
||||
|
@ -368,19 +445,35 @@ class SchemaShellTest extends CakeTestCase {
|
|||
'connection' => 'test_suite'
|
||||
);
|
||||
$this->Shell->startup();
|
||||
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'config' . DS . 'schema' . DS;
|
||||
$this->assertEqual($this->Shell->Schema->path, $expected);
|
||||
|
||||
unset($this->Shell->Schema);
|
||||
$this->Shell->params = array(
|
||||
'plugin' => 'TestPlugin',
|
||||
'connection' => 'test_suite',
|
||||
'path' => '/some/path'
|
||||
);
|
||||
$this->Shell->startup();
|
||||
$expected = '/some/path';
|
||||
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'config' . DS . 'schema';
|
||||
$this->assertEqual($this->Shell->Schema->path, $expected);
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* test that using Plugin.name with write.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testPluginDotSyntaxWithCreate() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
$this->Shell->params = array(
|
||||
'connection' => 'test_suite'
|
||||
);
|
||||
$this->Shell->args = array('TestPlugin.TestPluginApp');
|
||||
$this->Shell->startup();
|
||||
$this->Shell->setReturnValue('in', 'y');
|
||||
$this->Shell->create();
|
||||
|
||||
$db =& ConnectionManager::getDataSource('test_suite');
|
||||
$sources = $db->listSources();
|
||||
$this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
|
||||
|
||||
$db->execute('DROP TABLE ' . $db->config['prefix'] . 'acos');
|
||||
App::build();
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -1,6 +1,4 @@
|
|||
<?php
|
||||
/* SVN FILE: $Id$ */
|
||||
|
||||
/**
|
||||
* Test for Schema database management
|
||||
*
|
||||
|
@ -8,23 +6,20 @@
|
|||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
||||
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
*
|
||||
* Licensed under The Open Group Test Suite License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @filesource
|
||||
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
* @since CakePHP(tm) v 1.2.0.5550
|
||||
* @version $Revision$
|
||||
* @modifiedby $LastChangedBy$
|
||||
* @lastmodified $Date$
|
||||
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||
*/
|
||||
App::import('Core', 'CakeSchema');
|
||||
App::import('Model', 'CakeSchema', false);
|
||||
|
||||
/**
|
||||
* Test for Schema database management
|
||||
|
@ -138,6 +133,7 @@ class TestAppSchema extends CakeSchema {
|
|||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
'tableParameters' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -155,6 +151,7 @@ class TestAppSchema extends CakeSchema {
|
|||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
'tableParameters' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -166,7 +163,8 @@ class TestAppSchema extends CakeSchema {
|
|||
var $posts_tags = array(
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
|
||||
'tag_id' => array('type' => 'string', 'null' => false, 'key' => 'primary'),
|
||||
'indexes' => array('posts_tag' => array('column' => array('tag_id', 'post_id'), 'unique' => 1))
|
||||
'indexes' => array('posts_tag' => array('column' => array('tag_id', 'post_id'), 'unique' => 1)),
|
||||
'tableParameters' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -180,7 +178,8 @@ class TestAppSchema extends CakeSchema {
|
|||
'tag' => array('type' => 'string', 'null' => false),
|
||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true))
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
'tableParameters' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -191,8 +190,9 @@ class TestAppSchema extends CakeSchema {
|
|||
*/
|
||||
var $datatypes = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true))
|
||||
'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => ''),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
'tableParameters' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -384,7 +384,9 @@ class CakeSchemaTest extends CakeTestCase {
|
|||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $fixtures = array('core.post', 'core.tag', 'core.posts_tag', 'core.comment', 'core.datatype');
|
||||
var $fixtures = array(
|
||||
'core.post', 'core.tag', 'core.posts_tag', 'core.comment', 'core.datatype', 'core.auth_user'
|
||||
);
|
||||
|
||||
/**
|
||||
* setUp method
|
||||
|
@ -431,7 +433,6 @@ class CakeSchemaTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testSchemaRead() {
|
||||
|
||||
$read = $this->Schema->read(array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'TestApp',
|
||||
|
@ -439,8 +440,14 @@ class CakeSchemaTest extends CakeTestCase {
|
|||
));
|
||||
unset($read['tables']['missing']);
|
||||
|
||||
$this->assertEqual($read['tables'], $this->Schema->tables);
|
||||
$this->assertIdentical(
|
||||
$expected = array('comments', 'datatypes', 'posts', 'posts_tags', 'tags');
|
||||
$this->assertEqual(array_keys($read['tables']), $expected);
|
||||
|
||||
foreach ($read['tables'] as $table => $fields) {
|
||||
$this->assertEqual(array_keys($fields), array_keys($this->Schema->tables[$table]));
|
||||
}
|
||||
|
||||
$this->assertEqual(
|
||||
$read['tables']['datatypes']['float_field'],
|
||||
$this->Schema->tables['datatypes']['float_field']
|
||||
);
|
||||
|
@ -453,6 +460,54 @@ class CakeSchemaTest extends CakeTestCase {
|
|||
$this->assertTrue(empty($read['tables']));
|
||||
}
|
||||
|
||||
/**
|
||||
* test reading schema from plugins.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testSchemaReadWithPlugins() {
|
||||
App::objects('model', null, false);
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
$Schema =& new CakeSchema();
|
||||
$Schema->plugin = 'TestPlugin';
|
||||
$read = $Schema->read(array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'TestApp',
|
||||
'models' => true
|
||||
));
|
||||
unset($read['tables']['missing']);
|
||||
$this->assertTrue(isset($read['tables']['posts']));
|
||||
$this->assertTrue(isset($read['tables']['auth_users']));
|
||||
$this->assertEqual(count($read['tables']), 2);
|
||||
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* test that tables are generated correctly
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testGenerateTable() {
|
||||
$fields = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'author_id' => array('type' => 'integer', 'null' => false),
|
||||
'title' => array('type' => 'string', 'null' => false),
|
||||
'body' => array('type' => 'text', 'null' => true, 'default' => null),
|
||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
);
|
||||
$result = $this->Schema->generateTable('posts', $fields);
|
||||
$this->assertPattern('/var \$posts/', $result);
|
||||
|
||||
eval(substr($result, 4));
|
||||
$this->assertEqual($posts, $fields);
|
||||
}
|
||||
/**
|
||||
* testSchemaWrite method
|
||||
*
|
||||
|
@ -482,27 +537,124 @@ class CakeSchemaTest extends CakeTestCase {
|
|||
'comments' => array(
|
||||
'add' => array(
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'title' => array('type' => 'string', 'null' => false, 'length' => 100)
|
||||
'title' => array('type' => 'string', 'null' => false, 'length' => 100),
|
||||
),
|
||||
'drop' => array(
|
||||
'article_id' => array('type' => 'integer', 'null' => false),
|
||||
'tableParameters' => array(),
|
||||
),
|
||||
'drop' => array('article_id' => array('type' => 'integer', 'null' => false)),
|
||||
'change' => array(
|
||||
'comment' => array('type' => 'text', 'null' => false, 'default' => null)
|
||||
'comment' => array('type' => 'text', 'null' => false, 'default' => null),
|
||||
)
|
||||
),
|
||||
'posts' => array(
|
||||
'add' => array('summary' => array('type' => 'text', 'null' => 1)),
|
||||
'add' => array(
|
||||
'summary' => array('type' => 'text', 'null' => 1),
|
||||
),
|
||||
'drop' => array(
|
||||
'tableParameters' => array(),
|
||||
),
|
||||
'change' => array(
|
||||
'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
|
||||
'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
|
||||
'published' => array(
|
||||
'type' => 'string', 'null' => true, 'default' => 'Y', 'length' => '1'
|
||||
)
|
||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => '1')
|
||||
)
|
||||
),
|
||||
);
|
||||
$this->assertEqual($expected, $compare);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test comparing tableParameters and indexes.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testTableParametersAndIndexComparison() {
|
||||
$old = array(
|
||||
'posts' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'author_id' => array('type' => 'integer', 'null' => false),
|
||||
'title' => array('type' => 'string', 'null' => false),
|
||||
'indexes' => array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => true)
|
||||
),
|
||||
'tableParameters' => array(
|
||||
'charset' => 'latin1',
|
||||
'collate' => 'latin1_general_ci'
|
||||
)
|
||||
),
|
||||
'comments' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'comment' => array('type' => 'text'),
|
||||
'indexes' => array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => true),
|
||||
'post_id' => array('column' => 'post_id'),
|
||||
),
|
||||
'tableParameters' => array(
|
||||
'engine' => 'InnoDB',
|
||||
'charset' => 'latin1',
|
||||
'collate' => 'latin1_general_ci'
|
||||
)
|
||||
)
|
||||
);
|
||||
$new = array(
|
||||
'posts' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'author_id' => array('type' => 'integer', 'null' => false),
|
||||
'title' => array('type' => 'string', 'null' => false),
|
||||
'indexes' => array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => true),
|
||||
'author_id' => array('column' => 'author_id'),
|
||||
),
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci',
|
||||
'engine' => 'MyISAM'
|
||||
)
|
||||
),
|
||||
'comments' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'comment' => array('type' => 'text'),
|
||||
'indexes' => array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => true),
|
||||
),
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci'
|
||||
)
|
||||
)
|
||||
);
|
||||
$compare = $this->Schema->compare($old, $new);
|
||||
$expected = array(
|
||||
'posts' => array(
|
||||
'add' => array(
|
||||
'indexes' => array('author_id' => array('column' => 'author_id')),
|
||||
),
|
||||
'change' => array(
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci',
|
||||
'engine' => 'MyISAM'
|
||||
)
|
||||
)
|
||||
),
|
||||
'comments' => array(
|
||||
'drop' => array(
|
||||
'indexes' => array('post_id' => array('column' => 'post_id')),
|
||||
),
|
||||
'change' => array(
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci',
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($compare, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testSchemaLoading method
|
||||
*
|
||||
|
@ -510,11 +662,27 @@ class CakeSchemaTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testSchemaLoading() {
|
||||
$Other = $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests'));
|
||||
$Other =& $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests'));
|
||||
$this->assertEqual($Other->name, 'MyOtherApp');
|
||||
$this->assertEqual($Other->tables, $this->Schema->tables);
|
||||
}
|
||||
|
||||
/**
|
||||
* test loading schema files inside of plugins.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testSchemaLoadingFromPlugin() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
$Other =& $this->Schema->load(array('name' => 'TestPluginApp', 'plugin' => 'TestPlugin'));
|
||||
$this->assertEqual($Other->name, 'TestPluginApp');
|
||||
$this->assertEqual(array_keys($Other->tables), array('acos'));
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* testSchemaCreateTable method
|
||||
*
|
||||
|
|
|
@ -1,26 +1,21 @@
|
|||
<?php
|
||||
/* SVN FILE: $Id$ */
|
||||
|
||||
/**
|
||||
* DboMysqlTest file
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
||||
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @filesource
|
||||
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs
|
||||
* @since CakePHP(tm) v 1.2.0
|
||||
* @version $Revision$
|
||||
* @modifiedby $LastChangedBy$
|
||||
* @lastmodified $Date$
|
||||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
||||
*/
|
||||
App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysql'));
|
||||
|
@ -190,7 +185,7 @@ class DboMysqlTest extends CakeTestCase {
|
|||
*
|
||||
* @access public
|
||||
*/
|
||||
function setUp() {
|
||||
function startTest() {
|
||||
$db = ConnectionManager::getDataSource('test_suite');
|
||||
$this->db = new DboMysqlTestDb($db->config);
|
||||
$this->model = new MysqlTestModel();
|
||||
|
@ -201,7 +196,7 @@ class DboMysqlTest extends CakeTestCase {
|
|||
*
|
||||
* @access public
|
||||
*/
|
||||
function tearDown() {
|
||||
function endTest() {
|
||||
unset($this->db);
|
||||
}
|
||||
|
||||
|
@ -568,7 +563,7 @@ class DboMysqlTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testAlterSchemaIndexes() {
|
||||
App::import('Core', 'CakeSchema');
|
||||
App::import('Model', 'CakeSchema');
|
||||
$this->db->cacheSources = $this->db->testing = false;
|
||||
|
||||
$schema1 =& new CakeSchema(array(
|
||||
|
@ -634,6 +629,56 @@ class DboMysqlTest extends CakeTestCase {
|
|||
$this->db->query($this->db->dropSchema($schema1));
|
||||
}
|
||||
|
||||
/**
|
||||
* test altering the table settings with schema.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testAlteringTableParameters() {
|
||||
App::import('Model', 'CakeSchema');
|
||||
$this->db->cacheSources = $this->db->testing = false;
|
||||
|
||||
$schema1 =& new CakeSchema(array(
|
||||
'name' => 'AlterTest1',
|
||||
'connection' => 'test_suite',
|
||||
'altertest' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
|
||||
'tableParameters' => array(
|
||||
'charset' => 'latin1',
|
||||
'collate' => 'latin1_general_ci',
|
||||
'engine' => 'MyISAM'
|
||||
)
|
||||
)
|
||||
));
|
||||
$this->db->query($this->db->createSchema($schema1));
|
||||
$schema2 =& new CakeSchema(array(
|
||||
'name' => 'AlterTest1',
|
||||
'connection' => 'test_suite',
|
||||
'altertest' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci',
|
||||
'engine' => 'InnoDB'
|
||||
)
|
||||
)
|
||||
));
|
||||
$result = $this->db->alterSchema($schema2->compare($schema1));
|
||||
$this->assertPattern('/DEFAULT CHARSET=utf8/', $result);
|
||||
$this->assertPattern('/ENGINE=InnoDB/', $result);
|
||||
$this->assertPattern('/COLLATE=utf8_general_ci/', $result);
|
||||
|
||||
$this->db->query($result);
|
||||
$result = $this->db->listDetailedSources('altertest');
|
||||
$this->assertEqual($result['Collation'], 'utf8_general_ci');
|
||||
$this->assertEqual($result['Engine'], 'InnoDB');
|
||||
$this->assertEqual($result['charset'], 'utf8');
|
||||
|
||||
$this->db->query($this->db->dropSchema($schema1));
|
||||
}
|
||||
|
||||
/**
|
||||
* testReadTableParameters method
|
||||
*
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
<?php
|
||||
/* SVN FILE: $Id: model.test.php 8225 2009-07-08 03:25:30Z mark_story $ */
|
||||
|
||||
/**
|
||||
* ModelReadTest file
|
||||
*
|
||||
|
@ -9,24 +7,20 @@
|
|||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
||||
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
*
|
||||
* Licensed under The Open Group Test Suite License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @filesource
|
||||
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
* @since CakePHP(tm) v 1.2.0.4206
|
||||
* @version $Revision: 8225 $
|
||||
* @modifiedby $LastChangedBy: mark_story $
|
||||
* @lastmodified $Date: 2009-07-07 23:25:30 -0400 (Tue, 07 Jul 2009) $
|
||||
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||
*/
|
||||
require_once dirname(__FILE__) . DS . 'model.test.php';
|
||||
require_once dirname(__FILE__) . DS . 'model_read.test.php';
|
||||
|
||||
/**
|
||||
* ModelReadTest
|
||||
|
@ -4877,19 +4871,21 @@ class ModelReadTest extends BaseModelTest {
|
|||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the database configuration assigned to the model can be changed using
|
||||
* (before|after)Find callbacks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
/**
|
||||
* Tests that the database configuration assigned to the model can be changed using
|
||||
* (before|after)Find callbacks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testCallbackSourceChange() {
|
||||
$this->loadFixtures('Post');
|
||||
$TestModel = new Post();
|
||||
$this->assertEqual(3, count($TestModel->find('all')));
|
||||
|
||||
$this->expectError(new PatternExpectation('/Non-existent data source foo/i'));
|
||||
$this->expectError(new PatternExpectation('/Only variable references/i'));
|
||||
if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 50300) {
|
||||
$this->expectError(new PatternExpectation('/Only variable references/i'));
|
||||
}
|
||||
$this->assertFalse($TestModel->find('all', array('connection' => 'foo')));
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* TestAppSchema file
|
||||
*
|
||||
* Use for testing the loading of schema files from plugins.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
||||
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @filesource
|
||||
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.app.config.sql
|
||||
* @since CakePHP(tm) v 1.3
|
||||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
||||
*/
|
||||
class TestPluginAppSchema extends CakeSchema {
|
||||
|
||||
var $name = 'TestPluginApp';
|
||||
|
||||
var $acos = array(
|
||||
'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'length' => 10, 'key' => 'primary'),
|
||||
'parent_id' => array('type'=>'integer', 'null' => true, 'default' => NULL, 'length' => 10),
|
||||
'model' => array('type'=>'string', 'null' => true),
|
||||
'foreign_key' => array('type'=>'integer', 'null' => true, 'default' => NULL, 'length' => 10),
|
||||
'alias' => array('type'=>'string', 'null' => true),
|
||||
'lft' => array('type'=>'integer', 'null' => true, 'default' => NULL, 'length' => 10),
|
||||
'rght' => array('type'=>'integer', 'null' => true, 'default' => NULL, 'length' => 10),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
|
||||
);
|
||||
|
||||
}
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue