diff --git a/cake/console/libs/schema.php b/cake/console/libs/schema.php index 12d866988..4c8121404 100644 --- a/cake/console/libs/schema.php +++ b/cake/console/libs/schema.php @@ -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 ..."); $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 \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 \n\t\tdrop tables and create database based on schema file"); - $this->out("\n\tschema update \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 \n\t\tdrop tables and create database based on schema file\n\t\toptional
arg for creating only one table"); + $this->out("\n\tschema update
\n\t\talter tables based on schema file\n\t\toptional
arg for altering only one table"); $this->out(""); exit(); } diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 089b16f58..9b2ce86e1 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -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; } diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index e408ecdb9..387c4d9fd 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -1732,7 +1732,7 @@ class DboSource extends DataSource { * @param unknown_type $schema * @return unknown */ - function alterSchema($compare) { + function alterSchema($compare, $table = null) { return false; } /** diff --git a/cake/libs/model/schema.php b/cake/libs/model/schema.php index d9f6bed77..1b7468fe1 100644 --- a/cake/libs/model/schema.php +++ b/cake/libs/model/schema.php @@ -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 = "*/\n{$out}?>";