Skip to content

Commit

Permalink
Merge pull request #36 from mirko-pagliai/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mirko-pagliai authored Jan 18, 2019
2 parents e770753 + de4dd52 commit cbbf5db
Show file tree
Hide file tree
Showing 38 changed files with 343 additions and 671 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# 2.x branch
## 2.6 branch
### 2.6.1
* added `DriverTestCase::getMockForDriver()` method;
* `DriverTestCase::allRecords()` method renamed as `getAllRecords()`;
* many small code fixes;
* requires `me-tools` package for dev;
* removed `ConsoleIntegrationTestTrait`, because it is now sufficient to use the
same trait provided by `me-tools`;
* updated for `php-tools` 1.1.12.

### 2.6.0
* `BackupShell` has been replaced with console commands. Every method of the
previous class is now a `Command` class;
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ before_test:

install:
- cd c:\
- curl -fsS -o php.zip https://windows.php.net/downloads/releases/php-5.6.39-nts-Win32-VC11-x86.zip
- curl -fsS -o php.zip https://windows.php.net/downloads/releases/php-5.6.40-nts-Win32-VC11-x86.zip
- 7z x php.zip -oc:\php > nul
- cd c:\php
- copy php.ini-production php.ini
Expand All @@ -41,9 +41,9 @@ install:
- echo extension=php_pdo_pgsql.dll >> php.ini
- echo extension=php_pdo_sqlite.dll >> php.ini
- cd c:\
- appveyor DownloadFile https://www.sqlite.org/2018/sqlite-tools-win32-x86-3220000.zip -Filename sqlite3.zip
- appveyor DownloadFile https://www.sqlite.org/2018/sqlite-tools-win32-x86-3260000.zip -Filename sqlite3.zip
- 7z x sqlite3.zip > nul
- rename sqlite-tools-win32-x86-3220000 sqlite3
- rename sqlite-tools-win32-x86-3260000 sqlite3
- cd C:\projects\cakephp-database-backup
- appveyor DownloadFile https://getcomposer.org/composer.phar
- php composer.phar install --prefer-dist --no-interaction --ansi --no-progress
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"require": {
"php": ">=5.5.9",
"cakephp/cakephp": "^3.7",
"mirko-pagliai/php-tools": "^1.1"
"mirko-pagliai/php-tools": "^1.1.12"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^3.0",
"mirko-pagliai/me-tools": "^2.18.3",
"phpunit/phpunit": "^5.7|^6.0"
},
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

//Sets the redirect to `/dev/null`. This string can be concatenated to shell commands
if (!defined('REDIRECT_TO_DEV_NULL')) {
define('REDIRECT_TO_DEV_NULL', is_win() ? ' 2>nul' : ' 2>/dev/null');
define('REDIRECT_TO_DEV_NULL', IS_WIN ? ' 2>nul' : ' 2>/dev/null');
}

//Binaries
Expand Down Expand Up @@ -48,7 +48,7 @@

//Checks for the target directory
$target = Configure::read('DatabaseBackup.target');
safe_mkdir($target);
@mkdir($target);

if (!is_writeable($target)) {
trigger_error(sprintf('Directory %s not writeable', $target), E_USER_ERROR);
Expand Down
5 changes: 3 additions & 2 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
<rule ref="Generic.Commenting.Todo">
<severity>0</severity>
</rule>

<rule ref="Generic.PHP.NoSilencedErrors.Discouraged">
<severity>0</severity>
</rule>
<rule ref="Internal.NoCodeFound">
<severity>0</severity>
</rule>

<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>/webroot</exclude-pattern>
</rule>
Expand Down
11 changes: 2 additions & 9 deletions src/BackupTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ public function getCompression($filename)
//Gets the extension from the filename
$extension = $this->getExtension($filename);

if (!array_key_exists($extension, $this->getValidCompressions())) {
return false;
}

return $this->getValidCompressions()[$extension];
return array_key_exists($extension, $this->getValidCompressions()) ? $this->getValidCompressions()[$extension] : false;
}

/**
Expand All @@ -96,10 +92,7 @@ public function getDriver(ConnectionInterface $connection = null)
$connection = $connection ?: $this->getConnection();
$className = get_class_short_name($connection->getDriver());
$driver = App::classname(sprintf('%s.%s', 'DatabaseBackup', $className), 'Driver');

if (!$driver) {
throw new InvalidArgumentException(__d('database_backup', 'The `{0}` driver does not exist', $className));
}
is_true_or_fail($driver, __d('database_backup', 'The `{0}` driver does not exist', $className), InvalidArgumentException::class);

return new $driver($connection);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Command/DeleteAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class DeleteAllCommand extends Command
*/
protected function buildOptionParser(ConsoleOptionParser $parser)
{
$parser->setDescription(__d('database_backup', 'Deletes all database backups'));

return $parser;
return $parser->setDescription(__d('database_backup', 'Deletes all database backups'));
}

/**
Expand All @@ -48,18 +46,20 @@ public function execute(Arguments $args, ConsoleIo $io)
{
parent::execute($args, $io);

$deleted = (new BackupManager)->deleteAll();
$files = (new BackupManager)->deleteAll();

if (!$deleted) {
if (!$files) {
$io->verbose(__d('database_backup', 'No backup has been deleted'));

return null;
}

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

$io->success(__d('database_backup', 'Deleted backup files: {0}', count($deleted)));
$io->success(__d('database_backup', 'Deleted backup files: {0}', count($files)));

return null;
}
}
64 changes: 31 additions & 33 deletions src/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,29 @@ class ExportCommand extends Command
*/
protected function buildOptionParser(ConsoleOptionParser $parser)
{
$parser->setDescription(__d('database_backup', 'Exports a database backup'));
$parser->addOptions([
'compression' => [
'choices' => $this->getValidCompressions(),
'help' => __d('database_backup', 'Compression type. By default, no compression will be used'),
'short' => 'c',
],
'filename' => [
'help' => __d('database_backup', 'Filename. It can be an absolute path and may contain ' .
'patterns. The compression type will be automatically setted'),
'short' => 'f',
],
'rotate' => [
'help' => __d('database_backup', 'Rotates backups. You have to indicate the number of backups you ' .
'want to keep. So, it will delete all backups that are older. By default, no backup will be deleted'),
'short' => 'r',
],
'send' => [
'help' => __d('database_backup', 'Sends the backup file via email. You have ' .
'to indicate the recipient\'s email address'),
'short' => 's',
],
]);

return $parser;
return $parser->setDescription(__d('database_backup', 'Exports a database backup'))
->addOptions([
'compression' => [
'choices' => $this->getValidCompressions(),
'help' => __d('database_backup', 'Compression type. By default, no compression will be used'),
'short' => 'c',
],
'filename' => [
'help' => __d('database_backup', 'Filename. It can be an absolute path and may contain ' .
'patterns. The compression type will be automatically setted'),
'short' => 'f',
],
'rotate' => [
'help' => __d('database_backup', 'Rotates backups. You have to indicate the number of backups you ' .
'want to keep. So, it will delete all backups that are older. By default, no backup will be deleted'),
'short' => 'r',
],
'send' => [
'help' => __d('database_backup', 'Sends the backup file via email. You have ' .
'to indicate the recipient\'s email address'),
'short' => 's',
],
]);
}

