Skip to content

Commit

Permalink
Support PostgresSQL in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Dec 3, 2023
1 parent a3ed4bb commit 50fd946
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ jobs:
echo 'DATABASE_URL="postgresql://postgres:[email protected]/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:[email protected]/packeton
php tests/import_db.php sqlite:////${PWD}/tests/dump/test.db postgresql://postgres:[email protected]/packeton
- name: "Run tests"
run: "composer tests"
67 changes: 51 additions & 16 deletions tests/import_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]: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();

Expand Down Expand Up @@ -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) {
}
}
}

0 comments on commit 50fd946

Please sign in to comment.