| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Doctrine\Migrations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\DBAL\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\DBAL\Platforms\AbstractPlatform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\DBAL\Schema\AbstractSchemaManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Doctrine\DBAL\Schema\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Doctrine\DBAL\Schema\Table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function in_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class MigrationTableUpdater | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var Connection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var AbstractSchemaManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $schemaManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var MigrationTable */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $migrationTable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var AbstractPlatform */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $platform; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 68 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         Connection $connection, | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         AbstractSchemaManager $schemaManager, | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         MigrationTable $migrationTable, | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         AbstractPlatform $platform | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 68 |  |         $this->connection     = $connection; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 68 |  |         $this->schemaManager  = $schemaManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 68 |  |         $this->migrationTable = $migrationTable; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 68 |  |         $this->platform       = $platform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 68 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |     public function updateMigrationTable() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |         $fromTable = $this->getFromTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |         $toTable   = $this->migrationTable->getDBALTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |         $fromSchema = $this->createSchema([$fromTable]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |         $toSchema   = $this->createSchema([$toTable]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |         $queries = $fromSchema->getMigrateToSql($toSchema, $this->platform); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |         $this->connection->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |             foreach ($queries as $query) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 3 |  |                 $this->connection->executeQuery($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |         } catch (Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |             $this->connection->rollBack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |         $this->connection->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param Table[] $tables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |     protected function createSchema(array $tables) : Schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         return new Schema($tables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |     private function getFromTable() : Table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |         $tableName   = $this->migrationTable->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |         $columnNames = $this->migrationTable->getColumnNames(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |         $columnName  = $this->migrationTable->getColumnName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |         $currentTable = $this->schemaManager->listTableDetails($tableName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |         $table = new Table($tableName, $currentTable->getColumns()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |         $table->setPrimaryKey([$columnName]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // remove columns from the table definition that we don't care about | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         // so we don't try to drop those columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 3 |  |         foreach ($table->getColumns() as $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 3 |  |             if (in_array($column->getName(), $columnNames, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             $table->dropColumn($column->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  |         return $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  |