/**
Expand Down Expand Up @@ -93,27 +91,27 @@ public function execute(Arguments $args, ConsoleIo $io)
//Exports
$file = $instance->export();
$io->success(__d('database_backup', 'Backup `{0}` has been exported', rtr($file)));
$verbose = $args->getOption('verbose');
$quiet = $args->getOption('quiet');

//Sends via email
if ($args->hasOption('send')) {
$SendCommand = new SendCommand;
$sendArgs = new Arguments(
$SendCommand->execute(new Arguments(
[$file, $args->getOption('send')],
['verbose' => $args->getOption('verbose'), 'quiet' => $args->getOption('quiet')],
compact('verbose', 'quiet'),
$SendCommand->getOptionParser()->argumentNames()
);
$SendCommand->execute($sendArgs, $io);
), $io);
}

//Rotates
if ($args->hasOption('rotate')) {
$RotateCommand = new RotateCommand;
$rotateArgs = new Arguments(
$RotateCommand->execute(new Arguments(
[$args->getOption('rotate')],
['verbose' => $args->getOption('verbose'), 'quiet' => $args->getOption('quiet')],
compact('verbose', 'quiet'),
$RotateCommand->getOptionParser()->argumentNames()
);
$RotateCommand->execute($rotateArgs, $io);
), $io);
}
} catch (Exception $e) {
$io->error($e->getMessage());
Expand Down
13 changes: 5 additions & 8 deletions src/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ class ImportCommand extends Command
*/
protected function buildOptionParser(ConsoleOptionParser $parser)
{
$parser->setDescription(__d('database_backup', 'Imports a database backup'));
$parser->addArgument('filename', [
'help' => __d('database_backup', 'Filename. It can be an absolute path'),
'required' => true,
]);

return $parser;
return $parser->setDescription(__d('database_backup', 'Imports a database backup'))
->addArgument('filename', [
'help' => __d('database_backup', 'Filename. It can be an absolute path'),
'required' => true,
]);
}

/**
Expand All @@ -56,7 +54,6 @@ public function execute(Arguments $args, ConsoleIo $io)

try {
$file = (new BackupImport)->filename($args->getArgument('filename'))->import();

$io->success(__d('database_backup', 'Backup `{0}` has been imported', rtr($file)));
} catch (Exception $e) {
$io->error($e->getMessage());
Expand Down
20 changes: 7 additions & 13 deletions src/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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 All @@ -32,9 +33,7 @@ class IndexCommand extends Command
*/
protected function buildOptionParser(ConsoleOptionParser $parser)
{
$parser->setDescription(__d('database_backup', 'Lists database backups'));

return $parser;
return $parser->setDescription(__d('database_backup', 'Lists database backups'));
}

/**
Expand All @@ -51,29 +50,24 @@ public function execute(Arguments $args, ConsoleIo $io)

//Gets all backups
$backups = (new BackupManager)->index();

$io->out(__d('database_backup', 'Backup files found: {0}', $backups->count()));

if ($backups->isEmpty()) {
return null;
}

//Parses backups
$backups = $backups->map(function ($backup) {
$backup->size = Number::toReadableSize($backup->size);

return array_values($backup->toArray());
})->toList();

//Table headers
$headers = [
__d('database_backup', 'Filename'),
__d('database_backup', 'Extension'),
__d('database_backup', 'Compression'),
__d('database_backup', 'Size'),
__d('database_backup', 'Datetime'),
];
$cells = $backups->map(function (Entity $backup) {
return $backup->set('size', Number::toReadableSize($backup->size))->toArray();
});
$io->helper('table')->output(array_merge([$headers], $cells->toList()));

$io->helper('table')->output(array_merge([$headers], $backups));
return null;
}
}
22 changes: 11 additions & 11 deletions src/Command/RotateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ class RotateCommand extends Command
*/
protected function buildOptionParser(ConsoleOptionParser $parser)
{
$parser->setDescription(__d('database_backup', 'Rotates backups'));
$parser->addArgument('keep', [
'help' => __d('database_backup', 'Number of backups you want to keep. So, it will delete all backups that are older'),
'required' => true,
]);

return $parser;
return $parser->setDescription(__d('database_backup', 'Rotates backups'))
->addArgument('keep', [
'help' => __d('database_backup', 'Number of backups you want to keep. So, it will delete all backups that are older'),
'required' => true,
]);
}

/**
Expand All @@ -58,22 +56,24 @@ public function execute(Arguments $args, ConsoleIo $io)

try {
//Gets deleted files
$deleted = (new BackupManager)->rotate($args->getArgument('keep'));
$files = (new BackupManager)->rotate($args->getArgument('keep'));

if (empty($deleted)) {
if (!$files) {
$io->verbose(__d('database_backup', 'No backup has been deleted'));

return null;
}

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

$io->success(__d('database_backup', 'Deleted backup files: {0}', count($deleted)));
$io->success(__d('database_backup', 'Deleted backup files: {0}', count($files)));
} catch (Exception $e) {
$io->error($e->getMessage());
$this->abort();
}

return null;
}
}
Loading

0 comments on commit cbbf5db

Please sign in to comment.