_welcome(); $this->out('Cake Schema Shell'); $this->hr(); } /** * Override startup * * @access public */ function startup() { $name = $file = $path = $connection = $plugin = null; if (!empty($this->params['name'])) { $name = $this->params['name']; $this->params['file'] = Inflector::underscore($name); } $path = $this->_getPath(); if (empty($this->params['file'])) { $this->params['file'] = 'schema.php'; } if (strpos($this->params['file'], '.php') === false) { $this->params['file'] .= '.php'; } $file = $this->params['file']; if (!empty($this->params['connection'])) { $connection = $this->params['connection']; } if (!empty($this->params['plugin'])) { $plugin = $this->params['plugin']; } $this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection', 'plugin')); } /** * 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; } return null; } /** * Override main * * @access public */ function main() { $this->help(); } /** * Read and output contents of schema object * path to read as second arg * * @access public */ function view() { $File = new File($this->Schema->path . DS . $this->params['file']); if ($File->exists()) { $this->out($File->read()); $this->_stop(); } else { $file = $this->Schema->path . DS . $this->params['file']; $this->err(sprintf(__('Schema file (%s) could not be found.', true), $file)); $this->_stop(); } } /** * Read database and Write schema object * accepts a connection as first arg or path to save as second arg * * @access public */ function generate() { $this->out(__('Generating Schema...', true)); $options = array(); if (isset($this->params['f'])) { $options = array('models' => false); } $snapshot = false; if (isset($this->args[0]) && $this->args[0] === 'snapshot') { $snapshot = true; } if (!$snapshot && file_exists($this->Schema->path . DS . $this->params['file'])) { $snapshot = true; $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')); if ($result === 'q') { return $this->_stop(); } if ($result === 'o') { $snapshot = false; } } $content = $this->Schema->read($options); $content['file'] = $this->params['file']; if ($snapshot === true) { $Folder =& new Folder($this->Schema->path); $result = $Folder->read(); $numToUse = false; if (isset($this->params['s'])) { $numToUse = $this->params['s']; } $count = 1; if (!empty($result[1])) { foreach ($result[1] as $file) { if (preg_match('/schema(?:[_\d]*)?\.php$/', $file)) { $count++; } } } if ($numToUse !== false) { if ($numToUse > $count) { $count = $numToUse; } } $fileName = rtrim($this->params['file'], '.php'); $content['file'] = $fileName . '_' . $count . '.php'; } if ($this->Schema->write($content)) { $this->out(sprintf(__('Schema file: %s generated', true), $content['file'])); $this->_stop(); } else { $this->err(__('Schema file: %s generated', true)); $this->_stop(); } } /** * 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)); $this->_stop(); } if (!empty($this->args[0])) { if ($this->args[0] == 'write') { $write = Inflector::underscore($this->Schema->name); } else { $write = $this->args[0]; } } $db =& ConnectionManager::getDataSource($this->Schema->connection); $contents = "#" . $Schema->name . " sql generated on: " . date('Y-m-d H:i:s') . " : " . time() . "\n\n"; $contents .= $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema); 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())); $this->_stop(); } else { $this->err(__('SQL dump could not be created', true)); $this->_stop(); } } $this->out($contents); return $contents; } /** * Run database create commands. Alias for run create. * * @return void **/ function create() { list($Schema, $table) = $this->_loadSchema(); $this->__create($Schema, $table); } /** * Run database create commands. Alias for run create. * * @return void **/ function update() { list($Schema, $table) = $this->_loadSchema(); $this->__update($Schema, $table); } /** * Prepares the Schema objects for database operations. * * @return void **/ function _loadSchema() { $name = null; if (isset($this->args[0])) { $name = $this->args[0]; } if (isset($this->params['name'])) { $name = $this->params['name']; } if (isset($this->params['dry'])) { $this->__dry = true; $this->out(__('Performing a dry run.', true)); } $options = array('name' => $name); if (isset($this->params['s'])) { $fileName = rtrim($this->Schema->file, '.php'); $options['file'] = $fileName . '_' . $this->params['s'] . '.php'; } $Schema =& $this->Schema->load($options); if (!$Schema) { $this->err(sprintf(__('%s could not be loaded', true), $this->Schema->file)); $this->_stop(); } $table = null; if (isset($this->args[1])) { $table = $this->args[1]; } return array(&$Schema, $table); } /** * Create database from Schema object * Should be called via the run method * * @access private */ function __create(&$Schema, $table = null) { $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)); $this->_stop(); } $this->out("\n" . __('The following table(s) will be dropped.', true)); $this->out(array_keys($drop)); if ('y' == $this->in(__('Are you sure you want to drop the table(s)?', true), array('y', 'n'), 'n')) { $this->out(__('Dropping table(s).', true)); $this->__run($drop, 'drop', $Schema); } $this->out("\n" . __('The following table(s) will be created.', true)); $this->out(array_keys($create)); if ('y' == $this->in(__('Are you sure you want to create the table(s)?', true), array('y', 'n'), 'y')) { $this->out(__('Creating table(s).', true)); $this->__run($create, 'create', $Schema); } $this->out(__('End create.', true)); } /** * Update database with Schema object * Should be called via the run method * * @access private */ function __update(&$Schema, $table = null) { $db =& ConnectionManager::getDataSource($this->Schema->connection); $this->out(__('Comparing Database to Schema...', true)); $options = array(); if (isset($this->params['f'])) { $options['models'] = false; } $Old = $this->Schema->read($options); $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)); $this->_stop(); } $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')) { $this->out(); $this->out(__('Updating Database...', true)); $this->__run($contents, 'update', $Schema); } $this->out(__('End update.', true)); } /** * Runs sql from __create() or __update() * * @access private */ function __run($contents, $event, &$Schema) { if (empty($contents)) { $this->err(__('Sql could not be run', true)); return; } Configure::write('debug', 2); $db =& ConnectionManager::getDataSource($this->Schema->connection); foreach ($contents as $table => $sql) { 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 { if (!$Schema->before(array($event => $table))) { return false; } $error = null; if (!$db->execute($sql)) { $error = $table . ': ' . $db->lastError(); } $Schema->after(array($event => $table, 'errors' => $error)); if (!empty($error)) { $this->out($error); } else { $this->out(sprintf(__('%s updated.', true), $table)); } } } } } /** * Displays help contents * * @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 ..."); $this->hr(); $this->out('Params:'); $this->out(); $this->out("\t-connection "); $this->out("\t\tset db config . uses 'default' if none is specified"); $this->out(); $this->out("\t-path "); $this->out("\t\tpath to read and write schema.php."); $this->out("\t\tdefault path: ". $this->Schema->path); $this->out(); $this->out("\t-name "); $this->out("\t\tclassname to use."); $this->out(); $this->out("\t-file "); $this->out("\t\tfile to read and write."); $this->out("\t\tdefault file: ". $this->Schema->file); $this->out(); $this->out("\t-s "); $this->out("\t\tsnapshot 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(); $this->out('Commands:'); $this->out(); $this->out("\tschema help"); $this->out("\t\tshows this help message."); $this->out(); $this->out("\tschema view"); $this->out("\t\tread and output contents of schema file"); $this->out(); $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 ' to generate snapshots"); $this->out("\t\twhich you can use with the -s parameter in the other operations."); $this->out(); $this->out("\tschema dump "); $this->out("\t\tDump database sql based on schema file to ."); $this->out("\t\tIf is write, schema dump will be written to a file"); $this->out("\t\tthat has the same name as the app directory."); $this->out(); $this->out("\tschema run create "); $this->out("\t\tDrop and create tables based on schema file"); $this->out("\t\toptional arg for selecting schema name"); $this->out("\t\toptional
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"); $this->out(); $this->out("\tschema run update
"); $this->out("\t\talter tables based on schema file"); $this->out("\t\toptional arg for selecting schema name."); $this->out("\t\toptional
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"); $this->out(); $this->_stop(); } } ?>