Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
meltmedia/meltconsole / src / App / Commands / PullDatabaseCommand.php
Size: Mime:
<?php

namespace MeltConsole\App\Commands;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Exception\LogicException;

/**
 * Class to pull database.
 */
class PullDatabaseCommand extends MeltCommand {

  /**
   * Question helper.
   *
   * @var \Symfony\Component\Console\Helper\QuestionHelper
   */
  private $question;

  /**
   * {@inheritdoc}
   */
  protected function configure() {
    // Initial config for command.
    $this
      ->setName('melt:pull-database')
      ->setAliases(['melt:pulld'])
      ->setDescription('Pull database from remote environment.')
      ->setHelp('Syncs local databse with remote environment...')
      ->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Environment e.g dev, qa, stage');
  }

  /**
   * {@inheritdoc}
   */
  protected function interact(InputInterface $input, OutputInterface $output) {
    // TODO: Change the autogenerated stub.
    parent::interact($input, $output);

    $this->question = $this->getHelper('question');

    if (!$input->getOption('env')) {
      // Ask the user some stuff.
      $envs = $this->getEnvironmentsFromServer();
      $question = new ChoiceQuestion('Choose environment to pull from: ', $envs);

      if (empty($envs)) {
        throw new LogicException('There are currently no environments.');
      }

      $env = $this->question->ask($input, $output, $question);

      // Set options based on users answer.
      $input->setOption('env', $env);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function execute(InputInterface $input, OutputInterface $output) {

    $env = $input->getOption('env');

    if (!$this->validateEnvironmentExistsOnServer($env)) {
      throw new \Exception('Environment doesn\'t exist.');
    }

    // Year month day hour minute.
    $timestamp = date('Ymdhi');
    $srcDir = "{$this->projectName}-{$env}";
    $drushAlias = "@melt.{$env}";
    $backup_name = "{$srcDir}-backup-{$timestamp}.sql";
    $backupDir = '/opt/backups';

    $output->writeln("Pulling database in progress...");
    $this->runServerCommands([
      "cd /{$this->serverRoot}/{$srcDir}",
      "drush sql:dump --gzip --result-file={$backupDir}/{$backup_name}",
    ], FALSE);

    $output->writeln("Created a backup on the server and saved it to <info>{$backupDir}/{$backup_name}.gz</info>");
    $output->writeln("Syncing <info>{$backupDir}/{$backup_name}.gz</info> from the server to your local...");
    $this->runLocalCommands([
      "drush rsync {$drushAlias}:{$backupDir}/{$backup_name}.gz @self:../{$backup_name}.gz",
    ]);

    $output->writeln("Added <info>{$backup_name}.gz</info> to your local.");

    // Should we auto-import the database for them?
    $question = new ConfirmationQuestion('Would you like to import the database? (n/Y)');
    if ($this->question->ask($input, $output, $question)) {
      $this->runLocalCommands([
        "drush sql-drop -y",
        "gunzip {$backup_name}.gz",
        "drush sqlc < {$backup_name}",
        'drush cr'
      ], FALSE);

      $output->writeln('Database imported!');
    }

  }

}