Allowing overriding of schema class and file name. Closes #5175. Fixed issue with php4 compatibility in CakeSchema

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7430 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2008-08-04 19:46:44 +00:00
parent b3f06defcd
commit e7e43252f3
2 changed files with 29 additions and 14 deletions

View file

@ -62,19 +62,28 @@ class SchemaShell extends Shell {
$name = null; $name = null;
if (!empty($this->params['name'])) { if (!empty($this->params['name'])) {
$name = $this->params['name']; $name = $this->params['name'];
$this->params['file'] = Inflector::underscore($name);
} }
$path = null; $path = null;
if (!empty($this->params['path'])) { if (!empty($this->params['path'])) {
$path = $this->params['path']; $path = $this->params['path'];
} }
$file = null; $file = null;
if (!empty($this->params['file'])) { if (empty($this->params['file'])) {
$file = $this->params['file']; $this->params['file'] = 'schema.php';
} }
if (strpos($this->params['file'], '.php') === false) {
$this->params['file'] .= '.php';
}
$file = $this->params['file'];
$connection = null; $connection = null;
if (!empty($this->params['connection'])) { if (!empty($this->params['connection'])) {
$connection = $this->params['connection']; $connection = $this->params['connection'];
} }
$this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection')); $this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection'));
} }
/** /**
@ -92,7 +101,7 @@ class SchemaShell extends Shell {
* @access public * @access public
*/ */
function view() { function view() {
$File = new File($this->Schema->path . DS .'schema.php'); $File = new File($this->Schema->path . DS . $this->params['file']);
if ($File->exists()) { if ($File->exists()) {
$this->out($File->read()); $this->out($File->read());
$this->_stop(); $this->_stop();
@ -119,7 +128,7 @@ class SchemaShell extends Shell {
$snapshot = true; $snapshot = true;
} }
if (!$snapshot && file_exists($this->Schema->path . DS . 'schema.php')) { if (!$snapshot && file_exists($this->Schema->path . DS . $this->params['file'])) {
$snapshot = true; $snapshot = true;
$result = $this->in("Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?", array('o', 's', 'q'), 's'); $result = $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') { if ($result === 'q') {
@ -131,7 +140,7 @@ class SchemaShell extends Shell {
} }
$content = $this->Schema->read($options); $content = $this->Schema->read($options);
$content['file'] = 'schema.php'; $content['file'] = $this->params['file'];
if ($snapshot === true) { if ($snapshot === true) {
$Folder =& new Folder($this->Schema->path); $Folder =& new Folder($this->Schema->path);
@ -157,7 +166,8 @@ class SchemaShell extends Shell {
} }
} }
$content['file'] = 'schema_'.$count.'.php'; $fileName = rtrim($this->params['file'], '.php'));
$content['file'] = $fileName . '_' . $count . '.php';
} }
if ($this->Schema->write($content)) { if ($this->Schema->write($content)) {
@ -183,7 +193,7 @@ class SchemaShell extends Shell {
$this->_stop(); $this->_stop();
} }
if (!empty($this->args[0])) { if (!empty($this->args[0])) {
if ($this->args[0] == 'true') { if ($this->args[0] == 'write') {
$write = Inflector::underscore($this->Schema->name); $write = Inflector::underscore($this->Schema->name);
} else { } else {
$write = $this->args[0]; $write = $this->args[0];
@ -227,15 +237,19 @@ class SchemaShell extends Shell {
if (isset($this->args[0])) { if (isset($this->args[0])) {
$name = $this->args[0]; $name = $this->args[0];
} }
if (isset($this->params['name'])) {
$name = $this->params['name'];
}
if (isset($this->params['dry'])) { if (isset($this->params['dry'])) {
$this->__dry = true; $this->__dry = true;
$this->out(__('Performing a dry run.', true)); $this->out(__('Performing a dry run.', true));
} }
$options = array('name' => $name, 'file' => $this->Schema->file); $options = array('name' => $name);
if (isset($this->params['s'])) { if (isset($this->params['s'])) {
$options = array('file' => 'schema_'.$this->params['s'].'.php'); $fileName = substr($name, 0, strpos($this->Schema->file, '.php'));
$options['file'] = $fileName . '_' . $this->params['s'] . '.php';
} }
$Schema = $this->Schema->load($options); $Schema = $this->Schema->load($options);
@ -397,6 +411,7 @@ class SchemaShell extends Shell {
$this->out('Params:'); $this->out('Params:');
$this->out("\n\t-connection <config>\n\t\tset db config <config>. uses 'default' if none is specified"); $this->out("\n\t-connection <config>\n\t\tset db config <config>. uses 'default' if none is specified");
$this->out("\n\t-path <dir>\n\t\tpath <dir> to read and write schema.php.\n\t\tdefault path: ". $this->Schema->path); $this->out("\n\t-path <dir>\n\t\tpath <dir> to read and write schema.php.\n\t\tdefault path: ". $this->Schema->path);
$this->out("\n\t-name <name>\n\t\tclassname to use.");
$this->out("\n\t-file <name>\n\t\tfile <name> to read and write.\n\t\tdefault file: ". $this->Schema->file); $this->out("\n\t-file <name>\n\t\tfile <name> to read and write.\n\t\tdefault file: ". $this->Schema->file);
$this->out("\n\t-s <number>\n\t\tsnapshot <number> to use for run."); $this->out("\n\t-s <number>\n\t\tsnapshot <number> to use for run.");
$this->out("\n\t-dry\n\t\tPerform a dry run on 'run' commands.\n\t\tQueries will be output to window instead of executed."); $this->out("\n\t-dry\n\t\tPerform a dry run on 'run' commands.\n\t\tQueries will be output to window instead of executed.");
@ -405,11 +420,11 @@ class SchemaShell extends Shell {
$this->out("\n\tschema help\n\t\tshows this help message."); $this->out("\n\tschema help\n\t\tshows this help message.");
$this->out("\n\tschema view\n\t\tread and output contents of schema file"); $this->out("\n\tschema view\n\t\tread and output contents of schema file");
$this->out("\n\tschema generate\n\t\treads from 'connection' writes to 'path'\n\t\tTo force generation of all tables into the schema, use the -f param.\n\t\tUse 'schema generate snapshot <number>' to generate snapshots which you can use with the -s parameter in the other operations."); $this->out("\n\tschema generate\n\t\treads from 'connection' writes to 'path'\n\t\tTo force generation of all tables into the schema, use the -f param.\n\t\tUse 'schema generate snapshot <number>' to generate snapshots which you can use with the -s parameter in the other operations.");
$this->out("\n\tschema dump <filename>\n\t\tdump database sql based on schema file to filename in schema path. \n\t\tif filename is true, default will use the app directory name."); $this->out("\n\tschema dump <filename>\n\t\tdump database sql based on schema file to filename in schema path. \n\t\tif filename is write, default will use the app directory name.");
$this->out("\n\tschema run create <schema> <table>\n\t\tdrop tables and create database based on schema file\n\t\toptional <schema> arg for selecting schema name\n\t\toptional <table> arg for creating only one table\n\t\tpass the -s param with a number to use a snapshot\n\t\tTo see the changes, perform a dry run with the -dry param"); $this->out("\n\tschema run create <schema> <table>\n\t\tdrop tables and create database based on schema file\n\t\toptional <schema> arg for selecting schema name\n\t\toptional <table> arg for creating only one table\n\t\tpass the -s param with a number to use a snapshot\n\t\tTo see the changes, perform a dry run with the -dry param");
$this->out("\n\tschema run update <schema> <table>\n\t\talter tables based on schema file\n\t\toptional <schema> arg for selecting schema name.\n\t\toptional <table> arg for altering only one table.\n\t\tTo use a snapshot, pass the -s param with the snapshot number\n\t\tTo see the changes, perform a dry run with the -dry param"); $this->out("\n\tschema run update <schema> <table>\n\t\talter tables based on schema file\n\t\toptional <schema> arg for selecting schema name.\n\t\toptional <table> arg for altering only one table.\n\t\tTo use a snapshot, pass the -s param with the snapshot number\n\t\tTo see the changes, perform a dry run with the -dry param");
$this->out(""); $this->out("");
$this->_stop(); $this->_stop();
} }
} }
?> ?>

View file

@ -79,7 +79,7 @@ class CakeSchema extends Object {
$this->name = preg_replace('/schema$/i', '', get_class($this)); $this->name = preg_replace('/schema$/i', '', get_class($this));
} }
if ($this->name === 'Cake') { if (strtolower($this->name) === 'cake') {
$this->name = Inflector::camelize(Inflector::slug(Configure::read('App.dir'))); $this->name = Inflector::camelize(Inflector::slug(Configure::read('App.dir')));
} }
@ -104,7 +104,7 @@ class CakeSchema extends Object {
$this->tables[$key] = $val; $this->tables[$key] = $val;
unset($this->{$key}); unset($this->{$key});
} elseif ($key !== 'tables') { } elseif ($key !== 'tables') {
if ($key === 'name' && $val !== $this->name) { if ($key === 'name' && $val !== $this->name && !isset($data['file'])) {
$file = Inflector::underscore($val) . '.php'; $file = Inflector::underscore($val) . '.php';
} }
$this->{$key} = $val; $this->{$key} = $val;