1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: |
|
15: |
|
16: | namespace LucidFrame\Core;
|
17: |
|
18: | use LucidFrame\Console\Command;
|
19: |
|
20: | |
21: | |
22: |
|
23: | class SchemaManager
|
24: | {
|
25: |
|
26: | protected $schema = array();
|
27: |
|
28: | private $driver = 'mysql';
|
29: |
|
30: | private $defaultOptions;
|
31: |
|
32: | private static $dataTypes = array(
|
33: | 'mysql' => array(
|
34: | 'smallint' => 'SMALLINT',
|
35: | 'mediumint' => 'MEDIUMINT',
|
36: | 'int' => 'INT',
|
37: | 'integer' => 'INT',
|
38: | 'bigint' => 'BIGINT',
|
39: | 'decimal' => 'NUMERIC',
|
40: | 'float' => 'DOUBLE',
|
41: |
|
42: |
|
43: |
|
44: |
|
45: | 'string' => 'VARCHAR',
|
46: | 'char' => 'CHAR',
|
47: | 'binary' => 'VARBINARY',
|
48: | 'tinytext' => 'TINYTEXT',
|
49: | 'text' => 'TEXT',
|
50: | 'mediumtext'=> 'MEDIUMTEXT',
|
51: | 'longtext' => 'LONGTEXT',
|
52: | 'tinyblob' => 'TINYBLOB',
|
53: | 'blob' => 'BLOB',
|
54: | 'mediumblob'=> 'MEDIUMBLOB',
|
55: | 'longblob' => 'LONGBLOB',
|
56: | 'array' => 'TEXT',
|
57: | 'json' => 'TEXT',
|
58: |
|
59: |
|
60: |
|
61: |
|
62: | 'boolean' => 'TINYINT',
|
63: | 'date' => 'DATE',
|
64: | 'datetime' => 'DATETIME',
|
65: | 'time' => 'TIME',
|
66: | ),
|
67: | );
|
68: |
|
69: | public static $relationships = array('1:m', 'm:1', 'm:m', '1:1');
|
70: |
|
71: | private $dbNamespace = 'default';
|
72: |
|
73: | private $sqlStatements = array();
|
74: |
|
75: | private $sqlExtension = '.sqlc';
|
76: |
|
77: | private $droppedTables = array();
|
78: |
|
79: | private $addedTables = array();
|
80: |
|
81: | private $droppedColumns = array();
|
82: |
|
83: | private $addedColumns = array();
|
84: |
|
85: | private $tablesRenamed = array();
|
86: |
|
87: | private $columnsRenamed = array();
|
88: |
|
89: | |
90: | |
91: | |
92: | |
93: |
|
94: | public function __construct($schema = array(), $dbNamespace = null)
|
95: | {
|
96: | $this->defaultOptions = array(
|
97: | 'timestamps' => true,
|
98: | 'constraints' => true,
|
99: | 'charset' => 'utf8mb4',
|
100: | 'collate' => 'utf8mb4_general_ci',
|
101: | 'engine' => 'InnoDB',
|
102: | );
|
103: |
|
104: | $this->setSchema($schema);
|
105: |
|
106: | if ($dbNamespace) {
|
107: | $this->dbNamespace = $dbNamespace;
|
108: | }
|
109: | }
|
110: |
|
111: | |
112: | |
113: | |
114: | |
115: |
|
116: | public function setSchema($schema)
|
117: | {
|
118: | if (!is_array($schema)) {
|
119: | $schema = array(
|
120: | '_options' => $this->defaultOptions
|
121: | );
|
122: | }
|
123: |
|
124: | $this->schema = $schema;
|
125: |
|
126: | return $this;
|
127: | }
|
128: |
|
129: | |
130: | |
131: | |
132: |
|
133: | public function getSchema()
|
134: | {
|
135: | return $this->schema;
|
136: | }
|
137: |
|
138: | |
139: | |
140: | |
141: | |
142: | |
143: |
|
144: | private function setDriver($driver)
|
145: | {
|
146: | $this->driver = $driver;
|
147: |
|
148: | return $this;
|
149: | }
|
150: |
|
151: | |
152: | |
153: | |
154: |
|
155: | public function getDriver()
|
156: | {
|
157: | return $this->driver;
|
158: | }
|
159: |
|
160: | |
161: | |
162: | |
163: | |
164: |
|
165: | public function setDbNamespace($namespace)
|
166: | {
|
167: | $this->dbNamespace = $namespace;
|
168: |
|
169: | return $this;
|
170: | }
|
171: |
|
172: | |
173: | |
174: | |
175: |
|
176: | public function getDbNamespace()
|
177: | {
|
178: | return $this->dbNamespace;
|
179: | }
|
180: |
|
181: | |
182: | |
183: | |
184: |
|
185: | private function getPKDefaultType()
|
186: | {
|
187: | return array(
|
188: | 'type' => 'int',
|
189: | 'autoinc' => true,
|
190: | 'null' => false,
|
191: | 'unsigned' => true
|
192: | );
|
193: | }
|
194: |
|
195: | |
196: | |
197: | |
198: | |
199: | |
200: |
|
201: | private function getRelationOptions($relation, $fkTable = '')
|
202: | {
|
203: | if (empty($relation)) {
|
204: | $relation = array('name' => $fkTable . '_id');
|
205: | }
|
206: |
|
207: | if (_arrayAssoc($relation)) {
|
208: | $relations[] = $relation;
|
209: | } else {
|
210: | $relations = $relation;
|
211: | }
|
212: |
|
213: | foreach ($relations as $i => $rel) {
|
214: | if (!isset($rel['name'])) {
|
215: | $rel['name'] = $fkTable . '_id';
|
216: | }
|
217: |
|
218: | $relations[$i] = $rel + array(
|
219: | 'unique' => false,
|
220: | 'default' => null,
|
221: | 'cascade' => false
|
222: | );
|
223: | }
|
224: |
|
225: | return $relations;
|
226: | }
|
227: |
|
228: | |
229: | |
230: | |
231: | |
232: | |
233: | |
234: |
|
235: | public function getFieldStatement($field, $definition, $collate = null)
|
236: | {
|
237: | $type = $this->getVendorFieldType($definition);
|
238: | if ($type === null) {
|
239: | return '';
|
240: | }
|
241: |
|
242: | $statement = "`{$field}` {$type}";
|
243: |
|
244: | $length = $this->getFieldLength($definition);
|
245: | if ($length) {
|
246: | $statement .= "($length)";
|
247: | }
|
248: |
|
249: | if (in_array($definition['type'], array('string', 'char', 'text', 'array', 'json'))) {
|
250: |
|
251: | $statement .= ' COLLATE ';
|
252: | $statement .= $collate ? $collate : $this->schema['_options']['collate'];
|
253: | }
|
254: |
|
255: | if (isset($definition['unsigned'])) {
|
256: |
|
257: | $statement .= ' unsigned';
|
258: | }
|
259: |
|
260: | if (isset($definition['null'])) {
|
261: |
|
262: |
|
263: | $statement .= $definition['null'] ? ' DEFAULT NULL' : ' NOT NULL';
|
264: | }
|
265: |
|
266: | if (isset($definition['default'])) {
|
267: | $statement .= sprintf(" DEFAULT '%d'", (int)$definition['default']);
|
268: | }
|
269: |
|
270: | if (isset($definition['autoinc']) && $definition['autoinc']) {
|
271: |
|
272: | $statement .= ' AUTO_INCREMENT';
|
273: | }
|
274: |
|
275: | return $statement;
|
276: | }
|
277: |
|
278: | |
279: | |
280: | |
281: | |
282: |
|
283: | public function getVendorFieldType(&$definition)
|
284: | {
|
285: | if (!isset(self::$dataTypes[$this->driver][$definition['type']])) {
|
286: |
|
287: | return null;
|
288: | }
|
289: |
|
290: | $type = self::$dataTypes[$this->driver][$definition['type']];
|
291: |
|
292: | if (in_array($definition['type'], array('text', 'blob', 'array', 'json'))) {
|
293: | if (isset($definition['length']) && in_array($definition['length'], array('tiny', 'medium', 'long'))) {
|
294: | return strtoupper($definition['length']) . $type;
|
295: | } else {
|
296: | return $definition['type'] == 'blob' ? self::$dataTypes[$this->driver]['blob'] : self::$dataTypes[$this->driver]['text'];
|
297: | }
|
298: | }
|
299: |
|
300: | if ($definition['type'] == 'boolean') {
|
301: |
|
302: | $definition['unsigned'] = true;
|
303: | $definition['null'] = false;
|
304: | if (!isset($definition['default'])) {
|
305: | $definition['default'] = false;
|
306: | }
|
307: | }
|
308: |
|
309: | return $type;
|
310: | }
|
311: |
|
312: | |
313: | |
314: | |
315: | |
316: |
|
317: | public function getFieldLength(&$definition)
|
318: | {
|
319: | $type = $definition['type'];
|
320: |
|
321: | if ($type == 'string' || $type == 'char') {
|
322: | $length = 255;
|
323: | } elseif ($type == 'int' || $type == 'integer') {
|
324: | $length = 11;
|
325: | } elseif ($type === 'boolean') {
|
326: | $length = 1;
|
327: | } elseif (in_array($type, array('text', 'blob', 'array', 'json'))) {
|
328: | $length = 0;
|
329: | } elseif ($type == 'decimal' || $type == 'float') {
|
330: | $length = isset($definition['length']) ? $definition['length'] : 0;
|
331: | if (is_array($length) && count($length) == 2) {
|
332: | $length = implode(', ', $length);
|
333: | }
|
334: | } else {
|
335: | $length = 0;
|
336: | }
|
337: |
|
338: | if (isset($definition['length']) && is_numeric($definition['length'])) {
|
339: | $length = $definition['length'];
|
340: | }
|
341: |
|
342: | return $length;
|
343: | }
|
344: |
|
345: | |
346: | |
347: | |
348: | |
349: | |
350: |
|
351: | protected function getFKField($fkTable, $relation)
|
352: | {
|
353: | $field = $relation['name'];
|
354: | $pkFields = $this->schema['_options']['pk'];
|
355: |
|
356: | if (isset($pkFields[$fkTable][$field])) {
|
357: | $fkField = $pkFields[$fkTable][$field];
|
358: | } else {
|
359: | $keys = array_keys($pkFields[$fkTable]);
|
360: | $firstPKField = array_shift($keys);
|
361: | $fkField = $pkFields[$fkTable][$firstPKField];
|
362: | }
|
363: |
|
364: | if (isset($fkField['autoinc'])) {
|
365: | unset($fkField['autoinc']);
|
366: | }
|
367: |
|
368: | if ($relation['unique']) {
|
369: | $fkField['unique'] = true;
|
370: | }
|
371: |
|
372: | if ($relation['default'] === null) {
|
373: | $fkField['null'] = true;
|
374: | } else {
|
375: | $fkField['default'] = $relation['default'];
|
376: | $fkField['null'] = false;
|
377: | }
|
378: |
|
379: | return $fkField;
|
380: | }
|
381: |
|
382: | |
383: | |
384: | |
385: | |
386: | |
387: | |
388: |
|
389: | protected function getFKConstraint($fkTable, $relation, $schema = array())
|
390: | {
|
391: | if ($this->schema['_options']['constraints']) {
|
392: | $pkFields = $this->schema['_options']['pk'];
|
393: | $field = $relation['name'];
|
394: | $refField = $field;
|
395: |
|
396: | if (!isset($pkFields[$fkTable][$refField])) {
|
397: | $refField = 'id';
|
398: | }
|
399: |
|
400: | if ($relation['cascade'] === true) {
|
401: | $cascade = 'CASCADE';
|
402: | } elseif ($relation['cascade'] === null) {
|
403: | $cascade = 'SET NULL';
|
404: | } else {
|
405: | $cascade = 'RESTRICT';
|
406: | }
|
407: |
|
408: | return array(
|
409: | 'name' => 'FK_' . strtoupper(_randomCode(15)),
|
410: | 'fields' => $field,
|
411: | 'reference_table' => $fkTable,
|
412: | 'reference_fields' => $refField,
|
413: | 'on_delete' => $cascade,
|
414: | 'on_update' => 'NO ACTION'
|
415: | );
|
416: | }
|
417: |
|
418: | return null;
|
419: | }
|
420: |
|
421: | |
422: | |
423: | |
424: |
|
425: | private function load()
|
426: | {
|
427: | $schema = $this->schema;
|
428: | unset($schema['_options']);
|
429: |
|
430: | if (count($schema) == 0) {
|
431: | return false;
|
432: | }
|
433: |
|
434: |
|
435: | $this->populatePrimaryKeys($schema);
|
436: |
|
437: | $constraints = $this->populatePivots($schema);
|
438: |
|
439: | $pkFields = $this->getPrimaryKeys();
|
440: |
|
441: | $sql = array();
|
442: | $sql[] = 'SET FOREIGN_KEY_CHECKS=0;';
|
443: |
|
444: |
|
445: | foreach ($schema as $table => $def) {
|
446: | $fullTableName = db_table($table);
|
447: | $createSql = $this->createTableStatement($table, $schema, $pkFields, $constraints);
|
448: | if ($createSql) {
|
449: | $sql[] = '--';
|
450: | $sql[] = '-- Table structure for table `' . $fullTableName . '`';
|
451: | $sql[] = '--';
|
452: | $sql[] = "DROP TABLE IF EXISTS `{$fullTableName}`;";
|
453: | $sql[] = $createSql;
|
454: | }
|
455: | }
|
456: |
|
457: |
|
458: | $constraintSql = $this->createConstraintStatements($constraints);
|
459: | if ($constraintSql) {
|
460: | $sql = array_merge($sql, $constraintSql);
|
461: | }
|
462: |
|
463: | $sql[] = 'SET FOREIGN_KEY_CHECKS=1;';
|
464: |
|
465: | $this->sqlStatements = $sql;
|
466: |
|
467: |
|
468: | $versions = $this->checkVersions($schema);
|
469: | if (is_array($versions) && count($versions)) {
|
470: | $currentVersion = str_replace($this->sqlExtension, '', array_pop($versions));
|
471: | } else {
|
472: | $currentVersion = 0;
|
473: | }
|
474: |
|
475: | $this->schema['_options']['version'] = $currentVersion;
|
476: | $schema['_options'] = $this->schema['_options'];
|
477: | $this->schema = $schema;
|
478: |
|
479: | return true;
|
480: | }
|
481: |
|
482: | |
483: | |
484: | |
485: | |
486: | |
487: |
|
488: | public function build($dbNamespace = null, $backup = false)
|
489: | {
|
490: | if (!$this->isLoaded()) {
|
491: | $this->load();
|
492: | }
|
493: |
|
494: | if ($dbNamespace === null) {
|
495: | $dbNamespace = $this->dbNamespace;
|
496: | }
|
497: |
|
498: | $fileName = self::getSchemaLockFileName($dbNamespace);
|
499: | $result = file_put_contents($fileName, serialize($this->schema));
|
500: | if ($result) {
|
501: | if ($backup) {
|
502: | copy($fileName, self::getSchemaLockFileName($dbNamespace, true));
|
503: | }
|
504: |
|
505: | return true;
|
506: | }
|
507: |
|
508: | return false;
|
509: | }
|
510: |
|
511: | |
512: | |
513: | |
514: | |
515: |
|
516: | public function import($dbNamespace = null)
|
517: | {
|
518: | if ($dbNamespace === null) {
|
519: | $dbNamespace = $this->dbNamespace;
|
520: | }
|
521: |
|
522: | if (!$this->isLoaded()) {
|
523: | $this->load();
|
524: | }
|
525: |
|
526: | if ($this->executeQueries($dbNamespace, $this->sqlStatements)) {
|
527: | $this->build($dbNamespace);
|
528: | return true;
|
529: | }
|
530: |
|
531: | return false;
|
532: | }
|
533: |
|
534: | |
535: | |
536: | |
537: | |
538: |
|
539: | public function export($dbNamespace = null)
|
540: | {
|
541: | if ($dbNamespace === null) {
|
542: | $dbNamespace = $this->dbNamespace;
|
543: | }
|
544: |
|
545: | $this->build($dbNamespace);
|
546: |
|
547: | if (!count($this->sqlStatements)) {
|
548: | return false;
|
549: | }
|
550: |
|
551: | $dump = '--' . PHP_EOL
|
552: | . '-- Generated by PHPLucidFrame ' . _version() . PHP_EOL
|
553: | . '-- ' . date('r') . PHP_EOL
|
554: | . '-- ;' . PHP_EOL . PHP_EOL
|
555: | . implode(PHP_EOL, $this->sqlStatements);
|
556: |
|
557: | return file_put_contents(DB . 'generated' . _DS_ . 'schema.' . $dbNamespace . '.sql', $dump) ? true : false;
|
558: | }
|
559: |
|
560: | |
561: | |
562: | |
563: | |
564: | |
565: |
|
566: | public function update(Command $cmd, $dbNamespace = null)
|
567: | {
|
568: | if ($dbNamespace === null) {
|
569: | $dbNamespace = $this->dbNamespace;
|
570: | }
|
571: |
|
572: | $schemaFrom = self::getSchemaLockDefinition($dbNamespace);
|
573: |
|
574: | if (!$this->isLoaded()) {
|
575: | $this->load();
|
576: | }
|
577: |
|
578: | $schemaTo = $this->schema;
|
579: | $isSchemaChanged = $this->isSchemaChanged($schemaFrom, $schemaTo);
|
580: | $versions = $this->checkVersions($schemaFrom);
|
581: |
|
582: | if (is_array($versions) && count($versions)) {
|
583: |
|
584: | $version = $this->migrate($versions, $schemaFrom, $schemaTo);
|
585: |
|
586: | if ($version) {
|
587: |
|
588: | $this->schema['_options']['version'] = $version;
|
589: | $this->build($dbNamespace);
|
590: |
|
591: | _writeln();
|
592: | _writeln('Your schema has been updated.');
|
593: | }
|
594: |
|
595: | return true;
|
596: | }
|
597: |
|
598: | if ($versions === 0 || $versions === 1) {
|
599: |
|
600: | if ($isSchemaChanged) {
|
601: |
|
602: | $sql = $this->generateSqlFromDiff($schemaFrom, $schemaTo, $cmd);
|
603: | } else {
|
604: | _writeln();
|
605: | _writeln('Your schema is up-to-date.');
|
606: |
|
607: | return true;
|
608: | }
|
609: | }
|
610: |
|
611: | if (!empty($sql['up'])) {
|
612: | _writeln();
|
613: | _writeln('##########');
|
614: | foreach ($sql['up'] as $query) {
|
615: | _writeln($query);
|
616: | }
|
617: | _writeln('##########');
|
618: | _writeln();
|
619: | }
|
620: |
|
621: | $dropConstraintSql = $this->dropConstraintStatements($this->getConstraints($schemaFrom));
|
622: | $createConstraintSql = $this->createConstraintStatements();
|
623: |
|
624: | if (empty($sql['up']) && count($dropConstraintSql) == 0 && count($createConstraintSql) == 0) {
|
625: | return false;
|
626: | }
|
627: |
|
628: |
|
629: | $statements = array();
|
630: | if ($cmd->confirm('Type "y" to execute or type "n" to abort:')) {
|
631: | $statements = array_merge($statements, $dropConstraintSql, $sql['up'], $createConstraintSql);
|
632: |
|
633: | $noOfQueries = $this->executeQueries($dbNamespace, $statements);
|
634: | if (!$noOfQueries) {
|
635: | return false;
|
636: | }
|
637: | } else {
|
638: | _writeln('Aborted.');
|
639: | return false;
|
640: | }
|
641: |
|
642: |
|
643: | if ($dbVersion = $this->exportVersionFile($sql['up'], $dbNamespace)) {
|
644: |
|
645: | $this->schema['_options']['version'] = $dbVersion;
|
646: | $this->build($dbNamespace);
|
647: | } else {
|
648: | return false;
|
649: | }
|
650: |
|
651: | _writeln('--------------------');
|
652: | _writeln('%d queries executed.', $noOfQueries);
|
653: |
|
654: | return true;
|
655: | }
|
656: |
|
657: | |
658: | |
659: | |
660: | |
661: | |
662: |
|
663: | public function diff(Command $cmd, $dbNamespace = null)
|
664: | {
|
665: | if ($dbNamespace === null) {
|
666: | $dbNamespace = $this->dbNamespace;
|
667: | }
|
668: |
|
669: | $schemaFrom = self::getSchemaLockDefinition($dbNamespace);
|
670: |
|
671: | if (!$this->isLoaded()) {
|
672: | $this->load();
|
673: | }
|
674: |
|
675: | $schemaTo = $this->schema;
|
676: | $isSchemaChanged = $this->isSchemaChanged($schemaFrom, $schemaTo);
|
677: | $versions = $this->checkVersions($schemaFrom);
|
678: |
|
679: | if (is_array($versions) && count($versions)) {
|
680: | return false;
|
681: | }
|
682: |
|
683: | if ($versions === 0 || $versions === 1) {
|
684: |
|
685: | if ($isSchemaChanged) {
|
686: |
|
687: | $sql = $this->generateSqlFromDiff($schemaFrom, $schemaTo, $cmd);
|
688: | if ($dbVersion = $this->exportVersionFile($sql['up'], $dbNamespace)) {
|
689: | $versionDir = $this->getVersionDir($dbNamespace);
|
690: |
|
691: | _writeln();
|
692: | _writeln($versionDir . _DS_ . $dbVersion . $this->sqlExtension . ' is exported.');
|
693: | _writeln('Check the file and run `php lucidframe schema:update ' . $dbNamespace . '`');
|
694: |
|
695: | return true;
|
696: | }
|
697: | }
|
698: | }
|
699: |
|
700: | return false;
|
701: | }
|
702: |
|
703: | |
704: | |
705: | |
706: | |
707: | |
708: |
|
709: | private function exportVersionFile(array $sql, $dbNamespace = null)
|
710: | {
|
711: | if (!count($sql)) {
|
712: | return false;
|
713: | }
|
714: |
|
715: | if ($dbNamespace === null) {
|
716: | $dbNamespace = $this->dbNamespace;
|
717: | }
|
718: |
|
719: |
|
720: | $dbVersion = date('YmdHis');
|
721: |
|
722: | $dump = '--' . PHP_EOL
|
723: | . '-- Version ' . $dbVersion . PHP_EOL
|
724: | . '-- Generated by PHPLucidFrame ' . _version() . PHP_EOL
|
725: | . '-- ' . date('r') . PHP_EOL
|
726: | . '-- ;' . PHP_EOL . PHP_EOL
|
727: | . implode(PHP_EOL . PHP_EOL, $sql);
|
728: |
|
729: | $versionDir = $this->getVersionDir($dbNamespace);
|
730: | if (file_put_contents($versionDir . _DS_ . $dbVersion . $this->sqlExtension, $dump)) {
|
731: | return $dbVersion;
|
732: | }
|
733: |
|
734: | return false;
|
735: | }
|
736: |
|
737: | |
738: | |
739: | |
740: | |
741: | |
742: | |
743: |
|
744: | public function generateSqlFromDiff($schemaFrom, $schemaTo, Command $cmd)
|
745: | {
|
746: | $fieldNamesChanged = array();
|
747: | $this->columnsRenamed = array();
|
748: |
|
749: | $sql = array(
|
750: | 'up' => array(),
|
751: | 'down' => array(),
|
752: | );
|
753: |
|
754: |
|
755: | $this->detectTableRenamings($schemaFrom, $schemaTo);
|
756: | if (count($this->tablesRenamed)) {
|
757: | _writeln();
|
758: | _writeln('Type "y" to rename or type "n" to drop/create for the following tables:');
|
759: | _writeln();
|
760: | }
|
761: |
|
762: |
|
763: | foreach ($this->tablesRenamed as $from => $to) {
|
764: | if (!$cmd->confirm('Table renaming from `' . $from . '` to `' . $to . '`:')) {
|
765: | unset($this->tablesRenamed[$from]);
|
766: | }
|
767: | }
|
768: |
|
769: |
|
770: | $this->detectColumnRenamings($schemaFrom, $schemaTo);
|
771: | if (count($this->columnsRenamed)) {
|
772: | _writeln();
|
773: | _writeln('Type "y" to rename or type "n" to drop/create for the following fields:');
|
774: | _writeln();
|
775: | }
|
776: |
|
777: |
|
778: | foreach ($this->columnsRenamed as $from => $to) {
|
779: | $fieldFrom = explode('.', $from);
|
780: | if (!$cmd->confirm('Field renaming from `' . $fieldFrom[1] . '` to `' . $fieldFrom[0] . '.' . $to . '`:')) {
|
781: | unset($this->columnsRenamed[$from]);
|
782: | }
|
783: | }
|
784: |
|
785: |
|
786: | foreach ($schemaFrom as $table => $tableDef) {
|
787: | if ($table == '_options') {
|
788: | continue;
|
789: | }
|
790: |
|
791: | $fullTableName = db_table($table);
|
792: | $renamedTable = $this->isRenamed($table, $this->tablesRenamed);
|
793: |
|
794: | if (isset($schemaTo[$table]) || ($renamedTable && isset($schemaTo[$renamedTable]))) {
|
795: |
|
796: | if ($renamedTable) {
|
797: |
|
798: | $table = $renamedTable;
|
799: | }
|
800: |
|
801: | foreach ($tableDef as $field => $fieldDef) {
|
802: | $collate = $this->getTableCollation($table, $schemaTo);
|
803: | $oldField = $field;
|
804: | $renamedField = $this->isRenamed($table . '.' . $field, $this->columnsRenamed);
|
805: |
|
806: | if (isset($schemaTo[$table][$field]) || ($renamedField && isset($schemaTo[$table][$renamedField]))) {
|
807: |
|
808: | if ($renamedField) {
|
809: | $field = $renamedField;
|
810: | }
|
811: |
|
812: | $diff = $fieldDef !== $schemaTo[$table][$field];
|
813: | if ($diff) {
|
814: |
|
815: | if (in_array($field, self::$relationships)) {
|
816: | continue;
|
817: | }
|
818: |
|
819: | if ($field == 'options') {
|
820: | if (!empty($fieldDef['m:m'])) {
|
821: |
|
822: | continue;
|
823: | }
|
824: |
|
825: | $fromFieldOptions = $fieldDef;
|
826: | $toFieldOptions = $schemaTo[$table][$field];
|
827: | $diffOptions = $this->diffColumns($fromFieldOptions, $toFieldOptions);
|
828: |
|
829: | foreach ($diffOptions['diff'] as $optName => $optValue) {
|
830: | switch ($optName) {
|
831: | case 'unique':
|
832: |
|
833: | if (isset($fromFieldOptions['unique'])) {
|
834: | foreach ($fromFieldOptions['unique'] as $keyName => $uniqueFields) {
|
835: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` DROP INDEX `IDX_$keyName`;";
|
836: | }
|
837: | }
|
838: |
|
839: | if (isset($toFieldOptions['unique'])) {
|
840: |
|
841: | foreach ($toFieldOptions['unique'] as $keyName => $uniqueFields) {
|
842: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` ADD UNIQUE `IDX_$keyName` (`" . implode('`,`', $uniqueFields) . "`);";
|
843: | }
|
844: | }
|
845: | break;
|
846: |
|
847: | case 'engine':
|
848: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` ENGINE={$toFieldOptions['engine']};";
|
849: | break;
|
850: |
|
851: | case 'charset':
|
852: | case 'collate':
|
853: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` CONVERT TO CHARACTER SET {$toFieldOptions['charset']} COLLATE {$toFieldOptions['collate']};";
|
854: | break;
|
855: | }
|
856: | }
|
857: |
|
858: | continue;
|
859: | }
|
860: |
|
861: | $newField = $field;
|
862: |
|
863: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` CHANGE COLUMN `{$oldField}` " .
|
864: | $this->getFieldStatement($newField, $schemaTo[$table][$newField], $collate) . ';';
|
865: |
|
866: | if (isset($schemaFrom[$table][$oldField]['unique']) && !isset($schemaTo[$table][$newField]['unique'])) {
|
867: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` DROP INDEX `IDX_$oldField`;";
|
868: | } elseif (!isset($schemaFrom[$table][$oldField]['unique']) && isset($schemaTo[$table][$newField]['unique'])) {
|
869: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` ADD UNIQUE `IDX_$newField` (`$newField`);";
|
870: | }
|
871: |
|
872: | $fieldNamesChanged[] = $table . '.' . $oldField;
|
873: | $fieldNamesChanged = array_unique($fieldNamesChanged);
|
874: | } else {
|
875: | if ($renamedField) {
|
876: | $fieldNamesChanged[] = $table . '.' . $renamedField;
|
877: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` CHANGE COLUMN `{$oldField}` " .
|
878: | $this->getFieldStatement($renamedField, $schemaTo[$table][$renamedField], $collate) . ';';
|
879: | }
|
880: | }
|
881: | } else {
|
882: |
|
883: | if (in_array($field, array('m:m', '1:m', 'm:1'))) {
|
884: | continue;
|
885: | }
|
886: |
|
887: | if (in_array($table . '.' . $field, $fieldNamesChanged)) {
|
888: |
|
889: | continue;
|
890: | }
|
891: |
|
892: | if ($field == '1:1') {
|
893: | foreach ($fieldDef as $fkFieldInTable) {
|
894: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` DROP COLUMN `{$fkFieldInTable['name']}`;";
|
895: | }
|
896: |
|
897: | continue;
|
898: | }
|
899: |
|
900: | $sql['up'][] = "ALTER TABLE `{$fullTableName}` DROP COLUMN `{$field}`;";
|
901: | }
|
902: | }
|
903: |
|
904: |
|
905: | if ($renamedTable) {
|
906: | $sql['up'][] = 'RENAME TABLE `' . $fullTableName . '` TO `' . db_table($renamedTable) . '`;';
|
907: | }
|
908: | } else {
|
909: |
|
910: | $sql['up'][] = "DROP TABLE IF EXISTS `{$fullTableName}`;";
|
911: | }
|
912: | }
|
913: |
|
914: | $pkFields = $this->getPrimaryKeys();
|
915: | $constraints = $this->getConstraints();
|
916: | foreach ($schemaTo as $table => $tableDef) {
|
917: | if ($table == '_options') {
|
918: | $dbOptions = $table;
|
919: | continue;
|
920: | }
|
921: |
|
922: | $collate = $this->getTableCollation($table, $schemaTo);
|
923: | $fullTableName = db_table($table);
|
924: | $tableFrom = $table;
|
925: | $fieldBefore = '';
|
926: |
|
927: | if (!isset($schemaFrom[$table])) {
|
928: | $oldTable = array_search($table, $this->tablesRenamed);
|
929: | if ($oldTable === false) {
|
930: |
|
931: | $createSql = trim($this->createTableStatement($table, $schemaTo, $pkFields, $constraints));
|
932: | if ($createSql) {
|
933: | $sql['up'][] = $createSql;
|
934: | }
|
935: |
|
936: | continue;
|
937: | } else {
|
938: | $tableFrom = $oldTable;
|
939: | }
|
940: | }
|
941: |
|
942: |
|
943: | foreach ($tableDef as $field => $fieldDef) {
|
944: | if (in_array($field, array_merge(SchemaManager::$relationships, array('options')))) {
|
945: | continue;
|
946: | }
|
947: |
|
948: | if (!isset($schemaFrom[$tableFrom][$field]) && array_search($table . '.' . $field, $fieldNamesChanged) === false) {
|
949: |
|
950: | $alterSql = "ALTER TABLE `{$fullTableName}` ADD COLUMN ";
|
951: | $alterSql .= $this->getFieldStatement($field, $fieldDef, $collate);
|
952: | if ($fieldBefore && $field != 'created') {
|
953: | $alterSql .= " AFTER `{$fieldBefore}`";
|
954: | }
|
955: | $alterSql .= ';';
|
956: | $sql['up'][] = $alterSql;
|
957: | }
|
958: |
|
959: | $fieldBefore = $field;
|
960: | }
|
961: | }
|
962: |
|
963: | return $sql;
|
964: | }
|
965: |
|
966: | |
967: | |
968: | |
969: | |
970: | |
971: | |
972: | |
973: |
|
974: | public function migrate(array $versions, array $schemaFrom, array $schemaTo, $verbose = true)
|
975: | {
|
976: |
|
977: | if ($dropConstraintSql = $this->dropConstraintStatements($this->getConstraints($schemaFrom))) {
|
978: | $this->executeQueries($this->dbNamespace, $dropConstraintSql);
|
979: | }
|
980: |
|
981: | if ($verbose) {
|
982: | _writeln();
|
983: | }
|
984: |
|
985: | $version = false;
|
986: | $noOfQueries = 0;
|
987: | foreach ($versions as $verFile) {
|
988: | $version = str_replace($this->sqlExtension, '', $verFile);
|
989: |
|
990: | if ($verbose) {
|
991: | _writeln('Executing ' . $version);
|
992: | }
|
993: |
|
994: | $sql = file_get_contents(DB . 'version' . _DS_ . $this->dbNamespace . _DS_ . $verFile);
|
995: | if (empty($sql)) {
|
996: | if ($verbose) {
|
997: | _writeln('No sql statements executed.');
|
998: | }
|
999: |
|
1000: | return false;
|
1001: | }
|
1002: |
|
1003: | $sqls = explode(';', $sql);
|
1004: | $sql = array_filter($sqls, function($line) {
|
1005: | $line = trim($line);
|
1006: | return !empty($line) && strpos($line, '--') === false;
|
1007: | });
|
1008: |
|
1009: | if (empty($sql)) {
|
1010: | if ($verbose) {
|
1011: | _writeln('No sql statements executed.');
|
1012: | }
|
1013: |
|
1014: | return false;
|
1015: | }
|
1016: |
|
1017: | $executed = $this->executeQueries($this->dbNamespace, $sql);
|
1018: | if (!$executed) {
|
1019: | return false;
|
1020: | }
|
1021: |
|
1022: | $noOfQueries += $executed;
|
1023: | if ($verbose) {
|
1024: | _writeln();
|
1025: | }
|
1026: | }
|
1027: |
|
1028: |
|
1029: | if ($createConstraintSql = $this->createConstraintStatements($this->getConstraints($schemaTo))) {
|
1030: | $this->executeQueries($this->dbNamespace, $createConstraintSql);
|
1031: | }
|
1032: |
|
1033: | return $version;
|
1034: | }
|
1035: |
|
1036: | |
1037: | |
1038: | |
1039: | |
1040: | |
1041: | |
1042: |
|
1043: | private function executeQueries($dbNamespace, $queries)
|
1044: | {
|
1045: | if (!count($queries)) {
|
1046: | return false;
|
1047: | }
|
1048: |
|
1049: | if ($this->dbNamespace !== $dbNamespace) {
|
1050: | db_switch($dbNamespace);
|
1051: | }
|
1052: |
|
1053: | db_transaction();
|
1054: |
|
1055: | array_unshift($queries, 'SET FOREIGN_KEY_CHECKS = 0;');
|
1056: | array_push($queries, 'SET FOREIGN_KEY_CHECKS = 1;');
|
1057: |
|
1058: | $count = 0;
|
1059: | $error = false;
|
1060: | foreach ($queries as $sql) {
|
1061: | $sql = trim($sql);
|
1062: |
|
1063: | if (empty($sql)) {
|
1064: | continue;
|
1065: | }
|
1066: |
|
1067: | if (substr($sql, 0, 2) == '--') {
|
1068: | continue;
|
1069: | }
|
1070: |
|
1071: | if (!db_query($sql)) {
|
1072: | $error = true;
|
1073: | break;
|
1074: | }
|
1075: |
|
1076: | $count++;
|
1077: | }
|
1078: |
|
1079: | if ($error) {
|
1080: | db_rollback();
|
1081: | } else {
|
1082: | db_commit();
|
1083: | }
|
1084: |
|
1085: | if ($this->dbNamespace !== $dbNamespace) {
|
1086: |
|
1087: | db_switch($this->dbNamespace);
|
1088: | }
|
1089: |
|
1090: | if ($error == true) {
|
1091: | return false;
|
1092: | } else {
|
1093: | return $count;
|
1094: | }
|
1095: | }
|
1096: |
|
1097: | |
1098: | |
1099: | |
1100: | |
1101: | |
1102: |
|
1103: | public function isSchemaChanged(array $from, array $to)
|
1104: | {
|
1105: | if (isset($from['_options']['version'])) {
|
1106: | unset($from['_options']['version']);
|
1107: | }
|
1108: |
|
1109: | if (isset($from['_options']['fkConstraints'])) {
|
1110: | unset($from['_options']['fkConstraints']);
|
1111: | }
|
1112: |
|
1113: | if (isset($to['_options']['version'])) {
|
1114: | unset($to['_options']['version']);
|
1115: | }
|
1116: |
|
1117: | if (isset($to['_options']['fkConstraints'])) {
|
1118: | unset($to['_options']['fkConstraints']);
|
1119: | }
|
1120: |
|
1121: | return $from != $to;
|
1122: | }
|
1123: |
|
1124: | |
1125: | |
1126: | |
1127: |
|
1128: | public function getCurrentVersion()
|
1129: | {
|
1130: | $version = 0;
|
1131: | if ($schema = self::getSchemaLockDefinition($this->dbNamespace)) {
|
1132: | $version = isset($schema['_options']['version']) ? $schema['_options']['version'] : 0;
|
1133: | }
|
1134: |
|
1135: | return $version;
|
1136: | }
|
1137: |
|
1138: | |
1139: | |
1140: | |
1141: | |
1142: | |
1143: | |
1144: | |
1145: |
|
1146: | public function checkVersions(array $schema)
|
1147: | {
|
1148: |
|
1149: | $versionDir = DB . 'version' . _DS_ . $this->dbNamespace;
|
1150: | if (!is_dir($versionDir)) {
|
1151: | return 0;
|
1152: | }
|
1153: |
|
1154: | $files = scandir($versionDir);
|
1155: | rsort($files);
|
1156: |
|
1157: |
|
1158: | $lastVersion = 0;
|
1159: | if (isset($schema['_options']['version'])) {
|
1160: | $lastVersion = $schema['_options']['version'];
|
1161: | if ($lastVersion . $this->sqlExtension == $files[0]) {
|
1162: | return 1;
|
1163: | }
|
1164: | }
|
1165: |
|
1166: |
|
1167: | $manager = $this;
|
1168: | $files = array_filter($files, function ($fileName) use ($lastVersion, $manager) {
|
1169: | if (preg_match('/\d{14}\\' . $manager->sqlExtension . '/', $fileName)) {
|
1170: | if ($lastVersion == 0) {
|
1171: | return true;
|
1172: | }
|
1173: |
|
1174: | $version = str_replace($manager->sqlExtension, '', $fileName);
|
1175: | if ($version > $lastVersion) {
|
1176: | return true;
|
1177: | }
|
1178: | }
|
1179: |
|
1180: | return false;
|
1181: | });
|
1182: |
|
1183: | if (count($files)) {
|
1184: | sort($files);
|
1185: | return $files;
|
1186: | }
|
1187: |
|
1188: | return 0;
|
1189: | }
|
1190: |
|
1191: | |
1192: | |
1193: | |
1194: |
|
1195: | public function isLoaded()
|
1196: | {
|
1197: | return isset($this->schema['_options']['pk']);
|
1198: | }
|
1199: |
|
1200: | |
1201: | |
1202: | |
1203: | |
1204: | |
1205: | |
1206: |
|
1207: | protected function isRenamed($needle, $haystack)
|
1208: | {
|
1209: | if (isset($haystack[$needle])) {
|
1210: | return $haystack[$needle];
|
1211: | } else {
|
1212: | return false;
|
1213: | }
|
1214: | }
|
1215: |
|
1216: | |
1217: | |
1218: | |
1219: | |
1220: |
|
1221: | public function hasTable($table)
|
1222: | {
|
1223: | if (!$this->isLoaded()) {
|
1224: | return false;
|
1225: | }
|
1226: |
|
1227: | $table = db_table($table);
|
1228: |
|
1229: | return isset($this->schema[$table]);
|
1230: | }
|
1231: |
|
1232: | |
1233: | |
1234: | |
1235: | |
1236: | |
1237: |
|
1238: | public function hasField($table, $field)
|
1239: | {
|
1240: | if (!$this->isLoaded()) {
|
1241: | return false;
|
1242: | }
|
1243: |
|
1244: | $table = db_table($table);
|
1245: |
|
1246: | return isset($this->schema[$table][$field]);
|
1247: | }
|
1248: |
|
1249: | |
1250: | |
1251: | |
1252: | |
1253: |
|
1254: | public function hasTimestamps($table)
|
1255: | {
|
1256: | if (!$this->isLoaded()) {
|
1257: | return false;
|
1258: | }
|
1259: |
|
1260: | $table = db_table($table);
|
1261: |
|
1262: | return (isset($this->schema[$table]['options']['timestamps']) && $this->schema[$table]['options']['timestamps']) ? true : false;
|
1263: | }
|
1264: |
|
1265: | |
1266: | |
1267: | |
1268: | |
1269: |
|
1270: | public function hasSlug($table)
|
1271: | {
|
1272: | if (!$this->isLoaded()) {
|
1273: | return false;
|
1274: | }
|
1275: |
|
1276: | $table = db_table($table);
|
1277: |
|
1278: | return isset($this->schema[$table]['slug']) ? true : false;
|
1279: | }
|
1280: |
|
1281: | |
1282: | |
1283: | |
1284: | |
1285: | |
1286: |
|
1287: | public function getFieldType($table, $field)
|
1288: | {
|
1289: | $table = db_table($table);
|
1290: |
|
1291: | if ($this->hasField($table, $field)) {
|
1292: | return $this->schema[$table][$field]['type'];
|
1293: | }
|
1294: |
|
1295: | return null;
|
1296: | }
|
1297: |
|
1298: | |
1299: | |
1300: | |
1301: | |
1302: | |
1303: |
|
1304: | protected function getOptions()
|
1305: | {
|
1306: | if (isset($this->schema['_options'])) {
|
1307: | $options = $this->schema['_options'] + $this->defaultOptions;
|
1308: | } else {
|
1309: | $options = $this->defaultOptions;
|
1310: | }
|
1311: |
|
1312: | return $options;
|
1313: | }
|
1314: |
|
1315: | |
1316: | |
1317: | |
1318: | |
1319: | |
1320: | |
1321: |
|
1322: | protected function getTableOptions($tableDef)
|
1323: | {
|
1324: | $options = $this->getOptions();
|
1325: |
|
1326: | if (isset($options['pk'])) {
|
1327: | unset($options['pk']);
|
1328: | }
|
1329: |
|
1330: | if (isset($options['fkConstraints'])) {
|
1331: | unset($options['fkConstraints']);
|
1332: | }
|
1333: |
|
1334: | if (isset($tableDef['options'])) {
|
1335: | $tableDef['options'] += $options;
|
1336: | } else {
|
1337: | $tableDef['options'] = $options;
|
1338: | }
|
1339: |
|
1340: | return $tableDef['options'];
|
1341: | }
|
1342: |
|
1343: | |
1344: | |
1345: | |
1346: | |
1347: |
|
1348: | public function populatePrimaryKeys(&$schema)
|
1349: | {
|
1350: |
|
1351: | $pkFields = array();
|
1352: | foreach ($schema as $table => $def) {
|
1353: | $def['options'] = $this->getTableOptions($def);
|
1354: |
|
1355: | if ($def['options']['timestamps']) {
|
1356: | $def['created'] = array('type' => 'datetime', 'null' => true);
|
1357: | $def['updated'] = array('type' => 'datetime', 'null' => true);
|
1358: | $def['deleted'] = array('type' => 'datetime', 'null' => true);
|
1359: | }
|
1360: |
|
1361: | $schema[$table] = $def;
|
1362: |
|
1363: |
|
1364: | $pkFields[$table] = array();
|
1365: | if (isset($def['options']['pk'])) {
|
1366: | foreach ($def['options']['pk'] as $pk) {
|
1367: | if (isset($def[$pk])) {
|
1368: |
|
1369: | $pkFields[$table][$pk] = $def[$pk];
|
1370: | } else {
|
1371: |
|
1372: | $pkFields[$table][$pk] = $this->getPKDefaultType();
|
1373: | }
|
1374: | }
|
1375: | } else {
|
1376: | $pkFields[$table]['id'] = $this->getPKDefaultType();
|
1377: | }
|
1378: | }
|
1379: |
|
1380: | $this->setPrimaryKeys($pkFields);
|
1381: |
|
1382: | return $pkFields;
|
1383: | }
|
1384: |
|
1385: | |
1386: | |
1387: | |
1388: | |
1389: |
|
1390: | public function populatePivots(&$schema)
|
1391: | {
|
1392: | $constraints = array();
|
1393: | $pkFields = $this->getPrimaryKeys();
|
1394: |
|
1395: | $manyToMany = array_filter($schema, function ($def) {
|
1396: | return isset($def['m:m']);
|
1397: | });
|
1398: |
|
1399: | foreach ($manyToMany as $table => $def) {
|
1400: | foreach ($def['m:m'] as $fkTable => $joint) {
|
1401: | if (!empty($joint['table']) && isset($schema[$joint['table']])) {
|
1402: |
|
1403: | continue;
|
1404: | }
|
1405: |
|
1406: | if (isset($schema[$table . '_to_' . $fkTable]) || isset($schema[$fkTable . '_to_' . $table])) {
|
1407: |
|
1408: | continue;
|
1409: | }
|
1410: |
|
1411: | if (isset($schema[$fkTable]['m:m'][$table])) {
|
1412: | if (empty($joint['table']) && !empty($schema[$fkTable]['m:m'][$table]['table'])) {
|
1413: | $joint['table'] = $schema[$fkTable]['m:m'][$table]['table'];
|
1414: | }
|
1415: |
|
1416: |
|
1417: | $jointTable = !empty($joint['table']) ? $joint['table'] : $table . '_to_' . $fkTable;
|
1418: | $schema[$jointTable]['options'] = array(
|
1419: | 'pk' => array(),
|
1420: | 'timestamps' => false,
|
1421: | 'm:m' => true
|
1422: | ) + $this->defaultOptions;
|
1423: |
|
1424: |
|
1425: | $relation = $this->getRelationOptions($joint, $table);
|
1426: | foreach ($relation as $rel) {
|
1427: | $field = $rel['name'];
|
1428: | $schema[$jointTable][$field] = $this->getFKField($table, $rel);
|
1429: | $schema[$jointTable][$field]['null'] = false;
|
1430: | $schema[$jointTable]['options']['pk'][] = $field;
|
1431: | $pkFields[$jointTable][$field] = $schema[$jointTable][$field];
|
1432: |
|
1433: | $constraint = $this->getFKConstraint($table, $rel, $schema);
|
1434: | if ($constraint) {
|
1435: | $constraints[$jointTable][$field] = $constraint;
|
1436: | }
|
1437: | }
|
1438: |
|
1439: |
|
1440: | $relation = $this->getRelationOptions($schema[$fkTable]['m:m'][$table], $fkTable);
|
1441: | foreach ($relation as $rel) {
|
1442: | $field = $rel['name'];
|
1443: | $schema[$jointTable][$field] = $this->getFKField($fkTable, $rel);
|
1444: | $schema[$jointTable][$field]['null'] = false;
|
1445: | $schema[$jointTable]['options']['pk'][] = $field;
|
1446: | $pkFields[$jointTable][$field] = $schema[$jointTable][$field];
|
1447: |
|
1448: | $constraint = $this->getFKConstraint($fkTable, $rel, $schema);
|
1449: | if ($constraint) {
|
1450: | $constraints[$jointTable][$field] = $constraint;
|
1451: | }
|
1452: | }
|
1453: | }
|
1454: | }
|
1455: | }
|
1456: |
|
1457: | $this->setPrimaryKeys($pkFields);
|
1458: | $this->setConstraints($constraints);
|
1459: |
|
1460: | return $constraints;
|
1461: | }
|
1462: |
|
1463: | |
1464: | |
1465: | |
1466: | |
1467: | |
1468: | |
1469: | |
1470: |
|
1471: | public function createTableStatement($table, &$schema, &$pkFields, &$constraints)
|
1472: | {
|
1473: | if (!isset($schema[$table])) {
|
1474: | return null;
|
1475: | }
|
1476: |
|
1477: | $def = $schema[$table];
|
1478: | $fullTableName = db_table($table);
|
1479: | $fkFields = array();
|
1480: |
|
1481: |
|
1482: | if (isset($def['m:1']) && is_array($def['m:1'])) {
|
1483: | foreach ($def['m:1'] as $fkTable) {
|
1484: | if (isset($schema[$fkTable]['1:m'][$table]) || array_search($table, $schema[$fkTable]['1:m']) !== false) {
|
1485: | $relationOptions = array();
|
1486: | if (isset($schema[$fkTable]['1:m'][$table])) {
|
1487: | $relationOptions = $schema[$fkTable]['1:m'][$table];
|
1488: | }
|
1489: |
|
1490: | $relation = $this->getRelationOptions($relationOptions, $fkTable);
|
1491: | foreach ($relation as $rel) {
|
1492: | $field = $rel['name'];
|
1493: |
|
1494: | $fkFields[$field] = $this->getFKField($fkTable, $rel);
|
1495: |
|
1496: | $constraint = $this->getFKConstraint($fkTable, $rel, $schema);
|
1497: | if ($constraint) {
|
1498: | $constraints[$table][$field] = $constraint;
|
1499: | }
|
1500: | }
|
1501: | }
|
1502: | }
|
1503: | }
|
1504: |
|
1505: |
|
1506: | if (isset($def['1:1']) && is_array($def['1:1'])) {
|
1507: | foreach ($def['1:1'] as $fkTable => $fk) {
|
1508: | $relationOptions = array();
|
1509: | if (is_numeric($fkTable)) {
|
1510: | $fkTable = $fk;
|
1511: | } else {
|
1512: | $relationOptions = $fk;
|
1513: | }
|
1514: |
|
1515: | $relation = $this->getRelationOptions($relationOptions, $fkTable);
|
1516: | foreach ($relation as $rel) {
|
1517: | $field = $rel['name'];
|
1518: |
|
1519: | $fkFields[$field] = $this->getFKField($fkTable, $rel);
|
1520: |
|
1521: | $constraint = $this->getFKConstraint($fkTable, $rel, $schema);
|
1522: | if ($constraint) {
|
1523: | $constraints[$table][$field] = $constraint;
|
1524: | }
|
1525: | }
|
1526: | }
|
1527: | }
|
1528: |
|
1529: | $this->setConstraints($constraints);
|
1530: |
|
1531: | $def = array_merge($pkFields[$table], $fkFields, $def);
|
1532: | $schema[$table] = $def;
|
1533: |
|
1534: |
|
1535: | if (isset($def['options']['m:m']) && $def['options']['m:m']) {
|
1536: | $jointTable = $table;
|
1537: | foreach ($schema[$jointTable] as $field => $rule) {
|
1538: | if ($field == 'options') {
|
1539: | continue;
|
1540: | }
|
1541: | $fkFields[$field] = $rule;
|
1542: | }
|
1543: | }
|
1544: |
|
1545: | $options = $this->getTableOptions($def);
|
1546: | $def['options'] = $options;
|
1547: |
|
1548: |
|
1549: | $sql = "CREATE TABLE IF NOT EXISTS `{$fullTableName}` (" . PHP_EOL;
|
1550: |
|
1551: |
|
1552: | $autoinc = false;
|
1553: | foreach ($def as $name => $rule) {
|
1554: |
|
1555: | if (in_array($name, self::$relationships) || $name == 'options') {
|
1556: | continue;
|
1557: | }
|
1558: |
|
1559: | $sql .= ' ' . $this->getFieldStatement($name, $rule, $this->getTableCollation($name, $schema)) . ',' . PHP_EOL;
|
1560: |
|
1561: |
|
1562: | if (isset($rule['unique']) && $rule['unique']) {
|
1563: | $fkFields[$name] = $rule;
|
1564: | }
|
1565: |
|
1566: | if (isset($rule['autoinc']) && $rule['autoinc']) {
|
1567: | $autoinc = true;
|
1568: | }
|
1569: | }
|
1570: |
|
1571: |
|
1572: | if (count($fkFields)) {
|
1573: | foreach (array_keys($fkFields) as $name) {
|
1574: | if (isset($fkFields[$name]['unique']) && $fkFields[$name]['unique']) {
|
1575: | $sql .= ' UNIQUE KEY';
|
1576: | } else {
|
1577: | $sql .= ' KEY';
|
1578: | }
|
1579: | $sql .= " `IDX_$name` (`$name`)," . PHP_EOL;
|
1580: | }
|
1581: | }
|
1582: |
|
1583: |
|
1584: | if (isset($options['unique']) && is_array($options['unique'])) {
|
1585: | foreach ($options['unique'] as $keyName => $uniqueFields) {
|
1586: | $sql .= ' UNIQUE KEY';
|
1587: | $sql .= " `IDX_$keyName` (`" . implode('`,`', $uniqueFields) . "`)," . PHP_EOL;
|
1588: | }
|
1589: | }
|
1590: |
|
1591: |
|
1592: | if (isset($pkFields[$table])) {
|
1593: | $sql .= ' PRIMARY KEY (`' . implode('`,`', array_keys($pkFields[$table])) . '`)' . PHP_EOL;
|
1594: | }
|
1595: |
|
1596: | $sql .= ')';
|
1597: | $sql .= ' ENGINE=' . $options['engine'];
|
1598: | $sql .= ' DEFAULT CHARSET=' . $options['charset'];
|
1599: | $sql .= ' COLLATE=' . $options['collate'];
|
1600: |
|
1601: | if ($autoinc) {
|
1602: | $sql .= ' AUTO_INCREMENT=1';
|
1603: | }
|
1604: |
|
1605: | $sql .= ';' . PHP_EOL;
|
1606: |
|
1607: | return $sql;
|
1608: | }
|
1609: |
|
1610: | |
1611: | |
1612: | |
1613: | |
1614: |
|
1615: | public function createConstraintStatements($constraints = null)
|
1616: | {
|
1617: | if ($constraints === null) {
|
1618: | $constraints = $this->getConstraints();
|
1619: | }
|
1620: |
|
1621: | $options = $this->getOptions();
|
1622: | $sql = array();
|
1623: |
|
1624: | if ($options['constraints']) {
|
1625: | foreach ($constraints as $table => $constraint) {
|
1626: | $fullTableName = db_table($table);
|
1627: | $constraintSql = "ALTER TABLE `{$fullTableName}`" . PHP_EOL;
|
1628: | $statement = array();
|
1629: | foreach ($constraint as $field => $rule) {
|
1630: | $statement[] = " ADD CONSTRAINT `{$rule['name']}` FOREIGN KEY (`{$rule['fields']}`)"
|
1631: | . " REFERENCES `{$rule['reference_table']}` (`{$rule['reference_fields']}`)"
|
1632: | . " ON DELETE {$rule['on_delete']}"
|
1633: | . " ON UPDATE {$rule['on_update']}";
|
1634: | }
|
1635: | $constraintSql .= implode(',' . PHP_EOL, $statement) . ';' . PHP_EOL;
|
1636: | $sql[] = $constraintSql;
|
1637: | }
|
1638: | }
|
1639: |
|
1640: | return count($sql) ? $sql : null;
|
1641: | }
|
1642: |
|
1643: | |
1644: | |
1645: | |
1646: | |
1647: |
|
1648: | public function dropConstraintStatements($constraints = null)
|
1649: | {
|
1650: | if ($constraints === null) {
|
1651: | $constraints = $this->getConstraints();
|
1652: | }
|
1653: |
|
1654: | $options = $this->getOptions();
|
1655: | $sql = array();
|
1656: |
|
1657: | if ($options['constraints']) {
|
1658: | $tables = array_keys($constraints);
|
1659: | foreach ($tables as $table) {
|
1660: | $fullTableName = db_table($table);
|
1661: | $result = db_query("SHOW CREATE TABLE `{$fullTableName}`");
|
1662: | if ($result && $row = db_fetchArray($result)) {
|
1663: | $fKeys = array();
|
1664: | if (preg_match_all('/CONSTRAINT `(FK_[A-Z0-9]+)` FOREIGN KEY/', $row[1], $matches)) {
|
1665: | foreach ($matches[1] as $constraintName) {
|
1666: | $fKeys[] = " DROP FOREIGN KEY `{$constraintName}`";
|
1667: | }
|
1668: | }
|
1669: |
|
1670: | if (count($fKeys)) {
|
1671: | $sql[] = "ALTER TABLE `{$fullTableName}`" . PHP_EOL . implode(',' . PHP_EOL, $fKeys) . ';';
|
1672: | }
|
1673: | }
|
1674: | }
|
1675: | }
|
1676: |
|
1677: | return count($sql) ? $sql : null;
|
1678: | }
|
1679: |
|
1680: | |
1681: | |
1682: | |
1683: | |
1684: |
|
1685: | public function setPrimaryKeys($pkFields)
|
1686: | {
|
1687: | $this->schema['_options']['pk'] = $pkFields;
|
1688: | }
|
1689: |
|
1690: | |
1691: | |
1692: | |
1693: | |
1694: |
|
1695: | public function getPrimaryKeys($schema = null)
|
1696: | {
|
1697: | if ($schema === null) {
|
1698: | $schema = $this->schema;
|
1699: | }
|
1700: |
|
1701: | return !empty($schema['_options']['pk']) ? $schema['_options']['pk'] : array();
|
1702: | }
|
1703: |
|
1704: | |
1705: | |
1706: | |
1707: | |
1708: |
|
1709: | public function setConstraints($constraints)
|
1710: | {
|
1711: | $this->schema['_options']['fkConstraints'] = $constraints;
|
1712: | }
|
1713: |
|
1714: | |
1715: | |
1716: | |
1717: | |
1718: |
|
1719: | public function getConstraints($schema = null)
|
1720: | {
|
1721: | if ($schema === null) {
|
1722: | $schema = $this->schema;
|
1723: | }
|
1724: |
|
1725: | return !empty($schema['_options']['fkConstraints']) ? $schema['_options']['fkConstraints'] : array();
|
1726: | }
|
1727: |
|
1728: | |
1729: | |
1730: | |
1731: | |
1732: | |
1733: |
|
1734: | public function getTableCollation($table, $schema = null)
|
1735: | {
|
1736: | if ($schema === null) {
|
1737: | $schema = $this->schema;
|
1738: | }
|
1739: |
|
1740: | return isset($schema[$table]['options']['collate']) ? $schema[$table]['options']['collate'] : null;
|
1741: | }
|
1742: |
|
1743: | |
1744: | |
1745: | |
1746: | |
1747: | |
1748: | |
1749: | |
1750: | |
1751: | |
1752: | |
1753: |
|
1754: | private function getSimilarity($needle, array $from, array $to, $table = null)
|
1755: | {
|
1756: | if (in_array($needle, array_merge(SchemaManager::$relationships, array('options')))) {
|
1757: | return false;
|
1758: | }
|
1759: |
|
1760: | if ($table) {
|
1761: | $compared = 'field';
|
1762: | if (isset($this->droppedColumns[$table]) && !in_array($needle, $this->droppedColumns[$table])) {
|
1763: | return false;
|
1764: | }
|
1765: |
|
1766: | $haystack = &$this->addedColumns[$table];
|
1767: | } else {
|
1768: | $compared = 'table';
|
1769: | if (!in_array($needle, $this->droppedTables)) {
|
1770: | return false;
|
1771: | }
|
1772: |
|
1773: | $haystack = &$this->addedTables;
|
1774: | }
|
1775: |
|
1776: | if (!is_array($haystack) || in_array($needle, $haystack)) {
|
1777: | return false;
|
1778: | }
|
1779: |
|
1780: | $similarity = array();
|
1781: | $matchingText = array();
|
1782: | $matchingMetaphone = array();
|
1783: |
|
1784: | foreach ($haystack as $i => $name) {
|
1785: | if ($needle === $name) {
|
1786: | return false;
|
1787: | }
|
1788: |
|
1789: | $scores = array();
|
1790: | $matching[$name] = array();
|
1791: |
|
1792: | $changes = 100;
|
1793: | if ($compared == 'table') {
|
1794: |
|
1795: | $diff = $this->diffTables($from[$needle], $to[$name]);
|
1796: | if ($diff['changes'] == 0) {
|
1797: | unset($haystack[$i]);
|
1798: | return $name;
|
1799: | }
|
1800: | $changes = $diff['changes'];
|
1801: | } else {
|
1802: |
|
1803: | $diff = $this->diffColumns($from[$needle], $to[$name]);
|
1804: | $changes = $diff['changes'];
|
1805: | }
|
1806: | $percentChanges = 100 - $changes;
|
1807: |
|
1808: |
|
1809: | similar_text(strtolower($needle), strtolower($name), $percent1);
|
1810: | $matchingText[$name] = (int)round($percent1);
|
1811: |
|
1812: |
|
1813: | $metaphone1 = metaphone(strtolower($needle));
|
1814: | $metaphone2 = metaphone(strtolower($name));
|
1815: | similar_text($metaphone1, $metaphone2, $percent2);
|
1816: | $matchingMetaphone[$name] = (int)round($percent2);
|
1817: |
|
1818: | $percentByTwo = round(($percent1 + $percent2) / 2);
|
1819: | $percent1 = round($percent1);
|
1820: |
|
1821: | if ($percent1 < 100 && $percent2 == 100) {
|
1822: |
|
1823: | $scores[] = $percent1 + $percentChanges;
|
1824: | }
|
1825: |
|
1826: | if ($percentByTwo >= 95 && $percentByTwo <= 100) {
|
1827: |
|
1828: | $scores[] = $percentByTwo + $percentChanges;
|
1829: | }
|
1830: |
|
1831: | if ($percent1 > 50 && $percent1 < 100) {
|
1832: |
|
1833: | $scores[] = $percent1 + $percentChanges;
|
1834: | }
|
1835: |
|
1836: | if ($compared == 'field' && strpos(strtolower($needle), 'id') !== false && strpos(strtolower($name), 'id') !== false) {
|
1837: |
|
1838: | $scores[] = 75 + $percentChanges;
|
1839: | }
|
1840: |
|
1841: | if (count($scores)) {
|
1842: | arsort($scores);
|
1843: | $similarity[$name] = (int)round(array_shift($scores));
|
1844: | }
|
1845: | }
|
1846: |
|
1847: | if (count($similarity) == 0) {
|
1848: | return false;
|
1849: | }
|
1850: |
|
1851: | arsort($similarity);
|
1852: | arsort($matchingText);
|
1853: | arsort($matchingMetaphone);
|
1854: |
|
1855: | foreach (array($similarity, $matchingText, $matchingMetaphone) as $i => $matchings) {
|
1856: | $dups = array_count_values($matchings);
|
1857: | if (array_pop($dups) == 1 || $i == 2) {
|
1858: | $candidate = array_keys($matchings);
|
1859: | $topSimilarity = array_shift($candidate);
|
1860: | break;
|
1861: | }
|
1862: | }
|
1863: |
|
1864: | unset($haystack[array_search($topSimilarity, $haystack)]);
|
1865: |
|
1866: | return $topSimilarity;
|
1867: | }
|
1868: |
|
1869: | |
1870: | |
1871: | |
1872: | |
1873: | |
1874: |
|
1875: | private function detectDroppedTables(array $schemaFrom, array $schemaTo)
|
1876: | {
|
1877: |
|
1878: | foreach ($schemaFrom as $table => $tableDef) {
|
1879: | if ($table == '_options') {
|
1880: | continue;
|
1881: | }
|
1882: |
|
1883: | if (!isset($schemaTo[$table])) {
|
1884: | $this->droppedTables[] = $table;
|
1885: | continue;
|
1886: | }
|
1887: | }
|
1888: | }
|
1889: |
|
1890: | |
1891: | |
1892: | |
1893: | |
1894: | |
1895: |
|
1896: | private function detectAddedTables(array $schemaFrom, array $schemaTo)
|
1897: | {
|
1898: |
|
1899: | foreach ($schemaTo as $table => $tableDef) {
|
1900: | if ($table == '_options') {
|
1901: | continue;
|
1902: | }
|
1903: |
|
1904: | if (!isset($schemaFrom[$table])) {
|
1905: | $this->addedTables[] = $table;
|
1906: | continue;
|
1907: | }
|
1908: | }
|
1909: | }
|
1910: |
|
1911: | |
1912: | |
1913: | |
1914: | |
1915: | |
1916: |
|
1917: | private function detectDroppedColumns(array $schemaFrom, array $schemaTo)
|
1918: | {
|
1919: |
|
1920: | foreach ($schemaFrom as $table => $tableDef) {
|
1921: | if ($table == '_options') {
|
1922: | continue;
|
1923: | }
|
1924: |
|
1925: |
|
1926: | foreach ($tableDef as $field => $fieldDef) {
|
1927: | if (in_array($field, array_merge(SchemaManager::$relationships, array('options')))) {
|
1928: | continue;
|
1929: | }
|
1930: |
|
1931: | if (!isset($schemaTo[$table][$field])) {
|
1932: |
|
1933: | $this->droppedColumns[$table][] = $field;
|
1934: | }
|
1935: | }
|
1936: | }
|
1937: | }
|
1938: |
|
1939: | |
1940: | |
1941: | |
1942: | |
1943: | |
1944: |
|
1945: | private function detectAddedColumns(array $schemaFrom, array $schemaTo)
|
1946: | {
|
1947: |
|
1948: | foreach ($schemaTo as $table => $tableDef) {
|
1949: | if ($table == '_options') {
|
1950: | continue;
|
1951: | }
|
1952: |
|
1953: |
|
1954: | foreach ($tableDef as $field => $fieldDef) {
|
1955: | if (in_array($field, array_merge(SchemaManager::$relationships, array('options')))) {
|
1956: | continue;
|
1957: | }
|
1958: |
|
1959: | if (!isset($schemaFrom[$table][$field])) {
|
1960: |
|
1961: | $this->addedColumns[$table][] = $field;
|
1962: | }
|
1963: | }
|
1964: | }
|
1965: | }
|
1966: |
|
1967: |
|
1968: | |
1969: | |
1970: | |
1971: | |
1972: | |
1973: | |
1974: | |
1975: |
|
1976: | private function detectTableRenamings(array $schemaFrom, array $schemaTo)
|
1977: | {
|
1978: | $this->detectDroppedTables($schemaFrom, $schemaTo);
|
1979: | $this->detectAddedTables($schemaFrom, $schemaTo);
|
1980: |
|
1981: |
|
1982: | foreach ($schemaFrom as $table => $tableDef) {
|
1983: | if ($table == '_options') {
|
1984: | continue;
|
1985: | }
|
1986: |
|
1987: | $renamedTable = $this->getSimilarity($table, $schemaFrom, $schemaTo);
|
1988: | if ($renamedTable) {
|
1989: | $this->tablesRenamed[$table] = $renamedTable;
|
1990: | }
|
1991: | }
|
1992: | }
|
1993: |
|
1994: | |
1995: | |
1996: | |
1997: | |
1998: | |
1999: | |
2000: | |
2001: |
|
2002: | private function detectColumnRenamings(array $schemaFrom, array $schemaTo)
|
2003: | {
|
2004: | $this->detectDroppedColumns($schemaFrom, $schemaTo);
|
2005: | $this->detectAddedColumns($schemaFrom, $schemaTo);
|
2006: |
|
2007: |
|
2008: | foreach ($schemaFrom as $table => $tableDef) {
|
2009: | if ($table == '_options') {
|
2010: | continue;
|
2011: | }
|
2012: |
|
2013: | $originalTable = $table;
|
2014: | $renamedTable = null;
|
2015: | if (isset($this->tablesRenamed[$table])) {
|
2016: | $renamedTable = $this->tablesRenamed[$table];
|
2017: | }
|
2018: |
|
2019: | if (isset($schemaTo[$table]) || ($renamedTable && isset($schemaTo[$renamedTable]))) {
|
2020: | if ($renamedTable) {
|
2021: | $table = $renamedTable;
|
2022: | }
|
2023: |
|
2024: | foreach ($tableDef as $field => $fieldDef) {
|
2025: | if (in_array($field, array_merge(SchemaManager::$relationships, array('options')))) {
|
2026: | continue;
|
2027: | }
|
2028: |
|
2029: | if (!isset($schemaTo[$table][$field])) {
|
2030: |
|
2031: | $renamedCol = $this->getSimilarity($field, $tableDef, $schemaTo[$table], $table);
|
2032: | if ($renamedCol) {
|
2033: | $this->columnsRenamed[$table . '.' . $field] = $renamedCol;
|
2034: | }
|
2035: | }
|
2036: | }
|
2037: | }
|
2038: | }
|
2039: | }
|
2040: |
|
2041: | |
2042: | |
2043: | |
2044: | |
2045: | |
2046: | |
2047: | |
2048: | |
2049: | |
2050: | |
2051: |
|
2052: | private function diffColumns(array $from, array $to)
|
2053: | {
|
2054: | $changes = 0;
|
2055: | $diff = array();
|
2056: | foreach ($from as $key => $value) {
|
2057: | if (!isset($to[$key])) {
|
2058: | $diff[$key] = $value;
|
2059: | $changes++;
|
2060: | continue;
|
2061: | }
|
2062: |
|
2063: | if (isset($to[$key]) && $from[$key] != $to[$key]) {
|
2064: | $diff[$key] = $to[$key];
|
2065: | $changes++;
|
2066: | continue;
|
2067: | }
|
2068: | }
|
2069: |
|
2070: | $fromKeys = array_keys($from);
|
2071: | $toKeys = array_keys($to);
|
2072: | $diffKeys = array_diff($toKeys, $fromKeys);
|
2073: | foreach ($diffKeys as $key) {
|
2074: | $diff[$key] = $to[$key];
|
2075: | $changes++;
|
2076: | }
|
2077: |
|
2078: | return array(
|
2079: | 'diff' => $diff,
|
2080: | 'changes' => $changes,
|
2081: | );
|
2082: | }
|
2083: |
|
2084: | |
2085: | |
2086: | |
2087: | |
2088: | |
2089: | |
2090: | |
2091: | |
2092: | |
2093: | |
2094: |
|
2095: | private function diffTables(array $from, array $to)
|
2096: | {
|
2097: | $changes = 0;
|
2098: | $diff = array();
|
2099: | foreach ($from as $key => $value) {
|
2100: | if (!isset($to[$key]) || (isset($to[$key]) && $from[$key] != $to[$key])) {
|
2101: | $diff[$key] = $value;
|
2102: | $changes++;
|
2103: | }
|
2104: | }
|
2105: |
|
2106: | return array(
|
2107: | 'diff' => $diff,
|
2108: | 'changes' => $changes,
|
2109: | );
|
2110: | }
|
2111: |
|
2112: | |
2113: | |
2114: | |
2115: | |
2116: |
|
2117: | private function getVersionDir($dbNamespace = null)
|
2118: | {
|
2119: | if ($dbNamespace === null) {
|
2120: | $dbNamespace = $this->dbNamespace;
|
2121: | }
|
2122: |
|
2123: | $versionDir = DB . 'version' . _DS_ . $dbNamespace;
|
2124: | if (!is_dir($versionDir)) {
|
2125: | mkdir($versionDir, 777, true);
|
2126: | }
|
2127: |
|
2128: | return $versionDir;
|
2129: | }
|
2130: |
|
2131: | |
2132: | |
2133: | |
2134: | |
2135: |
|
2136: | public static function getSchemaLockDefinition($dbNamespace = null)
|
2137: | {
|
2138: | $file = DB . _DS_ . 'build' . _DS_ . 'schema';
|
2139: | if ($dbNamespace) {
|
2140: | $file .= '.' . $dbNamespace;
|
2141: | }
|
2142: | $file .= '.lock';
|
2143: |
|
2144: | if (!(is_file($file) && file_exists($file))) {
|
2145: | return null;
|
2146: | }
|
2147: |
|
2148: | return unserialize(file_get_contents($file));
|
2149: | }
|
2150: |
|
2151: | |
2152: | |
2153: | |
2154: | |
2155: | |
2156: |
|
2157: | public static function getSchemaLockFileName($dbNamespace = null, $backupFileName = false)
|
2158: | {
|
2159: | $file = DB . _DS_ . 'build' . _DS_;
|
2160: |
|
2161: | if ($backupFileName) {
|
2162: | $file .= '~';
|
2163: | }
|
2164: |
|
2165: | $file .= 'schema';
|
2166: |
|
2167: | if ($dbNamespace) {
|
2168: | $file .= '.' . $dbNamespace;
|
2169: | }
|
2170: |
|
2171: | $file .= '.lock';
|
2172: |
|
2173: | return $file;
|
2174: | }
|
2175: | }
|
2176: | |