-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
||
|
@@ -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) { | ||
} | ||
} | ||
} |