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.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 19:26:26 +00:00
|
|
|
* @package cake.console.shells
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5550
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-08 01:12:50 +00:00
|
|
|
App::uses('File', 'Utility');
|
2011-04-17 16:03:43 +00:00
|
|
|
App::uses('Folder', 'Utility');
|
2010-12-08 01:12:50 +00:00
|
|
|
App::uses('CakeSchema', 'Model');
|
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.
|
|
|
|
*
|
2010-12-24 19:26:26 +00:00
|
|
|
* @package cake.console.shells
|
2010-04-05 03:31:50 +00:00
|
|
|
* @link http://book.cakephp.org/view/1523/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
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__dry = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-10 04:23:25 +00:00
|
|
|
/**
|
|
|
|
* Schema class being used.
|
|
|
|
*
|
|
|
|
* @var CakeSchema
|
|
|
|
*/
|
|
|
|
public $Schema;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override initialize
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
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
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
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'];
|
2009-10-04 18:06:07 +00:00
|
|
|
} elseif (!empty($this->args[0])) {
|
|
|
|
$name = $this->params['name'] = $this->args[0];
|
|
|
|
}
|
2009-11-16 00:55:20 +00:00
|
|
|
|
2009-10-04 18:06:07 +00:00
|
|
|
if (strpos($name, '.')) {
|
2009-11-16 00:55:20 +00:00
|
|
|
list($this->params['plugin'], $splitName) = pluginSplit($name);
|
|
|
|
$name = $this->params['name'] = $splitName;
|
2009-10-04 18:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($name) {
|
2008-08-04 19:46:44 +00:00
|
|
|
$this->params['file'] = Inflector::underscore($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-10-04 18:06:07 +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
|
|
|
}
|
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
|
|
|
|
2009-10-04 18:06:07 +00:00
|
|
|
if (!empty($this->params['path'])) {
|
|
|
|
$path = $this->params['path'];
|
|
|
|
}
|
|
|
|
|
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'];
|
2010-07-24 01:46:06 +00:00
|
|
|
if (empty($name)) {
|
|
|
|
$name = $plugin;
|
|
|
|
}
|
2009-08-28 05:06:21 +00:00
|
|
|
}
|
2010-11-13 04:05:44 +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
|
|
|
/**
|
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
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
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'];
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', 'Schema file (%s) could not be found.', $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
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function generate() {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Generating Schema...'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = array();
|
2010-10-16 04:43:03 +00:00
|
|
|
if (isset($this->params['force'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-29 14:24:28 +00:00
|
|
|
$cacheDisable = Configure::read('Cache.disable');
|
|
|
|
Configure::write('Cache.disable', true);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$content = $this->Schema->read($options);
|
2008-08-04 19:46:44 +00:00
|
|
|
$content['file'] = $this->params['file'];
|
2011-04-17 16:03:43 +00:00
|
|
|
|
2011-01-29 14:24:28 +00:00
|
|
|
Configure::write('Cache.disable', $cacheDisable);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($snapshot === true) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Folder = new Folder($this->Schema->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $Folder->read();
|
2008-08-04 16:53:39 +00:00
|
|
|
|
|
|
|
$numToUse = false;
|
2010-10-16 04:43:03 +00:00
|
|
|
if (isset($this->params['snapshot'])) {
|
|
|
|
$numToUse = $this->params['snapshot'];
|
2008-08-04 16:53:39 +00:00
|
|
|
}
|
|
|
|
|
2010-05-23 04:17:22 +00:00
|
|
|
$count = 0;
|
2008-05-30 11:40:08 +00:00
|
|
|
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)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Schema file: %s generated', $content['file']));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', 'Schema file: %s generated'));
|
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
|
2009-10-04 18:47:35 +00:00
|
|
|
* 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.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function dump() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$write = false;
|
|
|
|
$Schema = $this->Schema->load();
|
|
|
|
if (!$Schema) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', 'Schema could not be loaded'));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-16 04:43:03 +00:00
|
|
|
if (!empty($this->params['write'])) {
|
2009-10-04 18:47:35 +00:00
|
|
|
if ($this->params['write'] == 1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$write = Inflector::underscore($this->Schema->name);
|
|
|
|
} else {
|
2009-10-04 18:47:35 +00:00
|
|
|
$write = $this->params['write'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-13 04:05:44 +00:00
|
|
|
$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';
|
|
|
|
}
|
2009-10-04 18:47:35 +00:00
|
|
|
if (strpos($write, DS) !== false) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$File = new File($write, true);
|
2009-10-04 18:47:35 +00:00
|
|
|
} else {
|
2010-11-13 04:05:44 +00:00
|
|
|
$File = new File($this->Schema->path . DS . $write, true);
|
2009-10-04 18:47:35 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($File->write($contents)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'SQL dump file created in %s', $File->pwd()));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', 'SQL dump could not be created'));
|
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
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-10-04 15:16:23 +00:00
|
|
|
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
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-10-04 15:16:23 +00:00
|
|
|
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
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-10-04 15:16:23 +00:00
|
|
|
function _loadSchema() {
|
2009-10-04 17:32:33 +00:00
|
|
|
$name = $plugin = null;
|
2010-10-16 04:43:03 +00:00
|
|
|
if (!empty($this->params['name'])) {
|
2008-08-04 19:46:44 +00:00
|
|
|
$name = $this->params['name'];
|
|
|
|
}
|
2010-10-16 04:43:03 +00:00
|
|
|
if (!empty($this->params['plugin'])) {
|
2009-10-04 18:47:35 +00:00
|
|
|
$plugin = $this->params['plugin'];
|
2009-10-04 17:32:33 +00:00
|
|
|
}
|
2011-04-17 16:03:43 +00:00
|
|
|
|
2010-10-17 19:43:20 +00:00
|
|
|
if (!empty($this->params['dry'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->__dry = true;
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Performing a dry run.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-10-04 17:32:33 +00:00
|
|
|
$options = array('name' => $name, 'plugin' => $plugin);
|
2010-10-16 04:43:03 +00:00
|
|
|
if (!empty($this->params['snapshot'])) {
|
2008-09-24 13:49:37 +00:00
|
|
|
$fileName = rtrim($this->Schema->file, '.php');
|
2010-10-16 04:43:03 +00:00
|
|
|
$options['file'] = $fileName . '_' . $this->params['snapshot'] . '.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:05:44 +00:00
|
|
|
$Schema = $this->Schema->load($options);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!$Schema) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', '%s could not be loaded', $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
|
|
|
|
*/
|
2010-12-02 04:49:43 +00:00
|
|
|
function __create($Schema, $table = null) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$db = ConnectionManager::getDataSource($this->Schema->connection);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$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)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Schema is up to date.'));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out("\n" . __d('cake_console', 'The following table(s) will be dropped.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out(array_keys($drop));
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
if ('y' == $this->in(__d('cake_console', 'Are you sure you want to drop the table(s)?'), array('y', 'n'), 'n')) {
|
|
|
|
$this->out(__d('cake_console', 'Dropping table(s).'));
|
2008-12-23 20:30:20 +00:00
|
|
|
$this->__run($drop, 'drop', $Schema);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out("\n" . __d('cake_console', 'The following table(s) will be created.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out(array_keys($create));
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
if ('y' == $this->in(__d('cake_console', 'Are you sure you want to create the table(s)?'), array('y', 'n'), 'y')) {
|
|
|
|
$this->out(__d('cake_console', 'Creating table(s).'));
|
2008-12-23 20:30:20 +00:00
|
|
|
$this->__run($create, 'create', $Schema);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'End create.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$db = ConnectionManager::getDataSource($this->Schema->connection);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Comparing Database to Schema...'));
|
2009-09-07 01:05:24 +00:00
|
|
|
$options = array();
|
2010-10-16 04:43:03 +00:00
|
|
|
if (isset($this->params['force'])) {
|
2009-09-07 01:05:24 +00:00
|
|
|
$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)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Schema is up to date.'));
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out("\n" . __d('cake_console', 'The following statements will run.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out(array_map('trim', $contents));
|
2011-03-19 17:32:35 +00:00
|
|
|
if ('y' == $this->in(__d('cake_console', 'Are you sure you want to alter the tables?'), array('y', 'n'), 'n')) {
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Updating Database...'));
|
2008-12-23 20:30:20 +00:00
|
|
|
$this->__run($contents, 'update', $Schema);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'End update.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', 'Sql could not be run'));
|
2008-05-30 11:40:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Configure::write('debug', 2);
|
2010-11-13 04:05:44 +00:00
|
|
|
$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)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', '%s is up to date.', $table));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
if ($this->__dry === true) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Dry run for %s :', $table));
|
2008-05-30 11:40:08 +00:00
|
|
|
$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 {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', '%s updated.', $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
|
|
|
/**
|
2010-10-16 04:43:03 +00:00
|
|
|
* get the option parser
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-16 04:43:03 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-10-16 04:43:03 +00:00
|
|
|
public function getOptionParser() {
|
|
|
|
$plugin = array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'The plugin to use.'),
|
2010-10-16 04:43:03 +00:00
|
|
|
);
|
|
|
|
$connection = array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Set the db config to use.'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'default' => 'default'
|
|
|
|
);
|
|
|
|
$path = array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Path to read and write schema.php'),
|
2010-10-23 04:36:24 +00:00
|
|
|
'default' => CONFIGS . 'schema'
|
2010-10-16 04:43:03 +00:00
|
|
|
);
|
|
|
|
$file = array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'File name to read and write.'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'default' => 'schema.php'
|
|
|
|
);
|
|
|
|
$name = array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Classname to use. If its Plugin.class, both name and plugin options will be set.')
|
2010-10-16 04:43:03 +00:00
|
|
|
);
|
|
|
|
$snapshot = array(
|
|
|
|
'short' => 's',
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Snapshot number to use/make.')
|
2010-10-16 04:43:03 +00:00
|
|
|
);
|
|
|
|
$dry = array(
|
|
|
|
'help' => 'Perform a dry run on create and update commands. Queries will be output instead of run.',
|
|
|
|
'boolean' => true
|
|
|
|
);
|
|
|
|
$force = array(
|
|
|
|
'short' => 'f',
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Force "generate" to create a new schema'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'boolean' => true
|
|
|
|
);
|
|
|
|
$write = array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Write the dumped SQL to a file.')
|
2010-10-16 04:43:03 +00:00
|
|
|
);
|
2011-04-17 16:03:43 +00:00
|
|
|
|
2010-10-16 04:43:03 +00:00
|
|
|
$parser = parent::getOptionParser();
|
|
|
|
$parser->description(
|
|
|
|
'The Schema Shell generates a schema object from' .
|
|
|
|
'the database and updates the database from the schema.'
|
|
|
|
)->addSubcommand('view', array(
|
|
|
|
'help' => 'read and output the contents of a schema file',
|
|
|
|
'parser' => array(
|
|
|
|
'options' => compact('plugin', 'path', 'file', 'name', 'connection'),
|
|
|
|
'arguments' => compact('name')
|
|
|
|
)
|
|
|
|
))->addSubcommand('generate', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Reads from --connection and writes to --path. Generate snapshots with -s'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'parser' => array(
|
|
|
|
'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'snapshot', 'force'),
|
|
|
|
'arguments' => array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'snapshot' => array('help' => __d('cake_console', 'Generate a snapshot.'))
|
2010-10-16 04:43:03 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
))->addSubcommand('dump', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Dump database SQL based on a schema file to stdout.'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'parser' => array(
|
|
|
|
'options' => compact('plugin', 'path', 'file', 'name', 'connection'),
|
|
|
|
'arguments' => compact('name')
|
|
|
|
)
|
|
|
|
))->addSubcommand('create', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Drop and create tables based on the schema file.'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'parser' => array(
|
|
|
|
'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot'),
|
|
|
|
'args' => array(
|
|
|
|
'name' => array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Name of schema to use.')
|
2010-10-16 04:43:03 +00:00
|
|
|
),
|
|
|
|
'table' => array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Only create the specified table.')
|
2010-10-16 04:43:03 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))->addSubcommand('update', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Alter the tables based on the schema file.'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'parser' => array(
|
|
|
|
'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot'),
|
|
|
|
'args' => array(
|
|
|
|
'name' => array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Name of schema to use.')
|
2010-10-16 04:43:03 +00:00
|
|
|
),
|
|
|
|
'table' => array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Only create the specified table.')
|
2010-10-16 04:43:03 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
));
|
|
|
|
return $parser;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|