Skip to content

Commit

Permalink
Add commands support for Warden Docker db container
Browse files Browse the repository at this point in the history
  • Loading branch information
ytorbyk committed Oct 12, 2021
1 parent a4704be commit 4f67c73
Show file tree
Hide file tree
Showing 14 changed files with 852 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/Commands/Database/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function handle(): void

Pdo::validateConfiguration();

$tmpFilePath = tempnam(Directory::getTmpDirectory(), 'roost_');
$tmpFilePath = tempnam(Directory::getTmpDirectory(), 'roost_tmp_dump_');
$pipeUnarchive = new Pipe();
$isUnarchive = Archive::addUnarchiveCommand($dbPath, $pipeUnarchive);
if ($isUnarchive) {
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Database/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function handle(): void
});
}

$tableRows = array_map(static function ($dbName) {
$dbRows = array_map(static function ($dbName) {
return [$dbName];
}, $dbList);

$table = new Table($this->output);
$table->setHeaders(['Databases']);
$table->setRows($tableRows);
$table->setRows($dbRows);
$table->render();
}
}
15 changes: 15 additions & 0 deletions app/Commands/Dump/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Services\AwsS3;
use App\Services\Dump;
use App\Commands\Database\ImportCommand;
use App\Commands\Warden\ImportCommand as WardenImportCommand;

class DownloadCommand extends Command
{
Expand All @@ -23,6 +24,7 @@ class DownloadCommand extends Command
protected $signature = self::COMMAND
. ' {dump? : Dump file name}'
. ' {--i|import : Import downloaded dump}'
. ' {--w|import-warden : Import downloaded dump into Warden Db container}'
. ' {--r|remove-file : Remove file after import}'
. ' {--no-progress : Do not display progress}'
. ' {--f|force : Overwrite local file if exits without confirmation}';
Expand Down Expand Up @@ -90,6 +92,19 @@ public function handle(): void
]
);

$this->processDeletingFile($dbFile);
} elseif ($this->option('import-warden')) {
$this->info('Import the dump:');

$this->call(
WardenImportCommand::COMMAND,
[
'file' => $dbFile,
'--no-progress' => $this->option('no-progress'),
'--quiet' => $this->option('quiet'),
]
);

$this->processDeletingFile($dbFile);
}
}
Expand Down
83 changes: 83 additions & 0 deletions app/Commands/Warden/BackupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace App\Commands\Warden;

use LaravelZero\Framework\Commands\Command;
use App\Traits\Command as AppCommand;
use App\Commands\Dump\CleanCommand;

class BackupCommand extends Command
{
use AppCommand;

const COMMAND = 'warden:db:backup';

/**
* @var string
*/
protected $signature = self::COMMAND
. ' {file? : File name}'
. ' {--t|tag= : A tag of the dump file}'
. ' {--s|strip= : Tables to strip (dump only structure of those tables)}'
. ' {--no-progress : Do not display progress}'
. ' {--print : Print command only, not run it}'
. ' {--skip-filter : Do not filter DEFINER and ROW_FORMAT}'
. ' {--compatibility : mysqldump 8 backward compatible with MySQL 5.7}'
. ' {--c|clean= : The number of latest dumps to keep, other will be removed}'
. ' {--f|force : Overwrite dump file if it already exits locally}'
. ' {--k|keep-file : Keep dump file}';

/**
* @var string
*/
protected $description = 'Export and upload DB from Warden db container to AWS';

/**
* @return void
*/
public function handle(): void
{
$this->call(
ExportCommand::COMMAND,
[
'file' => $this->argument('file'),
'--magento-directory' => $this->option('magento-directory'),
'--db-host' => $this->option('db-host'),
'--db-port' => $this->option('db-port'),
'--db-name' => $this->option('db-name'),
'--db-username' => $this->option('db-username'),
'--db-password' => $this->option('db-password'),
'--storage' => $this->option('storage'),
'--aws-bucket' => $this->option('aws-bucket'),
'--aws-access-key' => $this->option('aws-access-key'),
'--aws-secret-key' => $this->option('aws-secret-key'),
'--aws-region' => $this->option('aws-region'),
'--tag' => $this->option('tag'),
'--strip' => $this->option('strip'),
'--compatibility' => $this->option('compatibility'),
'--no-progress' => $this->option('no-progress'),
'--print' => $this->option('print'),
'--skip-filter' => $this->option('skip-filter'),
'--project' => $this->option('project'),
'--force' => $this->option('force'),
'--quiet' => $this->option('quiet'),
'--upload' => true,
'--remove-file' => !$this->option('keep-file')
]
);

$clean = (int)$this->option('clean');
if ($clean > 0) {
$this->call(
CleanCommand::COMMAND,
[
'count' => $clean,
'--project' => $this->option('project'),
'--tag' => $this->option('tag'),
]
);
}
}
}
61 changes: 61 additions & 0 deletions app/Commands/Warden/CreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace App\Commands\Warden;

use App\Services\WardenDatabase;
use App\Traits\Command as AppCommand;
use LaravelZero\Framework\Commands\Command;

class CreateCommand extends Command
{
use AppCommand;

const COMMAND = 'warden:db:create';

/**
* @var string
*/
protected $signature = self::COMMAND
. ' {name? : Database name}'
. ' {--f|force : Re-create if DB with the same name already exists}';

/**
* @var string
*/
protected $description = 'Create Database from Warden db container';

/**
* @return void
*/
public function handle(): void
{
$dbName = $this->argument('name');

if ($this->option('force')) {
$this->call(DropCommand::COMMAND, ['name' => $dbName]);
}

$taskMessage = $dbName
? sprintf('Create DB %s if not exists', $dbName)
: 'Create DB if not exists';

$this->task($taskMessage, static function () use ($dbName) {
try {
$wardenCommand = WardenDatabase::createWardenDbCommand('create');
if ($dbName) {
$wardenCommand->argument($dbName);
}
$wardenCommand->exec();

$result = true;
} catch (\Symfony\Component\Process\Exception\ProcessFailedException $e) {
$result = $e->getProcess()->getCommandLine();
} catch (\Exception $e) {
$result = $e->getMessage();
}
return $result;
});
}
}
57 changes: 57 additions & 0 deletions app/Commands/Warden/DropCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace App\Commands\Warden;

use App\Traits\Command as AppCommand;
use App\Services\WardenDatabase;
use LaravelZero\Framework\Commands\Command;

class DropCommand extends Command
{
use AppCommand;

const COMMAND = 'warden:db:drop';

/**
* @var string
*/
protected $signature = self::COMMAND
. ' {name? : Database name}';

/**
* @var string
*/
protected $description = 'Drop Database from Warden db container';

/**
* @return void
*/
public function handle(): void
{
$dbName = $this->argument('name');

$taskMessage = $dbName
? sprintf('Drop DB "%s" if exists', $dbName)
: 'Drop DB if exists';

$this->task($taskMessage, static function () use ($dbName) {
try {

$wardenCommand = WardenDatabase::createWardenDbCommand('drop');
if ($dbName) {
$wardenCommand->argument($dbName);
}
$wardenCommand->exec();

$result = true;
} catch (\Symfony\Component\Process\Exception\ProcessFailedException $e) {
$result = $e->getProcess()->getCommandLine();
} catch (\Exception $e) {
$result = $e->getMessage();
}
return $result;
});
}
}
Loading

0 comments on commit 4f67c73

Please sign in to comment.