소스 검색

Fix auto-generated index names to ensure uniqueness

phpdevcommunity 4 달 전
부모
커밋
f8551b7746
3개의 변경된 파일13개의 추가작업 그리고 13개의 파일을 삭제
  1. 3 2
      src/Migration/PaperMigration.php
  2. 4 5
      src/Platform/AbstractPlatform.php
  3. 6 6
      tests/MigrationTest.php

+ 3 - 2
src/Migration/PaperMigration.php

@@ -64,7 +64,6 @@ final class PaperMigration
         $migrationContent = <<<'SQL'
 -- UP MIGRATION --
 %s
-
 -- DOWN MIGRATION --
 %s
 SQL;
@@ -171,9 +170,11 @@ SQL;
     {
         $migration = $this->directory->getMigration($version);
         $pdo = $this->getConnection()->getPdo();
+        $currentQuery = '';
         try {
             $pdo->beginTransaction();
             foreach (explode(';' . PHP_EOL, self::contentDown($migration)) as $query) {
+                $currentQuery = $query;
                 $this->getConnection()->executeStatement(rtrim($query, ';') . ';');
             }
             $this->getConnection()->executeStatement('DELETE FROM ' . $this->tableName . ' WHERE version = :version', ['version' => $version]);
@@ -182,7 +183,7 @@ SQL;
 
         } catch (PDOException $e) {
             $pdo->rollBack();
-            throw new RuntimeException("Failed to execute DOWN migration: " . $e->getMessage());
+            throw new RuntimeException(sprintf('Failed to migrate version %s : %s -> %s', $version, $e->getMessage(), $currentQuery));
         }
 
     }

+ 4 - 5
src/Platform/AbstractPlatform.php

@@ -110,7 +110,7 @@ abstract class AbstractPlatform implements PlatformInterface
 
         $indexesExisting = [];
         foreach ($indexes as $index) {
-            $indexMetadata = new IndexMetadata($tableName, $index->getName() ?: $this->generateIndexName($index->getColumns()), $index->getColumns(), $index->isUnique());
+            $indexMetadata = new IndexMetadata($tableName, $index->getName() ?: $this->generateIndexName($tableName, $index->getColumns()), $index->getColumns(), $index->isUnique());
             $indexFound = $indexesFromTable->findOneBy('getName', $indexMetadata->getName());
             if ($indexFound) {
                 if ($indexMetadata->toArray() != $indexFound->toArray()) {
@@ -131,12 +131,11 @@ abstract class AbstractPlatform implements PlatformInterface
         return [$indexesToAdd, $indexesToUpdate, $indexesToDrop, $indexesFromTable->toArray()];
     }
 
-    final protected function generateIndexName(array $columnNames): string
+    final protected function generateIndexName(string $tableName, array $columnNames): string
     {
-        $hash = implode("", array_map(static function ($column) {
+        $hash = implode('', array_map(static function ($column) {
             return dechex(crc32($column));
-        }, $columnNames));
-
+        }, array_merge([$tableName], $columnNames)));
 
         return strtoupper(substr($this->getPrefixIndexName() . $hash, 0, $this->getMaxLength()));
     }

+ 6 - 6
tests/MigrationTest.php

@@ -54,14 +54,14 @@ class MigrationTest extends TestCase
 
         $this->assertStringContains(file_get_contents($migrationFile), '-- UP MIGRATION --');
         $this->assertStringContains(file_get_contents($migrationFile), 'CREATE TABLE user (id INTEGER PRIMARY KEY NOT NULL,firstname VARCHAR(255) NOT NULL,lastname VARCHAR(255) NOT NULL,email VARCHAR(255) NOT NULL,password VARCHAR(255) NOT NULL,is_active BOOLEAN NOT NULL,created_at DATETIME NOT NULL,last_post_id INTEGER NOT NULL,FOREIGN KEY (last_post_id) REFERENCES post (id));');
-        $this->assertStringContains(file_get_contents($migrationFile), 'CREATE INDEX IX_2D053F64 ON user (last_post_id);');
+        $this->assertStringContains(file_get_contents($migrationFile), 'CREATE INDEX IX_8D93D6492D053F64 ON user (last_post_id);');
         $this->assertStringContains(file_get_contents($migrationFile), 'CREATE TABLE post (id INTEGER PRIMARY KEY NOT NULL,title VARCHAR(255) NOT NULL,content VARCHAR(255) NOT NULL,created_at DATETIME NOT NULL,user_id INTEGER NOT NULL,FOREIGN KEY (user_id) REFERENCES user (id));');
-        $this->assertStringContains(file_get_contents($migrationFile), 'CREATE INDEX IX_A76ED395 ON post (user_id);');
+        $this->assertStringContains(file_get_contents($migrationFile), 'CREATE INDEX IX_5A8A6C8DA76ED395 ON post (user_id);');
 
         $this->assertStringContains(file_get_contents($migrationFile), '-- DOWN MIGRATION --');
-        $this->assertStringContains(file_get_contents($migrationFile), 'DROP INDEX IX_2D053F64;');
+        $this->assertStringContains(file_get_contents($migrationFile), 'DROP INDEX IX_8D93D6492D053F64;');
         $this->assertStringContains(file_get_contents($migrationFile), 'DROP TABLE user;');
-        $this->assertStringContains(file_get_contents($migrationFile), 'DROP INDEX IX_A76ED395;');
+        $this->assertStringContains(file_get_contents($migrationFile), 'DROP INDEX IX_5A8A6C8DA76ED395;');
         $this->assertStringContains(file_get_contents($migrationFile), 'DROP TABLE post;');
      }
 
@@ -91,8 +91,8 @@ class MigrationTest extends TestCase
         $this->assertTrue(count($successList) === 1);
         $this->assertEquals(pathinfo($migrationFile, PATHINFO_FILENAME), $successList[0]);
         $this->assertStringContains(file_get_contents( $migrationFile ), 'ALTER TABLE user ADD childs INTEGER NOT NULL DEFAULT 0;');
-        $this->assertStringContains(file_get_contents( $migrationFile ), 'CREATE UNIQUE INDEX IX_E7927C74 ON user (email);');
-        $this->assertStringContains(file_get_contents( $migrationFile ), 'DROP INDEX IX_E7927C74;');
+        $this->assertStringContains(file_get_contents( $migrationFile ), 'CREATE UNIQUE INDEX IX_8D93D649E7927C74 ON user (email);');
+        $this->assertStringContains(file_get_contents( $migrationFile ), 'DROP INDEX IX_8D93D649E7927C74;');
     }
 
     private function testFailedMigration(): void