Skip to content
Snippets Groups Projects
Commit 3bffbaed authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Pas mal de bugs corrigés

ajout de save et load, copy-* et simplification des commandes
parent 398f7642
No related branches found
No related tags found
No related merge requests found
Pipeline #31798 passed
Showing with 341 additions and 39 deletions
......@@ -6,17 +6,10 @@ return [
'unicaen-bddadmin' => [
'connection' => [
'default' => [
'driver' => 'Postgresql', // par défaut, ou Oracle ou Mysql
'host' => '',
'port' => '',
'dbname' => '',
'user' => '',
'password' => '',
],
// Autres instances de la BDD
],
'current_connection' => 'default',
'ddl' => [
'dir' => 'data/ddl',
'columns_positions_file' => 'data/ddl_columns_pos.php',
......@@ -61,23 +54,30 @@ return [
'service_manager' => [
'factories' => [
Bdd::class => BddFactory::class,
Command\InstallBddCommand::class => Command\CommandFactory::class,
Command\UpdateBddCommand::class => Command\CommandFactory::class,
Command\InstallCommand::class => Command\CommandFactory::class,
Command\UpdateCommand::class => Command\CommandFactory::class,
Command\UpdateDdlCommand::class => Command\CommandFactory::class,
Command\UpdateDataCommand::class => Command\CommandFactory::class,
Command\ClearBddCommand::class => Command\CommandFactory::class,
Command\ClearCommand::class => Command\CommandFactory::class,
Command\CopyToCommand::class => Command\CommandFactory::class,
Command\CopyFromCommand::class => Command\CommandFactory::class,
Command\LoadCommand::class => Command\CommandFactory::class,
Command\SaveCommand::class => Command\CommandFactory::class,
],
],
'laminas-cli' => [
'commands' => [
'bddadmin:install-bdd' => Command\InstallBddCommand::class,
'bddadmin:update-bdd' => Command\UpdateBddCommand::class,
'bddadmin:clear-bdd' => Command\ClearBddCommand::class,
'bddadmin:install' => Command\InstallCommand::class,
'bddadmin:update' => Command\UpdateCommand::class,
'bddadmin:clear' => Command\ClearCommand::class,
'bddadmin:update-ddl' => Command\UpdateDdlCommand::class,
'bddadmin:update-data' => Command\UpdateDataCommand::class,
'bddadmin:copy-to' => Command\CopyToCommand::class,
'bddadmin:copy-from' => Command\CopyFromCommand::class,
'bddadmin:load' => Command\LoadCommand::class,
'bddadmin:save' => Command\SaveCommand::class,
],
],
];
\ No newline at end of file
......@@ -7,7 +7,7 @@
return [
'unicaen-bddadmin' => [
'connection' => [
'default' => [ // généralement : base de dév
'default' => [ // généralement : base de dév, peut s'appeler autrement que 'default'
'host' => 'à renseigner ...',
'port' => 'à renseigner ...',
'dbname' => 'à renseigner ...',
......@@ -26,5 +26,8 @@ return [
...
*/
],
/* Connexion à utiliser par défaut, nom à sélectionner parmi la liste des connexions disponibles */
//'current_connection' => 'default',
],
];
\ No newline at end of file
......@@ -37,9 +37,8 @@ class Bdd
use LoggerAwareTrait;
use OptionsTrait;
const CONNECTION_DEFAULT = 'default';
const OPTION_CONNECTION = 'connection';
const OPTION_CURRENT_CONNECTION = 'current_connection';
const OPTION_DDL = 'ddl';
const OPTION_DATA = 'data';
const OPTION_MIGRATION = 'migration';
......@@ -123,7 +122,7 @@ class Bdd
Ddl::SCHEMA . '.drop' => 'Suppression des schémas',
];
private string $connection = self::CONNECTION_DEFAULT;
private string $connection = 'default';
/** @var array|Bdd[] */
private array $bdds = [];
......@@ -149,16 +148,20 @@ class Bdd
public function __construct(array $options = [], string $connection = self::CONNECTION_DEFAULT)
public function __construct(array $options = [], ?string $connection = null)
{
$this->connection = $connection;
if (!empty($options)) {
// Compatibilité avec l'ancienne configuration des connections
if (!isset($options[self::OPTION_CONNECTION]) && isset($options['host'])) {
$options = [self::OPTION_CONNECTION => [$connection => $options]];
}
$this->setOptions($options);
if (empty($connection)){
$connection = $this->getOption(self::OPTION_CURRENT_CONNECTION, 'default');
}
$this->connection = $connection;
$this->connect();
}
......@@ -179,6 +182,23 @@ class Bdd
public function getConnectionName(): string
{
return $this->connection;
}
public function getBdds(): array
{
$bdds = $this->getOption(self::OPTION_CONNECTION);
unset($bdds[$this->getOption(self::OPTION_CURRENT_CONNECTION)]);
return array_keys($bdds);
}
public function getBdd(string $connection): self
{
if (!array_key_exists($connection, $this->bdds)) {
......@@ -858,8 +878,8 @@ class Bdd
$this->logTitle('Génération de la DDL à partir de la base de données');
try {
$filters = $bdd->getFiltersForUpdateDdl();
$ddl = $bdd->getDdl($filters);
$filters = $this->getFiltersForUpdateDdl();
$ddl = $this->getDdl($filters);
$ddl->saveToDir();
$this->logSuccess('DDL mise à jour');
} catch (\Throwable $e) {
......@@ -1087,6 +1107,8 @@ class Bdd
$this->create($source, $filters);
$this->majSequences();
$this->logEnd();
return $this;
......@@ -1185,10 +1207,12 @@ class Bdd
$ddl->loadFromFile($tmpPath . '/bdd.ddl');
$ddl->filter($filters);
$schDdl = $ddl->get(Ddl::SCHEMA);
$sDdl = $ddl->get(Ddl::SEQUENCE);
$tDdl = $ddl->get(Ddl::TABLE);
$this->drop();
$this->create([Ddl::TABLE => $tDdl]);
$this->create([Ddl::SCHEMA => $schDdl, Ddl::SEQUENCE => $sDdl, Ddl::TABLE => $tDdl]);
$tables = array_keys($tDdl);
......@@ -1203,8 +1227,14 @@ class Bdd
}
}
$this->inCopy = false;
$this->create($ddl, [Ddl::SCHEMA => ['excludes' => '%']]);
$this->create($ddl, [Ddl::TABLE => ['excludes' => '%']]);
if ($filters instanceof DdlFilters) {
$filters = $filters->toArray();
}
$filters[Ddl::SCHEMA] = ['excludes' => '%'];
$filters[Ddl::SEQUENCE] = ['excludes' => '%'];
$filters[Ddl::TABLE] = ['excludes' => '%'];
$this->create($ddl, $filters);
$this->majSequences($ddl);
......
......@@ -11,14 +11,21 @@ use Unicaen\BddAdmin\Data\DataManager;
use Unicaen\BddAdmin\Ddl\Ddl;
/**
* Description of ClearBddCommand
* Description of ClearCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class ClearBddCommand extends Command
class ClearCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this->setDescription('Vide la base de données');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
......
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Unicaen\BddAdmin\BddAwareTrait;
use Unicaen\BddAdmin\Data\DataManager;
use Unicaen\BddAdmin\Ddl\Ddl;
/**
* Description of CopyFromCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class CopyFromCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this
->setDescription('Copie depuis une autre BDD')
->addArgument('source', InputArgument::OPTIONAL, 'Nom de la BDD d\'origine');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
$bdds = $bdd->getBdds();
if (empty($bdds)) {
$io->error('Aucune base de donnée possible n\'est en source : copie impossible');
$io->info('Veuillez ajouter d\'autres connexions en configuration dans unicaen-bddadmin/connection');
return Command::INVALID;
}
$source = $input->getArgument('source');
if (!$source) {
$source = $io->choice(
'Veuillez choisir une base de données source',
$bdd->getBdds()
);
}
// Lance la copie
$bdd->copy($source);
return Command::SUCCESS;
}
}
\ No newline at end of file
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Unicaen\BddAdmin\BddAwareTrait;
use Unicaen\BddAdmin\Data\DataManager;
use Unicaen\BddAdmin\Ddl\Ddl;
/**
* Description of CopyToCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class CopyToCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this
->setDescription('Copie vers une autre BDD')
->addArgument('destination', InputArgument::OPTIONAL, 'Nom de la BDD de destination');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
$bdds = $bdd->getBdds();
if (empty($bdds)) {
$io->error('Aucune base de donnée possible n\'est en destination : copie impossible');
$io->info('Veuillez ajouter d\'autres connexions en configuration dans unicaen-bddadmin/connection');
return Command::INVALID;
}
$destination = $input->getArgument('destination');
if (!$destination) {
// Si l'argument n'est pas fourni, on demande à l'utilisateur de choisir une valeur
$destination = $io->choice(
'Veuillez choisir une base de données de destination',
$bdd->getBdds()
);
}
$bdd->copyTo($destination);
return Command::SUCCESS;
}
}
\ No newline at end of file
......@@ -15,10 +15,17 @@ use Unicaen\BddAdmin\Ddl\Ddl;
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class InstallBddCommand extends Command
class InstallCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this->setDescription('Peuple la base de données à partir de la DDL et du jeu de données par défaut');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
......
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Unicaen\BddAdmin\BddAwareTrait;
use Unicaen\BddAdmin\Data\DataManager;
use Unicaen\BddAdmin\Ddl\Ddl;
/**
* Description of LoadCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class LoadCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this->setDescription('Chargement de la base de données depuis un fichier')
->addArgument('filename', InputArgument::REQUIRED, 'Chemin du fichier de sauvegarde');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
$filename = $input->getArgument('filename');
if (!str_starts_with($filename, DIRECTORY_SEPARATOR)){
$filename = $_SERVER['PWD'].DIRECTORY_SEPARATOR.$filename;
}
if (!file_exists($filename)){
$io->error('Le fichier '.$filename.' n\'existe pas');
return Command::FAILURE;
}
$bdd->load($filename);
return Command::SUCCESS;
}
}
\ No newline at end of file
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Unicaen\BddAdmin\BddAwareTrait;
use Unicaen\BddAdmin\Data\DataManager;
use Unicaen\BddAdmin\Ddl\Ddl;
/**
* Description of SaveCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class SaveCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this->setDescription('Sauvegarde de la base de données dans un fichier')
->addArgument('filename', InputArgument::REQUIRED, 'Chemin du fichier de destination');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
$filename = $input->getArgument('filename');
if (!str_starts_with($filename, DIRECTORY_SEPARATOR)){
$filename = $_SERVER['PWD'].DIRECTORY_SEPARATOR.$filename;
}
if (file_exists($filename)){
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Le fichier existe déjà, voulez-vous le remplacer ? (yes/no) [no]: ', false);
if (!$helper->ask($input, $output, $question)) {
$io->warning('Opération annulée par l\'utilisateur.');
return Command::FAILURE;
}else{
unlink($filename);
}
}
$bdd->save($filename);
return Command::SUCCESS;
}
}
\ No newline at end of file
......@@ -12,14 +12,21 @@ use Unicaen\BddAdmin\Ddl\Ddl;
use Unicaen\BddAdmin\Migration\MigrationManager;
/**
* Description of UpdateBddCommand
* Description of UpdateCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class UpdateBddCommand extends Command
class UpdateCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this->setDescription('Met à jour la base de données & ses données à partir de la DDL et du jeu de données');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
......
......@@ -18,6 +18,13 @@ class UpdateDataCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this->setDescription('Met à jour les données de la base de données');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
......
......@@ -17,6 +17,13 @@ class UpdateDdlCommand extends Command
{
use BddAwareTrait;
protected function configure(): void
{
$this->setDescription('Met à jour la DDL à partir des définitions de la base de données');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
......
......@@ -80,7 +80,7 @@ class DefaultLogger implements LoggerInterface
public function error(Throwable|string $e): void
public function error(\Throwable|string $e): void
{
if ($e instanceof \Throwable) {
$e = $e->getMessage();
......
......@@ -12,7 +12,7 @@ interface LoggerInterface
public function error(Throwable|string $e): void;
public function error(\Throwable|string $e): void;
......
......@@ -69,7 +69,7 @@ class SymfonyStyleLogger implements LoggerInterface
public function error(Throwable|string $e): void
public function error(\Throwable|string $e): void
{
if ($e instanceof \Throwable) {
$e = $e->getMessage();
......
......@@ -55,7 +55,11 @@ class Util
. self::arrayExport($value, "$indent ");
}
if (!empty($r)) {
return "[\n" . implode(",\n", $r) . ",\n" . $indent . "]";
}else{
return "[]";
}
case "boolean":
return $var ? "TRUE" : "FALSE";
default:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment