Skip to content

Commit

Permalink
Merge pull request #98 from mirko-pagliai/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mirko-pagliai authored Oct 25, 2023
2 parents 527328a + 88070ea commit 90ac332
Show file tree
Hide file tree
Showing 29 changed files with 271 additions and 124 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1']
php-version: ['7.4', '8.0', '8.1', '8.2']
db-type: [sqlite, mysql, pgsql]
prefer-lowest: ['']
exclude:
Expand All @@ -30,6 +30,9 @@ jobs:
- php-version: '8.1'
db-type: 'mysql'
prefer-lowest: 'prefer-lowest'
- php-version: '8.2'
db-type: 'mysql'
prefer-lowest: 'prefer-lowest'

steps:
- name: Setup Database
Expand All @@ -53,7 +56,7 @@ jobs:

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
Expand Down Expand Up @@ -93,7 +96,7 @@ jobs:
- name: Submit code coverage
if: matrix.php-version == '8.0'
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v3

cs-stan:
name: Coding Standard & Static Analysis
Expand All @@ -105,7 +108,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
coverage: none
tools: cs2pr

Expand All @@ -126,13 +129,13 @@ jobs:
if: success() || failure()
env:
db_dsn: 'sqlite:///:memory:'
run: vendor/bin/psalm.phar --output-format=github --php-version=8.0
run: vendor/bin/psalm --output-format=github --php-version=8.1

- name: Run phpstan
if: success() || failure()
env:
db_dsn: 'sqlite:///:memory:'
run: vendor/bin/phpstan.phar analyse --error-format=github
run: vendor/bin/phpstan analyse --error-format=github


testsuite-windows:
Expand Down Expand Up @@ -161,7 +164,7 @@ jobs:

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.directory
.idea
.phpunit.result.cache
.phpunit.*cache
*_old
*.old.*
composer.lock
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# 2.x branch
## 2.12 branch
### 2.12.2
* improved and fixed a bug for `ExportCommand` and `ImportCommand`, in handling some exceptions;
* it no longer needs the `me-tools` package. This removes several (useless) dependencies;
* some, possible changes that prepare it for CakePHP 5 and PHPUnit 10 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97));
* little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97));
* improved `BackuManager::index()` method, also regarding the correct files sorting. This also solves a small bug for
the `rotate()` method (which precisely affects `index()`). The `index()` method now returns a collection of arrays (
and no longer a collection of `Entity`);
* some testing methods that have been missing for a long time have been added;
* the `BackupTrait::getDriverName()` method can no longer be static;
* removed (old and useless) `BaseCommandTestCase` class;
* added tests for PHP 8.2.

### 2.12.1
* fixed a little bug in the `bootstrap.php` file;
* the `Exceptionist` class provided by me-tools is no longer used (in anticipation of an upcoming deprecation).
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=7.4.0",
"ext-fileinfo": "*",
"cakephp/cakephp": "^4.0",
"mirko-pagliai/php-tools": "~1.7.4"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^4.4",
"cakephp/migrations": "^3.2",
"mirko-pagliai/me-tools": "^2.20.9",
"phpunit/phpunit": "^9.1|^9.5",
"phpstan/phpstan": "^1.7",
"psalm/phar": "^4.24"
"phpstan/phpstan": "^1.10.38",
"vimeo/psalm": "^5.15.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
use Cake\Core\Configure;
use Symfony\Component\Process\ExecutableFinder;

/**
* @todo remove the `file_exists()` on CakePHP >= 5
*/
if (file_exists(CAKE . 'functions.php')) {
require CAKE . 'functions.php';
}

