From 55f58c9c5e8c523b7547c5d8199f0efef6941b93 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 11 Feb 2015 21:06:32 +0100 Subject: Update UserConfig schema --- src/Wallabag/CoreBundle/Command/InstallCommand.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/Wallabag/CoreBundle/Command/InstallCommand.php') diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index feaaebf6..bf2f747d 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -136,18 +136,18 @@ class InstallCommand extends ContainerAwareCommand $em->persist($user); $pagerConfig = new UsersConfig(); - $pagerConfig->setUserId($user->getId()); + $pagerConfig->setUser($user); $pagerConfig->setName('pager'); $pagerConfig->setValue(10); $em->persist($pagerConfig); - // $languageConfig = new LanguageConfig(); - // $languageConfig->setUserId($user->getId()); - // $languageConfig->setName('language'); - // $languageConfig->setValue('en_EN.UTF8'); + $languageConfig = new LanguageConfig(); + $languageConfig->setUser($user); + $languageConfig->setName('language'); + $languageConfig->setValue('en_EN'); - // $em->persist($languageConfig); + $em->persist($languageConfig); $em->flush(); } -- cgit v1.2.3 From 4d85d7e9ba676bd5ac3428976ce9227f460eb542 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Mon, 16 Feb 2015 21:28:49 +0100 Subject: Implement simple config --- src/Wallabag/CoreBundle/Command/InstallCommand.php | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) (limited to 'src/Wallabag/CoreBundle/Command/InstallCommand.php') diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index bf2f747d..a4301958 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -6,7 +6,7 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Wallabag\CoreBundle\Entity\User; -use Wallabag\CoreBundle\Entity\UsersConfig; +use Wallabag\CoreBundle\Entity\Config; class InstallCommand extends ContainerAwareCommand { @@ -135,21 +135,13 @@ class InstallCommand extends ContainerAwareCommand $em->persist($user); - $pagerConfig = new UsersConfig(); - $pagerConfig->setUser($user); - $pagerConfig->setName('pager'); - $pagerConfig->setValue(10); + $config = new Config(); + $config->setUser($user); + $config->setTheme('baggy'); + $config->setItemsPerPage(10); + $config->setLanguage('en_US'); - $em->persist($pagerConfig); - - $languageConfig = new LanguageConfig(); - $languageConfig->setUser($user); - $languageConfig->setName('language'); - $languageConfig->setValue('en_EN'); - - $em->persist($languageConfig); - - $em->flush(); + $em->persist($config); } protected function runCommand($command, InputInterface $input, OutputInterface $output) -- cgit v1.2.3 From 0bd2cb1ecd2f9194735af77142390a94723d1b39 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 22 Feb 2015 10:50:27 +0100 Subject: Handle default value for new user --- src/Wallabag/CoreBundle/Command/InstallCommand.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/Wallabag/CoreBundle/Command/InstallCommand.php') diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index a4301958..c1b72604 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -135,11 +135,10 @@ class InstallCommand extends ContainerAwareCommand $em->persist($user); - $config = new Config(); - $config->setUser($user); - $config->setTheme('baggy'); - $config->setItemsPerPage(10); - $config->setLanguage('en_US'); + $config = new Config($user); + $config->setTheme($this->getContainer()->getParameter('theme')); + $config->setItemsPerPage($this->getContainer()->getParameter('items_on_page')); + $config->setLanguage($this->getContainer()->getParameter('language')); $em->persist($config); } -- cgit v1.2.3 From 0bf99bb144561525a34a50315b95efd6f543fe35 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 22 Feb 2015 14:35:36 +0100 Subject: Improve install command & add test Also add fixtures for Config InstallCommand now check if database, schema are here and ask the user what to do (keep or trash & re-create) --- src/Wallabag/CoreBundle/Command/InstallCommand.php | 270 ++++++++++++++++----- 1 file changed, 207 insertions(+), 63 deletions(-) (limited to 'src/Wallabag/CoreBundle/Command/InstallCommand.php') diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index c1b72604..a528c309 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -4,134 +4,194 @@ namespace Wallabag\CoreBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\NullOutput; use Wallabag\CoreBundle\Entity\User; use Wallabag\CoreBundle\Entity\Config; class InstallCommand extends ContainerAwareCommand { + /** + * @var InputInterface + */ + protected $defaultInput; + + /** + * @var OutputInterface + */ + protected $defaultOutput; + protected function configure() { $this ->setName('wallabag:install') ->setDescription('Wallabag installer.') + ->addOption( + 'reset', + null, + InputOption::VALUE_NONE, + 'Reset current database' + ) ; } protected function execute(InputInterface $input, OutputInterface $output) { - $output->writeln('Installing Wallabag.'); + $this->defaultInput = $input; + $this->defaultOutput = $output; + + $output->writeln('Installing Wallabag...'); $output->writeln(''); $this - ->checkStep($output) - ->setupStep($input, $output) + ->checkRequirements() + ->setupDatabase() + ->setupAdmin() + ->setupAsset() ; $output->writeln('Wallabag has been successfully installed.'); $output->writeln('Just execute `php app/console server:run` for using wallabag: http://localhost:8000'); } - protected function checkStep(OutputInterface $output) + protected function checkRequirements() { - $output->writeln('Checking system requirements.'); + $this->defaultOutput->writeln('Step 1 of 4. Checking system requirements.'); $fulfilled = true; // @TODO: find a better way to check requirements - $output->writeln('Check PCRE'); + $label = 'PCRE'; if (extension_loaded('pcre')) { - $output->writeln(' OK'); + $status = 'OK!'; + $help = ''; } else { $fulfilled = false; - $output->writeln(' ERROR'); - $output->writeln('You should enabled PCRE extension'); + $status = 'ERROR!'; + $help = 'You should enabled PCRE extension'; } + $rows[] = array($label, $status, $help); - $output->writeln('Check DOM'); + $label = 'DOM'; if (extension_loaded('DOM')) { - $output->writeln(' OK'); + $status = 'OK!'; + $help = ''; } else { $fulfilled = false; - $output->writeln(' ERROR'); - $output->writeln('You should enabled DOM extension'); + $status = 'ERROR!'; + $help = 'You should enabled DOM extension'; } + $rows[] = array($label, $status, $help); + + $this->getHelper('table') + ->setHeaders(array('Checked', 'Status', 'Recommendation')) + ->setRows($rows) + ->render($this->defaultOutput); if (!$fulfilled) { - throw new RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.'); + throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.'); + } else { + $this->defaultOutput->writeln('Success! Your system can run Wallabag properly.'); } - $output->writeln(''); + $this->defaultOutput->writeln(''); return $this; } - protected function setupStep(InputInterface $input, OutputInterface $output) + protected function setupDatabase() { - $output->writeln('Setting up database.'); + $this->defaultOutput->writeln('Step 2 of 4. Setting up database.'); - $this->setupDatabase($input, $output); + // user want to reset everything? Don't care about what is already here + if (true === $this->defaultInput->getOption('reset')) { + $this->defaultOutput->writeln('Droping database, creating database and schema'); - // if ($this->getHelperSet()->get('dialog')->askConfirmation($output, 'Load fixtures (Y/N)?', false)) { - // $this->setupFixtures($input, $output); - // } + $this + ->runCommand('doctrine:database:drop', array('--force' => true)) + ->runCommand('doctrine:database:create') + ->runCommand('doctrine:schema:create') + ; - $output->writeln(''); - $output->writeln('Administration setup.'); + return $this; + } - $this->setupAdmin($output); + if (!$this->isDatabasePresent()) { + $this->defaultOutput->writeln('Creating database and schema, clearing the cache'); - $output->writeln(''); + $this + ->runCommand('doctrine:database:create') + ->runCommand('doctrine:schema:create') + ->runCommand('cache:clear') + ; - return $this; - } + return $this; + } - protected function setupDatabase(InputInterface $input, OutputInterface $output) - { - if ($this->getHelperSet()->get('dialog')->askConfirmation($output, 'Drop current database (Y/N)?', true)) { - $connection = $this->getContainer()->get('doctrine')->getConnection(); - $params = $connection->getParams(); + $dialog = $this->getHelper('dialog'); - $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false); - unset($params['dbname']); + if ($dialog->askConfirmation($this->defaultOutput, 'It appears that your database already exists. Would you like to reset it? (y/N) ', false)) { + $this->defaultOutput->writeln('Droping database, creating database and schema'); - if (!isset($params['path'])) { - $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name); - } + $this + ->runCommand('doctrine:database:drop', array('--force' => true)) + ->runCommand('doctrine:database:create') + ->runCommand('doctrine:schema:create') + ; + } elseif ($this->isSchemaPresent()) { + if ($dialog->askConfirmation($this->defaultOutput, 'Seems like your database contains schema. Do you want to reset it? (y/N) ', false)) { + $this->defaultOutput->writeln('Droping schema and creating schema'); - $connection->getSchemaManager()->dropDatabase($name); + $this + ->runCommand('doctrine:schema:drop', array('--force' => true)) + ->runCommand('doctrine:schema:create') + ; + } } else { - throw new \Exception("Install setup stopped, database need to be dropped. Please backup your current one and re-launch the install command."); + $this->defaultOutput->writeln('Creating schema'); + + $this + ->runCommand('doctrine:schema:create') + ; } - $this - ->runCommand('doctrine:database:create', $input, $output) - ->runCommand('doctrine:schema:create', $input, $output) - ->runCommand('cache:clear', $input, $output) - ->runCommand('assets:install', $input, $output) - ->runCommand('assetic:dump', $input, $output) - ; - } + $this->defaultOutput->writeln('Clearing the cache'); + $this->runCommand('cache:clear'); + + /* + if ($this->getHelperSet()->get('dialog')->askConfirmation($this->defaultOutput, 'Load fixtures (Y/N)?', false)) { + $doctrineConfig = $this->getContainer()->get('doctrine.orm.entity_manager')->getConnection()->getConfiguration(); + $logger = $doctrineConfig->getSQLLogger(); + // speed up fixture load + $doctrineConfig->setSQLLogger(null); + $this->runCommand('doctrine:fixtures:load'); + $doctrineConfig->setSQLLogger($logger); + } + */ - protected function setupFixtures(InputInterface $input, OutputInterface $output) - { - $doctrineConfig = $this->getContainer()->get('doctrine.orm.entity_manager')->getConnection()->getConfiguration(); - $logger = $doctrineConfig->getSQLLogger(); - // speed up fixture load - $doctrineConfig->setSQLLogger(null); - $this->runCommand('doctrine:fixtures:load', $input, $output); - $doctrineConfig->setSQLLogger($logger); + $this->defaultOutput->writeln(''); + + return $this; } - protected function setupAdmin(OutputInterface $output) + protected function setupAdmin() { + $this->defaultOutput->writeln('Step 3 of 4. Administration setup.'); + $dialog = $this->getHelperSet()->get('dialog'); + + if (false === $dialog->askConfirmation($this->defaultOutput, 'Would you like to create a new user ? (y/N)', true)) { + return $this; + } + $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $user = new User(); - $user->setUsername($dialog->ask($output, 'Username (default: wallabag) :', 'wallabag')); - $user->setPassword($dialog->ask($output, 'Password (default: wallabag) :', 'wallabag')); - $user->setEmail($dialog->ask($output, 'Email:', '')); + $user->setUsername($dialog->ask($this->defaultOutput, 'Username (default: wallabag) :', 'wallabag')); + $user->setPassword($dialog->ask($this->defaultOutput, 'Password (default: wallabag) :', 'wallabag')); + $user->setEmail($dialog->ask($this->defaultOutput, 'Email:', '')); $em->persist($user); @@ -141,16 +201,100 @@ class InstallCommand extends ContainerAwareCommand $config->setLanguage($this->getContainer()->getParameter('language')); $em->persist($config); + + $em->flush(); + + $this->defaultOutput->writeln(''); + + return $this; } - protected function runCommand($command, InputInterface $input, OutputInterface $output) + protected function setupAsset() { + $this->defaultOutput->writeln('Step 4 of 4. Installing assets.'); + $this - ->getApplication() - ->find($command) - ->run($input, $output) + ->runCommand('assets:install') + ->runCommand('assetic:dump') ; + $this->defaultOutput->writeln(''); + + return $this; + } + + /** + * Run a command + * + * @param string $command + * @param array $parameters Parameters to this command (usually 'force' => true) + */ + protected function runCommand($command, $parameters = array()) + { + $parameters = array_merge( + array('command' => $command), + $parameters, + array( + '--no-debug' => true, + '--env' => $this->defaultInput->getOption('env') ?: 'dev', + ) + ); + + if ($this->defaultInput->getOption('no-interaction')) { + $parameters = array_merge($parameters, array('--no-interaction' => true)); + } + + $this->getApplication()->setAutoExit(false); + $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput()); + + if (0 !== $exitCode) { + $this->getApplication()->setAutoExit(true); + + $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode); + $this->defaultOutput->writeln("$errorMessage"); + $exception = new \Exception($errorMessage, $exitCode); + + throw $exception; + } + + // PDO does not always close the connection after Doctrine commands. + // See https://github.com/symfony/symfony/issues/11750. + $this->getContainer()->get('doctrine')->getManager()->getConnection()->close(); + return $this; } + + /** + * Check if the database already exists + * + * @return boolean + */ + private function isDatabasePresent() + { + $databaseName = $this->getContainer()->getParameter('database_name'); + + try { + $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager(); + } catch (\Exception $exception) { + if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) { + return false; + } + + throw $exception; + } + + return in_array($databaseName, $schemaManager->listDatabases()); + } + + /** + * Check if the schema is already created + * + * @return boolean + */ + private function isSchemaPresent() + { + $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager(); + + return $schemaManager->tablesExist(array('entry')); + } } -- cgit v1.2.3 From 732c2ad8971f427ec196acfac53dad034cefdef4 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 22 Feb 2015 17:18:54 +0100 Subject: sqlite doesn't support getListDatabasesSQL --- src/Wallabag/CoreBundle/Command/InstallCommand.php | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/Wallabag/CoreBundle/Command/InstallCommand.php') diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index a528c309..ac7583ea 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -283,6 +283,17 @@ class InstallCommand extends ContainerAwareCommand throw $exception; } + // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite + if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) { + $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams(); + + if (isset($params['path']) && file_exists($params['path'])) { + return true; + } + + return false; + } + return in_array($databaseName, $schemaManager->listDatabases()); } -- cgit v1.2.3