Skip to content

Commit 8d49769

Browse files
Dump schema to sqlite command
1 parent deeba01 commit 8d49769

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

src/Console/DumpDatabaseCommand.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
namespace LaravelDoctrine\ORM\Console;
4+
5+
use Doctrine\Common\Persistence\ManagerRegistry;
6+
use Illuminate\Contracts\Config\Repository;
7+
8+
class DumpDatabaseCommand extends Command
9+
{
10+
/**
11+
* @var ManagerRegistry
12+
*/
13+
protected $registry;
14+
15+
/**
16+
* @var Repository
17+
*/
18+
protected $config;
19+
20+
/**
21+
* The name and signature of the console command.
22+
*
23+
* @var string
24+
*/
25+
protected $signature = 'doctrine:dump:sqlite
26+
{--connection=sqlite}
27+
{--em=}
28+
{--dump=tests/_data/dump.sql : Choose the path for your dump file}
29+
{--no-seeding : Disable seeding in the dump process}
30+
{--seeder=DatabaseSeeder : Choose the seeder class}
31+
{--binary=sqlite3}';
32+
33+
/**
34+
* The console command description.
35+
*
36+
* @var string
37+
*/
38+
protected $description = 'Create a dump from a certain connection';
39+
40+
/**
41+
* @param ManagerRegistry $registry
42+
* @param Repository $config
43+
*/
44+
public function handle(ManagerRegistry $registry, Repository $config)
45+
{
46+
$this->registry = $registry;
47+
$this->config = $config;
48+
49+
$em = $this->option('em') != '' ? $this->option('em') : $registry->getDefaultManagerName();
50+
$connection = $this->option('connection');
51+
52+
$dumped = $this->connect($connection, $em)
53+
->dropSchema($em)
54+
->createSchema($em)
55+
->seed()
56+
->dump($em);
57+
58+
if ($dumped) {
59+
$this->info('Database dump created successfully.');
60+
} else {
61+
$this->error('Something went wrong when creating database dump!');
62+
}
63+
}
64+
65+
/**
66+
* @param string $connection
67+
* @param string $em
68+
* @return $this
69+
*/
70+
private function connect($connection, $em)
71+
{
72+
// Change connection of given manager to the new format
73+
$settings = $this->config->get('doctrine.managers.' . $em);
74+
$settings['connection'] = $connection;
75+
76+
// Reset
77+
$this->registry->resetManager($em);
78+
79+
// Add new manager with new connection
80+
$this->registry->addManager($em, $settings);
81+
$this->registry->addConnection($em);
82+
83+
return $this;
84+
}
85+
86+
/**
87+
* @param $em
88+
* @return $this
89+
*/
90+
private function dropSchema($em)
91+
{
92+
$this->callSilent('doctrine:schema:drop', [
93+
'--force' => true,
94+
'--full' => true,
95+
'--em' => $em
96+
]);
97+
98+
return $this;
99+
}
100+
101+
/**
102+
* @param $em
103+
* @return $this
104+
*/
105+
private function createSchema($em)
106+
{
107+
$this->callSilent('doctrine:schema:create', [
108+
'--em' => $em
109+
]);
110+
111+
return $this;
112+
}
113+
114+
/**
115+
* @return $this
116+
*/
117+
private function seed()
118+
{
119+
if (!$this->option('no-seeding')) {
120+
$this->call('db:seed', [
121+
'--class' => $this->option('seeder'),
122+
'--force' => true
123+
]);
124+
}
125+
126+
return $this;
127+
}
128+
129+
/**
130+
* @param string $em
131+
* @return bool
132+
*/
133+
private function dump($em)
134+
{
135+
$conn = $this->registry->getManager($em)->getConnection();
136+
137+
$db = $conn->getDatabase();
138+
$binary = $this->option('binary');
139+
$dump = base_path($this->option('dump'));
140+
141+
$binary = is_null($binary) ? 'sqlite3' : $binary;
142+
$command = "$binary $db '.dump'";
143+
$command .= " > $dump";
144+
exec($command, $output, $status);
145+
146+
return $status == 0;
147+
}
148+
}

src/DoctrineServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use LaravelDoctrine\ORM\Console\ClearResultCacheCommand;
2222
use LaravelDoctrine\ORM\Console\ConvertConfigCommand;
2323
use LaravelDoctrine\ORM\Console\ConvertMappingCommand;
24+
use LaravelDoctrine\ORM\Console\DumpDatabaseCommand;
2425
use LaravelDoctrine\ORM\Console\EnsureProductionSettingsCommand;
2526
use LaravelDoctrine\ORM\Console\GenerateEntitiesCommand;
2627
use LaravelDoctrine\ORM\Console\GenerateProxiesCommand;
@@ -296,7 +297,8 @@ protected function registerConsoleCommands()
296297
ConvertConfigCommand::class,
297298
MappingImportCommand::class,
298299
GenerateEntitiesCommand::class,
299-
ConvertMappingCommand::class
300+
ConvertMappingCommand::class,
301+
DumpDatabaseCommand::class
300302
]);
301303
}
302304

0 commit comments

Comments
 (0)