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