mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
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:
parent
8e213b8124
commit
84ff113d1d
3 changed files with 74 additions and 40 deletions
|
@ -109,14 +109,22 @@ class SchemaShell extends Shell {
|
|||
*/
|
||||
function dump() {
|
||||
$write = false;
|
||||
if (!empty($this->args[0]) && $this->args[0] == 'write') {
|
||||
$write = true;
|
||||
}
|
||||
$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);
|
||||
$contents = $db->dropSchema($Schema) . $db->createSchema($Schema);
|
||||
if($write === true) {
|
||||
$File = new File($this->Schema->path . DS . Inflector::underscore($this->Schema->name) .'.sql', true);
|
||||
$contents = "#". $Schema->name ." sql generated on: " . date('Y-m-d H:m:s') . " : ". time()."\n\n";
|
||||
$contents .= $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema);
|
||||
if($write) {
|
||||
if(strpos($write, '.sql') === false) {
|
||||
$write .= '.sql';
|
||||
}
|
||||
$File = new File($this->Schema->path . DS . $write, true);
|
||||
if($File->write($contents)) {
|
||||
$this->out(__('SQL dump file created in '. $File->pwd(), true));
|
||||
exit();
|
||||
|
@ -143,26 +151,32 @@ class SchemaShell extends Shell {
|
|||
$table = $this->args[0];
|
||||
$event = array($table);
|
||||
}
|
||||
|
||||
$errors = array();
|
||||
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
||||
$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, $table);
|
||||
$create = $db->createSchema($Schema, $table);
|
||||
$this->out('Updating Database...');
|
||||
if(!$this->Schema->before($event)) {
|
||||
return false;
|
||||
}
|
||||
if ($db->_execute($contents)) {
|
||||
$this->Schema->after($event);
|
||||
$this->out(__('Database created', true));
|
||||
exit();
|
||||
} else {
|
||||
$this->err(__('Database could not be created', true));
|
||||
$this->err($db->lastError());
|
||||
exit();
|
||||
$contents = array_map('trim', explode(";", $drop. $create));
|
||||
foreach($contents as $sql) {
|
||||
if(!empty($sql)) {
|
||||
if(!$this->Schema->before(array('created'=> $event))) {
|
||||
return false;
|
||||
}
|
||||
if (!$db->_execute($sql)) {
|
||||
$errors[] = $db->lastError();
|
||||
}
|
||||
$this->Schema->after(array('created'=> $event, 'errors'=> $errors));
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!empty($errors)) {
|
||||
$this->err($errors);
|
||||
exit();
|
||||
}
|
||||
$this->out(__('Database updated', true));
|
||||
exit();
|
||||
}
|
||||
/**
|
||||
* 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 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 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 update <table>\n\t\talter tables based on schema file\n\t\toptional <table> arg for altering only one table");
|
||||
$this->out("");
|
||||
|
|
|
@ -593,7 +593,7 @@ class DboMysql extends DboSource {
|
|||
$out = '';
|
||||
foreach ($schema->tables as $curTable => $columns) {
|
||||
if (!$table || $table == $curTable) {
|
||||
$out .= 'DROP TABLE ' . $this->fullTableName($curTable) . ";\n";
|
||||
$out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n";
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
|
@ -635,16 +635,16 @@ class DboMysql extends DboSource {
|
|||
}
|
||||
$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';
|
||||
} elseif (isset($column['key']) && $column['key'] == 'primary') {
|
||||
$out .= ' NOT NULL';
|
||||
} elseif (isset($column['default'])) {
|
||||
$out .= ' DEFAULT ' . $this->value($column['default'], $type);
|
||||
} elseif (isset($column['null']) && $column['null'] == true) {
|
||||
$out .= ' DEFAULT NULL';
|
||||
} elseif (isset($column['default']) && isset($column['null']) && $column['null'] == false) {
|
||||
$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) {
|
||||
$out .= ' NOT NULL';
|
||||
}
|
||||
|
|
|
@ -159,33 +159,52 @@ class CakeSchema extends Object {
|
|||
$options
|
||||
));
|
||||
$db =& ConnectionManager::getDataSource($connection);
|
||||
$tables = $db->sources();
|
||||
|
||||
|
||||
if (empty($models)) {
|
||||
$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();
|
||||
foreach ($models as $model) {
|
||||
if (!class_exists($model)) {
|
||||
loadModel($model);
|
||||
if($model == 'ArosAco') {
|
||||
$model = 'Permission';
|
||||
}
|
||||
$Object =& new $model();
|
||||
$Object->setDataSource($connection);
|
||||
if (is_object($Object)) {
|
||||
if(empty($tables[$Object->table])) {
|
||||
$tables[$Object->table] = $this->__columns($Object);
|
||||
$tables[$Object->table]['indexes'] = $db->index($Object);
|
||||
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);
|
||||
}
|
||||
}
|
||||
if(class_exists(low($model))) {
|
||||
$Object =& new $model();
|
||||
$Object->setDataSource($connection);
|
||||
if (is_object($Object)) {
|
||||
if(empty($tables[$Object->table])) {
|
||||
$tables[$Object->table] = $this->__columns($Object);
|
||||
$tables[$Object->table]['indexes'] = $db->index($Object);
|
||||
}
|
||||
if(!empty($Object->hasAndBelongsToMany)) {
|
||||
foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) {
|
||||
$tables[$Object->$Assoc->table] = $this->__columns($Object->$Assoc);
|
||||
$tables[$Object->$Assoc->table]['indexes'] = $db->index($Object->$Assoc);
|
||||
$class = $assocData['className'];
|
||||
$tables[$Object->$Assoc->table] = $this->__columns($Object->$class);
|
||||
$tables[$Object->$Assoc->table]['indexes'] = $db->index($Object->$class);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
trigger_error('Schema generation error: model class ' . $class . ' not found', E_USER_WARNING);
|
||||
}
|
||||
} else {
|
||||
trigger_error('Schema generation error: model class ' . $class . ' not found', E_USER_WARNING);
|
||||
}
|
||||
|
||||
}
|
||||
ksort($tables);
|
||||
return compact('name', 'tables');
|
||||
}
|
||||
/**
|
||||
|
@ -222,9 +241,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();
|
||||
}
|
||||
|
@ -370,6 +389,7 @@ class CakeSchema extends Object {
|
|||
function __columns(&$Obj) {
|
||||
$db =& ConnectionManager::getDataSource($Obj->useDbConfig);
|
||||
$fields = $Obj->schema(true);
|
||||
//pr($fields);
|
||||
$columns = $props = array();
|
||||
foreach ($fields->value as $name => $value) {
|
||||
|
||||
|
@ -389,7 +409,7 @@ class CakeSchema extends Object {
|
|||
unset($value['limit']);
|
||||
}
|
||||
|
||||
if (empty($value['default']) && $value['null'] == true) {
|
||||
if (empty($value['default'])) {
|
||||
unset($value['default']);
|
||||
}
|
||||
if (empty($value['length'])) {
|
||||
|
|
Loading…
Add table
Reference in a new issue