~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/libs/model/cake_schema.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Schema database management for CakePHP.
 
4
 *
 
5
 * PHP versions 4 and 5
 
6
 *
 
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
8
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
9
 *
 
10
 * Licensed under The MIT License
 
11
 * Redistributions of files must retain the above copyright notice.
 
12
 *
 
13
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
14
 * @link          http://cakephp.org CakePHP(tm) Project
 
15
 * @package       cake
 
16
 * @subpackage    cake.cake.libs.model
 
17
 * @since         CakePHP(tm) v 1.2.0.5550
 
18
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 
19
 */
 
20
App::import('Core', array('Model', 'ConnectionManager'));
 
21
 
 
22
/**
 
23
 * Base Class for Schema management
 
24
 *
 
25
 * @package       cake
 
26
 * @subpackage    cake.cake.libs.model
 
27
 */
 
28
class CakeSchema extends Object {
 
29
 
 
30
/**
 
31
 * Name of the App Schema
 
32
 *
 
33
 * @var string
 
34
 * @access public
 
35
 */
 
36
        var $name = null;
 
37
 
 
38
/**
 
39
 * Path to write location
 
40
 *
 
41
 * @var string
 
42
 * @access public
 
43
 */
 
44
        var $path = null;
 
45
 
 
46
/**
 
47
 * File to write
 
48
 *
 
49
 * @var string
 
50
 * @access public
 
51
 */
 
52
        var $file = 'schema.php';
 
53
 
 
54
/**
 
55
 * Connection used for read
 
56
 *
 
57
 * @var string
 
58
 * @access public
 
59
 */
 
60
        var $connection = 'default';
 
61
 
 
62
/**
 
63
 * plugin name.
 
64
 *
 
65
 * @var string
 
66
 */
 
67
        var $plugin = null;
 
68
 
 
69
/**
 
70
 * Set of tables
 
71
 *
 
72
 * @var array
 
73
 * @access public
 
74
 */
 
75
        var $tables = array();
 
76
 
 
77
/**
 
78
 * Constructor
 
79
 *
 
80
 * @param array $options optional load object properties
 
81
 */
 
82
        function __construct($options = array()) {
 
83
                parent::__construct();
 
84
 
 
85
                if (empty($options['name'])) {
 
86
                        $this->name = preg_replace('/schema$/i', '', get_class($this));
 
87
                }
 
88
                if (!empty($options['plugin'])) {
 
89
                        $this->plugin = $options['plugin'];
 
90
                }
 
91
 
 
92
                if (strtolower($this->name) === 'cake') {
 
93
                        $this->name = Inflector::camelize(Inflector::slug(Configure::read('App.dir')));
 
94
                }
 
95
 
 
96
                if (empty($options['path'])) {
 
97
                        if (is_dir(CONFIGS . 'schema')) {
 
98
                                $this->path = CONFIGS . 'schema';
 
99
                        } else {
 
100
                                $this->path = CONFIGS . 'sql';
 
101
                        }
 
102
                }
 
103
 
 
104
                $options = array_merge(get_object_vars($this), $options);
 
105
                $this->_build($options);
 
106
        }
 
107
 
 
108
/**
 
109
 * Builds schema object properties
 
110
 *
 
111
 * @param array $data loaded object properties
 
112
 * @return void
 
113
 * @access protected
 
114
 */
 
115
        function _build($data) {
 
116
                $file = null;
 
117
                foreach ($data as $key => $val) {
 
118
                        if (!empty($val)) {
 
119
                                if (!in_array($key, array('plugin', 'name', 'path', 'file', 'connection', 'tables', '_log'))) {
 
120
                                        $this->tables[$key] = $val;
 
121
                                        unset($this->{$key});
 
122
                                } elseif ($key !== 'tables') {
 
123
                                        if ($key === 'name' && $val !== $this->name && !isset($data['file'])) {
 
124
                                                $file = Inflector::underscore($val) . '.php';
 
125
                                        }
 
126
                                        $this->{$key} = $val;
 
127
                                }
 
128
                        }
 
129
                }
 
130
                if (file_exists($this->path . DS . $file) && is_file($this->path . DS . $file)) {
 
131
                        $this->file = $file;
 
132
                } elseif (!empty($this->plugin)) {
 
133
                        $this->path = App::pluginPath($this->plugin) . 'config' . DS . 'schema';
 
134
                }
 
135
        }
 
136
 
 
137
/**
 
138
 * Before callback to be implemented in subclasses
 
139
 *
 
140
 * @param array $events schema object properties
 
141
 * @return boolean Should process continue
 
142
 * @access public
 
143
 */
 
144
        function before($event = array()) {
 
145
                return true;
 
146
        }
 
147
 
 
148
/**
 
149
 * After callback to be implemented in subclasses
 
150
 *
 
151
 * @param array $events schema object properties
 
152
 * @access public
 
153
 */
 
154
        function after($event = array()) {
 
155
        }
 
156
 
 
157
/**
 
158
 * Reads database and creates schema tables
 
159
 *
 
160
 * @param array $options schema object properties
 
161
 * @return array Set of name and tables
 
162
 * @access public
 
163
 */
 
164
        function &load($options = array()) {
 
165
                if (is_string($options)) {
 
166
                        $options = array('path' => $options);
 
167
                }
 
168
 
 
169
                $this->_build($options);
 
170
                extract(get_object_vars($this));
 
171
 
 
172
                $class =  $name .'Schema';
 
173
 
 
174
                if (!class_exists($class)) {
 
175
                        if (file_exists($path . DS . $file) && is_file($path . DS . $file)) {
 
176
                                require_once($path . DS . $file);
 
177
                        } elseif (file_exists($path . DS . 'schema.php') && is_file($path . DS . 'schema.php')) {
 
178
                                require_once($path . DS . 'schema.php');
 
179
                        }
 
180
                }
 
181
 
 
182
                if (class_exists($class)) {
 
183
                        $Schema =& new $class($options);
 
184
                        return $Schema;
 
185
                }
 
186
                $false = false;
 
187
                return $false;
 
188
        }
 
189
 
 
190
/**
 
191
 * Reads database and creates schema tables
 
192
 *
 
193
 * Options
 
194
 *
 
195
 * - 'connection' - the db connection to use
 
196
 * - 'name' - name of the schema
 
197
 * - 'models' - a list of models to use, or false to ignore models
 
198
 *
 
199
 * @param array $options schema object properties
 
200
 * @return array Array indexed by name and tables
 
201
 * @access public
 
202
 */
 
203
        function read($options = array()) {
 
204
                extract(array_merge(
 
205
                        array(
 
206
                                'connection' => $this->connection,
 
207
                                'name' => $this->name,
 
208
                                'models' => true,
 
209
                        ),
 
210
                        $options
 
211
                ));
 
212
                $db =& ConnectionManager::getDataSource($connection);
 
213
 
 
214
                App::import('Model', 'AppModel');
 
215
                if (isset($this->plugin)) {
 
216
                        App::import('Model', Inflector::camelize($this->plugin) . 'AppModel');
 
217
                }
 
218
 
 
219
                $tables = array();
 
220
                $currentTables = $db->listSources();
 
221
 
 
222
                $prefix = null;
 
223
                if (isset($db->config['prefix'])) {
 
224
                        $prefix = $db->config['prefix'];
 
225
                }
 
226
 
 
227
                if (!is_array($models) && $models !== false) {
 
228
                        if (isset($this->plugin)) {
 
229
                                $models = App::objects('model', App::pluginPath($this->plugin) . 'models' . DS, false);
 
230
                        } else {
 
231
                                $models = App::objects('model');
 
232
                        }
 
233
                }
 
234
 
 
235
                if (is_array($models)) {
 
236
                        foreach ($models as $model) {
 
237
                                $importModel = $model;
 
238
                                if (isset($this->plugin)) {
 
239
                                        $importModel = $this->plugin . '.' . $model;
 
240
                                }
 
241
                                if (!App::import('Model', $importModel)) {
 
242
                                        continue;
 
243
                                }
 
244
                                $vars = get_class_vars($model);
 
245
                                if (empty($vars['useDbConfig']) || $vars['useDbConfig'] != $connection) {
 
246
                                        continue;
 
247
                                }
 
248
 
 
249
                                if (PHP5) {
 
250
                                        $Object = ClassRegistry::init(array('class' => $model, 'ds' => $connection));
 
251
                                } else {
 
252
                                        $Object =& ClassRegistry::init(array('class' => $model, 'ds' => $connection));
 
253
                                }
 
254
 
 
255
                                if (is_object($Object) && $Object->useTable !== false) {
 
256
                                        $fulltable = $table = $db->fullTableName($Object, false);
 
257
                                        if ($prefix && strpos($table, $prefix) !== 0) {
 
258
                                                continue;
 
259
                                        }
 
260
                                        $table = str_replace($prefix, '', $table);
 
261
 
 
262
                                        if (in_array($fulltable, $currentTables)) {
 
263
                                                $key = array_search($fulltable, $currentTables);
 
264
                                                if (empty($tables[$table])) {
 
265
                                                        $tables[$table] = $this->__columns($Object);
 
266
                                                        $tables[$table]['indexes'] = $db->index($Object);
 
267
                                                        $tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
 
268
                                                        unset($currentTables[$key]);
 
269
                                                }
 
270
                                                if (!empty($Object->hasAndBelongsToMany)) {
 
271
                                                        foreach ($Object->hasAndBelongsToMany as $Assoc => $assocData) {
 
272
                                                                if (isset($assocData['with'])) {
 
273
                                                                        $class = $assocData['with'];
 
274
                                                                }
 
275
                                                                if (is_object($Object->$class)) {
 
276
                                                                        $withTable = $db->fullTableName($Object->$class, false);
 
277
                                                                        if (in_array($withTable, $currentTables)) {
 
278
                                                                                $key = array_search($withTable, $currentTables);
 
279
                                                                                $tables[$withTable] = $this->__columns($Object->$class);
 
280
                                                                                $tables[$withTable]['indexes'] = $db->index($Object->$class);
 
281
                                                                                $tables[$withTable]['tableParameters'] = $db->readTableParameters($withTable);
 
282
                                                                                unset($currentTables[$key]);
 
283
                                                                        }
 
284
                                                                }
 
285
                                                        }
 
286
                                                }
 
287
                                        }
 
288
                                }
 
289
                        }
 
290
                }
 
291
 
 
292
                if (!empty($currentTables)) {
 
293
                        foreach ($currentTables as $table) {
 
294
                                if ($prefix) {
 
295
                                        if (strpos($table, $prefix) !== 0) {
 
296
                                                continue;
 
297
                                        }
 
298
                                        $table = str_replace($prefix, '', $table);
 
299
                                }
 
300
                                $Object = new AppModel(array(
 
301
                                        'name' => Inflector::classify($table), 'table' => $table, 'ds' => $connection
 
302
                                ));
 
303
 
 
304
                                $systemTables = array(
 
305
                                        'aros', 'acos', 'aros_acos', Configure::read('Session.table'), 'i18n'
 
306
                                );
 
307
 
 
308
                                if (in_array($table, $systemTables)) {
 
309
                                        $tables[$Object->table] = $this->__columns($Object);
 
310
                                        $tables[$Object->table]['indexes'] = $db->index($Object);
 
311
                                        $tables[$Object->table]['tableParameters'] = $db->readTableParameters($table);
 
312
                                } elseif ($models === false) {
 
313
                                        $tables[$table] = $this->__columns($Object);
 
314
                                        $tables[$table]['indexes'] = $db->index($Object);
 
315
                                        $tables[$table]['tableParameters'] = $db->readTableParameters($table);
 
316
                                } else {
 
317
                                        $tables['missing'][$table] = $this->__columns($Object);
 
318
                                        $tables['missing'][$table]['indexes'] = $db->index($Object);
 
319
                                        $tables['missing'][$table]['tableParameters'] = $db->readTableParameters($table);
 
320
                                }
 
321
                        }
 
322
                }
 
323
 
 
324
                ksort($tables);
 
325
                return compact('name', 'tables');
 
326
        }
 
327
 
 
328
/**
 
329
 * Writes schema file from object or options
 
330
 *
 
331
 * @param mixed $object schema object or options array
 
332
 * @param array $options schema object properties to override object
 
333
 * @return mixed false or string written to file
 
334
 * @access public
 
335
 */
 
336
        function write($object, $options = array()) {
 
337
                if (is_object($object)) {
 
338
                        $object = get_object_vars($object);
 
339
                        $this->_build($object);
 
340
                }
 
341
 
 
342
                if (is_array($object)) {
 
343
                        $options = $object;
 
344
                        unset($object);
 
345
                }
 
346
 
 
347
                extract(array_merge(
 
348
                        get_object_vars($this), $options
 
349
                ));
 
350
 
 
351
                $out = "class {$name}Schema extends CakeSchema {\n";
 
352
 
 
353
                $out .= "\tvar \$name = '{$name}';\n\n";
 
354
 
 
355
                if ($path !== $this->path) {
 
356
                        $out .= "\tvar \$path = '{$path}';\n\n";
 
357
                }
 
358
 
 
359
                if ($file !== $this->file) {
 
360
                        $out .= "\tvar \$file = '{$file}';\n\n";
 
361
                }
 
362
 
 
363
                if ($connection !== 'default') {
 
364
                        $out .= "\tvar \$connection = '{$connection}';\n\n";
 
365
                }
 
366
 
 
367
                $out .= "\tfunction before(\$event = array()) {\n\t\treturn true;\n\t}\n\n\tfunction after(\$event = array()) {\n\t}\n\n";
 
368
 
 
369
                if (empty($tables)) {
 
370
                        $this->read();
 
371
                }
 
372
 
 
373
                foreach ($tables as $table => $fields) {
 
374
                        if (!is_numeric($table) && $table !== 'missing') {
 
375
                                $out .= $this->generateTable($table, $fields);
 
376
                        }
 
377
                }
 
378
                $out .= "}\n";
 
379
 
 
380
                $File =& new File($path . DS . $file, true);
 
381
                $header = '$Id';
 
382
                $content = "<?php \n/* SVN FILE: {$header}$ */\n/* {$name} schema generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
 
383
                $content = $File->prepare($content);
 
384
                if ($File->write($content)) {
 
385
                        return $content;
 
386
                }
 
387
                return false;
 
388
        }
 
389
 
 
390
/**
 
391
 * Generate the code for a table. Takes a table name and $fields array
 
392
 * Returns a completed variable declaration to be used in schema classes
 
393
 *
 
394
 * @param string $table Table name you want returned.
 
395
 * @param array $fields Array of field information to generate the table with.
 
396
 * @return string Variable declaration for a schema class
 
397
 */
 
398
        function generateTable($table, $fields) {
 
399
                $out = "\tvar \${$table} = array(\n";
 
400
                if (is_array($fields)) {
 
401
                        $cols = array();
 
402
                        foreach ($fields as $field => $value) {
 
403
                                if ($field != 'indexes' && $field != 'tableParameters') {
 
404
                                        if (is_string($value)) {
 
405
                                                $type = $value;
 
406
                                                $value = array('type'=> $type);
 
407
                                        }
 
408
                                        $col = "\t\t'{$field}' => array('type' => '" . $value['type'] . "', ";
 
409
                                        unset($value['type']);
 
410
                                        $col .= join(', ',  $this->__values($value));
 
411
                                } elseif ($field == 'indexes') {
 
412
                                        $col = "\t\t'indexes' => array(";
 
413
                                        $props = array();
 
414
                                        foreach ((array)$value as $key => $index) {
 
415
                                                $props[] = "'{$key}' => array(" . join(', ',  $this->__values($index)) . ")";
 
416
                                        }
 
417
                                        $col .= join(', ', $props);
 
418
                                } elseif ($field == 'tableParameters') {
 
419
                                        //@todo add charset, collate and engine here
 
420
                                        $col = "\t\t'tableParameters' => array(";
 
421
                                        $props = array();
 
422
                                        foreach ((array)$value as $key => $param) {
 
423
                                                $props[] = "'{$key}' => '$param'";
 
424
                                        }
 
425
                                        $col .= join(', ', $props);
 
426
                                }
 
427
                                $col .= ")";
 
428
                                $cols[] = $col;
 
429
                        }
 
430
                        $out .= join(",\n", $cols);
 
431
                }
 
432
                $out .= "\n\t);\n";
 
433
                return $out;
 
434
        }
 
435
 
 
436
/**
 
437
 * Compares two sets of schemas
 
438
 *
 
439
 * @param mixed $old Schema object or array
 
440
 * @param mixed $new Schema object or array
 
441
 * @return array Tables (that are added, dropped, or changed)
 
442
 * @access public
 
443
 */
 
444
        function compare($old, $new = null) {
 
445
                if (empty($new)) {
 
446
                        $new =& $this;
 
447
                }
 
448
                if (is_array($new)) {
 
449
                        if (isset($new['tables'])) {
 
450
                                $new = $new['tables'];
 
451
                        }
 
452
                } else {
 
453
                        $new = $new->tables;
 
454
                }
 
455
 
 
456
                if (is_array($old)) {
 
457
                        if (isset($old['tables'])) {
 
458
                                $old = $old['tables'];
 
459
                        }
 
460
                } else {
 
461
                        $old = $old->tables;
 
462
                }
 
463
                $tables = array();
 
464
                foreach ($new as $table => $fields) {
 
465
                        if ($table == 'missing') {
 
466
                                continue;
 
467
                        }
 
468
                        if (!array_key_exists($table, $old)) {
 
469
                                $tables[$table]['add'] = $fields;
 
470
                        } else {
 
471
                                $diff = array_diff_assoc($fields, $old[$table]);
 
472
                                if (!empty($diff)) {
 
473
                                        $tables[$table]['add'] = $diff;
 
474
                                }
 
475
                                $diff = array_diff_assoc($old[$table], $fields);
 
476
                                if (!empty($diff)) {
 
477
                                        $tables[$table]['drop'] = $diff;
 
478
                                }
 
479
                        }
 
480
 
 
481
                        foreach ($fields as $field => $value) {
 
482
                                if (isset($old[$table][$field])) {
 
483
                                        $diff = array_diff_assoc($value, $old[$table][$field]);
 
484
                                        if (!empty($diff) && $field !== 'indexes' && $field !== 'tableParameters') {
 
485
                                                $tables[$table]['change'][$field] = array_merge($old[$table][$field], $diff);
 
486
                                        }
 
487
                                }
 
488
 
 
489
                                if (isset($add[$table][$field])) {
 
490
                                        $wrapper = array_keys($fields);
 
491
                                        if ($column = array_search($field, $wrapper)) {
 
492
                                                if (isset($wrapper[$column - 1])) {
 
493
                                                        $tables[$table]['add'][$field]['after'] = $wrapper[$column - 1];
 
494
                                                }
 
495
                                        }
 
496
                                }
 
497
                        }
 
498
 
 
499
                        if (isset($old[$table]['indexes']) && isset($new[$table]['indexes'])) {
 
500
                                $diff = $this->_compareIndexes($new[$table]['indexes'], $old[$table]['indexes']);
 
501
                                if ($diff) {
 
502
                                        if (!isset($tables[$table])) {
 
503
                                                $tables[$table] = array();
 
504
                                        }
 
505
                                        if (isset($diff['drop'])) {
 
506
                                                $tables[$table]['drop']['indexes'] = $diff['drop'];
 
507
                                        }
 
508
                                        if ($diff && isset($diff['add'])) {
 
509
                                                $tables[$table]['add']['indexes'] = $diff['add'];
 
510
                                        }
 
511
                                }
 
512
                        }
 
513
                        if (isset($old[$table]['tableParameters']) && isset($new[$table]['tableParameters'])) {
 
514
                                $diff = $this->_compareTableParameters($new[$table]['tableParameters'], $old[$table]['tableParameters']);
 
515
                                if ($diff) {
 
516
                                        $tables[$table]['change']['tableParameters'] = $diff;
 
517
                                }
 
518
                        }
 
519
                }
 
520
                return $tables;
 
521
        }
 
522
 
 
523
/**
 
524
 * Formats Schema columns from Model Object
 
525
 *
 
526
 * @param array $values options keys(type, null, default, key, length, extra)
 
527
 * @return array Formatted values
 
528
 * @access public
 
529
 */
 
530
        function __values($values) {
 
531
                $vals = array();
 
532
                if (is_array($values)) {
 
533
                        foreach ($values as $key => $val) {
 
534
                                if (is_array($val)) {
 
535
                                        $vals[] = "'{$key}' => array('" . implode("', '",  $val) . "')";
 
536
                                } else if (!is_numeric($key)) {
 
537
                                        $val = var_export($val, true);
 
538
                                        $vals[] = "'{$key}' => {$val}";
 
539
                                }
 
540
                        }
 
541
                }
 
542
                return $vals;
 
543
        }
 
544
 
 
545
/**
 
546
 * Formats Schema columns from Model Object
 
547
 *
 
548
 * @param array $Obj model object
 
549
 * @return array Formatted columns
 
550
 * @access public
 
551
 */
 
552
        function __columns(&$Obj) {
 
553
                $db =& ConnectionManager::getDataSource($Obj->useDbConfig);
 
554
                $fields = $Obj->schema(true);
 
555
                $columns = $props = array();
 
556
                foreach ($fields as $name => $value) {
 
557
                        if ($Obj->primaryKey == $name) {
 
558
                                $value['key'] = 'primary';
 
559
                        }
 
560
                        if (!isset($db->columns[$value['type']])) {
 
561
                                trigger_error(sprintf(__('Schema generation error: invalid column type %s does not exist in DBO', true), $value['type']), E_USER_NOTICE);
 
562
                                continue;
 
563
                        } else {
 
564
                                $defaultCol = $db->columns[$value['type']];
 
565
                                if (isset($defaultCol['limit']) && $defaultCol['limit'] == $value['length']) {
 
566
                                        unset($value['length']);
 
567
                                } elseif (isset($defaultCol['length']) && $defaultCol['length'] == $value['length']) {
 
568
                                        unset($value['length']);
 
569
                                }
 
570
                                unset($value['limit']);
 
571
                        }
 
572
 
 
573
                        if (isset($value['default']) && ($value['default'] === '' || $value['default'] === false)) {
 
574
                                unset($value['default']);
 
575
                        }
 
576
                        if (empty($value['length'])) {
 
577
                                unset($value['length']);
 
578
                        }
 
579
                        if (empty($value['key'])) {
 
580
                                unset($value['key']);
 
581
                        }
 
582
                        $columns[$name] = $value;
 
583
                }
 
584
 
 
585
                return $columns;
 
586
        }
 
587
 
 
588
/**
 
589
 * Compare two schema files table Parameters
 
590
 *
 
591
 * @param array $new New indexes
 
592
 * @param array $old Old indexes
 
593
 * @return mixed False on failure, or an array of parameters to add & drop.
 
594
 */
 
595
        function _compareTableParameters($new, $old) {
 
596
                if (!is_array($new) || !is_array($old)) {
 
597
                        return false;
 
598
                }
 
599
                $change = array_diff_assoc($new, $old);
 
600
                return $change;
 
601
        }
 
602
 
 
603
/**
 
604
 * Compare two schema indexes
 
605
 *
 
606
 * @param array $new New indexes
 
607
 * @param array $old Old indexes
 
608
 * @return mixed false on failure or array of indexes to add and drop
 
609
 */
 
610
        function _compareIndexes($new, $old) {
 
611
                if (!is_array($new) || !is_array($old)) {
 
612
                        return false;
 
613
                }
 
614
 
 
615
                $add = $drop = array();
 
616
 
 
617
                $diff = array_diff_assoc($new, $old);
 
618
                if (!empty($diff)) {
 
619
                        $add = $diff;
 
620
                }
 
621
 
 
622
                $diff = array_diff_assoc($old, $new);
 
623
                if (!empty($diff)) {
 
624
                        $drop = $diff;
 
625
                }
 
626
 
 
627
                foreach ($new as $name => $value) {
 
628
                        if (isset($old[$name])) {
 
629
                                $newUnique = isset($value['unique']) ? $value['unique'] : 0;
 
630
                                $oldUnique = isset($old[$name]['unique']) ? $old[$name]['unique'] : 0;
 
631
                                $newColumn = $value['column'];
 
632
                                $oldColumn = $old[$name]['column'];
 
633
 
 
634
                                $diff = false;
 
635
 
 
636
                                if ($newUnique != $oldUnique) {
 
637
                                        $diff = true;
 
638
                                } elseif (is_array($newColumn) && is_array($oldColumn)) {
 
639
                                        $diff = ($newColumn !== $oldColumn);
 
640
                                } elseif (is_string($newColumn) && is_string($oldColumn)) {
 
641
                                        $diff = ($newColumn != $oldColumn);
 
642
                                } else {
 
643
                                        $diff = true;
 
644
                                }
 
645
                                if ($diff) {
 
646
                                        $drop[$name] = null;
 
647
                                        $add[$name] = $value;
 
648
                                }
 
649
                        }
 
650
                }
 
651
                return array_filter(compact('add', 'drop'));
 
652
        }
 
653
}