From 50fd946c16aaaff13b9f5fde512b7f6cab268e41 Mon Sep 17 00:00:00 2001 From: Uladzimir Tsykun Date: Sun, 3 Dec 2023 19:06:16 +0100 Subject: [PATCH] Support PostgresSQL in tests --- .github/workflows/run_tests.yml | 3 +- tests/import_db.php | 67 +++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index 865677df..748bf932 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -77,8 +77,7 @@ jobs: echo 'DATABASE_URL="postgresql://postgres:123456@127.0.0.1/packeton?serverVersion=14.0&charset=utf8"' >> .env.test; bin/console doctrine:schema:update --force --complete - sudo apt install pgloader gunzip -c tests/dump/test.db.gz > tests/dump/test.db - pgloader sqlite:////${PWD}/tests/dump/test.db pgsql://postgres:123456@127.0.0.1/packeton + php tests/import_db.php sqlite:////${PWD}/tests/dump/test.db postgresql://postgres:123456@127.0.0.1/packeton - name: "Run tests" run: "composer tests" diff --git a/tests/import_db.php b/tests/import_db.php index 15c8dd3a..f3ef45fa 100644 --- a/tests/import_db.php +++ b/tests/import_db.php @@ -3,37 +3,40 @@ namespace Packeton\Tests; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Symfony\Component\Yaml\Yaml; use Doctrine\DBAL\Types\Type as DBALType; require_once __DIR__ . '/../vendor/autoload.php'; -$projectDir = dirname(__DIR__); +$in = $argv[1] ?? null; +$out = $argv[2] ?? null; -$initDb = __DIR__ . '/dump/test.db'; -if (!file_exists($initDb)) { - $db = gzdecode(file_get_contents($projectDir.'/tests/dump/test.db.gz')); - file_put_contents($initDb, $db); +if (empty($in) || empty($out)) { + echo "Usage:\n"; + echo " php import_db.php 'sqlite:////var/www/app/test.db' 'postgresql://postgres:pass123@127.0.0.1:5433/py1'\n"; + return 0; +} + +$dbalTypes = []; +if (file_exists($conf_file = __DIR__.'/config/packages/doctrine.yaml') + || file_exists($conf_file = dirname(__DIR__).'/config/packages/doctrine.yaml') +) { + $dbalTypes = Yaml::parse(file_get_contents($conf_file))['doctrine']['dbal']['types'] ?? []; } -$dbalTypes = Yaml::parse(file_get_contents($projectDir.'/config/packages/doctrine.yaml'))['doctrine']['dbal']['types'] ?? []; foreach ($dbalTypes as $type => $class) { if (!DBALType::hasType($type)) { DBALType::addType($type, $class); } } -$conn0 = DriverManager::getConnection(['url' => 'sqlite:///'.$initDb]); +$conn0 = DriverManager::getConnection(['url' => $in]); $sm0 = $conn0->createSchemaManager(); $tab0 = $sm0->listTables(); -if (!$url = getenv('DATABASE_URL')) { - echo "DATABASE_URL is empty"; - exit(1); -} - -$conn1 = DriverManager::getConnection(['url' => $url]); +$conn1 = DriverManager::getConnection(['url' => $out]); $sm1 = $conn1->createSchemaManager(); $tab1 = $sm1->listTables(); @@ -83,8 +86,40 @@ } foreach ($tab1 as $table) { - $table->getColumns(); - $tableMap0[$table->getName()] = $table; + $columns = array_map(fn(Column $column) => $column->getName(), $table->getColumns()); + $tableMapColumn1[$table->getName()] = $columns; } -print_r($commitOfOrders); +$persister = static function(string $table) use ($conn0, $conn1, $tableMapColumn1, $tableMap0) { + $data = $conn0->executeQuery("SELECT * FROM $table")->fetchAllAssociative(); + $columns = $tableMapColumn1[$table]; + foreach ($data as $item) { + foreach ($item as $key => $value) { + if (!isset($columns[$key])) { + unset($item[$key]); + } + } + $conn1->insert($table, $item); + } + return count($data); +}; + +foreach ($commitOfOrders as $table) { + if (!isset($tableMap0[$table])) { + continue; + } + + $result = $persister($table); + echo "Import $table. Rows $result\n"; + if (str_starts_with($out, 'postgresql')) { + try { + $max = $conn1->executeQuery("SELECT max(id) FROM $table")->fetchOne(); + if ($max > 0) { + $max = $max+1; + $conn1->executeQuery("ALTER SEQUENCE {$table}_id_seq RESTART WITH $max")->fetchOne(); + echo "ALTER SEQUENCE {$table}_id_seq RESTART WITH $max\n"; + } + } catch (\Throwable $e) { + } + } +}