Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Mar 14, 2024
1 parent 12bfa8a commit 6812e99
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/config/GeneralConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5820,7 +5820,7 @@ public function resourceBaseUrl(string $value): self
* ```
*
* @group Environment
* @param string|null|false $value
* @param string|null|false|array $value
* @return self
* @see $restoreCommand
* @since 4.2.0
Expand Down
12 changes: 12 additions & 0 deletions src/db/BackupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace craft\db;

use Closure;

class BackupCommand
{
public ?array $ignoreTables = null;
public bool $archiveFormat = false;
public ?Closure $callback = null;
}
13 changes: 9 additions & 4 deletions src/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use craft\db\mysql\Schema as MysqlSchema;
use craft\db\pgsql\QueryBuilder as PgsqlQueryBuilder;
use craft\db\pgsql\Schema as PgsqlSchema;
use craft\elements\actions\Restore;
use craft\errors\DbConnectException;
use craft\errors\ShellCommandException;
use craft\events\BackupEvent;
Expand Down Expand Up @@ -274,9 +275,8 @@ public function backupTo(string $filePath): void
if ($backupCommand === null || is_array($backupCommand)) {
/** @var PgsqlSchema|MysqlSchema $schema */
$schema = $this->getSchema();
$schema->backupCommandOptions = is_array($backupCommand)
? $backupCommand
: null;
$schema->backupCommand = Craft::createObject(BackupCommand::class);
Craft::configure($schema->backupCommand, $backupCommand ?? []);

$backupCommand = $this->getSchema()->getDefaultBackupCommand($event->ignoreTables);
}
Expand Down Expand Up @@ -343,7 +343,12 @@ public function restore(string $filePath): void
// Determine the command that should be executed
$restoreCommand = Craft::$app->getConfig()->getGeneral()->restoreCommand;

if ($restoreCommand === null) {
if ($restoreCommand === null || is_array($restoreCommand)) {
/** @var PgsqlSchema|MysqlSchema $schema */
$schema = $this->getSchema();
$schema->restoreCommand = Craft::createObject(RestoreCommand::class);
Craft::configure($schema->restoreCommand, $restoreCommand ?? []);

$restoreCommand = $this->getSchema()->getDefaultRestoreCommand();
}

Expand Down
11 changes: 11 additions & 0 deletions src/db/RestoreCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace craft\db;

use Closure;

class RestoreCommand
{
public bool $archiveFormat = false;
public ?Closure $callback = null;
}
24 changes: 12 additions & 12 deletions src/db/mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

use Composer\Util\Platform;
use Craft;
use craft\db\BackupCommand;
use craft\db\Connection;
use craft\db\RestoreCommand;
use craft\db\TableSchema;
use craft\helpers\App;
use craft\helpers\Db;
Expand Down Expand Up @@ -52,14 +54,14 @@ class Schema extends \yii\db\mysql\Schema
public ?string $tempMyCnfPath = null;

/**
* @var array
* @var BackupCommand|null
*/
public array $backupCommandOptions = [];
public ?BackupCommand $backupCommand = null;

/**
* @var array
* @var RestoreCommand|null
*/
public array $restoreCommandOptions = [];
public ?RestoreCommand $restoreCommand = null;

/**
* @inheritdoc
Expand Down Expand Up @@ -171,7 +173,7 @@ protected function getSupportsColumnStatistics(): bool

$success = $shellCommand->execute();

// if there was output, then column-statistics is supported and we should disable it
// if there was output, then column-statistics is supported
return $success && $shellCommand->getOutput();
}

Expand All @@ -185,9 +187,8 @@ protected function getSupportsColumnStatistics(): bool
public function getDefaultBackupCommand(?array $ignoreTables = null): string
{
$ignoreTables = $ignoreTables
?? $this->backupCommandOptions['ignoreTables']
?? $this->backupCommand?->ignoreTables
?? $this->db->getIgnoredBackupTables();
$callback = $this->backupCommandOptions['callback'] ?? null;
$useSingleTransaction = true;
$serverVersion = App::normalizeVersion($this->getServerVersion());

Expand Down Expand Up @@ -222,8 +223,8 @@ public function getDefaultBackupCommand(?array $ignoreTables = null): string
$shellCommand->addArg('--column-statistics=', '0');
}

if ($callback) {
$shellCommand = $callback($shellCommand);
if ($this->backupCommand->callback) {
$shellCommand = ($this->backupCommand->callback)($shellCommand);
}

$schemaDump = (clone $shellCommand)
Expand Down Expand Up @@ -256,13 +257,12 @@ public function getDefaultBackupCommand(?array $ignoreTables = null): string
public function getDefaultRestoreCommand(): string
{
$shellCommand = new ShellCommand('mysql');
$callback = $this->restoreCommandOptions['callback'] ?? null;

$shellCommand->addArg('--defaults-file=', $this->_createDumpConfigFile());
$shellCommand->addArg('{database}');

if ($callback) {
$shellCommand = $callback($shellCommand);
if ($this->restoreCommand->callback) {
$shellCommand = ($this->restoreCommand->callback)($shellCommand);
}

return $shellCommand->getExecCommand() . ' < "{file}"';
Expand Down
32 changes: 15 additions & 17 deletions src/db/pgsql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

use Composer\Util\Platform;
use Craft;
use craft\db\BackupCommand;
use craft\db\Connection;
use craft\db\RestoreCommand;
use craft\db\TableSchema;
use mikehaertl\shellcommand\Command as ShellCommand;
use yii\db\Exception;
Expand All @@ -29,14 +31,14 @@ class Schema extends \yii\db\pgsql\Schema
public int $maxObjectNameLength = 63;

/**
* @var array
* @var BackupCommand|null
*/
public array $backupCommandOptions = [];
public ?BackupCommand $backupCommand = null;

/**
* @var array
* @var RestoreCommand|null
*/
public array $restoreCommandOptions = [];
public ?RestoreCommand $restoreCommand = null;

/**
* Creates a query builder for the database.
Expand Down Expand Up @@ -128,18 +130,16 @@ public function getLastInsertID($sequenceName = ''): string
public function getDefaultBackupCommand(?array $ignoreTables = null): string
{
$shellCommand = new ShellCommand('pg_dump');
$archiveFormat = $this->backupCommandOptions['archiveFormat'] ?? false;
$ignoreTables = $ignoreTables
?? $this->backupCommandOptions['ignoreTables']
?? $this->backupCommand->ignoreTables
?? $this->db->getIgnoredBackupTables();
$callback = $this->backupCommandOptions['callback'] ?? null;

foreach ($ignoreTables as $table) {
$table = $this->getRawTableName($table);
$shellCommand->addArg('--exclude-table-data', "{schema}.$table");
}

if ($archiveFormat) {
if ($this->backupCommand->archiveFormat) {
$shellCommand->addArg('--format=', 'custom');
}

Expand All @@ -156,8 +156,8 @@ public function getDefaultBackupCommand(?array $ignoreTables = null): string
->addArg('--file=', '{file}')
->addArg('--schema=', '{schema}');

if ($callback) {
$shellCommand = $callback($shellCommand);
if ($this->backupCommand->callback) {
$shellCommand = ($this->backupCommand->callback)($shellCommand);
}

return $this->_pgpasswordCommand() . $shellCommand->getExecCommand();
Expand All @@ -170,27 +170,25 @@ public function getDefaultBackupCommand(?array $ignoreTables = null): string
*/
public function getDefaultRestoreCommand(): string
{
$archiveFormat = $this->restoreCommandOptions['archiveFormat'] ?? false;
$shellCommand = new ShellCommand($archiveFormat ? 'pg_restore' : 'psql');
$callback = $this->backupCommandOptions['callback'] ?? null;
$shellCommand = new ShellCommand($this->restoreCommand->archiveFormat ? 'pg_restore' : 'psql');

$shellCommand->addArg('--dbname=', '{database}');
$shellCommand->addArg('--host=', '{server}');
$shellCommand->addArg('--port=', '{port}');
$shellCommand->addArg('--username=', '{user}');
$shellCommand->addArg('--no-password');

if ($archiveFormat) {
if ($this->restoreCommand->archiveFormat) {
$shellCommand->addArg('--file=', '{file}');
}

if ($callback) {
$shellCommand = $callback($shellCommand);
if ($this->backupCommand->callback) {
$shellCommand = ($this->backupCommand->callback)($shellCommand);
}

return $this->_pgpasswordCommand()
. $shellCommand->getExecCommand()
. $archiveFormat ? '' : '< "{file}"';
. $this->restoreCommand->archiveFormat ? '' : '< "{file}"';
}

/**
Expand Down

0 comments on commit 6812e99

Please sign in to comment.