Răsfoiți Sursa

add MariaDB/MySQL driver support

phpdevcommunity 1 lună în urmă
părinte
comite
26b5e833fc

+ 1 - 1
src/Command/MigrationDiffCommand.php → src/Command/Migration/MigrationDiffCommand.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace PhpDevCommunity\PaperORM\Command;
+namespace PhpDevCommunity\PaperORM\Command\Migration;
 
 use PhpDevCommunity\Console\Command\CommandInterface;
 use PhpDevCommunity\Console\InputInterface;

+ 1 - 1
src/Command/MigrationMigrateCommand.php → src/Command/Migration/MigrationMigrateCommand.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace PhpDevCommunity\PaperORM\Command;
+namespace PhpDevCommunity\PaperORM\Command\Migration;
 
 use PhpDevCommunity\Console\Command\CommandInterface;
 use PhpDevCommunity\Console\InputInterface;

+ 1 - 1
src/Command/QueryExecuteCommand.php

@@ -48,7 +48,7 @@ class QueryExecuteCommand implements CommandInterface
             throw new \LogicException("SQL query is required");
         }
         $io->title('Starting query on ' . $this->entityManager->createDatabasePlatform()->getDatabaseName());
-
+        
         $data = $this->entityManager->getConnection()->fetchAll($query);
         $io->listKeyValues([
             'query' => $query,

+ 1 - 1
src/Debugger/SqlDebugger.php

@@ -2,7 +2,7 @@
 
 namespace PhpDevCommunity\PaperORM\Debugger;
 
-final class SqlDebugger
+class SqlDebugger
 {
     private array $queries = [];
 

+ 2 - 2
src/Michel/Package/MichelPaperORMPackage.php

@@ -6,8 +6,8 @@ use LogicException;
 use PhpDevCommunity\Michel\Package\PackageInterface;
 use PhpDevCommunity\PaperORM\Command\DatabaseCreateCommand;
 use PhpDevCommunity\PaperORM\Command\DatabaseDropCommand;
-use PhpDevCommunity\PaperORM\Command\MigrationDiffCommand;
-use PhpDevCommunity\PaperORM\Command\MigrationMigrateCommand;
+use PhpDevCommunity\PaperORM\Command\Migration\MigrationDiffCommand;
+use PhpDevCommunity\PaperORM\Command\Migration\MigrationMigrateCommand;
 use PhpDevCommunity\PaperORM\Command\QueryExecuteCommand;
 use PhpDevCommunity\PaperORM\Command\ShowTablesCommand;
 use PhpDevCommunity\PaperORM\EntityManager;

+ 11 - 2
src/Migration/PaperMigration.php

@@ -53,6 +53,15 @@ final class PaperMigration
         $this->directory = new MigrationDirectory($directory);
     }
 
+    /**
+     * @return array<string>
+     */
+    public function getVersionAlreadyMigrated() : array
+    {
+        $version = $this->getConnection()->fetchAll('SELECT version FROM ' . $this->tableName);
+        return array_column($version, 'version');
+    }
+
     public function generateMigration(array $sqlUp = [], array $sqlDown = []): string
     {
         $i = 1;
@@ -97,10 +106,10 @@ SQL;
         $this->createVersion();
 
         $this->successList = [];
-        $versions = $this->getConnection()->fetchAll('SELECT version FROM ' . $this->tableName);
+        $versions = $this->getVersionAlreadyMigrated();
         foreach ($this->directory->getMigrations() as $version => $migration) {
 
-            if (in_array($version, array_column($versions, 'version'))) {
+            if (in_array($version, $versions)) {
                 continue;
             }
 

+ 2 - 2
tests/Common/OrmTestMemory.php

@@ -24,12 +24,12 @@ class OrmTestMemory extends TestCase
     {
         foreach (DataBaseHelperTest::drivers() as  $params) {
             $em = new EntityManager($params);
-            DataBaseHelperTest::init($em, 10, false);
+            DataBaseHelperTest::init($em, 1000, false);
             $memory = memory_get_usage();
             $users = $em->getRepository(UserTest::class)
                 ->findBy()
                 ->toObject();
-            $this->assertStrictEquals(10, count($users));
+            $this->assertStrictEquals(1000, count($users));
             foreach ($users as $user) {
                 $this->assertInstanceOf(UserTest::class, $user);
                 $this->assertNotEmpty($user);

+ 24 - 22
tests/MigrationTest.php

@@ -131,17 +131,29 @@ class MigrationTest extends TestCase
         $this->assertEquals(pathinfo($migrationFile, PATHINFO_FILENAME), $successList[0]);
         switch (get_class($driver)) {
             case SqliteDriver::class:
+                $schema = $driver->createDatabaseSchema();
                 $lines = file($migrationFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
-                $this->assertEquals($lines, array (
-                    0 => '-- UP MIGRATION --',
-                    1 => 'ALTER TABLE user ADD childs INTEGER NOT NULL DEFAULT 0;',
-                    2 => 'CREATE UNIQUE INDEX IX_8D93D649E7927C74 ON user (email);',
-                    3 => '-- Modify column email is not supported with PhpDevCommunity\\PaperORM\\Schema\\SqliteSchema. Consider creating a new column and migrating the data.;',
-                    4 => '-- DOWN MIGRATION --',
-                    5 => '-- Drop column childs is not supported with PhpDevCommunity\\PaperORM\\Schema\\SqliteSchema. You might need to manually drop the column.;',
-                    6 => 'DROP INDEX IX_8D93D649E7927C74;',
-                ));
-
+                if ($schema->supportsDropColumn()) {
+                    $this->assertEquals($lines, array (
+                        0 => '-- UP MIGRATION --',
+                        1 => 'ALTER TABLE user ADD childs INTEGER NOT NULL DEFAULT 0;',
+                        2 => 'CREATE UNIQUE INDEX IX_8D93D649E7927C74 ON user (email);',
+                        3 => '-- Modify column email is not supported with PhpDevCommunity\\PaperORM\\Schema\\SqliteSchema. Consider creating a new column and migrating the data.;',
+                        4 => '-- DOWN MIGRATION --',
+                        5 => 'ALTER TABLE user DROP COLUMN childs;',
+                        6 => 'DROP INDEX IX_8D93D649E7927C74;',
+                    ));
+                } else {
+                    $this->assertEquals($lines, array (
+                        0 => '-- UP MIGRATION --',
+                        1 => 'ALTER TABLE user ADD childs INTEGER NOT NULL DEFAULT 0;',
+                        2 => 'CREATE UNIQUE INDEX IX_8D93D649E7927C74 ON user (email);',
+                        3 => '-- Modify column email is not supported with PhpDevCommunity\\PaperORM\\Schema\\SqliteSchema. Consider creating a new column and migrating the data.;',
+                        4 => '-- DOWN MIGRATION --',
+                        5 => '-- Drop column childs is not supported with PhpDevCommunity\\PaperORM\\Schema\\SqliteSchema. You might need to manually drop the column.;',
+                        6 => 'DROP INDEX IX_8D93D649E7927C74;',
+                    ));
+                }
                 break;
             case MariaDBDriver::class:
                 $lines = file($migrationFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
@@ -165,18 +177,8 @@ class MigrationTest extends TestCase
     private function testFailedMigration(PaperMigration  $paperMigration): void
     {
         $em = $paperMigration->getEntityManager();
-        $driver = $em->getConnection()->getDriver();
-        if ($driver instanceof MariaDBDriver) {
-            return;
-        }
-        $userColumns = ColumnMapper::getColumns(UserTest::class);
-        $userColumns[3] = (new StringColumn(null, 100, true, null, true))->bindProperty('email');
-        $file = $paperMigration->diff([
-            'user' => [
-                'columns' => $userColumns,
-                'indexes' => []
-            ]
-        ]);
+
+        $paperMigration->generateMigration();
 
         $this->expectException(RuntimeException::class, function () use ($paperMigration){
             $paperMigration->migrate();