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

Mise en place des bons filtrages de DDL en maj BDD ou maj DDL

parent a02cc0b7
No related branches found
No related tags found
No related merge requests found
Pipeline #31669 passed
...@@ -21,6 +21,8 @@ return [ ...@@ -21,6 +21,8 @@ return [
'dir' => 'data/ddl', 'dir' => 'data/ddl',
'columns_positions_file' => 'data/ddl_columns_pos.php', 'columns_positions_file' => 'data/ddl_columns_pos.php',
'filters' => [], 'filters' => [],
'update-bdd-filters' => [],
'update-ddl-filters' => [],
], ],
'data' => [ 'data' => [
......
...@@ -498,12 +498,50 @@ class Bdd ...@@ -498,12 +498,50 @@ class Bdd
public function getFiltersForUpdateBdd(Ddl $ddl): DdlFilters
{
$filters = new DdlFilters();
$filters->addArray($ddl->getOption(Ddl::OPTION_FILTERS));
$filters->addArray($ddl->getOption(Ddl::OPTION_UPDATE_BDD_FILTERS));
$filters->setExplicit(true);
// On ajouter toute la DDL qu'on donne en référence
$filters->addDdl($ddl);
// On ajoute toutes les dépendances des tables qu'on a inclu dans la DDL au cas où elles ne seraient pas listées
$filters->addTablesDeps($this);
return $filters;
}
public function getFiltersForUpdateDdl(): DdlFilters
{
$filters = new DdlFilters();
$filters->addArray($this->getOption(self::OPTION_DDL.'/'.Ddl::OPTION_FILTERS));
$filters->addArray($this->getOption(self::OPTION_DDL.'/'.Ddl::OPTION_UPDATE_DDL_FILTERS));
return $filters;
}
public function getNewDdl(): Ddl
{
$ddl = new Ddl();
$ddl->setOptions($this->getOption(self::OPTION_DDL, []));
return $ddl;
}
public function getDdl(DdlFilters|array $filters = []): Ddl public function getDdl(DdlFilters|array $filters = []): Ddl
{ {
$this->logBegin("Récupération de la DDL"); $this->logBegin("Récupération de la DDL");
$filters = DdlFilters::normalize($filters); $filters = DdlFilters::normalize($filters);
$ddl = new Ddl(); $ddl = $this->getNewDdl();
$ddl->setOptions($this->getOption(self::OPTION_DDL, []));
$managers = $this->managerList(); $managers = $this->managerList();
foreach ($managers as $type => $libelle) { foreach ($managers as $type => $libelle) {
if (!($filters->isExplicit() && $filters->get($type)->isEmpty())) { if (!($filters->isExplicit() && $filters->get($type)->isEmpty())) {
...@@ -523,8 +561,7 @@ class Bdd ...@@ -523,8 +561,7 @@ class Bdd
public function getRefDdl(): Ddl public function getRefDdl(): Ddl
{ {
$ddl = new Ddl(); $ddl = $this->getNewDdl();
$ddl->setOptions($this->getOption(self::OPTION_DDL, []));
$ddl->loadFromDir(); $ddl->loadFromDir();
return $ddl; return $ddl;
......
...@@ -26,10 +26,10 @@ class InstallBddCommand extends Command ...@@ -26,10 +26,10 @@ class InstallBddCommand extends Command
$io->title('Installation de la base de données'); $io->title('Installation de la base de données');
// Mise ne place des objets // Mise en place des objets
try { try {
$ref = $bdd->getRefDdl(); $ddl = $bdd->getRefDdl();
$bdd->create($ref); $bdd->create($ddl);
$io->success('Objets en place'); $io->success('Objets en place');
} catch (\Throwable $e) { } catch (\Throwable $e) {
$io->error($e->getMessage()); $io->error($e->getMessage());
......
...@@ -26,39 +26,16 @@ class UpdateBddCommand extends Command ...@@ -26,39 +26,16 @@ class UpdateBddCommand extends Command
$io->title('Mise à jour de la base de données'); $io->title('Mise à jour de la base de données');
$ref = $bdd->getRefDdl(); $ddl = $bdd->getRefDdl();
$filters = $bdd->getFiltersForUpdateBdd($ddl);
// Construction de la config de DDL pour filtrer
$filters = [];
foreach ($ref as $ddlClass => $objects) {
foreach ($objects as $object => $objectDdl) {
$filters[$ddlClass]['includes'][] = $object;
}
}
$tablesDep = [
Ddl::INDEX,
Ddl::PRIMARY_CONSTRAINT,
Ddl::REF_CONSTRAINT,
Ddl::UNIQUE_CONSTRAINT,
];
foreach ($tablesDep as $tableDep) {
$objects = $bdd->manager($tableDep)->get();
foreach ($objects as $obj) {
if (in_array($obj['table'], $filters['table']['includes'])) {
$filters[$tableDep]['includes'][] = $obj['name'];
}
}
}
// Initialisation et lancement de la pré-migration // Initialisation et lancement de la pré-migration
//$mm = new MigrationManager($oa, $ref, $filters); //$mm = new MigrationManager($oa, $ddl, $filters);
//$mm->migration('before'); //$mm->migration('before');
try { try {
$bdd->alter($ref, $filters, true); $bdd->alter($ddl, $filters, true);
$io->success('Objets à jour'); $io->success('Objets à jour');
} catch (\Throwable $e) { } catch (\Throwable $e) {
$io->error($e->getMessage()); $io->error($e->getMessage());
...@@ -66,7 +43,7 @@ class UpdateBddCommand extends Command ...@@ -66,7 +43,7 @@ class UpdateBddCommand extends Command
// Mise à jour des séquences // Mise à jour des séquences
$bdd->majSequences($ref); $bdd->majSequences($ddl);
// Mise à jour des données // Mise à jour des données
......
...@@ -22,12 +22,10 @@ class UpdateDdlCommand extends Command ...@@ -22,12 +22,10 @@ class UpdateDdlCommand extends Command
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io); $bdd = $this->getBdd()->setLogger($io);
$filters = [
];
$io->title('Génération de la DDL à partir de la base de données'); $io->title('Génération de la DDL à partir de la base de données');
try { try {
$filters = $bdd->getFiltersForUpdateDdl();
$ddl = $bdd->getDdl($filters); $ddl = $bdd->getDdl($filters);
$ddl->saveToDir(); $ddl->saveToDir();
$io->success('DDL mise à jour'); $io->success('DDL mise à jour');
......
...@@ -27,6 +27,9 @@ class Ddl implements Iterator, ArrayAccess ...@@ -27,6 +27,9 @@ class Ddl implements Iterator, ArrayAccess
const OPTION_DIR = 'dir'; const OPTION_DIR = 'dir';
const OPTION_COLUMNS_POSITIONS_FILE = 'columns_positions_file'; const OPTION_COLUMNS_POSITIONS_FILE = 'columns_positions_file';
const OPTION_FILTERS = 'filters';
const OPTION_UPDATE_BDD_FILTERS = 'update-bdd-filters';
const OPTION_UPDATE_DDL_FILTERS = 'update-ddl-filters';
protected array $data = []; protected array $data = [];
......
...@@ -65,6 +65,13 @@ class DdlFilter implements \ArrayAccess ...@@ -65,6 +65,13 @@ class DdlFilter implements \ArrayAccess
public function hasInclude(string $include): bool
{
return in_array($include, $this->includes);
}
public function getExcludes(): array public function getExcludes(): array
{ {
return $this->excludes; return $this->excludes;
...@@ -96,6 +103,13 @@ class DdlFilter implements \ArrayAccess ...@@ -96,6 +103,13 @@ class DdlFilter implements \ArrayAccess
public function hasExclude(string $exclude): bool
{
return in_array($exclude, $this->excludes);
}
public function toArray(): array public function toArray(): array
{ {
return [ return [
......
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
namespace Unicaen\BddAdmin\Ddl; namespace Unicaen\BddAdmin\Ddl;
use Unicaen\BddAdmin\Bdd;
use Unicaen\BddAdmin\Util;
class DdlFilters implements \Iterator, \ArrayAccess class DdlFilters implements \Iterator, \ArrayAccess
{ {
const EXPLICIT = 'explicit'; const EXPLICIT = 'explicit';
...@@ -65,13 +68,22 @@ class DdlFilters implements \Iterator, \ArrayAccess ...@@ -65,13 +68,22 @@ class DdlFilters implements \Iterator, \ArrayAccess
{ {
$this->setExplicit(true); $this->setExplicit(true);
$filters = [];
foreach ($ddl as $ddlClass => $objects) { foreach ($ddl as $ddlClass => $objects) {
foreach ($objects as $object => $objectDdl) { foreach ($objects as $object => $objectDdl) {
$filters[$ddlClass][DdlFilter::INCLUDES][] = $object; $ddlFilter = $this->get($ddlClass);
if (!$ddlFilter->hasInclude($object)) {
$ddlFilter->addInclude($object);
}
} }
} }
return $this;
}
public function addTablesDeps(Bdd $bdd): self
{
$tablesDep = [ $tablesDep = [
Ddl::INDEX, Ddl::INDEX,
Ddl::PRIMARY_CONSTRAINT, Ddl::PRIMARY_CONSTRAINT,
...@@ -79,16 +91,24 @@ class DdlFilters implements \Iterator, \ArrayAccess ...@@ -79,16 +91,24 @@ class DdlFilters implements \Iterator, \ArrayAccess
Ddl::UNIQUE_CONSTRAINT, Ddl::UNIQUE_CONSTRAINT,
]; ];
$ddlTableFilter = $this->get(Ddl::TABLE);
foreach ($tablesDep as $tableDep) { foreach ($tablesDep as $tableDep) {
$objects = $ddl->get($tableDep); $ddlFilter = $this->get($tableDep);
$objects = $bdd->manager($tableDep)->get();
foreach ($objects as $obj) { foreach ($objects as $obj) {
if (is_array($obj) && in_array($obj['table'], $filters['table'][DdlFilter::INCLUDES])) { if (is_array($obj)){
$filters[$tableDep][DdlFilter::INCLUDES][] = $obj['name']; $tableName = Util::fullObjectName($obj['schema'] ?? null, $obj['table']);
if ($ddlTableFilter->hasInclude($tableName)) {
$objectName = Util::fullObjectName($obj['schema'] ?? null, $obj['name']);
if (!$ddlFilter->hasInclude($objectName)) {
$ddlFilter->addInclude($objectName);
}
}
} }
} }
} }
$this->addArray($filters);
return $this; return $this;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment