1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: App::import('File');
24: App::import('Model', 'Schema');
25: 26: 27: 28: 29: 30: 31:
32: class SchemaShell extends Shell {
33: 34: 35: 36: 37: 38:
39: var $__dry = null;
40: 41: 42: 43: 44:
45: function initialize() {
46: $this->_welcome();
47: $this->out('Cake Schema Shell');
48: $this->hr();
49: }
50: 51: 52: 53: 54:
55: function startup() {
56: $name = null;
57: if (!empty($this->params['name'])) {
58: $name = $this->params['name'];
59: $this->params['file'] = Inflector::underscore($name);
60: }
61:
62: $path = null;
63: if (!empty($this->params['path'])) {
64: $path = $this->params['path'];
65: }
66:
67: $file = null;
68: if (empty($this->params['file'])) {
69: $this->params['file'] = 'schema.php';
70: }
71: if (strpos($this->params['file'], '.php') === false) {
72: $this->params['file'] .= '.php';
73: }
74: $file = $this->params['file'];
75:
76: $connection = null;
77: if (!empty($this->params['connection'])) {
78: $connection = $this->params['connection'];
79: }
80:
81: $this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection'));
82: }
83: 84: 85: 86: 87:
88: function main() {
89: $this->help();
90: }
91: 92: 93: 94: 95: 96:
97: function view() {
98: $File = new File($this->Schema->path . DS . $this->params['file']);
99: if ($File->exists()) {
100: $this->out($File->read());
101: $this->_stop();
102: } else {
103: $this->err(__('Schema could not be found', true));
104: $this->_stop();
105: }
106: }
107: 108: 109: 110: 111: 112:
113: function generate() {
114: $this->out(__('Generating Schema...', true));
115: $options = array();
116: if (isset($this->params['f'])) {
117: $options = array('models' => false);
118: }
119:
120: $snapshot = false;
121: if (isset($this->args[0]) && $this->args[0] === 'snapshot') {
122: $snapshot = true;
123: }
124:
125: if (!$snapshot && file_exists($this->Schema->path . DS . $this->params['file'])) {
126: $snapshot = true;
127: $result = strtolower($this->in("Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?", array('o', 's', 'q'), 's'));
128: if ($result === 'q') {
129: return $this->_stop();
130: }
131: if ($result === 'o') {
132: $snapshot = false;
133: }
134: }
135:
136: $content = $this->Schema->read($options);
137: $content['file'] = $this->params['file'];
138:
139: if ($snapshot === true) {
140: $Folder =& new Folder($this->Schema->path);
141: $result = $Folder->read();
142:
143: $numToUse = false;
144: if (isset($this->params['s'])) {
145: $numToUse = $this->params['s'];
146: }
147:
148: $count = 1;
149: if (!empty($result[1])) {
150: foreach ($result[1] as $file) {
151: if (preg_match('/schema(?:[_\d]*)?\.php$/', $file)) {
152: $count++;
153: }
154: }
155: }
156:
157: if ($numToUse !== false) {
158: if ($numToUse > $count) {
159: $count = $numToUse;
160: }
161: }
162:
163: $fileName = rtrim($this->params['file'], '.php');
164: $content['file'] = $fileName . '_' . $count . '.php';
165: }
166:
167: if ($this->Schema->write($content)) {
168: $this->out(sprintf(__('Schema file: %s generated', true), $content['file']));
169: $this->_stop();
170: } else {
171: $this->err(__('Schema file: %s generated', true));
172: $this->_stop();
173: }
174: }
175: 176: 177: 178: 179: 180: 181:
182: function dump() {
183: $write = false;
184: $Schema = $this->Schema->load();
185: if (!$Schema) {
186: $this->err(__('Schema could not be loaded', true));
187: $this->_stop();
188: }
189: if (!empty($this->args[0])) {
190: if ($this->args[0] == 'write') {
191: $write = Inflector::underscore($this->Schema->name);
192: } else {
193: $write = $this->args[0];
194: }
195: }
196: $db =& ConnectionManager::getDataSource($this->Schema->connection);
197: $contents = "#" . $Schema->name . " sql generated on: " . date('Y-m-d H:i:s') . " : " . time() . "\n\n";
198: $contents .= $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema);
199: if ($write) {
200: if (strpos($write, '.sql') === false) {
201: $write .= '.sql';
202: }
203: $File = new File($this->Schema->path . DS . $write, true);
204: if ($File->write($contents)) {
205: $this->out(sprintf(__('SQL dump file created in %s', true), $File->pwd()));
206: $this->_stop();
207: } else {
208: $this->err(__('SQL dump could not be created', true));
209: $this->_stop();
210: }
211: }
212: $this->out($contents);
213: return $contents;
214: }
215: 216: 217: 218: 219:
220: function run() {
221: if (!isset($this->args[0])) {
222: $this->err(__('Command not found', true));
223: $this->_stop();
224: }
225:
226: $command = $this->args[0];
227:
228: $this->Dispatch->shiftArgs();
229:
230: $name = null;
231: if (isset($this->args[0])) {
232: $name = $this->args[0];
233: }
234: if (isset($this->params['name'])) {
235: $name = $this->params['name'];
236: }
237:
238: if (isset($this->params['dry'])) {
239: $this->__dry = true;
240: $this->out(__('Performing a dry run.', true));
241: }
242:
243: $options = array('name' => $name);
244: if (isset($this->params['s'])) {
245: $fileName = rtrim($this->Schema->file, '.php');
246: $options['file'] = $fileName . '_' . $this->params['s'] . '.php';
247: }
248:
249: $Schema = $this->Schema->load($options);
250:
251: if (!$Schema) {
252: $this->err(sprintf(__('%s could not be loaded', true), $this->Schema->file));
253: $this->_stop();
254: }
255:
256: $table = null;
257: if (isset($this->args[1])) {
258: $table = $this->args[1];
259: }
260:
261: switch ($command) {
262: case 'create':
263: $this->__create($Schema, $table);
264: break;
265: case 'update':
266: $this->__update($Schema, $table);
267: break;
268: default:
269: $this->err(__('Command not found', true));
270: $this->_stop();
271: }
272: }
273: 274: 275: 276: 277: 278:
279: function __create(&$Schema, $table = null) {
280: $db =& ConnectionManager::getDataSource($this->Schema->connection);
281:
282: $drop = $create = array();
283:
284: if (!$table) {
285: foreach ($Schema->tables as $table => $fields) {
286: $drop[$table] = $db->dropSchema($Schema, $table);
287: $create[$table] = $db->createSchema($Schema, $table);
288: }
289: } elseif (isset($Schema->tables[$table])) {
290: $drop[$table] = $db->dropSchema($Schema, $table);
291: $create[$table] = $db->createSchema($Schema, $table);
292: }
293: if (empty($drop) || empty($create)) {
294: $this->out(__('Schema is up to date.', true));
295: $this->_stop();
296: }
297:
298: $this->out("\n" . __('The following table(s) will be dropped.', true));
299: $this->out(array_keys($drop));
300:
301: if ('y' == $this->in(__('Are you sure you want to drop the table(s)?', true), array('y', 'n'), 'n')) {
302: $this->out(__('Dropping table(s).', true));
303: $this->__run($drop, 'drop', $Schema);
304: }
305:
306: $this->out("\n" . __('The following table(s) will be created.', true));
307: $this->out(array_keys($create));
308:
309: if ('y' == $this->in(__('Are you sure you want to create the table(s)?', true), array('y', 'n'), 'y')) {
310: $this->out(__('Creating table(s).', true));
311: $this->__run($create, 'create', $Schema);
312: }
313:
314: $this->out(__('End create.', true));
315: }
316: 317: 318: 319: 320: 321:
322: function __update(&$Schema, $table = null) {
323: $db =& ConnectionManager::getDataSource($this->Schema->connection);
324:
325: $this->out(__('Comparing Database to Schema...', true));
326: $options = array();
327: if (isset($this->params['f'])) {
328: $options['models'] = false;
329: }
330: $Old = $this->Schema->read($options);
331: $compare = $this->Schema->compare($Old, $Schema);
332:
333: $contents = array();
334:
335: if (empty($table)) {
336: foreach ($compare as $table => $changes) {
337: $contents[$table] = $db->alterSchema(array($table => $changes), $table);
338: }
339: } elseif (isset($compare[$table])) {
340: $contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
341: }
342:
343: if (empty($contents)) {
344: $this->out(__('Schema is up to date.', true));
345: $this->_stop();
346: }
347:
348: $this->out("\n" . __('The following statements will run.', true));
349: $this->out(array_map('trim', $contents));
350: if ('y' == $this->in(__('Are you sure you want to alter the tables?', true), array('y', 'n'), 'n')) {
351: $this->out('');
352: $this->out(__('Updating Database...', true));
353: $this->__run($contents, 'update', $Schema);
354: }
355:
356: $this->out(__('End update.', true));
357: }
358: 359: 360: 361: 362:
363: function __run($contents, $event, &$Schema) {
364: if (empty($contents)) {
365: $this->err(__('Sql could not be run', true));
366: return;
367: }
368: Configure::write('debug', 2);
369: $db =& ConnectionManager::getDataSource($this->Schema->connection);
370: $db->fullDebug = true;
371:
372: foreach ($contents as $table => $sql) {
373: if (empty($sql)) {
374: $this->out(sprintf(__('%s is up to date.', true), $table));
375: } else {
376: if ($this->__dry === true) {
377: $this->out(sprintf(__('Dry run for %s :', true), $table));
378: $this->out($sql);
379: } else {
380: if (!$Schema->before(array($event => $table))) {
381: return false;
382: }
383: $error = null;
384: if (!$db->execute($sql)) {
385: $error = $table . ': ' . $db->lastError();
386: }
387:
388: $Schema->after(array($event => $table, 'errors' => $error));
389:
390: if (!empty($error)) {
391: $this->out($error);
392: } else {
393: $this->out(sprintf(__('%s updated.', true), $table));
394: }
395: }
396: }
397: }
398: }
399: 400: 401: 402: 403:
404: function help() {
405: $this->out("The Schema Shell generates a schema object from");
406: $this->out("the 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: ?>