Skip to content

Commit

Permalink
Merge pull request #14 from WeareJH/bugfix/fix-import-command
Browse files Browse the repository at this point in the history
Fix DB dump import command
  • Loading branch information
maciejslawik authored Nov 10, 2023
2 parents 79289fd + 9ed4323 commit 7df72d3
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 26 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ After you have edited the env.php file, run the following command immediately :
bin/magento app:config:import
```

## Manual Run
## Run upload manually

You can also manually trigger the stripped database upload from the command line by running the following command :

Expand All @@ -95,6 +95,21 @@ To do a full DB dump
bin/magento wearejh:stripped-db-provider:upload-to-remote --full
```

## Import a remote dump locally

You can use the module to import a dump from S3 directly to your local. It will back up your local admin accounts and
reimport them

```
bin/magento wearejh:stripped-db-provider:wearejh:stripped-db-provider:import-from-remote PROJECT NAME
```

To skip admin accounts backup

```
bin/magento wearejh:stripped-db-provider:wearejh:stripped-db-provider:import-from-remote PROJECT NAME --no-admin-backup=1
```

## Issues / Feature Request

Please open github issues for any issues you encounter or feature requests you want to see. PRs are of course welcomed.
30 changes: 28 additions & 2 deletions src/Console/ImportFromRemoteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Jh\StrippedDbProvider\Model\DbFacade;
use Jh\StrippedDbProvider\Model\ProjectMeta;

class ImportFromRemoteCommand extends Command
{
const ARGUMENT_PROJECT_NAME = 'source-project-name';
private const ARGUMENT_PROJECT_NAME = 'source-project-name';
private const OPTION_NO_ADMIN_ACCOUNT_BACKUP = 'no-admin-backup';

public function __construct(
private DbFacade $dbFacade,
Expand All @@ -34,6 +36,13 @@ protected function configure()
InputArgument::REQUIRED,
'Source Project Name to import from eg. prod-stroustrup-workshop.'
);
$this->addOption(
self::OPTION_NO_ADMIN_ACCOUNT_BACKUP,
null,
InputOption::VALUE_OPTIONAL,
'Set this flag to skip backup of local admin accounts',
false
);
}

/**
Expand All @@ -43,6 +52,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$sourceProjectMeta = new ProjectMeta($input->getArgument(self::ARGUMENT_PROJECT_NAME));
$backupAdminAccounts = !$input->getOption(self::OPTION_NO_ADMIN_ACCOUNT_BACKUP);

$output->writeln('<fg=cyan;options=bold>Downloading Database From Cloud Storage...</>');
$this->dbFacade->downloadDatabaseDump($sourceProjectMeta);
$output->writeln(sprintf(
Expand All @@ -51,15 +62,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
));
$output->writeln('<fg=cyan;options=bold>Uncompressing Database ...</>');
$this->dbFacade->uncompressDatabaseDump($sourceProjectMeta);
$output->writeln('<fg=cyan;options=bold>Importing Database ...</>');
$output->writeln('<fg=cyan;options=bold>Dropping local tables ...</>');

if ($backupAdminAccounts) {
$output->writeln('<fg=cyan;options=bold>Backing up local admin accounts ...</>');
$this->dbFacade->backupLocalAdminAccounts($sourceProjectMeta);
}

$output->writeln("<info>Starting the Database import</info>");
$this->dbFacade->importDatabaseDump($sourceProjectMeta);
$output->writeln("<info>Database successfully imported.</info>");

if ($backupAdminAccounts) {
$output->writeln('<fg=cyan;options=bold>Restoring local admin accounts ...</>');
$this->dbFacade->restoreLocalAdminAccountsFromBackup($sourceProjectMeta);
}
} catch (\Exception $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
} finally {
if (isset($sourceProjectMeta)) {
$this->dbFacade->cleanUpLocalDumpFiles($sourceProjectMeta);
}
if ($backupAdminAccounts) {
$this->dbFacade->cleanUpAdminAccountsBackup($sourceProjectMeta);
}
}
return 0;
}
Expand Down
77 changes: 77 additions & 0 deletions src/Model/Db/DbAdminAccountsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Jh\StrippedDbProvider\Model\Db;

use Ifsnop\Mysqldump\Mysqldump;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Jh\StrippedDbProvider\Model\Config;
use Jh\StrippedDbProvider\Model\ProjectMeta;
use Magento\Framework\Shell;

class DbAdminAccountsManager
{
private const BACKUP_FILENAME = 'admin_accounts.sql';

public function __construct(
private Config $config,
private Shell $shell
) {
}

/**
* @throws \Exception
*/
public function backupAdminAccounts(ProjectMeta $projectMeta): void
{
$hostName = $this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_HOST);
$dbName = $this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_NAME);
$dumper = new Mysqldump(
"mysql:host={$hostName};dbname={$dbName}",
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_USER),
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_PASSWORD),
[
'skip-definer' => true,
'add-drop-table' => true,
'include-tables' => [
'admin_passwords',
'admin_system_messages',
'admin_user',
'admin_user_session',
'authorization_role',
'authorization_rule'
]
]
);

$dumper->start($this->getAdminAccountsBackupFilePath($projectMeta));
}

