-
Notifications
You must be signed in to change notification settings - Fork 890
/
PdoAdapter.php
974 lines (864 loc) · 29.2 KB
/
PdoAdapter.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
<?php
/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
namespace Phinx\Db\Adapter;
use BadMethodCallException;
use InvalidArgumentException;
use PDO;
use PDOException;
use Phinx\Config\Config;
use Phinx\Db\Action\AddColumn;
use Phinx\Db\Action\AddForeignKey;
use Phinx\Db\Action\AddIndex;
use Phinx\Db\Action\ChangeColumn;
use Phinx\Db\Action\ChangeComment;
use Phinx\Db\Action\ChangePrimaryKey;
use Phinx\Db\Action\DropForeignKey;
use Phinx\Db\Action\DropIndex;
use Phinx\Db\Action\DropTable;
use Phinx\Db\Action\RemoveColumn;
use Phinx\Db\Action\RenameColumn;
use Phinx\Db\Action\RenameTable;
use Phinx\Db\Table as DbTable;
use Phinx\Db\Table\Column;
use Phinx\Db\Table\ForeignKey;
use Phinx\Db\Table\Index;
use Phinx\Db\Table\Table;
use Phinx\Db\Util\AlterInstructions;
use Phinx\Migration\MigrationInterface;
use Phinx\Util\Literal;
use RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Phinx PDO Adapter.
*
* @author Rob Morgan <robbym@gmail.com>
*/
abstract class PdoAdapter extends AbstractAdapter implements DirectActionInterface
{
/**
* @var \PDO|null
*/
protected $connection;
/**
* Writes a message to stdout if verbose output is on
*
* @param string $message The message to show
* @return void
*/
protected function verboseLog($message)
{
if (
!$this->isDryRunEnabled() &&
$this->getOutput()->getVerbosity() < OutputInterface::VERBOSITY_VERY_VERBOSE
) {
return;
}
$this->getOutput()->writeln($message);
}
/**
* Create PDO connection
*
* @param string $dsn Connection string
* @param string|null $username Database username
* @param string|null $password Database password
* @param array $options Connection options
* @return \PDO
*/
protected function createPdoConnection($dsn, $username = null, $password = null, array $options = [])
{
$adapterOptions = $this->getOptions() + [
'attr_errmode' => PDO::ERRMODE_EXCEPTION,
];
try {
$db = new PDO($dsn, $username, $password, $options);
foreach ($adapterOptions as $key => $option) {
if (strpos($key, 'attr_') === 0) {
$pdoConstant = '\PDO::' . strtoupper($key);
if (!defined($pdoConstant)) {
throw new \UnexpectedValueException('Invalid PDO attribute: ' . $key . ' (' . $pdoConstant . ')');
}
$db->setAttribute(constant($pdoConstant), $option);
}
}
} catch (PDOException $e) {
throw new InvalidArgumentException(sprintf(
'There was a problem connecting to the database: %s',
$e->getMessage()
), $e->getCode(), $e);
}
return $db;
}
/**
* @inheritDoc
*/
public function setOptions(array $options)
{
parent::setOptions($options);
if (isset($options['connection'])) {
$this->setConnection($options['connection']);
}
return $this;
}
/**
* Sets the database connection.
*
* @param \PDO $connection Connection
* @return \Phinx\Db\Adapter\AdapterInterface
*/
public function setConnection(PDO $connection)
{
$this->connection = $connection;
// Create the schema table if it doesn't already exist
if (!$this->hasSchemaTable()) {
$this->createSchemaTable();
} else {
$table = new DbTable($this->getSchemaTableName(), [], $this);
if (!$table->hasColumn('migration_name')) {
$table
->addColumn(
'migration_name',
'string',
['limit' => 100, 'after' => 'version', 'default' => null, 'null' => true]
)
->save();
}
if (!$table->hasColumn('breakpoint')) {
$table
->addColumn('breakpoint', 'boolean', ['default' => false])
->save();
}
}
return $this;
}
/**
* Gets the database connection
*
* @return \PDO
*/
public function getConnection()
{
if ($this->connection === null) {
$this->connect();
}
return $this->connection;
}
/**
* @inheritDoc
*/
public function connect()
{
}
/**
* @inheritDoc
*/
public function disconnect()
{
}
/**
* @inheritDoc
*/
public function execute($sql)
{
$sql = rtrim($sql, "; \t\n\r\0\x0B") . ';';
$this->verboseLog($sql);
if ($this->isDryRunEnabled()) {
return 0;
}
return $this->getConnection()->exec($sql);
}
/**
* Returns the Cake\Database connection object using the same underlying
* PDO object as this connection.
*
* @return \Cake\Database\Connection
*/
abstract public function getDecoratedConnection();
/**
* @inheritDoc
*/
public function getQueryBuilder()
{
return $this->getDecoratedConnection()->newQuery();
}
/**
* Executes a query and returns PDOStatement.
*
* @param string $sql SQL
* @return \PDOStatement
*/
public function query($sql)
{
return $this->getConnection()->query($sql);
}
/**
* @inheritDoc
*/
public function fetchRow($sql)
{
return $this->query($sql)->fetch();
}
/**
* @inheritDoc
*/
public function fetchAll($sql)
{
return $this->query($sql)->fetchAll();
}
/**
* @inheritDoc
*/
public function insert(Table $table, $row)
{
$sql = sprintf(
'INSERT INTO %s ',
$this->quoteTableName($table->getName())
);
$columns = array_keys($row);
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $columns)) . ')';
foreach ($row as $column => $value) {
if (is_bool($value)) {
$row[$column] = $this->castToBool($value);
}
}
if ($this->isDryRunEnabled()) {
$sql .= ' VALUES (' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ');';
$this->output->writeln($sql);
} else {
$sql .= ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
$stmt = $this->getConnection()->prepare($sql);
$stmt->execute(array_values($row));
}
}
/**
* Quotes a database value.
*
* @param mixed $value The value to quote
* @return mixed
*/
protected function quoteValue($value)
{
if (is_numeric($value)) {
return $value;
}
if ($value === null) {
return 'null';
}
return $this->getConnection()->quote($value);
}
/**
* Quotes a database string.
*
* @param string $value The string to quote
* @return string
*/
protected function quoteString($value)
{
return $this->getConnection()->quote($value);
}
/**
* @inheritDoc
*/
public function bulkinsert(Table $table, $rows)
{
$sql = sprintf(
'INSERT INTO %s ',
$this->quoteTableName($table->getName())
);
$current = current($rows);
$keys = array_keys($current);
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $keys)) . ') VALUES ';
if ($this->isDryRunEnabled()) {
$values = array_map(function ($row) {
return '(' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ')';
}, $rows);
$sql .= implode(', ', $values) . ';';
$this->output->writeln($sql);
} else {
$count_keys = count($keys);
$query = '(' . implode(', ', array_fill(0, $count_keys, '?')) . ')';
$count_vars = count($rows);
$queries = array_fill(0, $count_vars, $query);
$sql .= implode(',', $queries);
$stmt = $this->getConnection()->prepare($sql);
$vals = [];
foreach ($rows as $row) {
foreach ($row as $v) {
if (is_bool($v)) {
$vals[] = $this->castToBool($v);
} else {
$vals[] = $v;
}
}
}
$stmt->execute($vals);
}
}
/**
* @inheritDoc
*/
public function getVersions()
{
$rows = $this->getVersionLog();
return array_keys($rows);
}
/**
* {@inheritDoc}
*
* @throws \RuntimeException
*/
public function getVersionLog()
{
$result = [];
switch ($this->options['version_order']) {
case Config::VERSION_ORDER_CREATION_TIME:
$orderBy = 'version ASC';
break;
case Config::VERSION_ORDER_EXECUTION_TIME:
$orderBy = 'start_time ASC, version ASC';
break;
default:
throw new RuntimeException('Invalid version_order configuration option');
}
// This will throw an exception if doing a --dry-run without any migrations as phinxlog
// does not exist, so in that case, we can just expect to trivially return empty set
try {
$rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY %s', $this->quoteTableName($this->getSchemaTableName()), $orderBy));
} catch (PDOException $e) {
if (!$this->isDryRunEnabled()) {
throw $e;
}
$rows = [];
}
foreach ($rows as $version) {
$result[$version['version']] = $version;
}
return $result;
}
/**
* @inheritDoc
*/
public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime)
{
if (strcasecmp($direction, MigrationInterface::UP) === 0) {
// up
$sql = sprintf(
"INSERT INTO %s (%s, %s, %s, %s, %s) VALUES ('%s', '%s', '%s', '%s', %s);",
$this->quoteTableName($this->getSchemaTableName()),
$this->quoteColumnName('version'),
$this->quoteColumnName('migration_name'),
$this->quoteColumnName('start_time'),
$this->quoteColumnName('end_time'),
$this->quoteColumnName('breakpoint'),
$migration->getVersion(),
substr($migration->getName(), 0, 100),
$startTime,
$endTime,
$this->castToBool(false)
);
$this->execute($sql);
} else {
// down
$sql = sprintf(
"DELETE FROM %s WHERE %s = '%s'",
$this->quoteTableName($this->getSchemaTableName()),
$this->quoteColumnName('version'),
$migration->getVersion()
);
$this->execute($sql);
}
return $this;
}
/**
* @inheritDoc
*/
public function toggleBreakpoint(MigrationInterface $migration)
{
$this->query(
sprintf(
'UPDATE %1$s SET %2$s = CASE %2$s WHEN %3$s THEN %4$s ELSE %3$s END, %7$s = %7$s WHERE %5$s = \'%6$s\';',
$this->quoteTableName($this->getSchemaTableName()),
$this->quoteColumnName('breakpoint'),
$this->castToBool(true),
$this->castToBool(false),
$this->quoteColumnName('version'),
$migration->getVersion(),
$this->quoteColumnName('start_time')
)
);
return $this;
}
/**
* @inheritDoc
*/
public function resetAllBreakpoints()
{
return $this->execute(
sprintf(
'UPDATE %1$s SET %2$s = %3$s, %4$s = %4$s WHERE %2$s <> %3$s;',
$this->quoteTableName($this->getSchemaTableName()),
$this->quoteColumnName('breakpoint'),
$this->castToBool(false),
$this->quoteColumnName('start_time')
)
);
}
/**
* @inheritDoc
*/
public function setBreakpoint(MigrationInterface $migration)
{
return $this->markBreakpoint($migration, true);
}
/**
* @inheritDoc
*/
public function unsetBreakpoint(MigrationInterface $migration)
{
return $this->markBreakpoint($migration, false);
}
/**
* Mark a migration breakpoint.
*
* @param \Phinx\Migration\MigrationInterface $migration The migration target for the breakpoint
* @param bool $state The required state of the breakpoint
* @return \Phinx\Db\Adapter\AdapterInterface
*/
protected function markBreakpoint(MigrationInterface $migration, $state)
{
$this->query(
sprintf(
'UPDATE %1$s SET %2$s = %3$s, %4$s = %4$s WHERE %5$s = \'%6$s\';',
$this->quoteTableName($this->getSchemaTableName()),
$this->quoteColumnName('breakpoint'),
$this->castToBool($state),
$this->quoteColumnName('start_time'),
$this->quoteColumnName('version'),
$migration->getVersion()
)
);
return $this;
}
/**
* {@inheritDoc}
*
* @throws \BadMethodCallException
* @return void
*/
public function createSchema($schemaName = 'public')
{
throw new BadMethodCallException('Creating a schema is not supported');
}
/**
* {@inheritDoc}
*
* @throws \BadMethodCallException
* @return void
*/
public function dropSchema($name)
{
throw new BadMethodCallException('Dropping a schema is not supported');
}
/**
* @inheritDoc
*/
public function getColumnTypes()
{
return [
'string',
'char',
'text',
'tinyinteger',
'smallinteger',
'integer',
'biginteger',
'bit',
'float',
'decimal',
'double',
'datetime',
'timestamp',
'time',
'date',
'blob',
'binary',
'varbinary',
'boolean',
'uuid',
// Geospatial data types
'geometry',
'point',
'linestring',
'polygon',
];
}
/**
* @inheritDoc
*/
public function castToBool($value)
{
return (bool)$value ? 1 : 0;
}
/**
* Retrieve a database connection attribute
*
* @see http://php.net/manual/en/pdo.getattribute.php
* @param int $attribute One of the PDO::ATTR_* constants
* @return mixed
*/
public function getAttribute($attribute)
{
return $this->connection->getAttribute($attribute);
}
/**
* Get the definition for a `DEFAULT` statement.
*
* @param mixed $default Default value
* @param string|null $columnType column type added
* @return string
*/
protected function getDefaultValueDefinition($default, $columnType = null)
{
if ($default instanceof Literal) {
$default = (string)$default;
} elseif (is_string($default) && strpos($default, 'CURRENT_TIMESTAMP') !== 0) {
// Ensure a defaults of CURRENT_TIMESTAMP(3) is not quoted.
$default = $this->getConnection()->quote($default);
} elseif (is_bool($default)) {
$default = $this->castToBool($default);
} elseif ($default !== null && $columnType === static::PHINX_TYPE_BOOLEAN) {
$default = $this->castToBool((bool)$default);
}
return isset($default) ? " DEFAULT $default" : '';
}
/**
* Executes all the ALTER TABLE instructions passed for the given table
*
* @param string $tableName The table name to use in the ALTER statement
* @param \Phinx\Db\Util\AlterInstructions $instructions The object containing the alter sequence
* @return void
*/
protected function executeAlterSteps($tableName, AlterInstructions $instructions)
{
$alter = sprintf('ALTER TABLE %s %%s', $this->quoteTableName($tableName));
$instructions->execute($alter, [$this, 'execute']);
}
/**
* @inheritDoc
*/
public function addColumn(Table $table, Column $column)
{
$instructions = $this->getAddColumnInstructions($table, $column);
$this->executeAlterSteps($table->getName(), $instructions);
}
/**
* Returns the instructions to add the specified column to a database table.
*
* @param \Phinx\Db\Table\Table $table Table
* @param \Phinx\Db\Table\Column $column Column
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getAddColumnInstructions(Table $table, Column $column);
/**
* @inheritdoc
*/
public function renameColumn($tableName, $columnName, $newColumnName)
{
$instructions = $this->getRenameColumnInstructions($tableName, $columnName, $newColumnName);
$this->executeAlterSteps($tableName, $instructions);
}
/**
* Returns the instructions to rename the specified column.
*
* @param string $tableName Table name
* @param string $columnName Column Name
* @param string $newColumnName New Column Name
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getRenameColumnInstructions($tableName, $columnName, $newColumnName);
/**
* @inheritdoc
*/
public function changeColumn($tableName, $columnName, Column $newColumn)
{
$instructions = $this->getChangeColumnInstructions($tableName, $columnName, $newColumn);
$this->executeAlterSteps($tableName, $instructions);
}
/**
* Returns the instructions to change a table column type.
*
* @param string $tableName Table name
* @param string $columnName Column Name
* @param \Phinx\Db\Table\Column $newColumn New Column
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn);
/**
* @inheritdoc
*/
public function dropColumn($tableName, $columnName)
{
$instructions = $this->getDropColumnInstructions($tableName, $columnName);
$this->executeAlterSteps($tableName, $instructions);
}
/**
* Returns the instructions to drop the specified column.
*
* @param string $tableName Table name
* @param string $columnName Column Name
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getDropColumnInstructions($tableName, $columnName);
/**
* @inheritdoc
*/
public function addIndex(Table $table, Index $index)
{
$instructions = $this->getAddIndexInstructions($table, $index);
$this->executeAlterSteps($table->getName(), $instructions);
}
/**
* Returns the instructions to add the specified index to a database table.
*
* @param \Phinx\Db\Table\Table $table Table
* @param \Phinx\Db\Table\Index $index Index
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getAddIndexInstructions(Table $table, Index $index);
/**
* @inheritdoc
*/
public function dropIndex($tableName, $columns)
{
$instructions = $this->getDropIndexByColumnsInstructions($tableName, $columns);
$this->executeAlterSteps($tableName, $instructions);
}
/**
* Returns the instructions to drop the specified index from a database table.
*
* @param string $tableName The name of of the table where the index is
* @param mixed $columns Column(s)
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getDropIndexByColumnsInstructions($tableName, $columns);
/**
* @inheritdoc
*/
public function dropIndexByName($tableName, $indexName)
{
$instructions = $this->getDropIndexByNameInstructions($tableName, $indexName);
$this->executeAlterSteps($tableName, $instructions);
}
/**
* Returns the instructions to drop the index specified by name from a database table.
*
* @param string $tableName The table name whe the index is
* @param string $indexName The name of the index
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getDropIndexByNameInstructions($tableName, $indexName);
/**
* @inheritdoc
*/
public function addForeignKey(Table $table, ForeignKey $foreignKey)
{
$instructions = $this->getAddForeignKeyInstructions($table, $foreignKey);
$this->executeAlterSteps($table->getName(), $instructions);
}
/**
* Returns the instructions to adds the specified foreign key to a database table.
*
* @param \Phinx\Db\Table\Table $table The table to add the constraint to
* @param \Phinx\Db\Table\ForeignKey $foreignKey The foreign key to add
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreignKey);
/**
* @inheritDoc
*/
public function dropForeignKey($tableName, $columns, $constraint = null)
{
if ($constraint) {
$instructions = $this->getDropForeignKeyInstructions($tableName, $constraint);
} else {
$instructions = $this->getDropForeignKeyByColumnsInstructions($tableName, $columns);
}
$this->executeAlterSteps($tableName, $instructions);
}
/**
* Returns the instructions to drop the specified foreign key from a database table.
*
* @param string $tableName The table where the foreign key constraint is
* @param string $constraint Constraint name
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getDropForeignKeyInstructions($tableName, $constraint);
/**
* Returns the instructions to drop the specified foreign key from a database table.
*
* @param string $tableName The table where the foreign key constraint is
* @param string[] $columns The list of column names
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getDropForeignKeyByColumnsInstructions($tableName, $columns);
/**
* @inheritdoc
*/
public function dropTable($tableName)
{
$instructions = $this->getDropTableInstructions($tableName);
$this->executeAlterSteps($tableName, $instructions);
}
/**
* Returns the instructions to drop the specified database table.
*
* @param string $tableName Table name
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getDropTableInstructions($tableName);
/**
* @inheritdoc
*/
public function renameTable($tableName, $newTableName)
{
$instructions = $this->getRenameTableInstructions($tableName, $newTableName);
$this->executeAlterSteps($tableName, $instructions);
}
/**
* Returns the instructions to rename the specified database table.
*
* @param string $tableName Table name
* @param string $newTableName New Name
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getRenameTableInstructions($tableName, $newTableName);
/**
* @inheritdoc
*/
public function changePrimaryKey(Table $table, $newColumns)
{
$instructions = $this->getChangePrimaryKeyInstructions($table, $newColumns);
$this->executeAlterSteps($table->getName(), $instructions);
}
/**
* Returns the instructions to change the primary key for the specified database table.
*
* @param \Phinx\Db\Table\Table $table Table
* @param string|string[]|null $newColumns Column name(s) to belong to the primary key, or null to drop the key
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getChangePrimaryKeyInstructions(Table $table, $newColumns);
/**
* @inheritdoc
*/
public function changeComment(Table $table, $newComment)
{
$instructions = $this->getChangeCommentInstructions($table, $newComment);
$this->executeAlterSteps($table->getName(), $instructions);
}
/**
* Returns the instruction to change the comment for the specified database table.
*
* @param \Phinx\Db\Table\Table $table Table
* @param string|null $newComment New comment string, or null to drop the comment
* @return \Phinx\Db\Util\AlterInstructions
*/
abstract protected function getChangeCommentInstructions(Table $table, $newComment);
/**
* {@inheritDoc}
*
* @throws \InvalidArgumentException
* @return void
*/
public function executeActions(Table $table, array $actions)
{
$instructions = new AlterInstructions();
foreach ($actions as $action) {
switch (true) {
case $action instanceof AddColumn:
$instructions->merge($this->getAddColumnInstructions($table, $action->getColumn()));
break;
case $action instanceof AddIndex:
$instructions->merge($this->getAddIndexInstructions($table, $action->getIndex()));
break;
case $action instanceof AddForeignKey:
$instructions->merge($this->getAddForeignKeyInstructions($table, $action->getForeignKey()));
break;
case $action instanceof ChangeColumn:
$instructions->merge($this->getChangeColumnInstructions(
$table->getName(),
$action->getColumnName(),
$action->getColumn()
));
break;
case $action instanceof DropForeignKey && !$action->getForeignKey()->getConstraint():
$instructions->merge($this->getDropForeignKeyByColumnsInstructions(
$table->getName(),
$action->getForeignKey()->getColumns()
));
break;
case $action instanceof DropForeignKey && $action->getForeignKey()->getConstraint():
$instructions->merge($this->getDropForeignKeyInstructions(
$table->getName(),
$action->getForeignKey()->getConstraint()
));
break;
case $action instanceof DropIndex && $action->getIndex()->getName() !== null:
$instructions->merge($this->getDropIndexByNameInstructions(
$table->getName(),
$action->getIndex()->getName()
));
break;
case $action instanceof DropIndex && $action->getIndex()->getName() == null:
$instructions->merge($this->getDropIndexByColumnsInstructions(
$table->getName(),
$action->getIndex()->getColumns()
));
break;
case $action instanceof DropTable:
$instructions->merge($this->getDropTableInstructions(
$table->getName()
));
break;
case $action instanceof RemoveColumn:
$instructions->merge($this->getDropColumnInstructions(
$table->getName(),
$action->getColumn()->getName()
));
break;
case $action instanceof RenameColumn:
$instructions->merge($this->getRenameColumnInstructions(
$table->getName(),
$action->getColumn()->getName(),
$action->getNewName()
));
break;
case $action instanceof RenameTable:
$instructions->merge($this->getRenameTableInstructions(
$table->getName(),
$action->getNewName()
));
break;
case $action instanceof ChangePrimaryKey:
$instructions->merge($this->getChangePrimaryKeyInstructions(
$table,
$action->getNewColumns()
));
break;
case $action instanceof ChangeComment:
$instructions->merge($this->getChangeCommentInstructions(
$table,
$action->getNewComment()
));
break;
default:
throw new InvalidArgumentException(
sprintf("Don't know how to execute action: '%s'", get_class($action))
);
}
}
$this->executeAlterSteps($table->getName(), $instructions);
}
}