/**
* Executables. Name of driver as keys, Then, as value, an array that contains
* first the executable to export and then the executable to import backups.
Expand Down
5 changes: 0 additions & 5 deletions php-doc-maker.xml

This file was deleted.

1 change: 0 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ parameters:
message: "#^Expression \"\\$this\\-\\>BackupExport\\-\\>noExistingProperty\" on a separate line does not do anything\\.$#"
count: 1
path: tests/TestCase/Utility/BackupExportAndImportTest.php

5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@
<code>empty($this-&gt;Driver)</code>
</TypeDoesNotContainType>
</file>
<file src="src/Utility/BackupManager.php">
<TooManyTemplateParams occurrences="1">
<code>\Cake\Collection\CollectionInterface&lt;\Cake\ORM\Entity&gt;</code>
</TooManyTemplateParams>
</file>
</files>
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
autoloader="tests/bootstrap.php"
errorBaseline="psalm-baseline.xml"
>
findUnusedBaselineEntry="true"
findUnusedCode="false">
<projectFiles>
<directory name="src" />
<ignoreFiles>
Expand Down
4 changes: 2 additions & 2 deletions src/BackupTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public static function getConnection(?string $name = null): ConnectionInterface
* @throws \ReflectionException
* @since 2.9.2
*/
public static function getDriverName(): string
public function getDriverName(): string
{
return get_class_short_name(self::getConnection()->getDriver());
return get_class_short_name($this->getConnection()->getDriver());
}

/**
Expand Down
19 changes: 13 additions & 6 deletions src/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\Exception\StopException;
use Cake\Core\Configure;
use DatabaseBackup\Console\Command;
use DatabaseBackup\Utility\BackupExport;
Expand Down Expand Up @@ -66,6 +67,15 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
]);
}

/**
* Internal method to get a `BackupExport` instance
* @return \DatabaseBackup\Utility\BackupExport
*/
protected function getBackupExport(): BackupExport
{
return new BackupExport();
}

/**
* Exports a database backup.
*
Expand All @@ -81,7 +91,7 @@ public function execute(Arguments $args, ConsoleIo $io): void
parent::execute($args, $io);

try {
$BackupExport = new BackupExport();
$BackupExport = $this->getBackupExport();

//Sets the output filename or the compression type. Regarding the `rotate` option, the
//`BackupShell::rotate()` method will be called at the end, instead of `BackupExport::rotate()`
Expand All @@ -95,11 +105,9 @@ public function execute(Arguments $args, ConsoleIo $io): void
$BackupExport->timeout((int)$args->getOption('timeout'));
}

/** @var string $file */
$file = $BackupExport->export();
if (!$file) {
$io->error(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeExport'));
$this->abort();
throw new StopException(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeExport'));
}
$io->success(__d('database_backup', 'Backup `{0}` has been exported', rtr($file)));

Expand All @@ -112,8 +120,7 @@ public function execute(Arguments $args, ConsoleIo $io): void
$this->executeCommand(RotateCommand::class, array_merge([(string)$args->getOption('rotate')], $extraOptions), $io);
}
} catch (Exception $e) {
$io->error($e->getMessage());
$this->abort();
$io->abort($e->getMessage());
}
}
}
19 changes: 13 additions & 6 deletions src/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\Exception\StopException;
use Cake\Core\Configure;
use DatabaseBackup\Console\Command;
use DatabaseBackup\Utility\BackupImport;
Expand Down Expand Up @@ -47,6 +48,15 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
]);
}

/**
* Internal method to get a `BackupImport` instance
* @return \DatabaseBackup\Utility\BackupImport
*/
protected function getBackupImport(): BackupImport
{
return new BackupImport();
}

/**
* Imports a database backup
* @param \Cake\Console\Arguments $args The command arguments
Expand All @@ -59,7 +69,7 @@ public function execute(Arguments $args, ConsoleIo $io): void
parent::execute($args, $io);

try {
$BackupImport = new BackupImport();
$BackupImport = $this->getBackupImport();

$BackupImport->filename((string)$args->getArgument('filename'));

Expand All @@ -68,16 +78,13 @@ public function execute(Arguments $args, ConsoleIo $io): void
$BackupImport->timeout((int)$args->getOption('timeout'));
}

/** @var string $file */
$file = $BackupImport->import();
if (!$file) {
$io->error(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeImport'));
$this->abort();
throw new StopException(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeImport'));
}
$io->success(__d('database_backup', 'Backup `{0}` has been imported', rtr($file)));
} catch (Exception $e) {
$io->error($e->getMessage());
$this->abort();
$io->abort($e->getMessage());
}
}
}
11 changes: 5 additions & 6 deletions src/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\I18n\Number;
use Cake\ORM\Entity;
use DatabaseBackup\Console\Command;
use DatabaseBackup\Utility\BackupManager;

