| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: schema.php 7961 2008-12-25 23:21:36Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Command-line database management utility to automate programmer chores.
|
|
|
5 |
*
|
|
|
6 |
* Schema is CakePHP's database management utility. This helps you maintain versions of
|
|
|
7 |
* of your database.
|
|
|
8 |
*
|
|
|
9 |
* PHP versions 4 and 5
|
|
|
10 |
*
|
|
|
11 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
12 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
13 |
*
|
|
|
14 |
* Licensed under The MIT License
|
|
|
15 |
* Redistributions of files must retain the above copyright notice.
|
|
|
16 |
*
|
|
|
17 |
* @filesource
|
|
|
18 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
19 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
20 |
* @package cake
|
|
|
21 |
* @subpackage cake.cake.console.libs
|
|
|
22 |
* @since CakePHP(tm) v 1.2.0.5550
|
|
|
23 |
* @version $Revision: 7961 $
|
|
|
24 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
25 |
* @lastmodified $Date: 2008-12-25 15:21:36 -0800 (Thu, 25 Dec 2008) $
|
|
|
26 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
27 |
*/
|
|
|
28 |
App::import('File');
|
|
|
29 |
App::import('Model', 'Schema');
|
|
|
30 |
/**
|
|
|
31 |
* Schema is a command-line database management utility for automating programmer chores.
|
|
|
32 |
*
|
|
|
33 |
* @package cake
|
|
|
34 |
* @subpackage cake.cake.console.libs
|
|
|
35 |
* @link http://book.cakephp.org/view/734/Schema-management-and-migrations
|
|
|
36 |
*/
|
|
|
37 |
class SchemaShell extends Shell {
|
|
|
38 |
/**
|
|
|
39 |
* is this a dry run?
|
|
|
40 |
*
|
|
|
41 |
* @var boolean
|
|
|
42 |
* @access private
|
|
|
43 |
*/
|
|
|
44 |
var $__dry = null;
|
|
|
45 |
/**
|
|
|
46 |
* Override initialize
|
|
|
47 |
*
|
|
|
48 |
* @access public
|
|
|
49 |
*/
|
|
|
50 |
function initialize() {
|
|
|
51 |
$this->_welcome();
|
|
|
52 |
$this->out('Cake Schema Shell');
|
|
|
53 |
$this->hr();
|
|
|
54 |
}
|
|
|
55 |
/**
|
|
|
56 |
* Override startup
|
|
|
57 |
*
|
|
|
58 |
* @access public
|
|
|
59 |
*/
|
|
|
60 |
function startup() {
|
|
|
61 |
$name = null;
|
|
|
62 |
if (!empty($this->params['name'])) {
|
|
|
63 |
$name = $this->params['name'];
|
|
|
64 |
$this->params['file'] = Inflector::underscore($name);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$path = null;
|
|
|
68 |
if (!empty($this->params['path'])) {
|
|
|
69 |
$path = $this->params['path'];
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$file = null;
|
|
|
73 |
if (empty($this->params['file'])) {
|
|
|
74 |
$this->params['file'] = 'schema.php';
|
|
|
75 |
}
|
|
|
76 |
if (strpos($this->params['file'], '.php') === false) {
|
|
|
77 |
$this->params['file'] .= '.php';
|
|
|
78 |
}
|
|
|
79 |
$file = $this->params['file'];
|
|
|
80 |
|
|
|
81 |
$connection = null;
|
|
|
82 |
if (!empty($this->params['connection'])) {
|
|
|
83 |
$connection = $this->params['connection'];
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection'));
|
|
|
87 |
}
|
|
|
88 |
/**
|
|
|
89 |
* Override main
|
|
|
90 |
*
|
|
|
91 |
* @access public
|
|
|
92 |
*/
|
|
|
93 |
function main() {
|
|
|
94 |
$this->help();
|
|
|
95 |
}
|
|
|
96 |
/**
|
|
|
97 |
* Read and output contents of schema object
|
|
|
98 |
* path to read as second arg
|
|
|
99 |
*
|
|
|
100 |
* @access public
|
|
|
101 |
*/
|
|
|
102 |
function view() {
|
|
|
103 |
$File = new File($this->Schema->path . DS . $this->params['file']);
|
|
|
104 |
if ($File->exists()) {
|
|
|
105 |
$this->out($File->read());
|
|
|
106 |
$this->_stop();
|
|
|
107 |
} else {
|
|
|
108 |
$this->err(__('Schema could not be found', true));
|
|
|
109 |
$this->_stop();
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
/**
|
|
|
113 |
* Read database and Write schema object
|
|
|
114 |
* accepts a connection as first arg or path to save as second arg
|
|
|
115 |
*
|
|
|
116 |
* @access public
|
|
|
117 |
*/
|
|
|
118 |
function generate() {
|
|
|
119 |
$this->out('Generating Schema...');
|
|
|
120 |
$options = array();
|
|
|
121 |
if (isset($this->params['f'])) {
|
|
|
122 |
$options = array('models' => false);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$snapshot = false;
|
|
|
126 |
if (isset($this->args[0]) && $this->args[0] === 'snapshot') {
|
|
|
127 |
$snapshot = true;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
if (!$snapshot && file_exists($this->Schema->path . DS . $this->params['file'])) {
|
|
|
131 |
$snapshot = true;
|
|
|
132 |
$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');
|
|
|
133 |
if ($result === 'q') {
|
|
|
134 |
$this->_stop();
|
|
|
135 |
}
|
|
|
136 |
if ($result === 'o') {
|
|
|
137 |
$snapshot = false;
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
$content = $this->Schema->read($options);
|
|
|
142 |
$content['file'] = $this->params['file'];
|
|
|
143 |
|
|
|
144 |
if ($snapshot === true) {
|
|
|
145 |
$Folder =& new Folder($this->Schema->path);
|
|
|
146 |
$result = $Folder->read();
|
|
|
147 |
|
|
|
148 |
$numToUse = false;
|
|
|
149 |
if (isset($this->params['s'])) {
|
|
|
150 |
$numToUse = $this->params['s'];
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
$count = 1;
|
|
|
154 |
if (!empty($result[1])) {
|
|
|
155 |
foreach ($result[1] as $file) {
|
|
|
156 |
if (preg_match('/schema(?:[_\d]*)?\.php$/', $file)) {
|
|
|
157 |
$count++;
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
if ($numToUse !== false) {
|
|
|
163 |
if ($numToUse > $count) {
|
|
|
164 |
$count = $numToUse;
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
$fileName = rtrim($this->params['file'], '.php');
|
|
|
169 |
$content['file'] = $fileName . '_' . $count . '.php';
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
if ($this->Schema->write($content)) {
|
|
|
173 |
$this->out(sprintf(__('Schema file: %s generated', true), $content['file']));
|
|
|
174 |
$this->_stop();
|
|
|
175 |
} else {
|
|
|
176 |
$this->err(__('Schema file: %s generated', true));
|
|
|
177 |
$this->_stop();
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
/**
|
|
|
181 |
* Dump Schema object to sql file
|
|
|
182 |
* if first arg == write, file will be written to sql file
|
|
|
183 |
* or it will output sql
|
|
|
184 |
*
|
|
|
185 |
* @access public
|
|
|
186 |
*/
|
|
|
187 |
function dump() {
|
|
|
188 |
$write = false;
|
|
|
189 |
$Schema = $this->Schema->load();
|
|
|
190 |
if (!$Schema) {
|
|
|
191 |
$this->err(__('Schema could not be loaded', true));
|
|
|
192 |
$this->_stop();
|
|
|
193 |
}
|
|
|
194 |
if (!empty($this->args[0])) {
|
|
|
195 |
if ($this->args[0] == 'write') {
|
|
|
196 |
$write = Inflector::underscore($this->Schema->name);
|
|
|
197 |
} else {
|
|
|
198 |
$write = $this->args[0];
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
|
|
202 |
$contents = "#". $Schema->name ." sql generated on: " . date('Y-m-d H:m:s') . " : ". time()."\n\n";
|
|
|
203 |
$contents .= $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema);
|
|
|
204 |
if ($write) {
|
|
|
205 |
if (strpos($write, '.sql') === false) {
|
|
|
206 |
$write .= '.sql';
|
|
|
207 |
}
|
|
|
208 |
$File = new File($this->Schema->path . DS . $write, true);
|
|
|
209 |
if ($File->write($contents)) {
|
|
|
210 |
$this->out(sprintf(__('SQL dump file created in %s', true), $File->pwd()));
|
|
|
211 |
$this->_stop();
|
|
|
212 |
} else {
|
|
|
213 |
$this->err(__('SQL dump could not be created', true));
|
|
|
214 |
$this->_stop();
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
$this->out($contents);
|
|
|
218 |
return $contents;
|
|
|
219 |
}
|
|
|
220 |
/**
|
|
|
221 |
* Run database commands: create, update
|
|
|
222 |
*
|
|
|
223 |
* @access public
|
|
|
224 |
*/
|
|
|
225 |
function run() {
|
|
|
226 |
if (!isset($this->args[0])) {
|
|
|
227 |
$this->err('command not found');
|
|
|
228 |
$this->_stop();
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
$command = $this->args[0];
|
|
|
232 |
|
|
|
233 |
$this->Dispatch->shiftArgs();
|
|
|
234 |
|
|
|
235 |
$name = null;
|
|
|
236 |
if (isset($this->args[0])) {
|
|
|
237 |
$name = $this->args[0];
|
|
|
238 |
}
|
|
|
239 |
if (isset($this->params['name'])) {
|
|
|
240 |
$name = $this->params['name'];
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
if (isset($this->params['dry'])) {
|
|
|
244 |
$this->__dry = true;
|
|
|
245 |
$this->out(__('Performing a dry run.', true));
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
$options = array('name' => $name);
|
|
|
249 |
if (isset($this->params['s'])) {
|
|
|
250 |
$fileName = rtrim($this->Schema->file, '.php');
|
|
|
251 |
$options['file'] = $fileName . '_' . $this->params['s'] . '.php';
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
$Schema = $this->Schema->load($options);
|
|
|
255 |
|
|
|
256 |
if (!$Schema) {
|
|
|
257 |
$this->err(sprintf(__('%s could not be loaded', true), $this->Schema->file));
|
|
|
258 |
$this->_stop();
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
$table = null;
|
|
|
262 |
if (isset($this->args[1])) {
|
|
|
263 |
$table = $this->args[1];
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
switch ($command) {
|
|
|
267 |
case 'create':
|
|
|
268 |
$this->__create($Schema, $table);
|
|
|
269 |
break;
|
|
|
270 |
case 'update':
|
|
|
271 |
$this->__update($Schema, $table);
|
|
|
272 |
break;
|
|
|
273 |
default:
|
|
|
274 |
$this->err(__('command not found', true));
|
|
|
275 |
$this->_stop();
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
/**
|
|
|
279 |
* Create database from Schema object
|
|
|
280 |
* Should be called via the run method
|
|
|
281 |
*
|
|
|
282 |
* @access private
|
|
|
283 |
*/
|
|
|
284 |
function __create($Schema, $table = null) {
|
|
|
285 |
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
|
|
286 |
|
|
|
287 |
$drop = $create = array();
|
|
|
288 |
|
|
|
289 |
if (!$table) {
|
|
|
290 |
foreach ($Schema->tables as $table => $fields) {
|
|
|
291 |
$drop[$table] = $db->dropSchema($Schema, $table);
|
|
|
292 |
$create[$table] = $db->createSchema($Schema, $table);
|
|
|
293 |
}
|
|
|
294 |
} elseif (isset($Schema->tables[$table])) {
|
|
|
295 |
$drop[$table] = $db->dropSchema($Schema, $table);
|
|
|
296 |
$create[$table] = $db->createSchema($Schema, $table);
|
|
|
297 |
}
|
|
|
298 |
if (empty($drop) || empty($create)) {
|
|
|
299 |
$this->out(__('Schema is up to date.', true));
|
|
|
300 |
$this->_stop();
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
$this->out("\n" . __('The following table(s) will be dropped.', true));
|
|
|
304 |
$this->out(array_keys($drop));
|
|
|
305 |
|
|
|
306 |
if ('y' == $this->in(__('Are you sure you want to drop the table(s)?', true), array('y', 'n'), 'n')) {
|
|
|
307 |
$this->out('Dropping table(s).');
|
|
|
308 |
$this->__run($drop, 'drop', $Schema);
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
$this->out("\n" . __('The following table(s) will be created.', true));
|
|
|
312 |
$this->out(array_keys($create));
|
|
|
313 |
|
|
|
314 |
if ('y' == $this->in(__('Are you sure you want to create the table(s)?', true), array('y', 'n'), 'y')) {
|
|
|
315 |
$this->out('Creating table(s).');
|
|
|
316 |
$this->__run($create, 'create', $Schema);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
$this->out(__('End create.', true));
|
|
|
320 |
}
|
|
|
321 |
/**
|
|
|
322 |
* Update database with Schema object
|
|
|
323 |
* Should be called via the run method
|
|
|
324 |
*
|
|
|
325 |
* @access private
|
|
|
326 |
*/
|
|
|
327 |
function __update($Schema, $table = null) {
|
|
|
328 |
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
|
|
329 |
|
|
|
330 |
$this->out('Comparing Database to Schema...');
|
|
|
331 |
$Old = $this->Schema->read();
|
|
|
332 |
$compare = $this->Schema->compare($Old, $Schema);
|
|
|
333 |
|
|
|
334 |
$contents = array();
|
|
|
335 |
|
|
|
336 |
if (empty($table)) {
|
|
|
337 |
foreach ($compare as $table => $changes) {
|
|
|
338 |
$contents[$table] = $db->alterSchema(array($table => $changes), $table);
|
|
|
339 |
}
|
|
|
340 |
} elseif (isset($compare[$table])) {
|
|
|
341 |
$contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
if (empty($contents)) {
|
|
|
345 |
$this->out(__('Schema is up to date.', true));
|
|
|
346 |
$this->_stop();
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
$this->out("\n" . __('The following statements will run.', true));
|
|
|
350 |
$this->out(array_map('trim', $contents));
|
|
|
351 |
if ('y' == $this->in(__('Are you sure you want to alter the tables?', true), array('y', 'n'), 'n')) {
|
|
|
352 |
$this->out('');
|
|
|
353 |
$this->out(__('Updating Database...', true));
|
|
|
354 |
$this->__run($contents, 'update', $Schema);
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
$this->out(__('End update.', true));
|
|
|
358 |
}
|
|
|
359 |
/**
|
|
|
360 |
* Runs sql from __create() or __update()
|
|
|
361 |
*
|
|
|
362 |
* @access private
|
|
|
363 |
*/
|
|
|
364 |
function __run($contents, $event, $Schema) {
|
|
|
365 |
if (empty($contents)) {
|
|
|
366 |
$this->err(__('Sql could not be run', true));
|
|
|
367 |
return;
|
|
|
368 |
}
|
|
|
369 |
Configure::write('debug', 2);
|
|
|
370 |
$db =& ConnectionManager::getDataSource($this->Schema->connection);
|
|
|
371 |
$db->fullDebug = true;
|
|
|
372 |
|
|
|
373 |
$errors = array();
|
|
|
374 |
foreach ($contents as $table => $sql) {
|
|
|
375 |
if (empty($sql)) {
|
|
|
376 |
$this->out(sprintf(__('%s is up to date.', true), $table));
|
|
|
377 |
} else {
|
|
|
378 |
if ($this->__dry === true) {
|
|
|
379 |
$this->out(sprintf(__('Dry run for %s :', true), $table));
|
|
|
380 |
$this->out($sql);
|
|
|
381 |
} else {
|
|
|
382 |
if (!$Schema->before(array($event => $table))) {
|
|
|
383 |
return false;
|
|
|
384 |
}
|
|
|
385 |
if (!$db->_execute($sql)) {
|
|
|
386 |
$error = $table . ': ' . $db->lastError();
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
$Schema->after(array($event => $table, 'errors'=> $errors));
|
|
|
390 |
|
|
|
391 |
if (isset($error)) {
|
|
|
392 |
$this->out($error);
|
|
|
393 |
} elseif ($this->__dry !== true) {
|
|
|
394 |
$this->out(sprintf(__('%s updated.', true), $table));
|
|
|
395 |
}
|
|
|
396 |
}
|
|
|
397 |
}
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
/**
|
|
|
401 |
* Displays help contents
|
|
|
402 |
*
|
|
|
403 |
* @access public
|
|
|
404 |
*/
|
|
|
405 |
function help() {
|
|
|
406 |
$this->out("The Schema Shell generates a schema object from \n\t\tthe database and updates the database from the schema.");
|
|
|
407 |
$this->hr();
|
|
|
408 |
$this->out("Usage: cake schema <command> <arg1> <arg2>...");
|
|
|
409 |
$this->hr();
|
|
|
410 |
$this->out('Params:');
|
|
|
411 |
$this->out("\n\t-connection <config>\n\t\tset db config <config>. uses 'default' if none is specified");
|
|
|
412 |
$this->out("\n\t-path <dir>\n\t\tpath <dir> to read and write schema.php.\n\t\tdefault path: ". $this->Schema->path);
|
|
|
413 |
$this->out("\n\t-name <name>\n\t\tclassname to use.");
|
|
|
414 |
$this->out("\n\t-file <name>\n\t\tfile <name> to read and write.\n\t\tdefault file: ". $this->Schema->file);
|
|
|
415 |
$this->out("\n\t-s <number>\n\t\tsnapshot <number> to use for run.");
|
|
|
416 |
$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.");
|
|
|
417 |
$this->out("\n\t-f\n\t\tforce 'generate' to create a new schema.");
|
|
|
418 |
$this->out('Commands:');
|
|
|
419 |
$this->out("\n\tschema help\n\t\tshows this help message.");
|
|
|
420 |
$this->out("\n\tschema view\n\t\tread and output contents of schema file");
|
|
|
421 |
$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\n\t\twhich you can use with the -s parameter in the other operations.");
|
|
|
422 |
$this->out("\n\tschema dump <filename>\n\t\tDump database sql based on schema file to <filename>. \n\t\tIf <filename> is write, schema dump will be written to a file\n\t\tthat has the same name as the app directory.");
|
|
|
423 |
$this->out("\n\tschema run create <schema> <table>\n\t\tDrop and create tables 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");
|
|
|
424 |
$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");
|
|
|
425 |
$this->out("");
|
|
|
426 |
$this->_stop();
|
|
|
427 |
}
|
|
|
428 |
}
|
|
|
429 |
?>
|