public function restoreAdminAccountsBackup(ProjectMeta $projectMeta): void
{
$hostName = $this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_HOST);
$dbName = $this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_NAME);
$dumper = new Mysqldump(
"mysql:host={$hostName};dbname={$dbName}",
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_USER),
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_PASSWORD),
['skip-definer' => true]
);
$dumper->restore($this->getAdminAccountsBackupFilePath($projectMeta));
}

public function cleanUp(ProjectMeta $projectMeta): void
{
try {
$this->shell->execute("rm %s", [$this->getAdminAccountsBackupFilePath($projectMeta)]);
} catch (\Exception $e) {
//empty
}
}

private function getAdminAccountsBackupFilePath(ProjectMeta $projectMeta): string
{
return $projectMeta->getLocalDumpStoragePath() . self::BACKUP_FILENAME;
}
}
2 changes: 1 addition & 1 deletion src/Model/Db/DbDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function dumpDb(ProjectMeta $projectMeta, bool $fullDump): void
"mysql:host={$hostName};dbname={$dbName}",
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_USER),
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_PASSWORD),
['skip-definer' => true]
['skip-definer' => true, 'add-drop-table' => true, 'skip-triggers' => true]
);

if (!$fullDump) {
Expand Down
18 changes: 8 additions & 10 deletions src/Model/Db/DbImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace Jh\StrippedDbProvider\Model\Db;

use Magento\Framework\Shell;
use Ifsnop\Mysqldump\Mysqldump;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Jh\StrippedDbProvider\Model\Config;
use Jh\StrippedDbProvider\Model\ProjectMeta;

class DbImporter
{
public function __construct(
private Config $config,
private Shell $shell
private Config $config
) {
}

Expand All @@ -23,15 +22,14 @@ public function __construct(
*/
public function importDatabase(ProjectMeta $projectMeta): void
{
$cmd = sprintf(
"mysql -h%s -u%s --password=%s %s < %s",
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_HOST),
$hostName = $this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_HOST);
$dbName = $this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_NAME);
$dumper = new Mysqldump(
"mysql:host={$hostName};dbname={$dbName}",
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_USER),
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_PASSWORD),
$this->config->getLocalDbConfigData(ConfigOptionsListConstants::KEY_NAME),
$projectMeta->getLocalAbsoluteFileDumpPath()
['skip-definer' => true]
);

$this->shell->execute($cmd);
$dumper->restore($projectMeta->getLocalAbsoluteFileDumpPath());
}
}
29 changes: 17 additions & 12 deletions src/Model/DbFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ public function __construct(
private Db\DbDownloader $downloader,
private Db\DbUploader $uploader,
private Db\DbImporter $importer,
private Db\DbCleaner $cleaner
private Db\DbCleaner $cleaner,
private Db\DbAdminAccountsManager $adminAccountsManager
) {
}

/**
* @param ProjectMeta $projectMeta
* @param bool $fullDump
* @throws \Exception
*/
public function dumpDatabase(ProjectMeta $projectMeta, bool $fullDump): void
Expand All @@ -37,7 +36,6 @@ public function downloadDatabaseDump(ProjectMeta $projectMeta): void
}

/**
* @param ProjectMeta $projectMeta
* @throws \Exception
*/
public function compressDatabaseDump(ProjectMeta $projectMeta): void
Expand All @@ -46,7 +44,6 @@ public function compressDatabaseDump(ProjectMeta $projectMeta): void
}

/**
* @param ProjectMeta $projectMeta
* @throws \Exception
*/
public function uncompressDatabaseDump(ProjectMeta $projectMeta): void
Expand All @@ -55,27 +52,35 @@ public function uncompressDatabaseDump(ProjectMeta $projectMeta): void
}

/**
* @param ProjectMeta $projectMeta
* @throws \Exception
*/
public function importDatabaseDump(ProjectMeta $projectMeta): void
{
$this->importer->importDatabase($projectMeta);
}

/**
* @param ProjectMeta $projectMeta
*/
public function cleanUpLocalDumpFiles(ProjectMeta $projectMeta): void
{
$this->cleaner->cleanUp($projectMeta);
}

/**
* @param ProjectMeta $projectMeta
*/
public function uploadDatabaseDump(ProjectMeta $projectMeta): void
{
$this->uploader->uploadDBDump($projectMeta);
}

public function backupLocalAdminAccounts(ProjectMeta $projectMeta): void
{
$this->adminAccountsManager->backupAdminAccounts($projectMeta);
}

public function restoreLocalAdminAccountsFromBackup(ProjectMeta $projectMeta): void
{
$this->adminAccountsManager->restoreAdminAccountsBackup($projectMeta);
}

public function cleanUpAdminAccountsBackup(ProjectMeta $projectMeta): void
{
$this->adminAccountsManager->cleanUp($projectMeta);
}
}
5 changes: 5 additions & 0 deletions src/Model/ProjectMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public function getRemoteDumpObjectKey(): string
{
return $this->remoteStoragePath . $this->getCompressedDumpFileName();
}

public function getLocalDumpStoragePath(): string
{
return $this->localStoragePath;
}
}

0 comments on commit 7df72d3

Please sign in to comment.