Expand Down Expand Up @@ -63,11 +62,11 @@ public function execute(Arguments $args, ConsoleIo $io): void
__d('database_backup', 'Size'),
__d('database_backup', 'Datetime'),
];
$cells = $backups->map(fn(Entity $backup): array => $backup
->set('compression', $backup->get('compression') ?: '')
->set('datetime', $backup->get('datetime')->nice())
->set('size', Number::toReadableSize($backup->get('size')))
->toArray());
$cells = $backups->map(fn(array $backup): array => [
'compression' => $backup['compression'] ?: '',
'datetime' => $backup['datetime']->nice(),
'size' => Number::toReadableSize($backup['size']),
] + $backup);
$io->helper('table')->output(array_merge([$headers], $cells->toList()));
}
}
2 changes: 1 addition & 1 deletion src/Command/RotateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function execute(Arguments $args, ConsoleIo $io): void
}

foreach ($files as $file) {
$io->verbose(__d('database_backup', 'Backup `{0}` has been deleted', $file->get('filename')));
$io->verbose(__d('database_backup', 'Backup `{0}` has been deleted', $file['filename']));
}

$io->success(__d('database_backup', 'Deleted backup files: {0}', count($files)));
Expand Down
7 changes: 3 additions & 4 deletions src/Command/SendCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
class SendCommand extends Command
{
/**
* Hook method for defining this command's option parser
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
* @return \Cake\Console\ConsoleOptionParser
* @inheritDoc
*/
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
Expand All @@ -53,7 +51,8 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
* @param \Cake\Console\Arguments $args The command arguments
* @param \Cake\Console\ConsoleIo $io The console io
* @return void
* @throws \Cake\Console\Exception\StopException|\ReflectionException
* @throws \Cake\Console\Exception\StopException
* @throws \ReflectionException
*/
public function execute(Arguments $args, ConsoleIo $io): void
{
Expand Down
5 changes: 4 additions & 1 deletion src/Driver/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ final public function implementedEvents(): array
*/
private function getExecutable(string $type): string
{
// @codeCoverageIgnoreStart
if (!in_array($type, ['export', 'import'])) {
throw new LogicException(__d('database_backup', '`$type` parameter should be `export` or `import`'));
}
$driverName = strtolower(self::getDriverName());
// @codeCoverageIgnoreEnd
$driverName = strtolower($this->getDriverName());
$replacements = [
'{{BINARY}}' => escapeshellarg($this->getBinary(DATABASE_BACKUP_EXECUTABLES[$driverName][$type])),
'{{AUTH_FILE}}' => method_exists($this, 'getAuthFilePath') && $this->getAuthFilePath() ? escapeshellarg($this->getAuthFilePath()) : '',
Expand All @@ -81,6 +83,7 @@ private function getExecutable(string $type): string
'{{DB_HOST}}' => $this->getConfig('host'),
'{{DB_NAME}}' => $this->getConfig('database'),
];
/** @var string $exec */
$exec = Configure::readOrFail('DatabaseBackup.' . $driverName . '.' . $type);

return str_replace(array_keys($replacements), $replacements, $exec);
Expand Down
8 changes: 5 additions & 3 deletions src/Driver/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ public function beforeImport(): bool

//Drops each table
foreach ($schemaCollection->listTables() as $table) {
array_map([$connection, 'execute'], $schemaCollection->describe($table)->dropSql($connection));
/** @var \Cake\Database\Schema\TableSchema $TableSchema */
$TableSchema = $schemaCollection->describe($table);
array_map([$connection, 'execute'], $TableSchema->dropSql($connection));
}

//Needs disconnect and re-connect because the database schema has changed
$connection->disconnect();
$connection->connect();
$connection->getDriver()->disconnect();
$connection->getDriver()->connect();

return true;
}
Expand Down
Loading

0 comments on commit 90ac332

Please sign in to comment.