From 4627cc01cf1844b3811126fa9c0421a906625c47 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 16 Oct 2010 00:43:03 -0400 Subject: [PATCH] Updating SchemaShell to use optionparser and removing old help. Updating test case to use new param names. --- cake/console/libs/schema.php | 187 +++++++++--------- cake/tests/cases/console/libs/schema.test.php | 2 +- 2 files changed, 100 insertions(+), 89 deletions(-) diff --git a/cake/console/libs/schema.php b/cake/console/libs/schema.php index bd9cc36dce9..103a38f1d47 100644 --- a/cake/console/libs/schema.php +++ b/cake/console/libs/schema.php @@ -102,14 +102,6 @@ public function startup() { $this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection', 'plugin')); } -/** - * Override main - * - */ - public function main() { - $this->help(); - } - /** * Read and output contents of schema object * path to read as second arg @@ -135,7 +127,7 @@ public function view() { public function generate() { $this->out(__('Generating Schema...')); $options = array(); - if (isset($this->params['f'])) { + if (isset($this->params['force'])) { $options = array('models' => false); } @@ -163,8 +155,8 @@ public function generate() { $result = $Folder->read(); $numToUse = false; - if (isset($this->params['s'])) { - $numToUse = $this->params['s']; + if (isset($this->params['snapshot'])) { + $numToUse = $this->params['snapshot']; } $count = 0; @@ -210,7 +202,7 @@ public function dump() { $this->err(__('Schema could not be loaded')); $this->_stop(); } - if (isset($this->params['write'])) { + if (!empty($this->params['write'])) { if ($this->params['write'] == 1) { $write = Inflector::underscore($this->Schema->name); } else { @@ -270,10 +262,10 @@ function update() { */ function _loadSchema() { $name = $plugin = null; - if (isset($this->params['name'])) { + if (!empty($this->params['name'])) { $name = $this->params['name']; } - if (isset($this->params['plugin'])) { + if (!empty($this->params['plugin'])) { $plugin = $this->params['plugin']; } @@ -283,9 +275,9 @@ function _loadSchema() { } $options = array('name' => $name, 'plugin' => $plugin); - if (isset($this->params['s'])) { + if (!empty($this->params['snapshot'])) { $fileName = rtrim($this->Schema->file, '.php'); - $options['file'] = $fileName . '_' . $this->params['s'] . '.php'; + $options['file'] = $fileName . '_' . $this->params['snapshot'] . '.php'; } $Schema =& $this->Schema->load($options); @@ -355,7 +347,7 @@ function __update(&$Schema, $table = null) { $this->out(__('Comparing Database to Schema...')); $options = array(); - if (isset($this->params['f'])) { + if (isset($this->params['force'])) { $options['models'] = false; } $Old = $this->Schema->read($options); @@ -429,78 +421,97 @@ function __run($contents, $event, &$Schema) { } /** - * Displays help contents + * get the option parser * + * @return void */ - public function help() { - $help = << ... ---------------------------------------------------------------- -Params: - -connection - set db config . uses 'default' if none is specified - - -path - path to read and write schema.php. - default path: {$this->Schema->path} - - -name - Classname to use. If is Plugin.className, it will - set the plugin and name params. - - -file - file to read and write. - default file: {$this->Schema->file} - - -s - snapshot 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 - 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 ' to generate snapshots - which you can use with the -s parameter in the other operations. - - schema dump - 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 - Drop and create tables based on schema file - optional
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
- Alter the tables based on schema file. Optional
- 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(); + public function getOptionParser() { + $plugin = array( + 'help' => __('The plugin to use.'), + ); + $connection = array( + 'help' => __('Set the db config to use.'), + 'default' => 'default' + ); + $path = array( + 'help' => __('Path to read and write schema.php'), + 'default' => 'app/config/schema' + ); + $file = array( + 'help' => __('File name to read and write.'), + 'default' => 'schema.php' + ); + $name = array( + 'help' => __('Classname to use. If its Plugin.class, both name and plugin options will be set.') + ); + $snapshot = array( + 'short' => 's', + 'help' => __('Snapshot number to use/make.') + ); + $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', + 'help' => __('Force "generate" to create a new schema'), + 'boolean' => true + ); + $write = array( + 'help' => __('Write the dumped SQL to a file.') + ); + + $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( + 'help' => __('Reads from --connection and writes to --path. Generate snapshots with -s'), + 'parser' => array( + 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'snapshot', 'force'), + 'arguments' => array( + 'snapshot' => array('help' => __('Generate a snapshot.')) + ) + ) + ))->addSubcommand('dump', array( + 'help' => __('Dump database SQL based on a schema file to stdout.'), + 'parser' => array( + 'options' => compact('plugin', 'path', 'file', 'name', 'connection'), + 'arguments' => compact('name') + ) + ))->addSubcommand('create', array( + 'help' => __('Drop and create tables based on the schema file.'), + 'parser' => array( + 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot'), + 'args' => array( + 'name' => array( + 'help' => __('Name of schema to use.') + ), + 'table' => array( + 'help' => __('Only create the specified table.') + ) + ) + ) + ))->addSubcommand('update', array( + 'help' => __('Alter the tables based on the schema file.'), + 'parser' => array( + 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot'), + 'args' => array( + 'name' => array( + 'help' => __('Name of schema to use.') + ), + 'table' => array( + 'help' => __('Only create the specified table.') + ) + ) + ) + )); + return $parser; } } diff --git a/cake/tests/cases/console/libs/schema.test.php b/cake/tests/cases/console/libs/schema.test.php index 41adf9c6262..72fd74fb279 100644 --- a/cake/tests/cases/console/libs/schema.test.php +++ b/cake/tests/cases/console/libs/schema.test.php @@ -424,7 +424,7 @@ public function testUpdateWithTable() { $this->Shell->params = array( 'connection' => 'test', - 'f' => true + 'force' => true ); $this->Shell->args = array('SchemaShellTest', 'articles'); $this->Shell->startup();