Skip to content

Commit

Permalink
Added --emptyDatabase flag when importing databases
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgrayisok committed Jan 27, 2025
1 parent ba9cc65 commit 0d62730
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Servd Assets and Helpers

## 4.0.13 - 2025-01-27

### Added

- Added the `--emptyDatabase` flag for the `servd/local/pull-database` CLI command to fully empty the local DB before running the import

## 4.0.12 - 2024-12-11

### Updated
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "servd/craft-asset-storage",
"description": "Servd Asset Storage and Helpers integration for Craft CMS",
"version": "4.0.12",
"version": "4.0.13",
"type": "craft-plugin",
"keywords": [
"cms",
Expand Down
16 changes: 14 additions & 2 deletions src/console/controllers/LocalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class LocalController extends Controller
public $skipBackup = false;
public $skipDelete = false;
public $verbose = false;
public $emptyDatabase = false;

private $leaveOpen = true;
private $baseServdDomain = 'https://app.servd.host';
Expand All @@ -55,7 +56,8 @@ public function options($actionID): array
'servdKey',
'skipBackup',
'skipDelete',
'verbose'
'verbose',
'emptyDatabase'
]);
}

Expand Down Expand Up @@ -118,6 +120,10 @@ public function actionPullDatabase()
$this->backupDatabase();
}

if ($this->emptyDatabase) {
$this->stdout("--emptyDatabase is set. All existing tables and data will be deleted from the database `$localDatabase`" . PHP_EOL, Console::FG_RED);
}

//Final confirmation
if ($this->interactive && !$this->confirm('Ready to import. Do you want to proceed?')) {
$this->revertRemoteDatabaseConnectivity();
Expand All @@ -126,6 +132,13 @@ public function actionPullDatabase()

$this->stdout('Starting streaming database import' . PHP_EOL);

$localPasswordPrompt = empty($localPassword) ? "" : "-p\"$localPassword\"";
if ($this->emptyDatabase) {
$this->stdout('Deleting and recreating database.' . PHP_EOL);
$command = "mysql -h $localHost --port $localPort -u $localUser $localPasswordPrompt -e 'DROP DATABASE IF EXISTS $localDatabase; CREATE DATABASE $localDatabase;'";
$this->runCommand($command);
}

$skipColStat = '';

// Find out if mysqldump supports column-statistics
Expand All @@ -149,7 +162,6 @@ public function actionPullDatabase()
}

//Perform a direct stream from the remote db into the local
$localPasswordPrompt = empty($localPassword) ? "" : "-p\"$localPassword\"";
$command = "mysqldump $skipColStat --no-tablespaces --add-drop-table --quick --single-transaction --compress -h $remoteHost --port $remotePort -u $remoteUser -p\"$remotePassword\" $remoteDatabase | mysql -h $localHost --port $localPort -u $localUser $localPasswordPrompt $localDatabase";
$this->runCommand($command);

Expand Down

0 comments on commit 0d62730

Please sign in to comment.