2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5550
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-11-25 21:05:00 +00:00
|
|
|
|
|
|
|
App::uses('AppShell', 'Console/Command');
|
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.
|
|
|
|
*
|
2011-12-08 15:35:02 +00:00
|
|
|
* Schema is CakePHP's database management utility. This helps you maintain versions of
|
|
|
|
* of your database.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console.Command
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells/schema-management-and-migrations.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-11-25 21:05:00 +00:00
|
|
|
class SchemaShell extends AppShell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-10 04:23:25 +00:00
|
|
|
/**
|
|
|
|
* Schema class being used.
|
|
|
|
*
|
|
|
|
* @var CakeSchema
|
|
|
|
*/
|
|
|
|
public $Schema;
|
|
|
|
|
2011-08-21 05:04:55 +00:00
|
|
|
/**
|
|
|
|
* is this a dry run?
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_dry = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override startup
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function startup() {
|
2012-02-26 15:21:04 +00:00
|
|
|
$this->_welcome();
|
|
|
|
$this->out('Cake Schema Shell');
|
|
|
|
$this->hr();
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2013-03-11 17:10:14 +00:00
|
|
|
Configure::write('Cache.disable', 1);
|
|
|
|
|
2011-04-22 19:37:31 +00:00
|
|
|
$name = $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'];
|
2011-11-21 00:43:51 +00:00
|
|
|
} elseif (!empty($this->args[0]) && $this->args[0] !== 'snapshot') {
|
2009-10-04 18:06:07 +00:00
|
|
|
$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
|
|
|
}
|
2012-08-08 14:35:59 +00:00
|
|
|
$name = Inflector::classify($name);
|
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
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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());
|
2013-07-01 12:15:54 +00:00
|
|
|
return $this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
$file = $this->Schema->path . DS . $this->params['file'];
|
|
|
|
$this->err(__d('cake_console', 'Schema file (%s) could not be found.', $file));
|
|
|
|
return $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
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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();
|
2011-08-30 04:37:57 +00:00
|
|
|
if ($this->params['force']) {
|
2012-08-08 12:15:28 +00:00
|
|
|
$options['models'] = false;
|
|
|
|
} elseif (!empty($this->params['models'])) {
|
|
|
|
$options['models'] = String::tokenize($this->params['models']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
2011-04-25 19:17:59 +00:00
|
|
|
$prompt = __d('cake_console', "Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?");
|
|
|
|
$result = strtolower($this->in($prompt, 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
|
|
|
|
2013-03-04 14:10:06 +00:00
|
|
|
if (!empty($this->params['exclude']) && !empty($content)) {
|
|
|
|
$excluded = String::tokenize($this->params['exclude']);
|
|
|
|
foreach ($excluded as $table) {
|
|
|
|
unset($content['tables'][$table]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($snapshot === true) {
|
2011-11-21 00:43:51 +00:00
|
|
|
$fileName = rtrim($this->params['file'], '.php');
|
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) {
|
2012-03-03 23:55:07 +00:00
|
|
|
if (preg_match('/' . preg_quote($fileName) . '(?:[_\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-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']));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
$this->err(__d('cake_console', 'Schema file: %s generated'));
|
|
|
|
return $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
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return string
|
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'));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $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);
|
2012-03-03 23:55:07 +00:00
|
|
|
$contents = "\n\n" . $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()));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $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'));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $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
|
|
|
/**
|
2012-12-22 22:48:15 +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
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function create() {
|
2009-10-04 15:16:23 +00:00
|
|
|
list($Schema, $table) = $this->_loadSchema();
|
2011-08-20 05:39:30 +00:00
|
|
|
$this->_create($Schema, $table);
|
2009-10-04 15:16:23 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-04 15:16:23 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Run database create commands. Alias for run create.
|
2009-10-04 15:16:23 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function update() {
|
2009-10-04 15:16:23 +00:00
|
|
|
list($Schema, $table) = $this->_loadSchema();
|
2011-08-20 05:39:30 +00:00
|
|
|
$this->_update($Schema, $table);
|
2009-10-04 15:16:23 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected 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'])) {
|
2011-08-20 04:43:34 +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) {
|
2012-09-04 01:06:48 +00:00
|
|
|
$this->err(__d('cake_console', 'The chosen schema could not be loaded. Attempted to load:'));
|
|
|
|
$this->err(__d('cake_console', 'File: %s', $this->Schema->path . DS . $this->Schema->file));
|
|
|
|
$this->err(__d('cake_console', 'Name: %s', $this->Schema->name));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $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
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param CakeSchema $Schema
|
|
|
|
* @param string $table
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-03-01 03:54:31 +00:00
|
|
|
protected function _create(CakeSchema $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.'));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $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));
|
|
|
|
|
2013-07-03 17:27:17 +00:00
|
|
|
if ($this->in(__d('cake_console', 'Are you sure you want to drop the table(s)?'), array('y', 'n'), 'n') === 'y') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Dropping table(s).'));
|
2011-08-20 05:39:30 +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));
|
|
|
|
|
2013-07-03 17:27:17 +00:00
|
|
|
if ($this->in(__d('cake_console', 'Are you sure you want to create the table(s)?'), array('y', 'n'), 'y') === 'y') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Creating table(s).'));
|
2011-08-20 05:39:30 +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
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param CakeSchema $Schema
|
|
|
|
* @param string $table
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected 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) {
|
2013-04-03 15:37:09 +00:00
|
|
|
if (isset($compare[$table]['create'])) {
|
|
|
|
$contents[$table] = $db->createSchema($Schema, $table);
|
|
|
|
} else {
|
|
|
|
$contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} elseif (isset($compare[$table])) {
|
2013-04-03 15:37:09 +00:00
|
|
|
if (isset($compare[$table]['create'])) {
|
|
|
|
$contents[$table] = $db->createSchema($Schema, $table);
|
|
|
|
} else {
|
|
|
|
$contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($contents)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Schema is up to date.'));
|
2013-07-01 12:15:54 +00:00
|
|
|
return $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));
|
2013-07-03 17:27:17 +00:00
|
|
|
if ($this->in(__d('cake_console', 'Are you sure you want to alter the tables?'), array('y', 'n'), 'n') === 'y') {
|
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...'));
|
2011-08-20 05:39:30 +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
|
|
|
/**
|
2011-08-20 05:39:30 +00:00
|
|
|
* Runs sql from _create() or _update()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param array $contents
|
|
|
|
* @param string $event
|
|
|
|
* @param CakeSchema $Schema
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-03-01 03:54:31 +00:00
|
|
|
protected function _run($contents, $event, CakeSchema $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 {
|
2011-08-20 04:43:34 +00:00
|
|
|
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;
|
2011-09-03 23:04:11 +00:00
|
|
|
try {
|
|
|
|
$db->execute($sql);
|
|
|
|
} catch (PDOException $e) {
|
2012-03-03 23:55:07 +00:00
|
|
|
$error = $table . ': ' . $e->getMessage();
|
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)) {
|
2011-10-01 01:56:57 +00:00
|
|
|
$this->err($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-11-24 12:29:27 +00:00
|
|
|
'short' => 'p',
|
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-11-24 12:29:27 +00:00
|
|
|
'short' => 'c',
|
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'),
|
2011-05-16 19:52:25 +00:00
|
|
|
'default' => APP . 'Config' . DS . '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
|
|
|
);
|
2012-08-08 12:15:28 +00:00
|
|
|
$models = array(
|
|
|
|
'short' => 'm',
|
|
|
|
'help' => __d('cake_console', 'Specify models as comma separated list.'),
|
|
|
|
);
|
2010-10-16 04:43:03 +00:00
|
|
|
$dry = array(
|
2011-04-25 19:17:59 +00:00
|
|
|
'help' => __d('cake_console', 'Perform a dry run on create and update commands. Queries will be output instead of run.'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'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
|
|
|
);
|
2013-03-04 14:10:06 +00:00
|
|
|
$exclude = array(
|
|
|
|
'help' => __d('cake_console', 'Tables to exclude as comma separated list.')
|
|
|
|
);
|
2011-04-17 16:03:43 +00:00
|
|
|
|
2010-10-16 04:43:03 +00:00
|
|
|
$parser = parent::getOptionParser();
|
|
|
|
$parser->description(
|
2011-04-25 19:17:59 +00:00
|
|
|
__d('cake_console', 'The Schema Shell generates a schema object from the database and updates the database from the schema.')
|
2010-10-16 04:43:03 +00:00
|
|
|
)->addSubcommand('view', array(
|
2011-04-25 19:17:59 +00:00
|
|
|
'help' => __d('cake_console', 'Read and output the contents of a schema file'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'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(
|
2013-03-04 14:10:06 +00:00
|
|
|
'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'snapshot', 'force', 'models', 'exclude'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'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(
|
2011-10-28 22:51:40 +00:00
|
|
|
'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'write'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'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(
|
2012-01-05 05:54:02 +00:00
|
|
|
'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot', 'force'),
|
2010-10-16 04:43:03 +00:00
|
|
|
'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
|
|
|
}
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|