Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Djar\Maisitauna\System\Commands\Db;
  4.  
  5. use Djar\Maisitauna\Models\Events;
  6. use Djar\Maisitauna\System\Commands\AbstractCommand;
  7. use Phalcon\Db\Adapter\Pdo\Postgresql;
  8. use Phalcon\Di;
  9. use Phalcon\Mvc\Model\Query;
  10. use Phinx\Db\Adapter\AdapterInterface;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13.  
  14. class DbInstallCommand extends AbstractCommand
  15. {
  16.     /**
  17.      * {@inheritdoc}
  18.      */
  19.     protected function configure()
  20.     {
  21.         $this
  22.             ->setName('db:install')
  23.             ->setDescription('Create the user and database');
  24.     }
  25.  
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     protected function execute(InputInterface $input, OutputInterface $output)
  30.     {
  31.         $output->writeln($this->createInfoMsg('Recreating user/database'));
  32.  
  33.         $dbConfig = Di::getDefault()->get('config')->database;
  34.  
  35.         $dbName     = $dbConfig->dbname;
  36.         $dbUsername = $dbConfig->username;
  37.         $dbPassword = $dbConfig->username;
  38.  
  39.         $output->writeln($this->createInfoMsg('Dropping the existing database'));
  40.         $this->executePsqlCommand(sprintf('DROP DATABASE %s;', $dbName));
  41.  
  42.         $output->writeln($this->createInfoMsg('Dropping the existing user'));
  43.         $this->executePsqlCommand(sprintf('DROP USER %s;', $dbUsername));
  44.  
  45.         $output->writeln($this->createInfoMsg('Creating the user ' . $dbUsername));
  46.         $this->executePsqlCommand(sprintf('CREATE USER %s WITH SUPERUSER PASSWORD \'%s\';', $dbUsername, $dbPassword));
  47.  
  48.         $output->writeln($this->createInfoMsg('Creating the database ' . $dbName));
  49.         $this->executePsqlCommand(sprintf('CREATE DATABASE %s WITH OWNER %s;', $dbName, $dbUsername));
  50.     }
  51.  
  52.     private function executePsqlCommand($command)
  53.     {
  54.         $command = sprintf('su postgres -c $\'psql -c "%s"\'', $command);
  55.         echo $command . PHP_EOL;
  56.         shell_exec($command);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement