updating schema generation, adding table option to schema shell commands

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5567 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-08-21 23:22:15 +00:00
parent 746d292be6
commit 4a79765b26
4 changed files with 76 additions and 67 deletions

View file

@ -52,7 +52,17 @@ class SchemaShell extends Shell {
* @return void
*/
function startup() {
$this->Schema =& new CakeSchema(array('path'=> CONFIGS .'sql'));
$settings = am(array('path'=> CONFIGS .'sql'), $this->params);
$this->Schema =& new CakeSchema($settings);
}
/**
* Override main
*
* @access public
* @return void
*/
function main() {
$this->help();
}
/**
* Read and output contents od schema object
@ -62,11 +72,7 @@ class SchemaShell extends Shell {
* @return void
*/
function view() {
$path = $this->Schema->path;
if (!empty($this->args[0])) {
$path = $this->args[0];
}
$File = new File($path . DS .'schema.php');
$File = new File($this->Schema->path . DS .'schema.php');
if ($File->exists()) {
$this->out($File->read());
exit();
@ -84,13 +90,6 @@ class SchemaShell extends Shell {
*/
function generate() {
$this->out('Generating Schema...');
if (!empty($this->args[0])) {
$this->Schema->connection = $this->args[0];
}
if (!empty($this->args[1])) {
$this->Schema->path = $this->args[1];
}
$content = $this->Schema->read();
if ($this->Schema->write($content)) {
$this->out(__('Schema file created.', true));
@ -110,13 +109,8 @@ class SchemaShell extends Shell {
*/
function dump() {
$write = false;
if (!empty($this->args[0])) {
if($this->args[0] == 'write') {
$write = true;
}
}
if (!empty($this->args[1])) {
$this->Schema->path = $this->args[1];
if (!empty($this->args[0]) && $this->args[0] == 'write') {
$write = true;
}
$Schema = $this->Schema->load();
$db =& ConnectionManager::getDataSource($this->Schema->connection);
@ -142,17 +136,25 @@ class SchemaShell extends Shell {
*/
function create() {
$Schema = $this->Schema->load();
$table = null;
$event = array_keys($Schema->tables);
if(isset($this->args[0])) {
$table = $this->args[0];
$event = array($table);
}
$db =& ConnectionManager::getDataSource($this->Schema->connection);
$drop = $db->dropSchema($Schema);
$drop = $db->dropSchema($Schema, $table);
$this->out($drop);
if('y' == $this->in('Are you sure you want to drop tables and create your database?', array('y', 'n'), 'n')) {
$contents = $db->createSchema($Schema);
$contents = $db->createSchema($Schema, $table);
$this->out('Updating Database...');
if(!$this->Schema->before($compare)) {
if(!$this->Schema->before($event)) {
return false;
}
if ($db->_execute($contents)) {
$this->Schema->after($compare);
$this->Schema->after($event);
$this->out(__('Database created', true));
exit();
} else {
@ -169,22 +171,23 @@ class SchemaShell extends Shell {
* @return void
*/
function update() {
$this->out('Comparing Database to Schema...');
if (!empty($this->args[0])) {
$this->Schema->connection = $this->args[0];
}
if (!empty($this->args[1])) {
$this->Schema->path = $this->args[1];
}
$Old = $this->Schema->read();
$Schema = $this->Schema->load();
$compare = $this->Schema->compare($Old, $Schema);
$table = null;
if(isset($this->args[0])) {
$table = $this->args[0];
$compare = array($table => $compare[$table]);
}
$db =& ConnectionManager::getDataSource($this->Schema->connection);
$db->fullDebug = true;
Configure::write('debug', 2);
$contents = $db->alterSchema($compare);
$contents = $db->alterSchema($compare, $table);
if(empty($contents)) {
$this->out(__('Current database is up to date.', true));
exit();
@ -217,13 +220,16 @@ class SchemaShell extends Shell {
$this->hr();
$this->out("Usage: cake schema <command> <arg1> <arg2>...");
$this->hr();
$this->out('Params:');
$this->out("\n\t-connection\n\t\tset db config. uses 'default' if none is specified");
$this->out("\n\t-path\n\t\tpath to read and write schema.php. uses ". $this->Schema->path ." by default");
$this->out('Commands:');
$this->out("\n\tschema help\n\t\tshows this help message.");
$this->out("\n\tschema view <path>\n\t\tread and output contents of schema file");
$this->out("\n\tschema generate <connection> <path>\n\t\treads from 'connection' writes to 'path'");
$this->out("\n\tschema dump 'write' <path>\n\t\tdump database sql based on schema file");
$this->out("\n\tschema create <path>\n\t\tdrop tables and create database based on schema file");
$this->out("\n\tschema update <path>\n\t\tmodify database based on 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'");
$this->out("\n\tschema dump 'write'\n\t\tdump database sql based on schema file");
$this->out("\n\tschema create <table>\n\t\tdrop tables and create database based on schema file\n\t\toptional <table> arg for creating only one table");
$this->out("\n\tschema update <table>\n\t\talter tables based on schema file\n\t\toptional <table> arg for altering only one table");
$this->out("");
exit();
}

View file

@ -537,41 +537,43 @@ class DboMysql extends DboSource {
* @param unknown_type $schema
* @return unknown
*/
function alterSchema($compare) {
function alterSchema($compare, $table = null) {
if(!is_array($compare)) {
return false;
}
$out = '';
$colList = array();
foreach($compare as $table => $types) {
$out .= 'ALTER TABLE ' . $this->fullTableName($table) . " \n";
foreach($types as $type => $column) {
switch($type) {
case 'add':
foreach($column as $field => $col) {
$col['name'] = $field;
$alter = 'ADD '.$this->buildColumn($col);
if(isset($col['after'])) {
$alter .= ' AFTER '. $this->name($col['after']);
foreach($compare as $curTable => $types) {
if (!$table || $table == $curTable) {
$out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
foreach($types as $type => $column) {
switch($type) {
case 'add':
foreach($column as $field => $col) {
$col['name'] = $field;
$alter = 'ADD '.$this->buildColumn($col);
if(isset($col['after'])) {
$alter .= ' AFTER '. $this->name($col['after']);
}
$colList[] = $alter;
}
$colList[] = $alter;
}
break;
case 'drop':
foreach($column as $field => $col) {
$col['name'] = $field;
$colList[] = 'DROP '.$this->name($field);
}
break;
case 'change':
foreach($column as $field => $col) {
$col['name'] = $field;
$colList[] = 'CHANGE '. $this->name($field).' '.$this->buildColumn($col);
}
break;
break;
case 'drop':
foreach($column as $field => $col) {
$col['name'] = $field;
$colList[] = 'DROP '.$this->name($field);
}
break;
case 'change':
foreach($column as $field => $col) {
$col['name'] = $field;
$colList[] = 'CHANGE '. $this->name($field).' '.$this->buildColumn($col);
}
break;
}
}
$out .= "\t" . join(",\n\t", $colList) . ";\n\n";
}
$out .= "\t" . join(",\n\t", $colList) . ";\n\n";
}
return $out;
}

View file

@ -1732,7 +1732,7 @@ class DboSource extends DataSource {
* @param unknown_type $schema
* @return unknown
*/
function alterSchema($compare) {
function alterSchema($compare, $table = null) {
return false;
}
/**

View file

@ -222,7 +222,9 @@ class CakeSchema extends Object {
if ($connection !== 'default') {
$out .= "\tvar \$connection = '{$connection}';\n\n";
}
$out .= "\tfunction before(\$event = array()) {\n\t\treturn true;\n\t}\n\n\tfunction after(\$event = array()) {\n\t}\n\n";
if(empty($tables)) {
$this->read();
}
@ -258,8 +260,7 @@ class CakeSchema extends Object {
$out .="\n";
}
}
$out .= "\n\tfunction before(\$event = array()) {\n\t}\n\n\tfunction after(\$event = array()) {\n";
$out .= "\t}\n}\n\n";
$out .="\n}\n\n";
$File =& new File($path . DS . 'schema.php', true);
$content = "<?php \n/*<!--". $name ." schema generated on: " . date('Y-m-d H:m:s') . " : ". time() . "-->*/\n{$out}?>";