Skip to content

Commit 51c10e2

Browse files
bastien70jmsche
andauthored
Added backup options when creating/updating database (#70)
* Added backup options when creating database * Added Database test for getBackupOptions method * Updated database-create-en doc image * Update migrations/Version20220729140330.php Co-authored-by: jmsche <[email protected]> * Update migrations/Version20220729140330.php Co-authored-by: jmsche <[email protected]> * Update migrations/Version20220729140330.php Co-authored-by: jmsche <[email protected]> * Update src/Service/BackupService.php Co-authored-by: jmsche <[email protected]> * Updated translations * Removed useless addRow in CRUD * Updated addDropTable option default value to false * Updated DatabaseTest * Recreated migrations file, moved options to embedded class * Added translations in DatabaseCrudController * Update migrations/Version20220801084444.php Co-authored-by: jmsche <[email protected]> * Update Options.php Updated Options methods return types * Resolved conflicts Co-authored-by: jmsche <[email protected]>
1 parent b8abec6 commit 51c10e2

File tree

10 files changed

+314
-3
lines changed

10 files changed

+314
-3
lines changed

docs/images/database-create-en.png

59 KB
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
final class Version20220801084444 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Add options for backups';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql('ALTER TABLE `database` ADD options_reset_auto_increment TINYINT(1) DEFAULT FALSE NOT NULL, ADD options_add_drop_database TINYINT(1) DEFAULT FALSE NOT NULL, ADD options_add_drop_table TINYINT(1) DEFAULT FALSE NOT NULL, ADD options_add_drop_trigger TINYINT(1) DEFAULT TRUE NOT NULL, ADD options_add_locks TINYINT(1) DEFAULT TRUE NOT NULL, ADD options_complete_insert TINYINT(1) DEFAULT FALSE NOT NULL');
20+
}
21+
22+
public function down(Schema $schema): void
23+
{
24+
$this->addSql('ALTER TABLE `database` DROP options_reset_auto_increment, DROP options_add_drop_database, DROP options_add_drop_table, DROP options_add_drop_trigger, DROP options_add_locks, DROP options_complete_insert');
25+
}
26+
27+
public function isTransactional(): bool
28+
{
29+
return false;
30+
}
31+
}

src/Controller/Admin/DatabaseCrudController.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
2929
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
3030
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
31+
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
3132
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
3233
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
3334
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
@@ -274,6 +275,33 @@ public function configureFields(string $pageName): iterable
274275
])
275276
->hideOnForm();
276277

278+
if (Crud::PAGE_INDEX !== $pageName) {
279+
yield FormField::addPanel('database.panel.backup_options', 'fas fa-gear');
280+
281+
yield BooleanField::new('options.resetAutoIncrement', 'database.field.options.reset_auto_increment')
282+
->renderAsSwitch(false)
283+
->setColumns(6);
284+
yield BooleanField::new('options.addDropDatabase', 'database.field.options.add_drop_database')
285+
->renderAsSwitch(false)
286+
->setColumns(6);
287+
288+
yield FormField::addRow();
289+
yield BooleanField::new('options.addDropTable', 'database.field.options.add_drop_table')
290+
->renderAsSwitch(false)
291+
->setColumns(6);
292+
yield BooleanField::new('options.addDropTrigger', 'database.field.options.add_drop_trigger')
293+
->renderAsSwitch(false)
294+
->setColumns(6);
295+
296+
yield FormField::addRow();
297+
yield BooleanField::new('options.addLocks', 'database.field.options.add_locks')
298+
->renderAsSwitch(false)
299+
->setColumns(6);
300+
yield BooleanField::new('options.completeInsert', 'database.field.options.complete_insert')
301+
->renderAsSwitch(false)
302+
->setColumns(6);
303+
}
304+
277305
if (Crud::PAGE_INDEX !== $pageName) {
278306
yield FormField::addPanel('database.panel.task_configuration', 'fa-solid fa-calendar');
279307
yield IntegerField::new('backupTask.periodicityNumber', 'database.field.backup_task.periodicity_number')

src/Entity/Database.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Entity;
66

77
use App\Entity\Embed\BackupTask;
8+
use App\Entity\Embed\Options;
89
use App\Entity\Traits\PrimaryKeyTrait;
910
use App\Repository\DatabaseRepository;
1011
use DateTimeImmutable;
@@ -75,9 +76,13 @@ class Database implements \Stringable
7576
#[ORM\Embedded(class: BackupTask::class)]
7677
private BackupTask $backupTask;
7778

79+
#[ORM\Embedded(class: Options::class)]
80+
private Options $options;
81+
7882
public function __construct()
7983
{
8084
$this->backupTask = new BackupTask();
85+
$this->options = new Options();
8186
$this->backups = new ArrayCollection();
8287
$this->createdAt = new DateTimeImmutable();
8388
}
@@ -294,6 +299,35 @@ public function setAdapter(?AdapterConfig $adapter): self
294299
return $this;
295300
}
296301

302+
/**
303+
* Returns an array of options for the mysqldump command.
304+
*
305+
* @return array{add-drop-table: bool, add-drop-database: bool, add-drop-trigger: bool, add-locks: bool, complete-insert: bool, reset-auto-increment: bool}
306+
*/
307+
public function getBackupOptions(): array
308+
{
309+
return [
310+
'add-drop-table' => $this->options->isAddDropTable(),
311+
'add-drop-database' => $this->options->isAddDropDatabase(),
312+
'add-drop-trigger' => $this->options->isAddDropTrigger(),
313+
'add-locks' => $this->options->isAddLocks(),
314+
'complete-insert' => $this->options->isCompleteInsert(),
315+
'reset-auto-increment' => $this->options->isResetAutoIncrement(),
316+
];
317+
}
318+
319+
public function getOptions(): Options
320+
{
321+
return $this->options;
322+
}
323+
324+
public function setOptions(Options $options): self
325+
{
326+
$this->options = $options;
327+
328+
return $this;
329+
}
330+
297331
public function getBackupTask(): BackupTask
298332
{
299333
return $this->backupTask;

src/Entity/Embed/Options.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Entity\Embed;
6+
7+
use Doctrine\DBAL\Types\Types;
8+
use Doctrine\ORM\Mapping as ORM;
9+
10+
#[ORM\Embeddable]
11+
class Options
12+
{
13+
#[ORM\Column(type: Types::BOOLEAN)]
14+
private bool $resetAutoIncrement = false;
15+
16+
#[ORM\Column(type: Types::BOOLEAN)]
17+
private bool $addDropDatabase = false;
18+
19+
#[ORM\Column(type: Types::BOOLEAN)]
20+
private bool $addDropTable = false;
21+
22+
#[ORM\Column(type: Types::BOOLEAN)]
23+
private bool $addDropTrigger = true;
24+
25+
#[ORM\Column(type: Types::BOOLEAN)]
26+
private bool $addLocks = true;
27+
28+
#[ORM\Column(type: Types::BOOLEAN)]
29+
private bool $completeInsert = false;
30+
31+
public function isResetAutoIncrement(): bool
32+
{
33+
return $this->resetAutoIncrement;
34+
}
35+
36+
public function setResetAutoIncrement(bool $resetAutoIncrement): self
37+
{
38+
$this->resetAutoIncrement = $resetAutoIncrement;
39+
40+
return $this;
41+
}
42+
43+
public function isAddDropDatabase(): bool
44+
{
45+
return $this->addDropDatabase;
46+
}
47+
48+
public function setAddDropDatabase(bool $addDropDatabase): self
49+
{
50+
$this->addDropDatabase = $addDropDatabase;
51+
52+
return $this;
53+
}
54+
55+
public function isAddDropTable(): bool
56+
{
57+
return $this->addDropTable;
58+
}
59+
60+
public function setAddDropTable(bool $addDropTable): self
61+
{
62+
$this->addDropTable = $addDropTable;
63+
64+
return $this;
65+
}
66+
67+
public function isAddDropTrigger(): bool
68+
{
69+
return $this->addDropTrigger;
70+
}
71+
72+
public function setAddDropTrigger(bool $addDropTrigger): self
73+
{
74+
$this->addDropTrigger = $addDropTrigger;
75+
76+
return $this;
77+
}
78+
79+
public function isAddLocks(): bool
80+
{
81+
return $this->addLocks;
82+
}
83+
84+
public function setAddLocks(bool $addLocks): self
85+
{
86+
$this->addLocks = $addLocks;
87+
88+
return $this;
89+
}
90+
91+
public function isCompleteInsert(): bool
92+
{
93+
return $this->completeInsert;
94+
}
95+
96+
public function setCompleteInsert(bool $completeInsert): self
97+
{
98+
$this->completeInsert = $completeInsert;
99+
100+
return $this;
101+
}
102+
}

src/Service/BackupService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ private function defineMysqlDumpObject(Database $database): Mysqldump
163163
$database->getDsn(),
164164
$database->getUser(),
165165
$this->encryptor->decrypt($database->getPassword()),
166-
[
167-
'add-drop-table' => true,
168-
]
166+
$database->getBackupOptions(),
169167
);
170168
}
171169
}

tests/Entity/DatabaseTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,47 @@ public function testAdapter(): void
160160
$entity->setAdapter($adapter);
161161
self::assertSame($adapter, $entity->getAdapter());
162162
}
163+
164+
public function testOptions(): void
165+
{
166+
$entity = new Database();
167+
self::assertNotNull($entity->getOptions());
168+
}
169+
170+
public function testGetBackupOptions(): void
171+
{
172+
$entity = new Database();
173+
174+
self::assertSame(
175+
[
176+
'add-drop-table' => false,
177+
'add-drop-database' => false,
178+
'add-drop-trigger' => true,
179+
'add-locks' => true,
180+
'complete-insert' => false,
181+
'reset-auto-increment' => false,
182+
],
183+
$entity->getBackupOptions()
184+
);
185+
186+
$options = $entity->getOptions();
187+
$options->setAddDropTable(true)
188+
->setAddDropDatabase(true)
189+
->setAddDropTrigger(false)
190+
->setAddLocks(false)
191+
->setCompleteInsert(true)
192+
->setResetAutoIncrement(true);
193+
194+
self::assertSame(
195+
[
196+
'add-drop-table' => true,
197+
'add-drop-database' => true,
198+
'add-drop-trigger' => false,
199+
'add-locks' => false,
200+
'complete-insert' => true,
201+
'reset-auto-increment' => true,
202+
],
203+
$entity->getBackupOptions()
204+
);
205+
}
163206
}

tests/Entity/Embed/OptionsTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Entity\Embed;
6+
7+
use App\Entity\Embed\Options;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class OptionsTest extends TestCase
11+
{
12+
public function testResetAutoIncrement(): void
13+
{
14+
$options = new Options();
15+
self::assertFalse($options->isResetAutoIncrement());
16+
$options->setResetAutoIncrement(true);
17+
self::assertTrue($options->isResetAutoIncrement());
18+
}
19+
20+
public function testAddDropDatabase(): void
21+
{
22+
$options = new Options();
23+
self::assertFalse($options->isAddDropDatabase());
24+
$options->setAddDropDatabase(true);
25+
self::assertTrue($options->isAddDropDatabase());
26+
}
27+
28+
public function testAddDropTable(): void
29+
{
30+
$options = new Options();
31+
self::assertFalse($options->isAddDropTable());
32+
$options->setAddDropTable(true);
33+
self::assertTrue($options->isAddDropTable());
34+
}
35+
36+
public function testAddDropTrigger(): void
37+
{
38+
$options = new Options();
39+
self::assertTrue($options->isAddDropTrigger());
40+
$options->setAddDropTrigger(false);
41+
self::assertFalse($options->isAddDropTrigger());
42+
}
43+
44+
public function testAddLocks(): void
45+
{
46+
$options = new Options();
47+
self::assertTrue($options->isAddLocks());
48+
$options->setAddLocks(false);
49+
self::assertFalse($options->isAddLocks());
50+
}
51+
52+
public function testCompleteInsert(): void
53+
{
54+
$options = new Options();
55+
self::assertFalse($options->isCompleteInsert());
56+
$options->setCompleteInsert(true);
57+
self::assertTrue($options->isCompleteInsert());
58+
}
59+
}

translations/messages.en.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ database:
9494
title: Edit database settings
9595
panel:
9696
main_info: Main information
97+
backup_options: Backup options
9798
task_configuration: Backups periodicity configuration
9899
action:
99100
new: Add a database
@@ -112,6 +113,13 @@ database:
112113
status: Status
113114
dsn: DSN
114115
adapter: Storage space
116+
options:
117+
reset_auto_increment: Reset auto increment
118+
add_drop_database: Add drop database
119+
add_drop_table: Add drop table
120+
add_drop_trigger: Add drop trigger
121+
add_locks: Add locks
122+
complete_insert: Complete insert
115123
backup_task:
116124
periodicity_number: Every
117125
periodicity: Periodicity

translations/messages.fr.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ database:
9494
title: Modifier les paramètres de la base de données
9595
panel:
9696
main_info: Informations principales
97+
backup_options: Options de sauvegarde
9798
task_configuration: Configuration de la périodicité des sauvegardes
9899
action:
99100
new: Ajouter une base de données
@@ -112,6 +113,13 @@ database:
112113
status: Statut
113114
dsn: DSN
114115
adapter: Espace de stockage
116+
options:
117+
reset_auto_increment: Reset auto increment
118+
add_drop_database: Add drop database
119+
add_drop_table: Add drop table
120+
add_drop_trigger: Add drop trigger
121+
add_locks: Add locks
122+
complete_insert: Complete insert
115123
backup_task:
116124
periodicity_number: Chaque
117125
periodicity: Périodicité

0 commit comments

Comments
 (0)