Skip to content

Commit 50e0f1b

Browse files
committed
Implement restore command
1 parent df8bb54 commit 50e0f1b

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

Command/RestoreCommand.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace RaphaelVigee\GitDoctrineMigrationBundle\Command;
4+
5+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class RestoreCommand extends ContainerAwareCommand
10+
{
11+
protected function configure()
12+
{
13+
$this
14+
// the name of the command (the part after "bin/console")
15+
->setName('git:restore')
16+
// the short description shown while running "php bin/console list"
17+
->setDescription('Restore DB dump')
18+
// the full command description shown when running the command with
19+
// the "--help" option
20+
->setHelp('This command allows you to restore DB dump');
21+
}
22+
23+
protected function execute(InputInterface $input, OutputInterface $output)
24+
{
25+
$container = $this->getContainer();
26+
$dumper = $container->get('raphael_vigee_git_doctrine_migration.mysql.dumper');
27+
$git = $container->get('raphael_vigee_git_doctrine_migration.git.service');
28+
29+
$currentHash = $git->getCurrentHash();
30+
31+
if (!$dumper->dumpExists($currentHash)) {
32+
throw new \Exception(sprintf("Dump file missing for hash %s", $currentHash));
33+
}
34+
35+
$dumpPath = $dumper->getDumpPath($currentHash);
36+
37+
$dumper->restore($dumpPath, true);
38+
}
39+
}

Dumper/MySQLDumper.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ public function __construct(EntityManagerInterface $em, GitService $git, $kernel
4242
$this->fs = new Filesystem();
4343
}
4444

45+
public function getDumpPath($hash)
46+
{
47+
$outFolder = sprintf('%s/../var/git-doctrine-migration', $this->kernelRootDir);
48+
49+
if (!$this->fs->exists($outFolder)) {
50+
$this->fs->mkdir($outFolder, 0700);
51+
}
52+
53+
$outPath = sprintf('%s/%s.sql', $outFolder, $hash);
54+
55+
return $outPath;
56+
}
57+
58+
public function dumpExists($hash)
59+
{
60+
return $this->fs->exists($this->getDumpPath($hash));
61+
}
62+
4563
/**
4664
* Dump
4765
*/
@@ -55,13 +73,7 @@ public function dump()
5573

5674
$currentHash = $this->git->getCurrentHash();
5775

58-
$outFolder = sprintf('%s/../var/git-doctrine-migration', $this->kernelRootDir);
59-
60-
if (!$this->fs->exists($outFolder)) {
61-
$this->fs->mkdir($outFolder, 0700);
62-
}
63-
64-
$outPath = sprintf('%s/%s.sql', $outFolder, $currentHash);
76+
$outPath = $this->getDumpPath($currentHash);
6577

6678
$command = sprintf(
6779
'mysqldump --user="%s" --password="%s" --host="%s" "%s" > "%s"',
@@ -84,7 +96,7 @@ public function restore($filePath, $force = true)
8496
$database = $connection->getDatabase();
8597

8698
$command = sprintf(
87-
'mysql -u "%s" -p "%s" -h "%s" %s "%s" < "%s"',
99+
'mysql --user="%s" --password="%s" --host="%s" %s "%s" < "%s"',
88100
$user,
89101
$password,
90102
$host,
@@ -93,8 +105,6 @@ public function restore($filePath, $force = true)
93105
$filePath
94106
);
95107

96-
die(dump($command));
97-
98108
exec($command);
99109
}
100110
}

0 commit comments

Comments
 (0)