updating schema

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5571 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-08-23 00:57:00 +00:00
parent 8e213b8124
commit 84ff113d1d
3 changed files with 74 additions and 40 deletions

View file

@ -109,14 +109,22 @@ class SchemaShell extends Shell {
*/ */
function dump() { function dump() {
$write = false; $write = false;
if (!empty($this->args[0]) && $this->args[0] == 'write') {
$write = true;
}
$Schema = $this->Schema->load(); $Schema = $this->Schema->load();
if (!empty($this->args[0])) {
if($this->args[0] == 'true') {
$write = Inflector::underscore($this->Schema->name);
} else {
$write = $this->args[0];
}
}
$db =& ConnectionManager::getDataSource($this->Schema->connection); $db =& ConnectionManager::getDataSource($this->Schema->connection);
$contents = $db->dropSchema($Schema) . $db->createSchema($Schema); $contents = "#". $Schema->name ." sql generated on: " . date('Y-m-d H:m:s') . " : ". time()."\n\n";
if($write === true) { $contents .= $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema);
$File = new File($this->Schema->path . DS . Inflector::underscore($this->Schema->name) .'.sql', true); if($write) {
if(strpos($write, '.sql') === false) {
$write .= '.sql';
}
$File = new File($this->Schema->path . DS . $write, true);
if($File->write($contents)) { if($File->write($contents)) {
$this->out(__('SQL dump file created in '. $File->pwd(), true)); $this->out(__('SQL dump file created in '. $File->pwd(), true));
exit(); exit();
@ -143,27 +151,33 @@ class SchemaShell extends Shell {
$table = $this->args[0]; $table = $this->args[0];
$event = array($table); $event = array($table);
} }
$errors = array();
$db =& ConnectionManager::getDataSource($this->Schema->connection); $db =& ConnectionManager::getDataSource($this->Schema->connection);
$drop = $db->dropSchema($Schema, $table); $drop = $db->dropSchema($Schema, $table);
$this->out($drop); $this->out($drop);
if('y' == $this->in('Are you sure you want to drop tables and create your database?', array('y', 'n'), 'n')) { if('y' == $this->in('Are you sure you want to drop tables and create your database?', array('y', 'n'), 'n')) {
$contents = $db->createSchema($Schema, $table); $create = $db->createSchema($Schema, $table);
$this->out('Updating Database...'); $this->out('Updating Database...');
if(!$this->Schema->before($event)) { $contents = array_map('trim', explode(";", $drop. $create));
foreach($contents as $sql) {
if(!empty($sql)) {
if(!$this->Schema->before(array('created'=> $event))) {
return false; return false;
} }
if ($db->_execute($contents)) { if (!$db->_execute($sql)) {
$this->Schema->after($event); $errors[] = $db->lastError();
$this->out(__('Database created', true)); }
exit(); $this->Schema->after(array('created'=> $event, 'errors'=> $errors));
} else {
$this->err(__('Database could not be created', true));
$this->err($db->lastError());
exit();
} }
} }
} }
if(!empty($errors)) {
$this->err($errors);
exit();
}
$this->out(__('Database updated', true));
exit();
}
/** /**
* Update database with Schema object * Update database with Schema object
* *
@ -227,7 +241,7 @@ 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'"); $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 dump <filename>\n\t\tdump database sql based on schema file to filename in schema path. if filename is true, default will use the app directory name.");
$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 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("\n\tschema update <table>\n\t\talter tables based on schema file\n\t\toptional <table> arg for altering only one table");
$this->out(""); $this->out("");

View file

@ -593,7 +593,7 @@ class DboMysql extends DboSource {
$out = ''; $out = '';
foreach ($schema->tables as $curTable => $columns) { foreach ($schema->tables as $curTable => $columns) {
if (!$table || $table == $curTable) { if (!$table || $table == $curTable) {
$out .= 'DROP TABLE ' . $this->fullTableName($curTable) . ";\n"; $out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n";
} }
} }
return $out; return $out;
@ -635,16 +635,16 @@ class DboMysql extends DboSource {
} }
$out .= '(' . $length . ')'; $out .= '(' . $length . ')';
} }
if (isset($column['key']) && $column['key'] == 'primary' && (!isset($column['extra']) || (isset($column['extra']) && $column['extra'] == 'auto_increment'))) { if (isset($column['key']) && $column['key'] == 'primary' && (isset($column['extra']) && $column['extra'] == 'auto_increment')) {
$out .= ' NOT NULL AUTO_INCREMENT'; $out .= ' NOT NULL AUTO_INCREMENT';
} elseif (isset($column['key']) && $column['key'] == 'primary') { } elseif (isset($column['key']) && $column['key'] == 'primary') {
$out .= ' NOT NULL'; $out .= ' NOT NULL';
} elseif (isset($column['default'])) {
$out .= ' DEFAULT ' . $this->value($column['default'], $type);
} elseif (isset($column['null']) && $column['null'] == true) { } elseif (isset($column['null']) && $column['null'] == true) {
$out .= ' DEFAULT NULL'; $out .= ' DEFAULT NULL';
} elseif (isset($column['default']) && isset($column['null']) && $column['null'] == false) { } elseif (isset($column['default']) && isset($column['null']) && $column['null'] == false) {
$out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL'; $out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL';
} elseif (isset($column['default'])) {
$out .= ' DEFAULT ' . $this->value($column['default'], $type);
} elseif (isset($column['null']) && $column['null'] == false) { } elseif (isset($column['null']) && $column['null'] == false) {
$out .= ' NOT NULL'; $out .= ' NOT NULL';
} }

View file

@ -159,33 +159,52 @@ class CakeSchema extends Object {
$options $options
)); ));
$db =& ConnectionManager::getDataSource($connection); $db =& ConnectionManager::getDataSource($connection);
$tables = $db->sources();
if (empty($models)) { if (empty($models)) {
$models = Configure::listObjects('model'); $models = Configure::listObjects('model');
} }
$Inflector = Inflector::getInstance();
$tablesWithoutModels = array_diff(array_map(array($Inflector, 'classify'), $tables), $models);
$modelsWithoutTables = array_diff($models, array_map(array($Inflector, 'classify'), $tables));
$models = am($tablesWithoutModels, array_diff($models, $modelsWithoutTables));
$tables = array(); $tables = array();
foreach ($models as $model) { foreach ($models as $model) {
if (!class_exists($model)) { if($model == 'ArosAco') {
$model = 'Permission';
}
if (!class_exists(low($model))) {
if(!class_exists(low('AclNode')) && in_array($model, array('Aro','Aco', 'Permission'))) {
uses('model' . DS . 'db_acl');
} else {
loadModel($model); loadModel($model);
} }
}
if(class_exists(low($model))) {
$Object =& new $model(); $Object =& new $model();
$Object->setDataSource($connection); $Object->setDataSource($connection);
if (is_object($Object)) { if (is_object($Object)) {
if(empty($tables[$Object->table])) { if(empty($tables[$Object->table])) {
$tables[$Object->table] = $this->__columns($Object); $tables[$Object->table] = $this->__columns($Object);
$tables[$Object->table]['indexes'] = $db->index($Object); $tables[$Object->table]['indexes'] = $db->index($Object);
}
if(!empty($Object->hasAndBelongsToMany)) { if(!empty($Object->hasAndBelongsToMany)) {
foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) { foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) {
$tables[$Object->$Assoc->table] = $this->__columns($Object->$Assoc); $class = $assocData['className'];
$tables[$Object->$Assoc->table]['indexes'] = $db->index($Object->$Assoc); $tables[$Object->$Assoc->table] = $this->__columns($Object->$class);
} $tables[$Object->$Assoc->table]['indexes'] = $db->index($Object->$class);
} }
} }
} else { } else {
trigger_error('Schema generation error: model class ' . $class . ' not found', E_USER_WARNING); trigger_error('Schema generation error: model class ' . $class . ' not found', E_USER_WARNING);
} }
} }
}
ksort($tables);
return compact('name', 'tables'); return compact('name', 'tables');
} }
/** /**
@ -370,6 +389,7 @@ class CakeSchema extends Object {
function __columns(&$Obj) { function __columns(&$Obj) {
$db =& ConnectionManager::getDataSource($Obj->useDbConfig); $db =& ConnectionManager::getDataSource($Obj->useDbConfig);
$fields = $Obj->schema(true); $fields = $Obj->schema(true);
//pr($fields);
$columns = $props = array(); $columns = $props = array();
foreach ($fields->value as $name => $value) { foreach ($fields->value as $name => $value) {
@ -389,7 +409,7 @@ class CakeSchema extends Object {
unset($value['limit']); unset($value['limit']);
} }
if (empty($value['default']) && $value['null'] == true) { if (empty($value['default'])) {
unset($value['default']); unset($value['default']);
} }
if (empty($value['length'])) { if (empty($value['length'])) {