2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Command-line database management utility to automate programmer chores.
|
|
|
|
*
|
|
|
|
* Schema is CakePHP's database management utility. This helps you maintain versions of
|
|
|
|
* of your database.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
2009-09-07 01:05:24 +00:00
|
|
|
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2009-09-07 01:05:24 +00:00
|
|
|
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
2008-10-30 17:30:26 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.5550
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-08-08 02:19:40 +00:00
|
|
|
App::import('Core', 'File', false);
|
2009-07-30 22:01:22 +00:00
|
|
|
App::import('Model', 'CakeSchema', false);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Schema is a command-line database management utility for automating programmer chores.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs
|
2008-10-31 19:47:43 +00:00
|
|
|
* @link http://book.cakephp.org/view/734/Schema-management-and-migrations
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class SchemaShell extends Shell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* is this a dry run?
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__dry = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override initialize
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function initialize() {
|
2008-06-03 05:11:04 +00:00
|
|
|
$this->_welcome();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out('Cake Schema Shell');
|
|
|
|
$this->hr();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override startup
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function startup() {
|
2009-08-28 05:06:21 +00:00
|
|
|
$name = $file = $path = $connection = $plugin = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($this->params['name'])) {
|
2008-06-20 20:17:23 +00:00
|
|
|
$name = $this->params['name'];
|
2008-08-04 19:46:44 +00:00
|
|
|
$this->params['file'] = Inflector::underscore($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-04 19:46:44 +00:00
|
|
|
if (empty($this->params['file'])) {
|
|
|
|
$this->params['file'] = 'schema.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-10-04 17:32:33 +00:00
|
|
|
if (!empty($this->params['path'])) {
|
|
|
|
$path = $this->params['path'];
|
|
|
|
}
|
2008-08-04 19:46:44 +00:00
|
|
|
if (strpos($this->params['file'], '.php') === false) {
|
|
|
|
$this->params['file'] .= '.php';
|
2008-08-26 18:39:18 +00:00
|
|
|
}
|
2008-08-04 19:46:44 +00:00
|
|
|
$file = $this->params['file'];
|
2008-08-26 18:39:18 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($this->params['connection'])) {
|
2008-06-20 20:17:23 +00:00
|
|
|
$connection = $this->params['connection'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-08-28 05:06:21 +00:00
|
|
|
if (!empty($this->params['plugin'])) {
|
|
|
|
$plugin = $this->params['plugin'];
|
|
|
|
}
|
2008-08-26 18:39:18 +00:00
|
|
|
|
2009-08-28 05:06:21 +00:00
|
|
|
$this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection', 'plugin'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override main
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function main() {
|
|
|
|
$this->help();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-26 18:39:18 +00:00
|
|
|
* Read and output contents of schema object
|
2008-05-30 11:40:08 +00:00
|
|
|
* path to read as second arg
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function view() {
|
2008-08-04 19:46:44 +00:00
|
|
|
$File = new File($this->Schema->path . DS . $this->params['file']);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($File->exists()) {
|
|
|
|
$this->out($File->read());
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-08-28 02:24:44 +00:00
|
|
|
$file = $this->Schema->path . DS . $this->params['file'];
|
|
|
|
$this->err(sprintf(__('Schema file (%s) could not be found.', true), $file));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Read database and Write schema object
|
|
|
|
* accepts a connection as first arg or path to save as second arg
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function generate() {
|
2009-08-11 01:54:29 +00:00
|
|
|
$this->out(__('Generating Schema...', true));
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = array();
|
|
|
|
if (isset($this->params['f'])) {
|
|
|
|
$options = array('models' => false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$snapshot = false;
|
|
|
|
if (isset($this->args[0]) && $this->args[0] === 'snapshot') {
|
|
|
|
$snapshot = true;
|
|
|
|
}
|
|
|
|
|
2008-08-04 19:46:44 +00:00
|
|
|
if (!$snapshot && file_exists($this->Schema->path . DS . $this->params['file'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$snapshot = true;
|
2009-08-10 23:47:41 +00:00
|
|
|
$result = strtolower($this->in("Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?", array('o', 's', 'q'), 's'));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($result === 'q') {
|
2009-08-09 03:46:00 +00:00
|
|
|
return $this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($result === 'o') {
|
|
|
|
$snapshot = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $this->Schema->read($options);
|
2008-08-04 19:46:44 +00:00
|
|
|
$content['file'] = $this->params['file'];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($snapshot === true) {
|
|
|
|
$Folder =& new Folder($this->Schema->path);
|
|
|
|
$result = $Folder->read();
|
2008-08-04 16:53:39 +00:00
|
|
|
|
|
|
|
$numToUse = false;
|
|
|
|
if (isset($this->params['s'])) {
|
|
|
|
$numToUse = $this->params['s'];
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$count = 1;
|
|
|
|
if (!empty($result[1])) {
|
|
|
|
foreach ($result[1] as $file) {
|
2008-12-05 17:01:20 +00:00
|
|
|
if (preg_match('/schema(?:[_\d]*)?\.php$/', $file)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-04 16:53:39 +00:00
|
|
|
|
|
|
|
if ($numToUse !== false) {
|
|
|
|
if ($numToUse > $count) {
|
|
|
|
$count = $numToUse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-05 02:53:56 +00:00
|
|
|
$fileName = rtrim($this->params['file'], '.php');
|
2008-08-04 19:46:44 +00:00
|
|
|
$content['file'] = $fileName . '_' . $count . '.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->Schema->write($content)) {
|
|
|
|
$this->out(sprintf(__('Schema file: %s generated', true), $content['file']));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$this->err(__('Schema file: %s generated', true));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Dump Schema object to sql file
|
|
|
|
* if first arg == write, file will be written to sql file
|
|
|
|
* or it will output sql
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function dump() {
|
|
|
|
$write = false;
|
|
|
|
$Schema = $this->Schema->load();
|
|
|
|
if (!$Schema) {
|
|
|
|
$this->err(__('Schema could not be loaded', true));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($this->args[0])) {
|
2008-08-04 19:46:44 +00:00
|
|
|
if ($this->args[0] == 'write') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$write = Inflector::underscore($this->Schema->name);
|
|
|
|
} else {
|
|
|
|
$write = $this->args[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
2009-07-28 20:01:42 +00:00
|
|
|
$contents = "#" . $Schema->name . " sql generated on: " . date('Y-m-d H:i:s') . " : " . time() . "\n\n";
|
2008-05-30 11:40:08 +00:00
|
|
|
$contents .= $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema);
|
2009-09-16 05:23:49 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($write) {
|
|
|
|
if (strpos($write, '.sql') === false) {
|
|
|
|
$write .= '.sql';
|
|
|
|
}
|
|
|
|
$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()));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$this->err(__('SQL dump could not be created', true));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->out($contents);
|
|
|
|
return $contents;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-10-04 15:16:23 +00:00
|
|
|
* Run database create commands. Alias for run create.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-10-04 15:16:23 +00:00
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function create() {
|
|
|
|
list($Schema, $table) = $this->_loadSchema();
|
|
|
|
$this->__create($Schema, $table);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-04 15:16:23 +00:00
|
|
|
/**
|
|
|
|
* Run database create commands. Alias for run create.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function update() {
|
|
|
|
list($Schema, $table) = $this->_loadSchema();
|
|
|
|
$this->__update($Schema, $table);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-04 15:16:23 +00:00
|
|
|
/**
|
|
|
|
* Prepares the Schema objects for database operations.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function _loadSchema() {
|
2009-10-04 17:32:33 +00:00
|
|
|
$name = $plugin = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($this->args[0])) {
|
|
|
|
$name = $this->args[0];
|
|
|
|
}
|
2008-08-04 19:46:44 +00:00
|
|
|
if (isset($this->params['name'])) {
|
|
|
|
$name = $this->params['name'];
|
|
|
|
}
|
2009-10-04 17:32:33 +00:00
|
|
|
if (strpos($name, '.') !== false) {
|
|
|
|
list($plugin, $name) = explode('.', $name);
|
|
|
|
}
|
2008-08-26 18:39:18 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($this->params['dry'])) {
|
|
|
|
$this->__dry = true;
|
|
|
|
$this->out(__('Performing a dry run.', true));
|
|
|
|
}
|
|
|
|
|
2009-10-04 17:32:33 +00:00
|
|
|
$options = array('name' => $name, 'plugin' => $plugin);
|
2008-06-20 20:17:23 +00:00
|
|
|
if (isset($this->params['s'])) {
|
2008-09-24 13:49:37 +00:00
|
|
|
$fileName = rtrim($this->Schema->file, '.php');
|
2008-08-04 19:46:44 +00:00
|
|
|
$options['file'] = $fileName . '_' . $this->params['s'] . '.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-10-04 15:16:23 +00:00
|
|
|
$Schema =& $this->Schema->load($options);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!$Schema) {
|
2009-10-04 17:32:33 +00:00
|
|
|
$this->err(sprintf(__('%s could not be loaded', true), $this->Schema->path . DS . $this->Schema->file));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$table = null;
|
|
|
|
if (isset($this->args[1])) {
|
|
|
|
$table = $this->args[1];
|
|
|
|
}
|
2009-10-04 15:16:23 +00:00
|
|
|
return array(&$Schema, $table);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Create database from Schema object
|
|
|
|
* Should be called via the run method
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-09-07 01:05:24 +00:00
|
|
|
function __create(&$Schema, $table = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
|
|
|
|
|
|
|
$drop = $create = array();
|
|
|
|
|
|
|
|
if (!$table) {
|
|
|
|
foreach ($Schema->tables as $table => $fields) {
|
|
|
|
$drop[$table] = $db->dropSchema($Schema, $table);
|
|
|
|
$create[$table] = $db->createSchema($Schema, $table);
|
|
|
|
}
|
|
|
|
} elseif (isset($Schema->tables[$table])) {
|
|
|
|
$drop[$table] = $db->dropSchema($Schema, $table);
|
|
|
|
$create[$table] = $db->createSchema($Schema, $table);
|
|
|
|
}
|
|
|
|
if (empty($drop) || empty($create)) {
|
|
|
|
$this->out(__('Schema is up to date.', true));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-08-04 16:53:39 +00:00
|
|
|
$this->out("\n" . __('The following table(s) will be dropped.', true));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out(array_keys($drop));
|
|
|
|
|
2008-08-04 16:53:39 +00:00
|
|
|
if ('y' == $this->in(__('Are you sure you want to drop the table(s)?', true), array('y', 'n'), 'n')) {
|
2009-08-11 01:38:09 +00:00
|
|
|
$this->out(__('Dropping table(s).', true));
|
2008-12-23 20:30:20 +00:00
|
|
|
$this->__run($drop, 'drop', $Schema);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-08-04 16:53:39 +00:00
|
|
|
$this->out("\n" . __('The following table(s) will be created.', true));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out(array_keys($create));
|
|
|
|
|
2008-08-04 16:53:39 +00:00
|
|
|
if ('y' == $this->in(__('Are you sure you want to create the table(s)?', true), array('y', 'n'), 'y')) {
|
2009-08-11 01:38:09 +00:00
|
|
|
$this->out(__('Creating table(s).', true));
|
2008-12-23 20:30:20 +00:00
|
|
|
$this->__run($create, 'create', $Schema);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$this->out(__('End create.', true));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Update database with Schema object
|
|
|
|
* Should be called via the run method
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-09-07 01:05:24 +00:00
|
|
|
function __update(&$Schema, $table = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
|
|
|
|
2009-08-11 01:38:09 +00:00
|
|
|
$this->out(__('Comparing Database to Schema...', true));
|
2009-09-07 01:05:24 +00:00
|
|
|
$options = array();
|
|
|
|
if (isset($this->params['f'])) {
|
|
|
|
$options['models'] = false;
|
|
|
|
}
|
|
|
|
$Old = $this->Schema->read($options);
|
2008-05-30 11:40:08 +00:00
|
|
|
$compare = $this->Schema->compare($Old, $Schema);
|
|
|
|
|
|
|
|
$contents = array();
|
|
|
|
|
|
|
|
if (empty($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));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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')) {
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out(__('Updating Database...', true));
|
2008-12-23 20:30:20 +00:00
|
|
|
$this->__run($contents, 'update', $Schema);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->out(__('End update.', true));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-26 18:39:18 +00:00
|
|
|
* Runs sql from __create() or __update()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-09-07 01:05:24 +00:00
|
|
|
function __run($contents, $event, &$Schema) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($contents)) {
|
|
|
|
$this->err(__('Sql could not be run', true));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Configure::write('debug', 2);
|
|
|
|
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
2009-09-07 01:05:24 +00:00
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
foreach ($contents as $table => $sql) {
|
2008-05-30 11:40:08 +00:00
|
|
|
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 {
|
2008-12-23 20:30:20 +00:00
|
|
|
if (!$Schema->before(array($event => $table))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-22 02:33:33 +00:00
|
|
|
$error = null;
|
|
|
|
if (!$db->execute($sql)) {
|
2008-06-22 19:53:30 +00:00
|
|
|
$error = $table . ': ' . $db->lastError();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-08-22 02:33:33 +00:00
|
|
|
$Schema->after(array($event => $table, 'errors' => $error));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-08-22 02:33:33 +00:00
|
|
|
if (!empty($error)) {
|
2008-06-22 19:53:30 +00:00
|
|
|
$this->out($error);
|
2009-08-22 02:33:33 +00:00
|
|
|
} else {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out(sprintf(__('%s updated.', true), $table));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Displays help contents
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function help() {
|
2009-08-11 01:38:09 +00:00
|
|
|
$this->out("The Schema Shell generates a schema object from");
|
|
|
|
$this->out("the database and updates the database from the schema.");
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
$this->out("Usage: cake schema <command> <arg1> <arg2>...");
|
|
|
|
$this->hr();
|
|
|
|
$this->out('Params:');
|
2009-10-04 14:04:48 +00:00
|
|
|
$this->out();
|
|
|
|
$this->out("\t-connection <config>");
|
|
|
|
$this->out("\t\tset db config <config>. uses 'default' if none is specified");
|
|
|
|
$this->out();
|
|
|
|
$this->out("\t-path <dir>");
|
|
|
|
$this->out("\t\tpath <dir> to read and write schema.php.");
|
|
|
|
$this->out("\t\tdefault path: ". $this->Schema->path);
|
|
|
|
$this->out();
|
|
|
|
$this->out("\t-name <name>");
|
|
|
|
$this->out("\t\tclassname to use.");
|
|
|
|
$this->out();
|
|
|
|
$this->out("\t-file <name>");
|
|
|
|
$this->out("\t\tfile <name> to read and write.");
|
|
|
|
$this->out("\t\tdefault file: ". $this->Schema->file);
|
|
|
|
$this->out();
|
|
|
|
$this->out("\t-s <number>");
|
|
|
|
$this->out("\t\tsnapshot <number> to use for run.");
|
|
|
|
$this->out();
|
|
|
|
$this->out("\t-dry");
|
|
|
|
$this->out("\t\tPerform a dry run on 'run' commands.");
|
|
|
|
$this->out("\t\tQueries will be output to window instead of executed.");
|
|
|
|
$this->out();
|
|
|
|
$this->out("\t-f");
|
|
|
|
$this->out("\t\tforce 'generate' to create a new schema.");
|
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out('Commands:');
|
2009-10-04 14:04:48 +00:00
|
|
|
$this->out();
|
2009-10-04 14:24:15 +00:00
|
|
|
$this->out("\tschema help");
|
|
|
|
$this->out("\t\tshows this help message.");
|
2009-10-04 14:04:48 +00:00
|
|
|
$this->out();
|
2009-10-04 14:24:15 +00:00
|
|
|
$this->out("\tschema view");
|
|
|
|
$this->out("\t\tread and output contents of schema file");
|
2009-10-04 14:04:48 +00:00
|
|
|
$this->out();
|
2009-10-04 14:24:15 +00:00
|
|
|
$this->out("\tschema generate");
|
|
|
|
$this->out("\t\treads from 'connection' writes to 'path'");
|
|
|
|
$this->out("\t\tTo force generation of all tables into the schema, use the -f param.");
|
|
|
|
$this->out("\t\tUse 'schema generate snapshot <number>' to generate snapshots");
|
|
|
|
$this->out("\t\twhich you can use with the -s parameter in the other operations.");
|
2009-10-04 14:04:48 +00:00
|
|
|
$this->out();
|
2009-10-04 14:24:15 +00:00
|
|
|
$this->out("\tschema dump <filename>");
|
|
|
|
$this->out("\t\tDump database sql based on schema file to <filename>.");
|
|
|
|
$this->out("\t\tIf <filename> is write, schema dump will be written to a file");
|
|
|
|
$this->out("\t\tthat has the same name as the app directory.");
|
2009-10-04 14:04:48 +00:00
|
|
|
$this->out();
|
2009-10-04 14:24:15 +00:00
|
|
|
$this->out("\tschema run create <schema> <table>");
|
|
|
|
$this->out("\t\tDrop and create tables based on schema file");
|
|
|
|
$this->out("\t\toptional <schema> arg for selecting schema name");
|
|
|
|
$this->out("\t\toptional <table> arg for creating only one table");
|
|
|
|
$this->out("\t\tpass the -s param with a number to use a snapshot");
|
|
|
|
$this->out("\t\tTo see the changes, perform a dry run with the -dry param");
|
2009-10-04 14:04:48 +00:00
|
|
|
$this->out();
|
2009-10-04 14:24:15 +00:00
|
|
|
$this->out("\tschema run update <schema> <table>");
|
|
|
|
$this->out("\t\talter tables based on schema file");
|
|
|
|
$this->out("\t\toptional <schema> arg for selecting schema name.");
|
|
|
|
$this->out("\t\toptional <table> arg for altering only one table.");
|
|
|
|
$this->out("\t\tTo use a snapshot, pass the -s param with the snapshot number");
|
|
|
|
$this->out("\t\tTo see the changes, perform a dry run with the -dry param");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
?>
|