|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Liip/TestFixturesBundle |
| 7 | + * |
| 8 | + * (c) Lukas Kahwe Smith <[email protected]> |
| 9 | + * |
| 10 | + * This source file is subject to the MIT license that is bundled |
| 11 | + * with this source code in the file LICENSE. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Liip\TestFixturesBundle\Services\DatabaseBackup; |
| 15 | + |
| 16 | +use Doctrine\Common\DataFixtures\Executor\AbstractExecutor; |
| 17 | +use Doctrine\Common\DataFixtures\ProxyReferenceRepository; |
| 18 | +use Doctrine\ORM\EntityManager; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Nikita Slutsky <[email protected]> |
| 22 | + */ |
| 23 | +final class PgsqlDatabaseBackup extends AbstractDatabaseBackup |
| 24 | +{ |
| 25 | + public function getBackupFilePath(): string |
| 26 | + { |
| 27 | + $cacheDir = $this->container->getParameter('kernel.cache_dir'); |
| 28 | + $hash = md5(serialize($this->metadatas).serialize($this->classNames)); |
| 29 | + |
| 30 | + return $cacheDir.'/test_postgresql_'.$hash.'.tar'; |
| 31 | + } |
| 32 | + |
| 33 | + public function isBackupActual(): bool |
| 34 | + { |
| 35 | + $backupFileName = $this->getBackupFilePath(); |
| 36 | + $backupReferenceFileName = $backupFileName.'.ser'; |
| 37 | + |
| 38 | + return file_exists($backupFileName) |
| 39 | + && file_exists($backupReferenceFileName) |
| 40 | + && $this->isBackupUpToDate($backupFileName); |
| 41 | + } |
| 42 | + |
| 43 | + public function backup(AbstractExecutor $executor): void |
| 44 | + { |
| 45 | + /** @var ProxyReferenceRepository $referenceRepository */ |
| 46 | + $referenceRepository = $executor->getReferenceRepository(); |
| 47 | + |
| 48 | + /** @var EntityManager $em */ |
| 49 | + $em = $referenceRepository->getManager(); |
| 50 | + |
| 51 | + $connection = $em->getConnection(); |
| 52 | + $params = $connection->getParams(); |
| 53 | + |
| 54 | + $referenceRepository->save($this->getBackupFilePath()); |
| 55 | + exec($this->createCommand('pg_dump --format=t', $params).' > '.$this->getBackupFilePath()); |
| 56 | + } |
| 57 | + |
| 58 | + public function restore(AbstractExecutor $executor, array $excludedTables = []): void |
| 59 | + { |
| 60 | + /** @var ProxyReferenceRepository $referenceRepository */ |
| 61 | + $referenceRepository = $executor->getReferenceRepository(); |
| 62 | + |
| 63 | + /** @var EntityManager $em */ |
| 64 | + $em = $referenceRepository->getManager(); |
| 65 | + |
| 66 | + $connection = $em->getConnection(); |
| 67 | + $params = $connection->getParams(); |
| 68 | + |
| 69 | + $this->executeQuery($connection, 'SET session_replication_role = \'replica\';'); |
| 70 | + exec($this->createCommand('pg_restore --format=t --clean', $params).' '.$this->getBackupFilePath()); |
| 71 | + $this->executeQuery($connection, 'SET session_replication_role = \'origin\';'); |
| 72 | + $referenceRepository->load($this->getBackupFilePath()); |
| 73 | + } |
| 74 | + |
| 75 | + protected function getReferenceBackup(): string |
| 76 | + { |
| 77 | + return file_get_contents($this->getBackupFilePath()); |
| 78 | + } |
| 79 | + |
| 80 | + private function createCommand(string $command, array $params): string |
| 81 | + { |
| 82 | + // doctrine-bundle >= 2.2 |
| 83 | + if (isset($params['primary'])) { |
| 84 | + $params = $params['primary']; |
| 85 | + } |
| 86 | + // doctrine-bundle < 2.2 |
| 87 | + elseif (isset($params['master'])) { |
| 88 | + $params = $params['master']; |
| 89 | + } |
| 90 | + |
| 91 | + $command .= isset($params['dbname']) && $params['dbname'] ? ' --dbname='.$params['dbname'] : ''; |
| 92 | + $command .= isset($params['host']) && $params['host'] ? ' --host='.$params['host'] : ''; |
| 93 | + $command .= isset($params['port']) && $params['port'] ? ' --port='.$params['port'] : ''; |
| 94 | + $command .= isset($params['user']) && $params['user'] ? ' --username='.$params['user'] : ''; |
| 95 | + |
| 96 | + if (isset($params['password']) && $params['password']) { |
| 97 | + $command = 'PGPASSWORD='.$params['password'].' '.$command.' --no-password'; |
| 98 | + } |
| 99 | + |
| 100 | + return $command; |
| 101 | + } |
| 102 | + |
| 103 | + private function executeQuery($connection, string $query): void |
| 104 | + { |
| 105 | + if (method_exists($connection, 'executeQuery')) { |
| 106 | + $connection->executeQuery($query); |
| 107 | + } else { |
| 108 | + $connection->query($query); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments