Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Djar\Maisitauna\System\Commands\Db;
- use Djar\Maisitauna\Models\Events;
- use Djar\Maisitauna\System\Commands\AbstractCommand;
- use Phalcon\Db\Adapter\Pdo\Postgresql;
- use Phalcon\Di;
- use Phalcon\Mvc\Model\Query;
- use Phinx\Db\Adapter\AdapterInterface;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- class DbInstallCommand extends AbstractCommand
- {
- /**
- * {@inheritdoc}
- */
- protected function configure()
- {
- $this
- ->setName('db:install')
- ->setDescription('Create the user and database');
- }
- /**
- * {@inheritdoc}
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $output->writeln($this->createInfoMsg('Recreating user/database'));
- $dbConfig = Di::getDefault()->get('config')->database;
- $dbName = $dbConfig->dbname;
- $dbUsername = $dbConfig->username;
- $dbPassword = $dbConfig->username;
- $output->writeln($this->createInfoMsg('Dropping the existing database'));
- $this->executePsqlCommand(sprintf('DROP DATABASE %s;', $dbName));
- $output->writeln($this->createInfoMsg('Dropping the existing user'));
- $this->executePsqlCommand(sprintf('DROP USER %s;', $dbUsername));
- $output->writeln($this->createInfoMsg('Creating the user ' . $dbUsername));
- $this->executePsqlCommand(sprintf('CREATE USER %s WITH SUPERUSER PASSWORD \'%s\';', $dbUsername, $dbPassword));
- $output->writeln($this->createInfoMsg('Creating the database ' . $dbName));
- $this->executePsqlCommand(sprintf('CREATE DATABASE %s WITH OWNER %s;', $dbName, $dbUsername));
- }
- private function executePsqlCommand($command)
- {
- $command = sprintf('su postgres -c $\'psql -c "%s"\'', $command);
- echo $command . PHP_EOL;
- shell_exec($command);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement