Skip to content

Commit

Permalink
feat(configuration): added date_format option (#10)
Browse files Browse the repository at this point in the history
* Change Date Format

* Update Test, changed dateFormat name, php-cs-fixer

* Configuration Treewalk and Readme
  • Loading branch information
dirkdrutschmann committed Apr 18, 2023
1 parent af0302f commit 396a6f1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# DatabaseBackupBundle

DatabaseBackupBundle is a Symfony Bundle to manage your databases backup.
DatabaseBackupBundle is a Symfony Bundle to manage your databases backup.

[![Build](https://github.com/Symandy/DatabaseBackupBundle/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/Symandy/DatabaseBackupBundle/actions/workflows/build.yml)

Expand All @@ -10,11 +10,11 @@ DatabaseBackupBundle is a Symfony Bundle to manage your databases backup.
composer require symandy/database-backup-bundle
```

If [Symfony Flex](https://github.com/symfony/flex) is not enabled yet for the bundle (A PR on
If [Symfony Flex](https://github.com/symfony/flex) is not enabled yet for the bundle (A PR on
[symfony/recipes-contrib](https://github.com/symfony/recipes-contrib) will be submitted soon), add the following lines
to `config/bundles.php`.

```php
```php
<?php

return [
Expand All @@ -27,7 +27,7 @@ return [
## Configuration

### YAML configuration
As in the previous part, if Symfony Flex is not enabled, add the following file (`symandy_database_backup.yaml`)
As in the previous part, if Symfony Flex is not enabled, add the following file (`symandy_database_backup.yaml`)
to `config/packages` directory.

#### Basic configuration
Expand Down Expand Up @@ -65,11 +65,12 @@ symandy_database_backup:
# backup_directory: "/var/www/backups" # The directory must be created and must have the right permissions
backup_directory: "%kernel.project_dir%/backups"
# backup_directory: ~ # The current directory will be used if no value is passed
# date_format: 'Y-m-d' # will be used if no value is passed

bar:
# Use Doctrine database url env parameter
# Use Doctrine database url env parameter
connection:
url: "%env(DATABASE_URL)%" # url key will ALWAYS override array configuration
url: "%env(DATABASE_URL)%" # url key will ALWAYS override array configuration
configuration:
user: john # Overridden by url
```
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BackupDatabasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->comment("Backup for $database database has started");
}

$date = (new DateTime())->format('Y-m-d');
$date = (new DateTime())->format($backup->getStrategy()->getDateFormat() ?: 'Y-m-d');
$filePath = "$backupDirectory/$backupName-$database-$date.sql";

$process = Process::fromShellCommandline(
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->integerNode('max_files')->isRequired()->defaultNull()->end()
->scalarNode('backup_directory')->isRequired()->defaultNull()->end()
->scalarNode('date_format')->end()
->end()
->end()
->end()
Expand Down
8 changes: 7 additions & 1 deletion src/Model/Backup/Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Strategy
{
public function __construct(
private readonly ?int $maxFiles = null,
private readonly ?string $backupDirectory = null
private readonly ?string $backupDirectory = null,
private readonly ?string $dateFormat = 'Y-m-d'
) {
}

Expand All @@ -21,4 +22,9 @@ public function getBackupDirectory(): ?string
{
return $this->backupDirectory;
}

public function getDateFormat(): ?string
{
return $this->dateFormat;
}
}
56 changes: 56 additions & 0 deletions tests/Unit/BackupRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ public function testRegisterFromNameAndURL(): void
self::assertEquals($expectedBackup, $backupRegistry->get('backup-1'));
}

public function testRegisterFromNameAndURLandDateFormat(): void
{
$backupFactory = new BackupFactory(new ConnectionFactory(), new Factory(Strategy::class));
$backupRegistry = new BackupRegistry($backupFactory);

$options = [
'connection' => ['url' => 'mysql://user:password@hostname:9999/db_1'],
'strategy' => ['max_files' => 1, 'backup_directory' => '/path/to/backup/dir', 'date_format' => 'Y-m-d-h-i-s'],
];

$expectedBackup = new Backup(
'backup-1',
new MySQLConnection(user: 'user', password: 'password', host: 'hostname', port: 9999, databases: ['db_1']),
new Strategy(1, '/path/to/backup/dir', 'Y-m-d-h-i-s')
);

$backupRegistry->registerFromNameAndOptions('backup-1', $options);

self::assertCount(1, $backupRegistry->all());
self::assertTrue($backupRegistry->has('backup-1'));
self::assertEquals($expectedBackup, $backupRegistry->get('backup-1'));
}

public function testRegisterURLOverridesConfigurationArray(): void
{
$backupFactory = new BackupFactory(new ConnectionFactory(), new Factory(Strategy::class));
Expand Down Expand Up @@ -90,4 +113,37 @@ public function testRegisterURLOverridesConfigurationArray(): void
self::assertTrue($backupRegistry->has('backup-1'));
self::assertEquals($expectedBackup, $backupRegistry->get('backup-1'));
}

public function testRegisterURLOverridesConfigurationArrayAndDateFormat(): void
{
$backupFactory = new BackupFactory(new ConnectionFactory(), new Factory(Strategy::class));
$backupRegistry = new BackupRegistry($backupFactory);

$options = [
'connection' => [
'url' => 'mysql://user:password@hostname:9999/db_1',
'driver' => ConnectionDriver::MySQL,
'configuration' => [
'user' => 'user2',
'password' => 'secret',
'host' => 'remote',
'port' => 9998,
'databases' => ['db_2'],
],
],
'strategy' => ['max_files' => 1, 'backup_directory' => '/path/to/backup/dir', 'date_format' => 'Y-m-d-h-i-s'],
];

$expectedBackup = new Backup(
'backup-1',
new MySQLConnection(user: 'user', password: 'password', host: 'hostname', port: 9999, databases: ['db_1']),
new Strategy(1, '/path/to/backup/dir', 'Y-m-d-h-i-s')
);

$backupRegistry->registerFromNameAndOptions('backup-1', $options);

self::assertCount(1, $backupRegistry->all());
self::assertTrue($backupRegistry->has('backup-1'));
self::assertEquals($expectedBackup, $backupRegistry->get('backup-1'));
}
}

0 comments on commit 396a6f1

Please sign in to comment.