-
Notifications
You must be signed in to change notification settings - Fork 824
/
DBSchemaManager.php
1164 lines (1050 loc) · 39.4 KB
/
DBSchemaManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace SilverStripe\ORM\Connect;
use Exception;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBPrimaryKey;
/**
* Represents and handles all schema management for a database
*/
abstract class DBSchemaManager
{
/**
*
* @config
* Check tables when running /dev/build, and repair them if necessary.
* In case of large databases or more fine-grained control on how to handle
* data corruption in tables, you can disable this behaviour and handle it
* outside of this class, e.g. through a nightly system task with extended logging capabilities.
*
* @var bool
*/
private static $check_and_repair_on_build = true;
/**
* Check if tables should be renamed in a case-sensitive fashion.
* Note: This should still work even on case-insensitive databases.
*
* @var bool
*/
private static $fix_table_case_on_build = true;
/**
* Instance of the database controller this schema belongs to
*
* @var Database
*/
protected $database = null;
/**
* If this is false, then information about database operations
* will be displayed, eg creation of tables.
*
* @var boolean
*/
protected $supressOutput = false;
/**
* @var array<string,string>
*/
protected static $table_name_warnings = [];
/**
* @param string $table
* @param string $class
*
* @deprecated 4.0.0:5.0.0
*/
public static function showTableNameWarning($table, $class)
{
static::$table_name_warnings[$table] = $class;
}
/**
* Injector injection point for database controller
*
* @param Database $database
*/
public function setDatabase(Database $database)
{
$this->database = $database;
}
/**
* The table list, generated by the tableList() function.
* Used by the requireTable() function.
*
* @var array
*/
protected $tableList;
/**
* Keeps track whether we are currently updating the schema.
*
* @var boolean
*/
protected $schemaIsUpdating = false;
/**
* Large array structure that represents a schema update transaction
*
* @var array
*/
protected $schemaUpdateTransaction;
/**
* Enable suppression of database messages.
*
* @param bool $quiet
*/
public function quiet($quiet = true)
{
$this->supressOutput = $quiet;
}
/**
* Execute the given SQL query.
* This abstract function must be defined by subclasses as part of the actual implementation.
* It should return a subclass of SS_Query as the result.
*
* @param string $sql The SQL query to execute
* @param int $errorLevel The level of error reporting to enable for the query
* @return Query
*/
public function query($sql, $errorLevel = E_USER_ERROR)
{
return $this->database->query($sql, $errorLevel);
}
/**
* Execute the given SQL parameterised query with the specified arguments
*
* @param string $sql The SQL query to execute. The ? character will denote parameters.
* @param array $parameters An ordered list of arguments.
* @param int $errorLevel The level of error reporting to enable for the query
* @return Query
*/
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
{
return $this->database->preparedQuery($sql, $parameters, $errorLevel);
}
/**
* Initiates a schema update within a single callback
*
* @param callable $callback
*/
public function schemaUpdate($callback)
{
// Begin schema update
$this->schemaIsUpdating = true;
// Update table list
$this->tableList = [];
$tables = $this->tableList();
foreach ($tables as $table) {
$this->tableList[strtolower($table)] = $table;
}
// Clear update list for client code to mess around with
$this->schemaUpdateTransaction = [];
/** @var Exception $error */
$error = null;
try {
// Yield control to client code
$callback();
// If the client code has cancelled the update then abort
if (!$this->isSchemaUpdating()) {
return;
}
// End schema update
foreach ($this->schemaUpdateTransaction as $tableName => $changes) {
$advancedOptions = isset($changes['advancedOptions']) ? $changes['advancedOptions'] : null;
switch ($changes['command']) {
case 'create':
$this->createTable(
$tableName,
$changes['newFields'],
$changes['newIndexes'],
$changes['options'],
$advancedOptions
);
break;
case 'alter':
$this->alterTable(
$tableName,
$changes['newFields'],
$changes['newIndexes'],
$changes['alteredFields'],
$changes['alteredIndexes'],
$changes['alteredOptions'],
$advancedOptions
);
break;
}
}
} finally {
$this->schemaUpdateTransaction = null;
$this->schemaIsUpdating = false;
}
}
/**
* Cancels the schema updates requested during (but not after) schemaUpdate() call.
*/
public function cancelSchemaUpdate()
{
$this->schemaUpdateTransaction = null;
$this->schemaIsUpdating = false;
}
/**
* Returns true if we are during a schema update.
*
* @return boolean
*/
function isSchemaUpdating()
{
return $this->schemaIsUpdating;
}
/**
* Returns true if schema modifications were requested during (but not after) schemaUpdate() call.
*
* @return boolean
*/
public function doesSchemaNeedUpdating()
{
return (bool) $this->schemaUpdateTransaction;
}
// Transactional schema altering functions - they don't do anything except for update schemaUpdateTransaction
/**
* Instruct the schema manager to record a table creation to later execute
*
* @param string $table Name of the table
* @param array $options Create table options (ENGINE, etc.)
* @param array $advanced_options Advanced table creation options
*/
public function transCreateTable($table, $options = null, $advanced_options = null)
{
$this->schemaUpdateTransaction[$table] = [
'command' => 'create',
'newFields' => [],
'newIndexes' => [],
'options' => $options,
'advancedOptions' => $advanced_options
];
}
/**
* Instruct the schema manager to record a table alteration to later execute
*
* @param string $table Name of the table
* @param array $options Create table options (ENGINE, etc.)
* @param array $advanced_options Advanced table creation options
*/
public function transAlterTable($table, $options, $advanced_options)
{
$this->transInitTable($table);
$this->schemaUpdateTransaction[$table]['alteredOptions'] = $options;
$this->schemaUpdateTransaction[$table]['advancedOptions'] = $advanced_options;
}
/**
* Instruct the schema manager to record a field to be later created
*
* @param string $table Name of the table to hold this field
* @param string $field Name of the field to create
* @param string $schema Field specification as a string
*/
public function transCreateField($table, $field, $schema)
{
$this->transInitTable($table);
$this->schemaUpdateTransaction[$table]['newFields'][$field] = $schema;
}
/**
* Instruct the schema manager to record an index to be later created
*
* @param string $table Name of the table to hold this index
* @param string $index Name of the index to create
* @param array $schema Already parsed index specification
*/
public function transCreateIndex($table, $index, $schema)
{
$this->transInitTable($table);
$this->schemaUpdateTransaction[$table]['newIndexes'][$index] = $schema;
}
/**
* Instruct the schema manager to record a field to be later updated
*
* @param string $table Name of the table to hold this field
* @param string $field Name of the field to update
* @param string $schema Field specification as a string
*/
public function transAlterField($table, $field, $schema)
{
$this->transInitTable($table);
$this->schemaUpdateTransaction[$table]['alteredFields'][$field] = $schema;
}
/**
* Instruct the schema manager to record an index to be later updated
*
* @param string $table Name of the table to hold this index
* @param string $index Name of the index to update
* @param array $schema Already parsed index specification
*/
public function transAlterIndex($table, $index, $schema)
{
$this->transInitTable($table);
$this->schemaUpdateTransaction[$table]['alteredIndexes'][$index] = $schema;
}
/**
* Handler for the other transXXX methods - mark the given table as being altered
* if it doesn't already exist
*
* @param string $table Name of the table to initialise
*/
protected function transInitTable($table)
{
if (!isset($this->schemaUpdateTransaction[$table])) {
$this->schemaUpdateTransaction[$table] = [
'command' => 'alter',
'newFields' => [],
'newIndexes' => [],
'alteredFields' => [],
'alteredIndexes' => [],
'alteredOptions' => ''
];
}
}
/**
* Generate the following table in the database, modifying whatever already exists
* as necessary.
*
* @todo Change detection for CREATE TABLE $options other than "Engine"
*
* @param string $table The name of the table
* @param array $fieldSchema A list of the fields to create, in the same form as DataObject::$db
* @param array $indexSchema A list of indexes to create. See {@link requireIndex()}
* The values of the array can be one of:
* - true: Create a single column index on the field named the same as the index.
* - ['fields' => ['A','B','C'], 'type' => 'index/unique/fulltext']: This gives you full
* control over the index.
* @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type
* @param array $options Create table options (ENGINE, etc.)
* @param array|bool $extensions List of extensions
*/
public function requireTable(
$table,
$fieldSchema = null,
$indexSchema = null,
$hasAutoIncPK = true,
$options = [],
$extensions = false
) {
if (!isset($this->tableList[strtolower($table)])) {
$this->transCreateTable($table, $options, $extensions);
$this->alterationMessage("Table $table: created", "created");
} else {
if (Config::inst()->get(static::class, 'fix_table_case_on_build')) {
$this->fixTableCase($table);
}
if (Config::inst()->get(static::class, 'check_and_repair_on_build')) {
$this->checkAndRepairTable($table);
}
// Check if options changed
$tableOptionsChanged = false;
// Check for DB constant on the schema class
$dbIDName = sprintf('%s::ID', static::class);
$dbID = defined($dbIDName) ? constant($dbIDName) : null;
if ($dbID && isset($options[$dbID])) {
if (preg_match('/ENGINE=([^\s]*)/', $options[$dbID], $alteredEngineMatches)) {
$alteredEngine = $alteredEngineMatches[1];
$tableStatus = $this->query(sprintf('SHOW TABLE STATUS LIKE \'%s\'', $table))->first();
$tableOptionsChanged = ($tableStatus['Engine'] != $alteredEngine);
}
}
if ($tableOptionsChanged || ($extensions && $this->database->supportsExtensions($extensions))) {
$this->transAlterTable($table, $options, $extensions);
}
}
//DB ABSTRACTION: we need to convert this to a db-specific version:
if (!isset($fieldSchema['ID'])) {
$this->requireField($table, 'ID', $this->IdColumn(false, $hasAutoIncPK));
}
// Create custom fields
if ($fieldSchema) {
foreach ($fieldSchema as $fieldName => $fieldSpec) {
//Is this an array field?
$arrayValue = '';
if (strpos($fieldSpec, '[') !== false) {
//If so, remove it and store that info separately
$pos = strpos($fieldSpec, '[');
$arrayValue = substr($fieldSpec, $pos);
$fieldSpec = substr($fieldSpec, 0, $pos);
}
/** @var DBField $fieldObj */
$fieldObj = Injector::inst()->create($fieldSpec, $fieldName);
$fieldObj->setArrayValue($arrayValue);
$fieldObj->setTable($table);
if ($fieldObj instanceof DBPrimaryKey) {
/** @var DBPrimaryKey $fieldObj */
$fieldObj->setAutoIncrement($hasAutoIncPK);
}
$fieldObj->requireField();
}
}
// Create custom indexes
if ($indexSchema) {
foreach ($indexSchema as $indexName => $indexSpec) {
$this->requireIndex($table, $indexName, $indexSpec);
}
}
// Check and display notice about $table_name
static $table_name_info_sent = false;
if (isset(static::$table_name_warnings[$table])) {
if (!$table_name_info_sent) {
$this->alterationMessage(
<<<'MESSAGE'
<strong>Please note:</strong> It is strongly recommended to define a
table_name for all namespaced models. Not defining a table_name may cause generated table
names to be too long and may not be supported by your current database engine. The generated
naming scheme will also change when upgrading to SilverStripe 5.0 and potentially break.
MESSAGE
,
'error'
);
$table_name_info_sent = true;
}
$this->alterationMessage('table_name not set for class ' . static::$table_name_warnings[$table], 'notice');
}
}
/**
* If the given table exists, move it out of the way by renaming it to _obsolete_(tablename).
* @param string $table The table name.
*/
public function dontRequireTable($table)
{
if (!isset($this->tableList[strtolower($table)])) {
return;
}
$prefix = "_obsolete_{$table}";
$suffix = '';
$renameTo = $prefix . $suffix;
while (isset($this->tableList[strtolower($renameTo)])) {
$suffix = $suffix
? ((int)$suffix + 1)
: 2;
$renameTo = $prefix . $suffix;
}
$renameFrom = $this->tableList[strtolower($table)];
$this->renameTable($renameFrom, $renameTo);
$this->alterationMessage("Table $table: renamed to $renameTo", "obsolete");
}
/**
* Generate the given index in the database, modifying whatever already exists as necessary.
*
* The keys of the array are the names of the index.
* The values of the array can be one of:
* - true: Create a single column index on the field named the same as the index.
* - ['type' => 'index|unique|fulltext', 'value' => 'FieldA, FieldB']: This gives you full
* control over the index.
*
* @param string $table The table name.
* @param string $index The index name.
* @param string|array|boolean $spec The specification of the index in any
* loose format. See requireTable() for more information.
*/
public function requireIndex($table, $index, $spec)
{
// Detect if adding to a new table
$newTable = !isset($this->tableList[strtolower($table)]);
// Force spec into standard array format
$specString = $this->convertIndexSpec($spec);
// Check existing index
$oldSpecString = null;
$indexKey = null;
if (!$newTable) {
$indexKey = $this->indexKey($table, $index, $spec);
$indexList = $this->indexList($table);
if (isset($indexList[$indexKey])) {
// $oldSpec should be in standard array format
$oldSpec = $indexList[$indexKey];
$oldSpecString = $this->convertIndexSpec($oldSpec);
}
}
// Initiate either generation or modification of index
if ($newTable || !isset($indexList[$indexKey])) {
// New index
$this->transCreateIndex($table, $index, $spec);
$this->alterationMessage("Index $table.$index: created as $specString", "created");
} elseif ($oldSpecString != $specString) {
// Updated index
$this->transAlterIndex($table, $index, $spec);
$this->alterationMessage(
"Index $table.$index: changed to $specString <i class=\"build-info-before\">(from $oldSpecString)</i>",
"changed"
);
}
}
/**
* Splits a spec string safely, considering quoted columns, whitespace,
* and cleaning brackets
*
* @param string $spec The input index specification string
* @return array List of columns in the spec
*/
protected function explodeColumnString($spec)
{
// Remove any leading/trailing brackets and outlying modifiers
// E.g. 'unique (Title, "QuotedColumn");' => 'Title, "QuotedColumn"'
$containedSpec = preg_replace('/(.*\(\s*)|(\s*\).*)/', '', $spec);
// Split potentially quoted modifiers
// E.g. 'Title, "QuotedColumn"' => ['Title', 'QuotedColumn']
return preg_split('/"?\s*,\s*"?/', trim($containedSpec, '(") '));
}
/**
* Builds a properly quoted column list from an array
*
* @param array $columns List of columns to implode
* @return string A properly quoted list of column names
*/
protected function implodeColumnList($columns)
{
if (empty($columns)) {
return '';
}
return '"' . implode('","', $columns) . '"';
}
/**
* Given an index specification in the form of a string ensure that each
* column name is property quoted, stripping brackets and modifiers.
* This index may also be in the form of a "CREATE INDEX..." sql fragment
*
* @param string $spec The input specification or query. E.g. 'unique (Column1, Column2)'
* @return string The properly quoted column list. E.g. '"Column1", "Column2"'
*/
protected function quoteColumnSpecString($spec)
{
$bits = $this->explodeColumnString($spec);
return $this->implodeColumnList($bits);
}
/**
* Given an index spec determines the index type
*
* @param array|string $spec
* @return string
*/
protected function determineIndexType($spec)
{
// check array spec
if (is_array($spec) && isset($spec['type'])) {
return $spec['type'];
} elseif (!is_array($spec) && preg_match('/(?<type>\w+)\s*\(/', $spec, $matchType)) {
return strtolower($matchType['type']);
} else {
return 'index';
}
}
/**
* This takes the index spec which has been provided by a class (ie static $indexes = blah blah)
* and turns it into a proper string.
* Some indexes may be arrays, such as fulltext and unique indexes, and this allows database-specific
* arrays to be created. See {@link requireTable()} for details on the index format.
*
* @see http://dev.mysql.com/doc/refman/5.0/en/create-index.html
* @see parseIndexSpec() for approximate inverse
*
* @param string|array $indexSpec
* @return string
*/
protected function convertIndexSpec($indexSpec)
{
// Return already converted spec
if (!is_array($indexSpec)
|| !array_key_exists('type', $indexSpec)
|| !array_key_exists('columns', $indexSpec)
|| !is_array($indexSpec['columns'])
|| array_key_exists('value', $indexSpec)
) {
throw new \InvalidArgumentException(
sprintf(
'argument to convertIndexSpec must be correct indexSpec, %s given',
var_export($indexSpec, true)
)
);
}
// Combine elements into standard string format
return sprintf('%s (%s)', $indexSpec['type'], $this->implodeColumnList($indexSpec['columns']));
}
/**
* Returns true if the given table is exists in the current database
*
* @param string $tableName Name of table to check
* @return boolean Flag indicating existence of table
*/
abstract public function hasTable($tableName);
/**
* Return true if the table exists and already has a the field specified
*
* @param string $tableName - The table to check
* @param string $fieldName - The field to check
* @return bool - True if the table exists and the field exists on the table
*/
public function hasField($tableName, $fieldName)
{
if (!$this->hasTable($tableName)) {
return false;
}
$fields = $this->fieldList($tableName);
return array_key_exists($fieldName, $fields);
}
/**
* Generate the given field on the table, modifying whatever already exists as necessary.
*
* @param string $table The table name.
* @param string $field The field name.
* @param array|string $spec The field specification. If passed in array syntax, the specific database
* driver takes care of the ALTER TABLE syntax. If passed as a string, its assumed to
* be prepared as a direct SQL framgment ready for insertion into ALTER TABLE. In this case you'll
* need to take care of database abstraction in your DBField subclass.
*/
public function requireField($table, $field, $spec)
{
//TODO: this is starting to get extremely fragmented.
//There are two different versions of $spec floating around, and their content changes depending
//on how they are structured. This needs to be tidied up.
$fieldValue = null;
$newTable = false;
// backwards compatibility patch for pre 2.4 requireField() calls
$spec_orig = $spec;
if (!is_string($spec)) {
$spec['parts']['name'] = $field;
$spec_orig['parts']['name'] = $field;
//Convert the $spec array into a database-specific string
$spec = $this->{$spec['type']}($spec['parts'], true);
}
// Collations didn't come in until MySQL 4.1. Anything earlier will throw a syntax error if you try and use
// collations.
// TODO: move this to the MySQLDatabase file, or drop it altogether?
if (!$this->database->supportsCollations()) {
$spec = preg_replace('/ *character set [^ ]+( collate [^ ]+)?( |$)/', '\\2', $spec);
}
if (!isset($this->tableList[strtolower($table)])) {
$newTable = true;
}
if (is_array($spec)) {
$specValue = $this->$spec_orig['type']($spec_orig['parts']);
} else {
$specValue = $spec;
}
// We need to get db-specific versions of the ID column:
if ($spec_orig == $this->IdColumn() || $spec_orig == $this->IdColumn(true)) {
$specValue = $this->IdColumn(true);
}
if (!$newTable) {
$fieldList = $this->fieldList($table);
if (isset($fieldList[$field])) {
if (is_array($fieldList[$field])) {
$fieldValue = $fieldList[$field]['data_type'];
} else {
$fieldValue = $fieldList[$field];
}
}
}
// Get the version of the field as we would create it. This is used for comparison purposes to see if the
// existing field is different to what we now want
if (is_array($spec_orig)) {
$spec_orig = $this->{$spec_orig['type']}($spec_orig['parts']);
}
if ($newTable || $fieldValue == '') {
$this->transCreateField($table, $field, $spec_orig);
$this->alterationMessage("Field $table.$field: created as $spec_orig", "created");
} elseif ($fieldValue != $specValue) {
// If enums/sets are being modified, then we need to fix existing data in the table.
// Update any records where the enum is set to a legacy value to be set to the default.
$enumValuesExpr = "/^(enum|set)\\s*\\(['\"](?<values>[^'\"]+)['\"]\\).*/i";
if (preg_match($enumValuesExpr, $specValue, $specMatches)
&& preg_match($enumValuesExpr, $spec_orig, $oldMatches)
) {
$new = preg_split("/'\\s*,\\s*'/", $specMatches['values']);
$old = preg_split("/'\\s*,\\s*'/", $oldMatches['values']);
$holder = [];
foreach ($old as $check) {
if (!in_array($check, $new)) {
$holder[] = $check;
}
}
if (count($holder)) {
// Get default pre-escaped for SQL. We just use this directly, as we don't have a real way to
// de-encode SQL values
$default = explode('default ', $spec_orig);
$defaultSQL = isset($default[1]) ? $default[1] : 'NULL';
// Reset to default any value in that is in the old enum, but not the new one
$placeholders = DB::placeholders($holder);
$query = "UPDATE \"{$table}\" SET \"{$field}\" = {$defaultSQL} WHERE \"{$field}\" IN ({$placeholders})";
$this->preparedQuery($query, $holder);
$amount = $this->database->affectedRows();
$this->alterationMessage(
"Changed $amount rows to default value of field $field (Value: $defaultSQL)"
);
}
}
$this->transAlterField($table, $field, $spec_orig);
$this->alterationMessage(
"Field $table.$field: changed to $specValue <i class=\"build-info-before\">(from {$fieldValue})</i>",
"changed"
);
}
}
/**
* If the given field exists, move it out of the way by renaming it to _obsolete_(fieldname).
*
* @param string $table
* @param string $fieldName
*/
public function dontRequireField($table, $fieldName)
{
$fieldList = $this->fieldList($table);
if (array_key_exists($fieldName, $fieldList)) {
$suffix = '';
while (isset($fieldList[strtolower("_obsolete_{$fieldName}$suffix")])) {
$suffix = $suffix
? ((int)$suffix + 1)
: 2;
}
$this->renameField($table, $fieldName, "_obsolete_{$fieldName}$suffix");
$this->alterationMessage(
"Field $table.$fieldName: renamed to $table._obsolete_{$fieldName}$suffix",
"obsolete"
);
}
}
/**
* Show a message about database alteration
*
* @param string $message to display
* @param string $type one of [created|changed|repaired|obsolete|deleted|error]
*/
public function alterationMessage($message, $type = "")
{
if (!$this->supressOutput) {
if (Director::is_cli()) {
switch ($type) {
case 'created':
case 'changed':
case 'repaired':
$sign = '+';
break;
case 'obsolete':
case 'deleted':
$sign = '-';
break;
case 'notice':
$sign = '*';
break;
case 'error':
$sign = '!';
break;
default:
$sign = ' ';
}
$message = strip_tags($message);
echo " $sign $message\n";
} else {
switch ($type) {
case 'created':
$class = 'success';
break;
case 'obsolete':
case 'error':
case 'deleted':
$class = 'error';
break;
case 'notice':
$class = 'warning';
break;
case 'changed':
case 'repaired':
$class = 'info';
break;
default:
$class = '';
}
echo "<li class=\"$class\">$message</li>";
}
}
}
/**
* This returns the data type for the id column which is the primary key for each table
*
* @param boolean $asDbValue
* @param boolean $hasAutoIncPK
* @return string
*/
abstract public function IdColumn($asDbValue = false, $hasAutoIncPK = true);
/**
* Checks a table's integrity and repairs it if necessary.
*
* @param string $tableName The name of the table.
* @return boolean Return true if the table has integrity after the method is complete.
*/
abstract public function checkAndRepairTable($tableName);
/**
* Ensure the given table has the correct case
*
* @param string $tableName Name of table in desired case
*/
public function fixTableCase($tableName)
{
// Check if table exists
$tables = $this->tableList();
if (!array_key_exists(strtolower($tableName), $tables)) {
return;
}
// Check if case differs
$currentName = $tables[strtolower($tableName)];
if ($currentName === $tableName) {
return;
}
$this->alterationMessage(
"Table $tableName: renamed from $currentName",
'repaired'
);
// Rename via temp table to avoid case-sensitivity issues
$tempTable = "__TEMP__{$tableName}";
$this->renameTable($currentName, $tempTable);
$this->renameTable($tempTable, $tableName);
}
/**
* Returns the values of the given enum field
*
* @param string $tableName Name of table to check
* @param string $fieldName name of enum field to check
* @return array List of enum values
*/
abstract public function enumValuesForField($tableName, $fieldName);
/*
* This is a lookup table for data types.
* For instance, Postgres uses 'INT', while MySQL uses 'UNSIGNED'
* So this is a DB-specific list of equivilents.
*
* @param string $type
* @return string
*/
abstract public function dbDataType($type);
/**
* Retrieves the list of all databases the user has access to
*
* @return array List of database names
*/
abstract public function databaseList();
/**
* Determine if the database with the specified name exists
*
* @param string $name Name of the database to check for
* @return boolean Flag indicating whether this database exists
*/
abstract public function databaseExists($name);
/**
* Create a database with the specified name
*
* @param string $name Name of the database to create
* @return boolean True if successful
*/
abstract public function createDatabase($name);
/**
* Drops a database with the specified name
*
* @param string $name Name of the database to drop
*/
abstract public function dropDatabase($name);
/**
* Alter an index on a table.
*
* @param string $tableName The name of the table.
* @param string $indexName The name of the index.
* @param array $indexSpec The specification of the index, see Database::requireIndex() for more details.
* @todo Find out where this is called from - Is it even used? Aren't indexes always dropped and re-added?
*/
abstract public function alterIndex($tableName, $indexName, $indexSpec);
/**
* Determines the key that should be used to identify this index
* when retrieved from DBSchemaManager->indexList.
* In some connectors this is the database-visible name, in others the
* usercode-visible name.
*
* @param string $table
* @param string $index
* @param array $spec
* @return string Key for this index
*/
abstract protected function indexKey($table, $index, $spec);
/**
* Return the list of indexes in a table.
*
* @param string $table The table name.
* @return array[] List of current indexes in the table, each in standard
* array form. The key for this array should be predictable using the indexKey
* method
*/
abstract public function indexList($table);
/**
* Returns a list of all tables in the database.
* Keys are table names in lower case, values are table names in case that
* database expects.
*
* @return array
*/
abstract public function tableList();
/**
* Create a new table.
*
* @param string $table The name of the table
* @param array $fields A map of field names to field types
* @param array $indexes A map of indexes
* @param array $options An map of additional options. The available keys are as follows:
* - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL.
* - 'temporary' - If true, then a temporary table will be created
* @param array $advancedOptions Advanced creation options
* @return string The table name generated. This may be different from the table name, for example with temporary
* tables.
*/
abstract public function createTable(
$table,
$fields = null,
$indexes = null,
$options = null,
$advancedOptions = null
);
/**
* Alter a table's schema.