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

DataManager OK, commandes OK

reste le migrationmanager à finaliser
parent 5e7648ba
No related branches found
No related tags found
No related merge requests found
Pipeline #31649 passed
......@@ -20,16 +20,20 @@ return [
'ddl' => [
'dir' => 'data/ddl',
'columns_positions_file' => 'data/ddl_columns_pos.php',
'filters' => [],
],
'data' => [
'sources' => [],
'actions' => [],
'actions' => [
'install' => 'Insertion des données',
'update' => 'Contrôle et mise à jour des données',
],
'config' => [], // configuration par tables
'sources' => [],
],
'migration' => [
'dir' => 'Application/src/Migration',
'dir' => null,
],
'id_column' => 'id',
......@@ -55,16 +59,23 @@ return [
'service_manager' => [
'factories' => [
Bdd::class => BddFactory::class,
Command\UpdateBddCommand::class => Command\UpdateBddCommandFactory::class,
Command\UpdateDdlCommand::class => Command\UpdateDdlCommandFactory::class,
Command\InstallBddCommand::class => Command\CommandFactory::class,
Command\UpdateBddCommand::class => Command\CommandFactory::class,
Command\UpdateDdlCommand::class => Command\CommandFactory::class,
Command\UpdateDataCommand::class => Command\CommandFactory::class,
Command\ClearBddCommand::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:update-ddl' => Command\UpdateDdlCommand::class,
'bddadmin:update-data' => Command\UpdateDataCommand::class,
],
],
];
\ No newline at end of file
......@@ -599,7 +599,6 @@ class Bdd
public function create(Bdd|Ddl|array $ddl, DdlFilters|array $filters = []): void
{
$this->logBegin('Mise en place de la base de données');
$filters = DdlFilters::normalize($filters);
if ($ddl instanceof self) {
$ddl = $ddl->getDdl($filters);
......@@ -632,14 +631,12 @@ class Bdd
}
}
$this->compilerTout();
$this->logEnd("Base de données créée");
}
public function alter(Bdd|Ddl|array $ddl, DdlFilters|array $filters = []): void
{
$this->logBegin('Application des changements sur la BDD');
$filters = DdlFilters::normalize($filters);
if ($ddl instanceof self) {
......@@ -680,20 +677,18 @@ class Bdd
$this->logEnd();
}
}
}
}
$this->logEnd();
$this->compilerTout();
$this->migration()->migration('after');
$this->logEnd('Changements appliqués');
$this->majSequences($ddl);
}
public function drop(DdlFilters|array $filters = []): void
{
$this->logBegin('Suppression de la base de données');
$filters = DdlFilters::normalize($filters);
foreach ($this->changements as $changement => $label) {
......@@ -721,7 +716,7 @@ class Bdd
}
}
}
$this->logEnd("Base de données vide");
$this->logEnd();
}
......@@ -848,13 +843,22 @@ class Bdd
public function compilerTout(): array
{
$this->logBegin("Compilation de tous les objets de la BDD");
$errors = [];
$compileTypes = [Ddl::PACKAGE, Ddl::VIEW, Ddl::TRIGGER];
$managers = [];
foreach ($compileTypes as $compileType) {
$manager = $this->manager($compileType);
if ($manager instanceof CompilableInterface) {
$managers[] = $manager;
}
}
$errors = [];
if ($managers) {
$this->logBegin("Compilation de tous les objets de la BDD");
foreach ($managers as $manager) {
$list = $manager->getList();
foreach ($list as $name) {
try {
......@@ -866,9 +870,10 @@ class Bdd
}
}
}
}
$this->logEnd("Fin de la compilation");
}
return $errors;
}
......
......@@ -3,6 +3,7 @@
namespace Unicaen\BddAdmin;
use Psr\Container\ContainerInterface;
use Unicaen\BddAdmin\Data\DataManager;
/**
* Description of BddFactory
......@@ -23,8 +24,24 @@ class BddFactory
{
$config = $container->get('config')['unicaen-bddadmin'];
$bdd = new Bdd($config);
$bdd = new Bdd($this->prepareConfig($config, $container));
return $bdd;
}
public function prepareConfig(array $config, ContainerInterface $container): array
{
if (isset($config[Bdd::OPTION_DATA][DataManager::OPTION_SOURCES])){
$sources = &$config[Bdd::OPTION_DATA][DataManager::OPTION_SOURCES];
foreach( $sources as $index => $source){
if ($container->has($source)){
$sources[$index] = $container->get($source);
}
}
}
return $config;
}
}
\ No newline at end of file
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
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 ClearBddCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class ClearBddCommand extends Command
{
use BddAwareTrait;
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
$io->title('Vidage complet de la base de données');
try {
$bdd->drop();
$io->success('Base de données vidée complètement');
} catch (\Throwable $e) {
$io->error($e->getMessage());
}
return Command::SUCCESS;
}
}
\ No newline at end of file
......@@ -7,11 +7,11 @@ use Unicaen\BddAdmin\Bdd;
/**
* Description of UpdateBddCommandFactory
* Description of CommandFactory
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class UpdateBddCommandFactory
class CommandFactory
{
/**
......@@ -19,11 +19,11 @@ class UpdateBddCommandFactory
* @param string $requestedName
* @param array|null $options
*
* @return UpdateBddCommand
* @return InstallBddCommand
*/
public function __invoke(ContainerInterface $container, $requestedName, $options = null): UpdateBddCommand
public function __invoke(ContainerInterface $container, $requestedName, $options = null)
{
$command = new UpdateBddCommand;
$command = new $requestedName;
$command->setBdd($container->get(Bdd::class));
return $command;
......
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
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 InstallBddCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class InstallBddCommand extends Command
{
use BddAwareTrait;
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
$io->title('Installation de la base de données');
// Mise ne place des objets
try {
$ref = $bdd->getRefDdl();
$bdd->create($ref);
$io->success('Objets en place');
} catch (\Throwable $e) {
$io->error($e->getMessage());
}
if (!empty($bdd->data()->getSources())) {
// Installation des données
$io->title('Insertion du jeu de données données');
try {
$bdd->data()->run(DataManager::ACTION_INSTALL);
$io->success('Données écrites');
} catch (\Throwable $e) {
$io->error($e->getMessage());
}
}
return Command::SUCCESS;
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ 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;
/**
......@@ -24,7 +25,6 @@ class UpdateBddCommand extends Command
$bdd = $this->getBdd()->setLogger($io);
$io->title('Mise à jour de la base de données');
$io->info('Mise à jour des définitions de la base de données. Merci de patienter ...');
$ref = $bdd->getRefDdl();
......@@ -57,8 +57,12 @@ class UpdateBddCommand extends Command
//$mm->migration('before');
// Mise à jour de la BDD (structures)
try {
$bdd->alter($ref, $filters, true);
$io->success('Objets à jour');
} catch (\Throwable $e) {
$io->error($e->getMessage());
}
// Mise à jour des séquences
......@@ -66,10 +70,15 @@ class UpdateBddCommand extends Command
// Mise à jour des données
//$bdd->logBegin('Contrôle et mise à jour des données');
//$bdd->dataUpdater()->run('update');
//$bdd->logEnd('Données à jour');
if (!empty($bdd->data()->getSources())) {
$io->title('Contrôle et mise à jour des données');
try {
$bdd->data()->run(DataManager::ACTION_UPDATE);
$io->success('Données à jour');
} catch (\Throwable $e) {
$io->error($e->getMessage());
}
}
// Post-migration
......
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
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\Ddl\Ddl;
/**
* Description of UpdateDataCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class UpdateDataCommand extends Command
{
use BddAwareTrait;
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
// Mise à jour des données
if (!empty($bdd->data()->getSources())) {
$io->title('Contrôle et mise à jour des données');
try {
$bdd->data()->run('update');
$io->success('Données à jour');
} catch (\Throwable $e) {
$io->error($e->getMessage());
}
}else{
$io->error('Aucune source de donnée fournie');
}
return Command::SUCCESS;
}
}
\ No newline at end of file
......@@ -27,9 +27,13 @@ class UpdateDdlCommand extends Command
$io->title('Génération de la DDL à partir de la base de données');
try {
$ddl = $bdd->getDdl($filters);
$ddl->saveToDir();
$io->success('DDL mise à jour');
} catch (\Throwable $e) {
$io->error($e->getMessage());
}
return Command::SUCCESS;
}
......
<?php
namespace Unicaen\BddAdmin\Command;
use Psr\Container\ContainerInterface;
use Unicaen\BddAdmin\Bdd;
/**
* Description of UpdateDdlCommandFactory
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class UpdateDdlCommandFactory
{
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
*
* @return UpdateBddCommand
*/
public function __invoke(ContainerInterface $container, $requestedName, $options = null): UpdateDdlCommand
{
$command = new UpdateDdlCommand;
$command->setBdd($container->get(Bdd::class));
return $command;
}
}
\ No newline at end of file
......@@ -11,17 +11,15 @@ class DataManager
use BddAwareTrait;
use OptionsTrait;
private array $config = [];
const OPTION_SOURCES = 'sources';
const OPTION_ACTIONS = 'actions';
const OPTION_CONFIG = 'config';
private array $sources = [];
const ACTION_INSTALL = 'install';
const ACTION_UPDATE = 'update';
private array $sourcesData = [];
private array $actions = [
'install' => 'Insertion des données',
'update' => 'Contrôle et mise à jour des données',
];
public function __construct(Bdd $bdd)
......@@ -33,15 +31,18 @@ class DataManager
public function run(string $action, ?string $table = null): DataManager
{
if (!isset($this->actions[$action])) {
if (!$this->hasAction($action)) {
throw new \Exception('Action "' . $action . '" inconnue');
}
if ($table && !isset($this->config[$table])) {
if ($table && !$this->hasConfig($table)) {
throw new \Exception('La table "' . $table . '" n\'est pas de directives de configuration : action impossible à réaliser');
}
$config = $table ? [$table => $this->config[$table]] : $this->config;
$config = $this->getOption(self::OPTION_CONFIG);
if ($table) {
$config = [$table => $config[$table]];
}
foreach ($config as $table => $config) {
$actions = (array)$config['actions'];
......@@ -64,15 +65,17 @@ class DataManager
$idCol = $this->getBdd()->getOption(Bdd::OPTION_ID_COLUMN);
$data = null;
foreach ($this->sources as $i => $source) {
if (is_object($source) && method_exists($source, $table)) {
$data = $source->$table($action);
break;
}
$sources = $this->getSources();
foreach ($sources as $i => $source) {
if (is_array($source) && isset($source[$table])) {
$data = $source[$table];
break;
}
$methodName = str_replace('.', '_', $table);
if (is_object($source) && method_exists($source, $methodName)) {
$data = $source->$methodName($action);
break;
}
if (is_string($source)) {
if (!isset($this->sourcesData[$i]) && file_exists($source)) {
$this->sourcesData[$i] = require $source;
......@@ -87,6 +90,9 @@ class DataManager
if (null === $data) {
throw new \Exception('Données sources introuvables pour la table "' . $table . '"');
}
if (!is_array($data)) {
throw new \Exception('Les données sources de la table "' . $table . '" doivent être présentées sous forme de tableau de lignes, chaque ligne étant un tableau de colonnes');
}
$result = $tableObject->merge(
$data,
......@@ -102,35 +108,17 @@ class DataManager
/**
* @return array
*/
public function getConfig(): array
{
return $this->config;
return $this->getOption(self::OPTION_CONFIG);
}
/**
* @param array $config
*
* @return DataManager
*/
public function setConfig(array $config): DataManager
public function hasConfig(string $tableName): bool
{
$this->config = $config;
return $this;
}
public function addConfig(string $table, array $config): DataManager
{
$this->config[$table] = $config;
return $this;
$config = $this->getConfig();
return array_key_exists($tableName, $config);
}
......@@ -140,14 +128,16 @@ class DataManager
*/
public function getSources(): array
{
return $this->sources;
return $this->getOption(self::OPTION_SOURCES);
}
public function addSource(string|array|object $source): DataManager
{
$this->sources[] = $source;
$sources = $this->getSources();
$sources[] = $source;
$this->setOptions(self::OPTION_SOURCES, $sources);
return $this;
}
......@@ -159,14 +149,24 @@ class DataManager
*/
public function getActions(): array
{
return $this->actions;
return $this->getOption(self::OPTION_ACTIONS);
}
public function hasAction(string $action): bool
{
$actions = $this->getActions();
return array_key_exists($action, $actions);
}
public function addAction(string $name, string $libelle): DataManager
{
$this->actions[$name] = $libelle;
$actions = $this->getActions();
$actions[$name] = $libelle;
$this->setOptions(self::OPTION_ACTIONS, $actions);
return $this;
}
......
......@@ -72,7 +72,7 @@ class SymfonyStyleLogger implements LoggerInterface
}
$this->lastRewrite = false;
$this->lastMessage = null;
$this->symfonyStyle->title($title);
$this->symfonyStyle->comment($title);
}
......@@ -82,10 +82,10 @@ class SymfonyStyleLogger implements LoggerInterface
if ($this->lastMessage && $this->lastRewrite) {
$msg .= str_pad('', strlen($this->lastMessage) - strlen($msg ?? '') + 2);
}
if (trim($msg)) {
if ($msg && trim($msg)) {
$this->symfonyStyle->comment($msg);
} else {
$this->println('');
$this->println(str_pad(' ', strlen($this->lastMessage ?? '')));
}
}
......
......@@ -390,7 +390,9 @@ class Table
$sql = "UPDATE " . $this->name . " SET $dataSql" . $this->makeWhere($where, $options, $params);
return $bdd->exec($sql, $params, $this->makeTypesOptions());
$bdd->exec($sql, $params, $this->makeTypesOptions());
return true;
}
......@@ -400,7 +402,8 @@ class Table
$params = [];
$sql = "DELETE FROM " . $this->name . $this->makeWhere($where, $options, $params);
return $this->getBdd()->exec($sql, $params);
$this->getBdd()->exec($sql, $params);
return true;
}
......@@ -409,7 +412,8 @@ class Table
{
$sql = "TRUNCATE TABLE " . $this->name;
return $this->getBdd()->exec($sql);
$this->getBdd()->exec($sql);
return true;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment