Skip to content

Commit

Permalink
Updating SchemaShell to use optionparser and removing old help.
Browse files Browse the repository at this point in the history
Updating test case to use new param names.
  • Loading branch information
markstory committed Oct 16, 2010
1 parent 6ec4e07 commit 4627cc0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 89 deletions.
187 changes: 99 additions & 88 deletions cake/console/libs/schema.php
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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'];
}

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -429,78 +421,97 @@ function __run($contents, $event, &$Schema) {
}

/**
* Displays help contents
* get the option parser
*
* @return void
*/
public function help() {
$help = <<<TEXT
The Schema Shell generates a schema object from
the database and updates the database from the schema.
---------------------------------------------------------------
Usage: cake schema <command> <arg1> <arg2>...
---------------------------------------------------------------
Params:
-connection <config>
set db config <config>. uses 'default' if none is specified
-path <dir>
path <dir> to read and write schema.php.
default path: {$this->Schema->path}
-name <name>
Classname to use. If <name> is Plugin.className, it will
set the plugin and name params.
-file <name>
file <name> to read and write.
default file: {$this->Schema->file}
-s <number>
snapshot <number> 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 <name>
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 <number>' to generate snapshots
which you can use with the -s parameter in the other operations.
schema dump <name>
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 <name> <table>
Drop and create tables based on schema file
optional <table> 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 <name> <table>
Alter the tables based on schema file. Optional <table>
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;
}
}
2 changes: 1 addition & 1 deletion cake/tests/cases/console/libs/schema.test.php
Expand Up @@ -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();
Expand Down

0 comments on commit 4627cc0

Please sign in to comment.