Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://cakephp.org CakePHP(tm) Project
12
 * @since         CakePHP(tm) v 1.2.0.5550
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
 
16
App::uses('AppShell', 'Console/Command');
17
App::uses('File', 'Utility');
18
App::uses('Folder', 'Utility');
19
App::uses('CakeSchema', 'Model');
20
 
21
/**
22
 * Schema is a command-line database management utility for automating programmer chores.
23
 *
24
 * Schema is CakePHP's database management utility. This helps you maintain versions of
25
 * of your database.
26
 *
27
 * @package       Cake.Console.Command
28
 * @link          http://book.cakephp.org/2.0/en/console-and-shells/schema-management-and-migrations.html
29
 */
30
class SchemaShell extends AppShell {
31
 
32
/**
33
 * Schema class being used.
34
 *
35
 * @var CakeSchema
36
 */
37
	public $Schema;
38
 
39
/**
40
 * is this a dry run?
41
 *
42
 * @var boolean
43
 */
44
	protected $_dry = null;
45
 
46
/**
47
 * Override startup
48
 *
49
 * @return void
50
 */
51
	public function startup() {
52
		$this->_welcome();
53
		$this->out('Cake Schema Shell');
54
		$this->hr();
55
 
56
		Configure::write('Cache.disable', 1);
57
 
58
		$name = $path = $connection = $plugin = null;
59
		if (!empty($this->params['name'])) {
60
			$name = $this->params['name'];
61
		} elseif (!empty($this->args[0]) && $this->args[0] !== 'snapshot') {
62
			$name = $this->params['name'] = $this->args[0];
63
		}
64
 
65
		if (strpos($name, '.')) {
66
			list($this->params['plugin'], $splitName) = pluginSplit($name);
67
			$name = $this->params['name'] = $splitName;
68
		}
69
 
70
		$defaultFile = 'schema.php';
71
		if (empty($this->params['file'])) {
72
			$this->params['file'] = $defaultFile;
73
		}
74
		if ($name && $this->params['file'] === $defaultFile) {
75
			$this->params['file'] = Inflector::underscore($name);
76
		}
77
		if (strpos($this->params['file'], '.php') === false) {
78
			$this->params['file'] .= '.php';
79
		}
80
		$file = $this->params['file'];
81
 
82
		if (!empty($this->params['path'])) {
83
			$path = $this->params['path'];
84
		}
85
 
86
		if (!empty($this->params['connection'])) {
87
			$connection = $this->params['connection'];
88
		}
89
		if (!empty($this->params['plugin'])) {
90
			$plugin = $this->params['plugin'];
91
			if (empty($name)) {
92
				$name = $plugin;
93
			}
94
		}
95
		$name = Inflector::classify($name);
96
		$this->Schema = new CakeSchema(compact('name', 'path', 'file', 'connection', 'plugin'));
97
	}
98
 
99
/**
100
 * Read and output contents of schema object
101
 * path to read as second arg
102
 *
103
 * @return void
104
 */
105
	public function view() {
106
		$File = new File($this->Schema->path . DS . $this->params['file']);
107
		if ($File->exists()) {
108
			$this->out($File->read());
109
			return $this->_stop();
110
		}
111
		$file = $this->Schema->path . DS . $this->params['file'];
112
		$this->err(__d('cake_console', 'Schema file (%s) could not be found.', $file));
113
		return $this->_stop();
114
	}
115
 
116
/**
117
 * Read database and Write schema object
118
 * accepts a connection as first arg or path to save as second arg
119
 *
120
 * @return void
121
 */
122
	public function generate() {
123
		$this->out(__d('cake_console', 'Generating Schema...'));
124
		$options = array();
125
		if ($this->params['force']) {
126
			$options['models'] = false;
127
		} elseif (!empty($this->params['models'])) {
128
			$options['models'] = String::tokenize($this->params['models']);
129
		}
130
 
131
		$snapshot = false;
132
		if (isset($this->args[0]) && $this->args[0] === 'snapshot') {
133
			$snapshot = true;
134
		}
135
 
136
		if (!$snapshot && file_exists($this->Schema->path . DS . $this->params['file'])) {
137
			$snapshot = true;
138
			$prompt = __d('cake_console', "Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?");
139
			$result = strtolower($this->in($prompt, array('o', 's', 'q'), 's'));
140
			if ($result === 'q') {
141
				return $this->_stop();
142
			}
143
			if ($result === 'o') {
144
				$snapshot = false;
145
			}
146
		}
147
 
148
		$cacheDisable = Configure::read('Cache.disable');
149
		Configure::write('Cache.disable', true);
150
 
151
		$content = $this->Schema->read($options);
152
		$content['file'] = $this->params['file'];
153
 
154
		Configure::write('Cache.disable', $cacheDisable);
155
 
156
		if (!empty($this->params['exclude']) && !empty($content)) {
157
			$excluded = String::tokenize($this->params['exclude']);
158
			foreach ($excluded as $table) {
159
				unset($content['tables'][$table]);
160
			}
161
		}
162
 
163
		if ($snapshot === true) {
164
			$fileName = rtrim($this->params['file'], '.php');
165
			$Folder = new Folder($this->Schema->path);
166
			$result = $Folder->read();
167
 
168
			$numToUse = false;
169
			if (isset($this->params['snapshot'])) {
170
				$numToUse = $this->params['snapshot'];
171
			}
172
 
173
			$count = 0;
174
			if (!empty($result[1])) {
175
				foreach ($result[1] as $file) {
176
					if (preg_match('/' . preg_quote($fileName) . '(?:[_\d]*)?\.php$/', $file)) {
177
						$count++;
178
					}
179
				}
180
			}
181
 
182
			if ($numToUse !== false) {
183
				if ($numToUse > $count) {
184
					$count = $numToUse;
185
				}
186
			}
187
 
188
			$content['file'] = $fileName . '_' . $count . '.php';
189
		}
190
 
191
		if ($this->Schema->write($content)) {
192
			$this->out(__d('cake_console', 'Schema file: %s generated', $content['file']));
193
			return $this->_stop();
194
		}
195
		$this->err(__d('cake_console', 'Schema file: %s generated'));
196
		return $this->_stop();
197
	}
198
 
199
/**
200
 * Dump Schema object to sql file
201
 * Use the `write` param to enable and control SQL file output location.
202
 * Simply using -write will write the sql file to the same dir as the schema file.
203
 * If -write contains a full path name the file will be saved there. If -write only
204
 * contains no DS, that will be used as the file name, in the same dir as the schema file.
205
 *
206
 * @return string
207
 */
208
	public function dump() {
209
		$write = false;
210
		$Schema = $this->Schema->load();
211
		if (!$Schema) {
212
			$this->err(__d('cake_console', 'Schema could not be loaded'));
213
			return $this->_stop();
214
		}
215
		if (!empty($this->params['write'])) {
216
			if ($this->params['write'] == 1) {
217
				$write = Inflector::underscore($this->Schema->name);
218
			} else {
219
				$write = $this->params['write'];
220
			}
221
		}
222
		$db = ConnectionManager::getDataSource($this->Schema->connection);
223
		$contents = "\n\n" . $db->dropSchema($Schema) . "\n\n" . $db->createSchema($Schema);
224
 
225
		if ($write) {
226
			if (strpos($write, '.sql') === false) {
227
				$write .= '.sql';
228
			}
229
			if (strpos($write, DS) !== false) {
230
				$File = new File($write, true);
231
			} else {
232
				$File = new File($this->Schema->path . DS . $write, true);
233
			}
234
 
235
			if ($File->write($contents)) {
236
				$this->out(__d('cake_console', 'SQL dump file created in %s', $File->pwd()));
237
				return $this->_stop();
238
			}
239
			$this->err(__d('cake_console', 'SQL dump could not be created'));
240
			return $this->_stop();
241
		}
242
		$this->out($contents);
243
		return $contents;
244
	}
245
 
246
/**
247
 * Run database create commands. Alias for run create.
248
 *
249
 * @return void
250
 */
251
	public function create() {
252
		list($Schema, $table) = $this->_loadSchema();
253
		$this->_create($Schema, $table);
254
	}
255
 
256
/**
257
 * Run database create commands. Alias for run create.
258
 *
259
 * @return void
260
 */
261
	public function update() {
262
		list($Schema, $table) = $this->_loadSchema();
263
		$this->_update($Schema, $table);
264
	}
265
 
266
/**
267
 * Prepares the Schema objects for database operations.
268
 *
269
 * @return void
270
 */
271
	protected function _loadSchema() {
272
		$name = $plugin = null;
273
		if (!empty($this->params['name'])) {
274
			$name = $this->params['name'];
275
		}
276
		if (!empty($this->params['plugin'])) {
277
			$plugin = $this->params['plugin'];
278
		}
279
 
280
		if (!empty($this->params['dry'])) {
281
			$this->_dry = true;
282
			$this->out(__d('cake_console', 'Performing a dry run.'));
283
		}
284
 
285
		$options = array('name' => $name, 'plugin' => $plugin);
286
		if (!empty($this->params['snapshot'])) {
287
			$fileName = rtrim($this->Schema->file, '.php');
288
			$options['file'] = $fileName . '_' . $this->params['snapshot'] . '.php';
289
		}
290
 
291
		$Schema = $this->Schema->load($options);
292
 
293
		if (!$Schema) {
294
			$this->err(__d('cake_console', 'The chosen schema could not be loaded. Attempted to load:'));
295
			$this->err(__d('cake_console', 'File: %s', $this->Schema->path . DS . $this->Schema->file));
296
			$this->err(__d('cake_console', 'Name: %s', $this->Schema->name));
297
			return $this->_stop();
298
		}
299
		$table = null;
300
		if (isset($this->args[1])) {
301
			$table = $this->args[1];
302
		}
303
		return array(&$Schema, $table);
304
	}
305
 
306
/**
307
 * Create database from Schema object
308
 * Should be called via the run method
309
 *
310
 * @param CakeSchema $Schema
311
 * @param string $table
312
 * @return void
313
 */
314
	protected function _create(CakeSchema $Schema, $table = null) {
315
		$db = ConnectionManager::getDataSource($this->Schema->connection);
316
 
317
		$drop = $create = array();
318
 
319
		if (!$table) {
320
			foreach ($Schema->tables as $table => $fields) {
321
				$drop[$table] = $db->dropSchema($Schema, $table);
322
				$create[$table] = $db->createSchema($Schema, $table);
323
			}
324
		} elseif (isset($Schema->tables[$table])) {
325
			$drop[$table] = $db->dropSchema($Schema, $table);
326
			$create[$table] = $db->createSchema($Schema, $table);
327
		}
328
		if (empty($drop) || empty($create)) {
329
			$this->out(__d('cake_console', 'Schema is up to date.'));
330
			return $this->_stop();
331
		}
332
 
333
		$this->out("\n" . __d('cake_console', 'The following table(s) will be dropped.'));
334
		$this->out(array_keys($drop));
335
 
336
		if ($this->in(__d('cake_console', 'Are you sure you want to drop the table(s)?'), array('y', 'n'), 'n') === 'y') {
337
			$this->out(__d('cake_console', 'Dropping table(s).'));
338
			$this->_run($drop, 'drop', $Schema);
339
		}
340
 
341
		$this->out("\n" . __d('cake_console', 'The following table(s) will be created.'));
342
		$this->out(array_keys($create));
343
 
344
		if ($this->in(__d('cake_console', 'Are you sure you want to create the table(s)?'), array('y', 'n'), 'y') === 'y') {
345
			$this->out(__d('cake_console', 'Creating table(s).'));
346
			$this->_run($create, 'create', $Schema);
347
		}
348
		$this->out(__d('cake_console', 'End create.'));
349
	}
350
 
351
/**
352
 * Update database with Schema object
353
 * Should be called via the run method
354
 *
355
 * @param CakeSchema $Schema
356
 * @param string $table
357
 * @return void
358
 */
359
	protected function _update(&$Schema, $table = null) {
360
		$db = ConnectionManager::getDataSource($this->Schema->connection);
361
 
362
		$this->out(__d('cake_console', 'Comparing Database to Schema...'));
363
		$options = array();
364
		if (isset($this->params['force'])) {
365
			$options['models'] = false;
366
		}
367
		$Old = $this->Schema->read($options);
368
		$compare = $this->Schema->compare($Old, $Schema);
369
 
370
		$contents = array();
371
 
372
		if (empty($table)) {
373
			foreach ($compare as $table => $changes) {
374
				if (isset($compare[$table]['create'])) {
375
					$contents[$table] = $db->createSchema($Schema, $table);
376
				} else {
377
					$contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
378
				}
379
			}
380
		} elseif (isset($compare[$table])) {
381
			if (isset($compare[$table]['create'])) {
382
				$contents[$table] = $db->createSchema($Schema, $table);
383
			} else {
384
				$contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
385
			}
386
		}
387
 
388
		if (empty($contents)) {
389
			$this->out(__d('cake_console', 'Schema is up to date.'));
390
			return $this->_stop();
391
		}
392
 
393
		$this->out("\n" . __d('cake_console', 'The following statements will run.'));
394
		$this->out(array_map('trim', $contents));
395
		if ($this->in(__d('cake_console', 'Are you sure you want to alter the tables?'), array('y', 'n'), 'n') === 'y') {
396
			$this->out();
397
			$this->out(__d('cake_console', 'Updating Database...'));
398
			$this->_run($contents, 'update', $Schema);
399
		}
400
 
401
		$this->out(__d('cake_console', 'End update.'));
402
	}
403
 
404
/**
405
 * Runs sql from _create() or _update()
406
 *
407
 * @param array $contents
408
 * @param string $event
409
 * @param CakeSchema $Schema
410
 * @return void
411
 */
412
	protected function _run($contents, $event, CakeSchema $Schema) {
413
		if (empty($contents)) {
414
			$this->err(__d('cake_console', 'Sql could not be run'));
415
			return;
416
		}
417
		Configure::write('debug', 2);
418
		$db = ConnectionManager::getDataSource($this->Schema->connection);
419
 
420
		foreach ($contents as $table => $sql) {
421
			if (empty($sql)) {
422
				$this->out(__d('cake_console', '%s is up to date.', $table));
423
			} else {
424
				if ($this->_dry === true) {
425
					$this->out(__d('cake_console', 'Dry run for %s :', $table));
426
					$this->out($sql);
427
				} else {
428
					if (!$Schema->before(array($event => $table))) {
429
						return false;
430
					}
431
					$error = null;
432
					try {
433
						$db->execute($sql);
434
					} catch (PDOException $e) {
435
						$error = $table . ': ' . $e->getMessage();
436
					}
437
 
438
					$Schema->after(array($event => $table, 'errors' => $error));
439
 
440
					if (!empty($error)) {
441
						$this->err($error);
442
					} else {
443
						$this->out(__d('cake_console', '%s updated.', $table));
444
					}
445
				}
446
			}
447
		}
448
	}
449
 
450
/**
451
 * get the option parser
452
 *
453
 * @return void
454
 */
455
	public function getOptionParser() {
456
		$plugin = array(
457
			'short' => 'p',
458
			'help' => __d('cake_console', 'The plugin to use.'),
459
		);
460
		$connection = array(
461
			'short' => 'c',
462
			'help' => __d('cake_console', 'Set the db config to use.'),
463
			'default' => 'default'
464
		);
465
		$path = array(
466
			'help' => __d('cake_console', 'Path to read and write schema.php'),
467
			'default' => APP . 'Config' . DS . 'Schema'
468
		);
469
		$file = array(
470
			'help' => __d('cake_console', 'File name to read and write.'),
471
			'default' => 'schema.php'
472
		);
473
		$name = array(
474
			'help' => __d('cake_console', 'Classname to use. If its Plugin.class, both name and plugin options will be set.')
475
		);
476
		$snapshot = array(
477
			'short' => 's',
478
			'help' => __d('cake_console', 'Snapshot number to use/make.')
479
		);
480
		$models = array(
481
			'short' => 'm',
482
			'help' => __d('cake_console', 'Specify models as comma separated list.'),
483
		);
484
		$dry = array(
485
			'help' => __d('cake_console', 'Perform a dry run on create and update commands. Queries will be output instead of run.'),
486
			'boolean' => true
487
		);
488
		$force = array(
489
			'short' => 'f',
490
			'help' => __d('cake_console', 'Force "generate" to create a new schema'),
491
			'boolean' => true
492
		);
493
		$write = array(
494
			'help' => __d('cake_console', 'Write the dumped SQL to a file.')
495
		);
496
		$exclude = array(
497
			'help' => __d('cake_console', 'Tables to exclude as comma separated list.')
498
		);
499
 
500
		$parser = parent::getOptionParser();
501
		$parser->description(
502
			__d('cake_console', 'The Schema Shell generates a schema object from the database and updates the database from the schema.')
503
		)->addSubcommand('view', array(
504
			'help' => __d('cake_console', 'Read and output the contents of a schema file'),
505
			'parser' => array(
506
				'options' => compact('plugin', 'path', 'file', 'name', 'connection'),
507
				'arguments' => compact('name')
508
			)
509
		))->addSubcommand('generate', array(
510
			'help' => __d('cake_console', 'Reads from --connection and writes to --path. Generate snapshots with -s'),
511
			'parser' => array(
512
				'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'snapshot', 'force', 'models', 'exclude'),
513
				'arguments' => array(
514
					'snapshot' => array('help' => __d('cake_console', 'Generate a snapshot.'))
515
				)
516
			)
517
		))->addSubcommand('dump', array(
518
			'help' => __d('cake_console', 'Dump database SQL based on a schema file to stdout.'),
519
			'parser' => array(
520
				'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'write'),
521
				'arguments' => compact('name')
522
			)
523
		))->addSubcommand('create', array(
524
			'help' => __d('cake_console', 'Drop and create tables based on the schema file.'),
525
			'parser' => array(
526
				'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot'),
527
				'args' => array(
528
					'name' => array(
529
						'help' => __d('cake_console', 'Name of schema to use.')
530
					),
531
					'table' => array(
532
						'help' => __d('cake_console', 'Only create the specified table.')
533
					)
534
				)
535
			)
536
		))->addSubcommand('update', array(
537
			'help' => __d('cake_console', 'Alter the tables based on the schema file.'),
538
			'parser' => array(
539
				'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'dry', 'snapshot', 'force'),
540
				'args' => array(
541
					'name' => array(
542
						'help' => __d('cake_console', 'Name of schema to use.')
543
					),
544
					'table' => array(
545
						'help' => __d('cake_console', 'Only create the specified table.')
546
					)
547
				)
548
			)
549
		));
550
		return $parser;
551
	}
552
 
553
}