]>
Commit | Line | Data |
---|---|---|
2e45e7be J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Command; | |
4 | ||
ca17abce JB |
5 | use FOS\UserBundle\Event\UserEvent; |
6 | use FOS\UserBundle\FOSUserEvents; | |
2e45e7be | 7 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
619cc453 JB |
8 | use Symfony\Component\Console\Helper\Table; |
9 | use Symfony\Component\Console\Input\ArrayInput; | |
2e45e7be | 10 | use Symfony\Component\Console\Input\InputInterface; |
0bf99bb1 | 11 | use Symfony\Component\Console\Input\InputOption; |
39a19bdf | 12 | use Symfony\Component\Console\Output\BufferedOutput; |
619cc453 | 13 | use Symfony\Component\Console\Output\OutputInterface; |
78507d28 | 14 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
619cc453 | 15 | use Symfony\Component\Console\Question\Question; |
4d85d7e9 | 16 | use Wallabag\CoreBundle\Entity\Config; |
d6ba77e8 | 17 | use Craue\ConfigBundle\Entity\Setting; |
2e45e7be J |
18 | |
19 | class InstallCommand extends ContainerAwareCommand | |
20 | { | |
0bf99bb1 J |
21 | /** |
22 | * @var InputInterface | |
23 | */ | |
24 | protected $defaultInput; | |
25 | ||
26 | /** | |
27 | * @var OutputInterface | |
28 | */ | |
29 | protected $defaultOutput; | |
30 | ||
fc6020b2 NL |
31 | /** |
32 | * @var array | |
33 | */ | |
db847ca0 | 34 | protected $functionExists = [ |
db847ca0 TC |
35 | 'curl_exec', |
36 | 'curl_multi_init', | |
fc6020b2 NL |
37 | ]; |
38 | ||
2e45e7be J |
39 | protected function configure() |
40 | { | |
41 | $this | |
42 | ->setName('wallabag:install') | |
88d5d94d | 43 | ->setDescription('wallabag installer.') |
0bf99bb1 J |
44 | ->addOption( |
45 | 'reset', | |
46 | null, | |
47 | InputOption::VALUE_NONE, | |
48 | 'Reset current database' | |
49 | ) | |
2e45e7be J |
50 | ; |
51 | } | |
52 | ||
53 | protected function execute(InputInterface $input, OutputInterface $output) | |
54 | { | |
0bf99bb1 J |
55 | $this->defaultInput = $input; |
56 | $this->defaultOutput = $output; | |
57 | ||
88d5d94d | 58 | $output->writeln('<info>Installing wallabag...</info>'); |
2e45e7be J |
59 | $output->writeln(''); |
60 | ||
61 | $this | |
0bf99bb1 J |
62 | ->checkRequirements() |
63 | ->setupDatabase() | |
64 | ->setupAdmin() | |
637dc4bb | 65 | ->setupConfig() |
2e45e7be J |
66 | ; |
67 | ||
88d5d94d | 68 | $output->writeln('<info>wallabag has been successfully installed.</info>'); |
0c6845a9 | 69 | $output->writeln('<comment>Just execute `php bin/console server:run --env=prod` for using wallabag: http://localhost:8000</comment>'); |
2e45e7be J |
70 | } |
71 | ||
0bf99bb1 | 72 | protected function checkRequirements() |
2e45e7be | 73 | { |
5ecdfcd0 | 74 | $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>'); |
cffcce0c | 75 | $doctrineManager = $this->getContainer()->get('doctrine')->getManager(); |
2e45e7be | 76 | |
001a7bad | 77 | $rows = []; |
2e45e7be | 78 | |
001a7bad JB |
79 | // testing if database driver exists |
80 | $fulfilled = true; | |
a730cae3 | 81 | $label = '<comment>PDO Driver (%s)</comment>'; |
0e49487b JB |
82 | $status = '<info>OK!</info>'; |
83 | $help = ''; | |
c61b68e8 JB |
84 | |
85 | if (!extension_loaded($this->getContainer()->getParameter('database_driver'))) { | |
db847ca0 TC |
86 | $fulfilled = false; |
87 | $status = '<error>ERROR!</error>'; | |
c61b68e8 | 88 | $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.'; |
db847ca0 TC |
89 | } |
90 | ||
a730cae3 | 91 | $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help]; |
001a7bad JB |
92 | |
93 | // testing if connection to the database can be etablished | |
94 | $label = '<comment>Database connection</comment>'; | |
95 | $status = '<info>OK!</info>'; | |
96 | $help = ''; | |
97 | ||
98 | try { | |
fc79f1ff JB |
99 | $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection(); |
100 | $conn->connect(); | |
001a7bad | 101 | } catch (\Exception $e) { |
39a19bdf JB |
102 | if (false === strpos($e->getMessage(), 'Unknown database') |
103 | && false === strpos($e->getMessage(), 'database "'.$this->getContainer()->getParameter('database_name').'" does not exist')) { | |
f62c3faf JB |
104 | $fulfilled = false; |
105 | $status = '<error>ERROR!</error>'; | |
106 | $help = 'Can\'t connect to the database: '.$e->getMessage(); | |
107 | } | |
001a7bad JB |
108 | } |
109 | ||
4d0ec0e7 | 110 | $rows[] = [$label, $status, $help]; |
db847ca0 | 111 | |
68003139 JB |
112 | // check MySQL & PostgreSQL version |
113 | $label = '<comment>Database version</comment>'; | |
cffcce0c JB |
114 | $status = '<info>OK!</info>'; |
115 | $help = ''; | |
116 | ||
fc79f1ff | 117 | // now check if MySQL isn't too old to handle utf8mb4 |
68003139 | 118 | if ($conn->isConnected() && 'mysql' === $conn->getDatabasePlatform()->getName()) { |
fc79f1ff JB |
119 | $version = $conn->query('select version()')->fetchColumn(); |
120 | $minimalVersion = '5.5.4'; | |
121 | ||
122 | if (false === version_compare($version, $minimalVersion, '>')) { | |
123 | $fulfilled = false; | |
68003139 JB |
124 | $status = '<error>ERROR!</error>'; |
125 | $help = 'Your MySQL version ('.$version.') is too old, consider upgrading ('.$minimalVersion.'+).'; | |
fc79f1ff JB |
126 | } |
127 | } | |
128 | ||
68003139 JB |
129 | // testing if PostgreSQL > 9.1 |
130 | if ($conn->isConnected() && 'postgresql' === $conn->getDatabasePlatform()->getName()) { | |
cffcce0c JB |
131 | // return version should be like "PostgreSQL 9.5.4 on x86_64-apple-darwin15.6.0, compiled by Apple LLVM version 8.0.0 (clang-800.0.38), 64-bit" |
132 | $version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn(); | |
133 | ||
134 | preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches); | |
135 | ||
136 | if (isset($matches[1]) & version_compare($matches[1], '9.2.0', '<')) { | |
137 | $fulfilled = false; | |
138 | $status = '<error>ERROR!</error>'; | |
139 | $help = 'PostgreSQL should be greater than 9.1 (actual version: '.$matches[1].')'; | |
140 | } | |
141 | } | |
142 | ||
143 | $rows[] = [$label, $status, $help]; | |
144 | ||
db847ca0 TC |
145 | foreach ($this->functionExists as $functionRequired) { |
146 | $label = '<comment>'.$functionRequired.'</comment>'; | |
0e49487b JB |
147 | $status = '<info>OK!</info>'; |
148 | $help = ''; | |
db847ca0 | 149 | |
0e49487b | 150 | if (!function_exists($functionRequired)) { |
fc6020b2 NL |
151 | $fulfilled = false; |
152 | $status = '<error>ERROR!</error>'; | |
f2fcb65b | 153 | $help = 'You need the '.$functionRequired.' function activated'; |
fc6020b2 | 154 | } |
db847ca0 | 155 | |
4d0ec0e7 | 156 | $rows[] = [$label, $status, $help]; |
2e45e7be | 157 | } |
0bf99bb1 | 158 | |
78507d28 JB |
159 | $table = new Table($this->defaultOutput); |
160 | $table | |
4d0ec0e7 | 161 | ->setHeaders(['Checked', 'Status', 'Recommendation']) |
0bf99bb1 | 162 | ->setRows($rows) |
78507d28 | 163 | ->render(); |
2e45e7be J |
164 | |
165 | if (!$fulfilled) { | |
0bf99bb1 | 166 | throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.'); |
2e45e7be J |
167 | } |
168 | ||
88d5d94d | 169 | $this->defaultOutput->writeln('<info>Success! Your system can run wallabag properly.</info>'); |
8a493541 | 170 | |
0bf99bb1 | 171 | $this->defaultOutput->writeln(''); |
2e45e7be J |
172 | |
173 | return $this; | |
174 | } | |
175 | ||
0bf99bb1 | 176 | protected function setupDatabase() |
2e45e7be | 177 | { |
5ecdfcd0 | 178 | $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>'); |
2e45e7be | 179 | |
0bf99bb1 J |
180 | // user want to reset everything? Don't care about what is already here |
181 | if (true === $this->defaultInput->getOption('reset')) { | |
d5027625 | 182 | $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache'); |
2e45e7be | 183 | |
0bf99bb1 | 184 | $this |
4d0ec0e7 | 185 | ->runCommand('doctrine:database:drop', ['--force' => true]) |
0bf99bb1 J |
186 | ->runCommand('doctrine:database:create') |
187 | ->runCommand('doctrine:schema:create') | |
d5027625 | 188 | ->runCommand('cache:clear') |
0bf99bb1 | 189 | ; |
2e45e7be | 190 | |
d5027625 JB |
191 | $this->defaultOutput->writeln(''); |
192 | ||
0bf99bb1 J |
193 | return $this; |
194 | } | |
2e45e7be | 195 | |
0bf99bb1 J |
196 | if (!$this->isDatabasePresent()) { |
197 | $this->defaultOutput->writeln('Creating database and schema, clearing the cache'); | |
2e45e7be | 198 | |
0bf99bb1 J |
199 | $this |
200 | ->runCommand('doctrine:database:create') | |
201 | ->runCommand('doctrine:schema:create') | |
202 | ->runCommand('cache:clear') | |
203 | ; | |
2e45e7be | 204 | |
d5027625 JB |
205 | $this->defaultOutput->writeln(''); |
206 | ||
0bf99bb1 J |
207 | return $this; |
208 | } | |
2e45e7be | 209 | |
78507d28 JB |
210 | $questionHelper = $this->getHelper('question'); |
211 | $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false); | |
2e45e7be | 212 | |
78507d28 | 213 | if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { |
0bf99bb1 | 214 | $this->defaultOutput->writeln('Droping database, creating database and schema'); |
2e45e7be | 215 | |
0bf99bb1 | 216 | $this |
4d0ec0e7 | 217 | ->runCommand('doctrine:database:drop', ['--force' => true]) |
0bf99bb1 J |
218 | ->runCommand('doctrine:database:create') |
219 | ->runCommand('doctrine:schema:create') | |
220 | ; | |
221 | } elseif ($this->isSchemaPresent()) { | |
78507d28 JB |
222 | $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false); |
223 | if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { | |
0bf99bb1 | 224 | $this->defaultOutput->writeln('Droping schema and creating schema'); |
2e45e7be | 225 | |
0bf99bb1 | 226 | $this |
4d0ec0e7 | 227 | ->runCommand('doctrine:schema:drop', ['--force' => true]) |
0bf99bb1 J |
228 | ->runCommand('doctrine:schema:create') |
229 | ; | |
230 | } | |
2e45e7be | 231 | } else { |
0bf99bb1 J |
232 | $this->defaultOutput->writeln('Creating schema'); |
233 | ||
234 | $this | |
235 | ->runCommand('doctrine:schema:create') | |
236 | ; | |
2e45e7be J |
237 | } |
238 | ||
0bf99bb1 J |
239 | $this->defaultOutput->writeln('Clearing the cache'); |
240 | $this->runCommand('cache:clear'); | |
241 | ||
0bf99bb1 J |
242 | $this->defaultOutput->writeln(''); |
243 | ||
244 | return $this; | |
2e45e7be J |
245 | } |
246 | ||
0bf99bb1 | 247 | protected function setupAdmin() |
2e45e7be | 248 | { |
5ecdfcd0 | 249 | $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>'); |
0bf99bb1 | 250 | |
78507d28 | 251 | $questionHelper = $this->getHelperSet()->get('question'); |
3c39f5ac | 252 | $question = new ConfirmationQuestion('Would you like to create a new admin user (recommended) ? (Y/n)', true); |
0bf99bb1 | 253 | |
78507d28 | 254 | if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) { |
0bf99bb1 J |
255 | return $this; |
256 | } | |
257 | ||
2e45e7be J |
258 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
259 | ||
ec3ce598 NL |
260 | $userManager = $this->getContainer()->get('fos_user.user_manager'); |
261 | $user = $userManager->createUser(); | |
78507d28 JB |
262 | |
263 | $question = new Question('Username (default: wallabag) :', 'wallabag'); | |
264 | $user->setUsername($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)); | |
265 | ||
266 | $question = new Question('Password (default: wallabag) :', 'wallabag'); | |
267 | $user->setPlainPassword($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)); | |
268 | ||
269 | $question = new Question('Email:', ''); | |
270 | $user->setEmail($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)); | |
271 | ||
a1691859 | 272 | $user->setEnabled(true); |
8f06a8c4 | 273 | $user->addRole('ROLE_SUPER_ADMIN'); |
2e45e7be J |
274 | |
275 | $em->persist($user); | |
276 | ||
ca17abce JB |
277 | // dispatch a created event so the associated config will be created |
278 | $event = new UserEvent($user); | |
279 | $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); | |
0bf99bb1 | 280 | |
637dc4bb JB |
281 | $this->defaultOutput->writeln(''); |
282 | ||
283 | return $this; | |
284 | } | |
285 | ||
286 | protected function setupConfig() | |
287 | { | |
5ecdfcd0 | 288 | $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Config setup.</info>'); |
637dc4bb JB |
289 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
290 | ||
d6ba77e8 JB |
291 | // cleanup before insert new stuff |
292 | $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute(); | |
293 | ||
294 | $settings = [ | |
d6ba77e8 | 295 | [ |
d0545b6b NL |
296 | 'name' => 'share_public', |
297 | 'value' => '1', | |
298 | 'section' => 'entry', | |
299 | ], | |
300 | [ | |
d6ba77e8 JB |
301 | 'name' => 'carrot', |
302 | 'value' => '1', | |
303 | 'section' => 'entry', | |
304 | ], | |
305 | [ | |
306 | 'name' => 'share_diaspora', | |
307 | 'value' => '1', | |
308 | 'section' => 'entry', | |
309 | ], | |
310 | [ | |
311 | 'name' => 'diaspora_url', | |
312 | 'value' => 'http://diasporapod.com', | |
313 | 'section' => 'entry', | |
314 | ], | |
8a9604aa NL |
315 | [ |
316 | 'name' => 'share_unmark', | |
317 | 'value' => '1', | |
318 | 'section' => 'entry', | |
319 | ], | |
320 | [ | |
321 | 'name' => 'unmark_url', | |
322 | 'value' => 'https://unmark.it', | |
323 | 'section' => 'entry', | |
324 | ], | |
d6ba77e8 JB |
325 | [ |
326 | 'name' => 'share_shaarli', | |
327 | 'value' => '1', | |
328 | 'section' => 'entry', | |
329 | ], | |
330 | [ | |
331 | 'name' => 'shaarli_url', | |
332 | 'value' => 'http://myshaarli.com', | |
333 | 'section' => 'entry', | |
334 | ], | |
fb9f100e F |
335 | [ |
336 | 'name' => 'share_scuttle', | |
337 | 'value' => '1', | |
338 | 'section' => 'entry', | |
339 | ], | |
340 | [ | |
341 | 'name' => 'scuttle_url', | |
342 | 'value' => 'http://scuttle.org', | |
343 | 'section' => 'entry', | |
b5a5377a | 344 | ], |
d6ba77e8 JB |
345 | [ |
346 | 'name' => 'share_mail', | |
347 | 'value' => '1', | |
348 | 'section' => 'entry', | |
349 | ], | |
350 | [ | |
351 | 'name' => 'share_twitter', | |
352 | 'value' => '1', | |
353 | 'section' => 'entry', | |
354 | ], | |
355 | [ | |
356 | 'name' => 'export_epub', | |
357 | 'value' => '1', | |
358 | 'section' => 'export', | |
359 | ], | |
360 | [ | |
361 | 'name' => 'export_mobi', | |
362 | 'value' => '1', | |
363 | 'section' => 'export', | |
364 | ], | |
365 | [ | |
366 | 'name' => 'export_pdf', | |
367 | 'value' => '1', | |
368 | 'section' => 'export', | |
369 | ], | |
a74a6ca2 JB |
370 | [ |
371 | 'name' => 'export_csv', | |
372 | 'value' => '1', | |
373 | 'section' => 'export', | |
374 | ], | |
375 | [ | |
376 | 'name' => 'export_json', | |
377 | 'value' => '1', | |
378 | 'section' => 'export', | |
379 | ], | |
380 | [ | |
381 | 'name' => 'export_txt', | |
382 | 'value' => '1', | |
383 | 'section' => 'export', | |
384 | ], | |
385 | [ | |
386 | 'name' => 'export_xml', | |
387 | 'value' => '1', | |
388 | 'section' => 'export', | |
389 | ], | |
40d2a294 | 390 | [ |
b3437d58 JB |
391 | 'name' => 'import_with_redis', |
392 | 'value' => '0', | |
393 | 'section' => 'import', | |
394 | ], | |
395 | [ | |
396 | 'name' => 'import_with_rabbitmq', | |
40d2a294 NL |
397 | 'value' => '0', |
398 | 'section' => 'import', | |
399 | ], | |
d6ba77e8 JB |
400 | [ |
401 | 'name' => 'show_printlink', | |
402 | 'value' => '1', | |
403 | 'section' => 'entry', | |
404 | ], | |
405 | [ | |
406 | 'name' => 'wallabag_support_url', | |
407 | 'value' => 'https://www.wallabag.org/pages/support.html', | |
408 | 'section' => 'misc', | |
409 | ], | |
410 | [ | |
411 | 'name' => 'wallabag_url', | |
aedd6ca0 | 412 | 'value' => '', |
d6ba77e8 JB |
413 | 'section' => 'misc', |
414 | ], | |
07643dde NL |
415 | [ |
416 | 'name' => 'piwik_enabled', | |
417 | 'value' => '0', | |
418 | 'section' => 'analytics', | |
419 | ], | |
420 | [ | |
421 | 'name' => 'piwik_host', | |
2cbf0d05 | 422 | 'value' => 'v2.wallabag.org', |
07643dde NL |
423 | 'section' => 'analytics', |
424 | ], | |
425 | [ | |
426 | 'name' => 'piwik_site_id', | |
427 | 'value' => '1', | |
428 | 'section' => 'analytics', | |
429 | ], | |
a4f42c59 JB |
430 | [ |
431 | 'name' => 'demo_mode_enabled', | |
432 | 'value' => '0', | |
433 | 'section' => 'misc', | |
434 | ], | |
435 | [ | |
436 | 'name' => 'demo_mode_username', | |
437 | 'value' => 'wallabag', | |
438 | 'section' => 'misc', | |
439 | ], | |
d1495dd0 JB |
440 | [ |
441 | 'name' => 'download_images_enabled', | |
442 | 'value' => '0', | |
aedd6ca0 | 443 | 'section' => 'misc', |
d1495dd0 | 444 | ], |
d64bf795 NL |
445 | [ |
446 | 'name' => 'restricted_access', | |
447 | 'value' => '0', | |
448 | 'section' => 'entry', | |
449 | ], | |
d6ba77e8 JB |
450 | ]; |
451 | ||
452 | foreach ($settings as $setting) { | |
453 | $newSetting = new Setting(); | |
454 | $newSetting->setName($setting['name']); | |
455 | $newSetting->setValue($setting['value']); | |
456 | $newSetting->setSection($setting['section']); | |
457 | $em->persist($newSetting); | |
458 | } | |
459 | ||
0bf99bb1 J |
460 | $em->flush(); |
461 | ||
462 | $this->defaultOutput->writeln(''); | |
463 | ||
464 | return $this; | |
2e45e7be J |
465 | } |
466 | ||
0bf99bb1 | 467 | /** |
4346a860 | 468 | * Run a command. |
0bf99bb1 J |
469 | * |
470 | * @param string $command | |
471 | * @param array $parameters Parameters to this command (usually 'force' => true) | |
472 | */ | |
4d0ec0e7 | 473 | protected function runCommand($command, $parameters = []) |
0bf99bb1 J |
474 | { |
475 | $parameters = array_merge( | |
4d0ec0e7 | 476 | ['command' => $command], |
0bf99bb1 | 477 | $parameters, |
4d0ec0e7 | 478 | [ |
0bf99bb1 J |
479 | '--no-debug' => true, |
480 | '--env' => $this->defaultInput->getOption('env') ?: 'dev', | |
4d0ec0e7 | 481 | ] |
0bf99bb1 J |
482 | ); |
483 | ||
484 | if ($this->defaultInput->getOption('no-interaction')) { | |
4d0ec0e7 | 485 | $parameters = array_merge($parameters, ['--no-interaction' => true]); |
0bf99bb1 J |
486 | } |
487 | ||
488 | $this->getApplication()->setAutoExit(false); | |
39a19bdf JB |
489 | |
490 | $output = new BufferedOutput(); | |
491 | $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output); | |
0bf99bb1 J |
492 | |
493 | if (0 !== $exitCode) { | |
494 | $this->getApplication()->setAutoExit(true); | |
495 | ||
39a19bdf JB |
496 | $this->defaultOutput->writeln(''); |
497 | $this->defaultOutput->writeln('<error>The command "'.$command.'" generates some errors: </error>'); | |
498 | $this->defaultOutput->writeln($output->fetch()); | |
0bf99bb1 | 499 | |
39a19bdf | 500 | die(); |
0bf99bb1 J |
501 | } |
502 | ||
503 | // PDO does not always close the connection after Doctrine commands. | |
504 | // See https://github.com/symfony/symfony/issues/11750. | |
505 | $this->getContainer()->get('doctrine')->getManager()->getConnection()->close(); | |
506 | ||
2e45e7be J |
507 | return $this; |
508 | } | |
0bf99bb1 J |
509 | |
510 | /** | |
4346a860 | 511 | * Check if the database already exists. |
0bf99bb1 | 512 | * |
4346a860 | 513 | * @return bool |
0bf99bb1 J |
514 | */ |
515 | private function isDatabasePresent() | |
516 | { | |
af43bd37 JB |
517 | $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection(); |
518 | $databaseName = $connection->getDatabase(); | |
0bf99bb1 J |
519 | |
520 | try { | |
af43bd37 | 521 | $schemaManager = $connection->getSchemaManager(); |
0bf99bb1 | 522 | } catch (\Exception $exception) { |
54a2241e | 523 | // mysql & sqlite |
0bf99bb1 J |
524 | if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) { |
525 | return false; | |
526 | } | |
527 | ||
54a2241e JB |
528 | // pgsql |
529 | if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) { | |
530 | return false; | |
531 | } | |
532 | ||
0bf99bb1 J |
533 | throw $exception; |
534 | } | |
535 | ||
732c2ad8 | 536 | // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite |
f62c3faf | 537 | if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) { |
732c2ad8 J |
538 | $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams(); |
539 | ||
540 | if (isset($params['path']) && file_exists($params['path'])) { | |
541 | return true; | |
542 | } | |
543 | ||
544 | return false; | |
545 | } | |
546 | ||
69c21157 JB |
547 | try { |
548 | return in_array($databaseName, $schemaManager->listDatabases()); | |
443cff98 | 549 | } catch (\Doctrine\DBAL\Exception\DriverException $e) { |
69c21157 JB |
550 | // it means we weren't able to get database list, assume the database doesn't exist |
551 | ||
552 | return false; | |
553 | } | |
0bf99bb1 J |
554 | } |
555 | ||
556 | /** | |
164bd801 | 557 | * Check if the schema is already created. |
4346a860 | 558 | * If we found at least oen table, it means the schema exists. |
0bf99bb1 | 559 | * |
4346a860 | 560 | * @return bool |
0bf99bb1 J |
561 | */ |
562 | private function isSchemaPresent() | |
563 | { | |
564 | $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager(); | |
565 | ||
164bd801 | 566 | return count($schemaManager->listTableNames()) > 0 ? true : false; |
0bf99bb1 | 567 | } |
2e45e7be | 568 | } |