Skip to content

Commit

Permalink
Merge pull request #13 from WeareJH/12-full-dump-option
Browse files Browse the repository at this point in the history
#12 Allow full DB dumps with a CLI argument
  • Loading branch information
maciejslawik authored Aug 25, 2023
2 parents 0f25cb5 + 945cdd3 commit 79289fd
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 197 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Add the following repositories to the `repositories` section of your `composer.j

```
{"type": "vcs", "url": "git@github.com:WeareJH/stripped-db-provider.git"},
{"type": "vcs", "url": "git@github.com:maciejslawik/mysqldump-php.git"}
```

Then install like any regular composer package
Expand Down Expand Up @@ -90,6 +89,12 @@ You can also manually trigger the stripped database upload from the command line
bin/magento wearejh:stripped-db-provider:upload-to-remote
```

To do a full DB dump

```
bin/magento wearejh:stripped-db-provider:upload-to-remote --full
```

## Issues / Feature Request

Please open github issues for any issues you encounter or feature requests you want to see. PRs are of course welcomed.
10 changes: 2 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
"name": "wearejh/stripped-db-provider",
"description": "Stripped DB Provider for Magento 2",
"type": "magento2-module",
"repositories": [
{
"type": "vcs",
"url": "git@github.com:maciejslawik/mysqldump-php.git"
}
],
"require": {
"php": "^7.1 || ^8.1.0",
"php": ">=8.2.0",
"aws/aws-sdk-php": "^3.112.0",
"ifsnop/mysqldump-php": ">=3.0"
"ifsnop/mysqldump-php": "^2.12"
},
"autoload": {
"files": [
Expand Down
11 changes: 3 additions & 8 deletions src/Console/ImportFromRemoteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ class ImportFromRemoteCommand extends Command
{
const ARGUMENT_PROJECT_NAME = 'source-project-name';

/**
* @var DbFacade
*/
private $dbFacade;

public function __construct(
DbFacade $dbFacade,
private DbFacade $dbFacade,
string $name = null
) {
parent::__construct($name);
$this->dbFacade = $dbFacade;
}

/**
Expand All @@ -45,7 +39,7 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$sourceProjectMeta = new ProjectMeta($input->getArgument(self::ARGUMENT_PROJECT_NAME));
Expand All @@ -67,5 +61,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->dbFacade->cleanUpLocalDumpFiles($sourceProjectMeta);
}
}
return 0;
}
}
21 changes: 7 additions & 14 deletions src/Console/UploadToRemoteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@

class UploadToRemoteCommand extends Command
{
/**
* @var DbFacade
*/
private $dbFacade;

/**
* @var Config
*/
private $config;
private const OPTION_FULL_DUMP = 'full';

public function __construct(DbFacade $dbFacade, Config $config, string $name = null)
public function __construct(private DbFacade $dbFacade, private Config $config, string $name = null)
{
parent::__construct($name);
$this->dbFacade = $dbFacade;
$this->config = $config;
}

/**
Expand All @@ -36,17 +26,19 @@ protected function configure()
{
$this->setName('wearejh:stripped-db-provider:upload-to-remote');
$this->setDescription("Upload a stripped DB dump to JH's Cloud Storage");
$this->addOption(self::OPTION_FULL_DUMP);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$fullDump = (bool) $input->getOption(self::OPTION_FULL_DUMP);
$projectMeta = $this->config->getProjectMeta();
$output->writeln('<fg=cyan;options=bold>Dumping Database...</>');
$this->dbFacade->dumpDatabase($projectMeta);
$this->dbFacade->dumpDatabase($projectMeta, $fullDump);
$output->writeln("<info>Dump created at {$projectMeta->getLocalAbsoluteFileDumpPath()}</info>");
$output->writeln('<fg=cyan;options=bold>Compressing Dump...</>');
$this->dbFacade->compressDatabaseDump($projectMeta);
Expand All @@ -60,5 +52,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->dbFacade->cleanUpLocalDumpFiles($projectMeta);
}
}
return 0;
}
}
28 changes: 8 additions & 20 deletions src/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,26 @@ class Config
/**
* General Config
*/
const XML_PATH_ENABLED = 'stripped_db_provider/general/enabled';
const XML_PATH_PROJECT_NAME = 'stripped_db_provider/general/project_name';
private const XML_PATH_ENABLED = 'stripped_db_provider/general/enabled';
private const XML_PATH_PROJECT_NAME = 'stripped_db_provider/general/project_name';

/**
* Amazon S3 Bucket Settings
*/
const XML_PATH_BUCKET_NAME = 'stripped_db_provider/storage/bucket_name';
const XML_PATH_BUCKET_REGION = 'stripped_db_provider/storage/region';
const XML_PATH_ACCESS_KEY_ID = 'stripped_db_provider/storage/access_key_id';
const XML_PATH_SECRET_ACCESS_KEY = 'stripped_db_provider/storage/secret_access_key';
private const XML_PATH_BUCKET_NAME = 'stripped_db_provider/storage/bucket_name';
private const XML_PATH_BUCKET_REGION = 'stripped_db_provider/storage/region';
private const XML_PATH_ACCESS_KEY_ID = 'stripped_db_provider/storage/access_key_id';
private const XML_PATH_SECRET_ACCESS_KEY = 'stripped_db_provider/storage/secret_access_key';

/**
* Dump Specific
*/
const XML_PATH_PROJECT_IGNORE_TABLES = 'stripped_db_provider/dump/project_ignore_tables';

/**
* @var ScopeConfigInterface
*/
private $config;

/**
* @var DeploymentConfig
*/
private $deploymentConfig;

public function __construct(
ScopeConfigInterface $config,
DeploymentConfig $deploymentConfig
private ScopeConfigInterface $config,
private DeploymentConfig $deploymentConfig
) {
$this->config = $config;
$this->deploymentConfig = $deploymentConfig;
}

public function isEnabled(): bool
Expand Down
16 changes: 2 additions & 14 deletions src/Model/Db/DbCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@

class DbCleaner
{
/**
* @var Shell
*/
private $shell;

public function __construct(Shell $shell)
public function __construct(private Shell $shell)
{
$this->shell = $shell;
}

/**
* Attempt to silently remove the database dumps
*
* @param ProjectMeta $projectMeta
* @return string
*/
public function cleanUp(ProjectMeta $projectMeta)
public function cleanUp(ProjectMeta $projectMeta): void
{
try {
$this->shell->execute("rm %s", [$projectMeta->getLocalAbsoluteFileDumpPath()]);
Expand Down
12 changes: 3 additions & 9 deletions src/Model/Db/DbCompresser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@

class DbCompresser
{
/**
* @var Shell
*/
private $shell;

public function __construct(Shell $shell)
public function __construct(private Shell $shell)
{
$this->shell = $shell;
}

/**
* @param ProjectMeta $projectMeta
* @throws LocalizedException
*/
public function compressDump(ProjectMeta $projectMeta)
public function compressDump(ProjectMeta $projectMeta): void
{
$this->shell->execute(
sprintf(
Expand All @@ -39,7 +33,7 @@ public function compressDump(ProjectMeta $projectMeta)
* @param ProjectMeta $projectMeta
* @throws LocalizedException
*/
public function uncompressDump(ProjectMeta $projectMeta)
public function uncompressDump(ProjectMeta $projectMeta): void
{
$this->shell->execute(
sprintf(
Expand Down
16 changes: 2 additions & 14 deletions src/Model/Db/DbDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,14 @@

class DbDownloader
{
/**
* @var Config
*/
private $config;

/**
* @var ClientProvider
*/
private $clientProvider;

public function __construct(ClientProvider $clientProvider, Config $config)
public function __construct(private ClientProvider $clientProvider, private Config $config)
{
$this->config = $config;
$this->clientProvider = $clientProvider;
}

/**
* @param ProjectMeta $projectMeta
*/
public function downloadDBDump(ProjectMeta $projectMeta)
public function downloadDBDump(ProjectMeta $projectMeta): void
{
$client = $this->clientProvider->getClient();
$bucketName = $this->config->getBucketName();
Expand Down
7 changes: 5 additions & 2 deletions src/Model/Db/DbDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public function __construct(

/**
* @param ProjectMeta $projectMeta
* @param bool $fullDump
* @throws \Exception
*/
public function dumpDb(ProjectMeta $projectMeta): void
public function dumpDb(ProjectMeta $projectMeta, bool $fullDump): void
{
$hostName = $this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_HOST);
$dbName = $this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_NAME);
Expand All @@ -52,7 +53,9 @@ public function dumpDb(ProjectMeta $projectMeta): void
['skip-definer' => true]
);

$dumper->setTableLimits($this->getTableLimits());
if (!$fullDump) {
$dumper->setTableLimits($this->getTableLimits());
}
$dumper->start($projectMeta->getLocalAbsoluteFileDumpPath());
}

Expand Down
16 changes: 2 additions & 14 deletions src/Model/Db/DbImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,10 @@

class DbImporter
{
/**
* @var Shell
*/
private $shell;

/**
* @var Config
*/
private $config;

public function __construct(
Config $config,
Shell $shell
private Config $config,
private Shell $shell
) {
$this->shell = $shell;
$this->config = $config;
}

/**
Expand Down
18 changes: 4 additions & 14 deletions src/Model/Db/DbTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,10 @@ class DbTables
'wishlist_item_option',
];

/**
* @var DeploymentConfig
*/
private $deploymentConfig;

/**
* @var ResourceConnection
*/
private $resourceConnection;

public function __construct(DeploymentConfig $deploymentConfig, ResourceConnection $resourceConnection)
{
$this->deploymentConfig = $deploymentConfig;
$this->resourceConnection = $resourceConnection;
public function __construct(
private DeploymentConfig $deploymentConfig,
private ResourceConnection $resourceConnection
) {
}

public function getStructureOnlyTables(): array
Expand Down
19 changes: 4 additions & 15 deletions src/Model/Db/DbUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,23 @@

namespace Jh\StrippedDbProvider\Model\Db;

use Aws\ResultInterface;
use Aws\S3\MultipartUploader;
use Jh\StrippedDbProvider\Model\S3\ClientProvider;
use Jh\StrippedDbProvider\Model\Config;
use Jh\StrippedDbProvider\Model\ProjectMeta;

class DbUploader
{
/**
* @var Config
*/
private $config;

/**
* @var ClientProvider
*/
private $clientProvider;

public function __construct(ClientProvider $clientProvider, Config $config)
public function __construct(private ClientProvider $clientProvider, private Config $config)
{
$this->config = $config;
$this->clientProvider = $clientProvider;
}

/**
* @param ProjectMeta $projectMeta
* @return \Aws\ResultInterface
* @return ResultInterface
*/
public function uploadDBDump(ProjectMeta $projectMeta)
public function uploadDBDump(ProjectMeta $projectMeta): ResultInterface
{
$client = $this->clientProvider->getClient();

Expand Down
Loading

0 comments on commit 79289fd

Please sign in to comment.