From 7fc7ed07b81acee945170c3a7a69acb51283dbba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20L=C3=A9cluse?= <laurent.lecluse@unicaen.fr>
Date: Tue, 3 May 2022 14:28:51 +0200
Subject: [PATCH] Simplification du code des scripts de migration
---
admin/actions/update-bdd.php | 4 ++--
admin/migration/OrdonnancementColonnesTbl.php | 19 +++----------------
admin/migration/v18Divers.php | 14 --------------
admin/migration/v18Plafonds.php | 16 +---------------
admin/migration/v18Statuts.php | 16 +---------------
admin/src/AbstractMigration.php | 14 +++++---------
admin/src/MigrationManager.php | 11 ++++++++---
7 files changed, 20 insertions(+), 74 deletions(-)
diff --git a/admin/actions/update-bdd.php b/admin/actions/update-bdd.php
index fc175a3872..c284c7b3d3 100644
--- a/admin/actions/update-bdd.php
+++ b/admin/actions/update-bdd.php
@@ -42,7 +42,7 @@ foreach ($tablesDep as $tableDep) {
// Initialisation et lancement de la pré-migration
$mm = new MigrationManager($oa, $ref, $filters);
-$mm->migration('pre');
+$mm->migration('before');
// Mise à jour de la BDD (structures)
@@ -66,7 +66,7 @@ $c->end();
// Post-migration
$c->println('');
-$mm->migration('post');
+$mm->migration('after');
// Néttoyage des caches
$oa->run('clear-cache');
\ No newline at end of file
diff --git a/admin/migration/OrdonnancementColonnesTbl.php b/admin/migration/OrdonnancementColonnesTbl.php
index 6fac976711..baa73b8a0e 100644
--- a/admin/migration/OrdonnancementColonnesTbl.php
+++ b/admin/migration/OrdonnancementColonnesTbl.php
@@ -6,9 +6,7 @@
class OrdonnancementColonnesTbl extends AbstractMigration
{
- protected $contexte = self::CONTEXTE_ALL;
-
- protected $tbls = [];
+ protected $tbls = [];
@@ -56,18 +54,7 @@ class OrdonnancementColonnesTbl extends AbstractMigration
- public function action(string $contexte)
- {
- if ($contexte == self::CONTEXTE_PRE) {
- $this->before();
- } else {
- $this->after();
- }
- }
-
-
-
- protected function before()
+ public function before()
{
$bdd = $this->manager->getBdd();
$console = $this->manager->getOseAdmin()->getConsole();
@@ -80,7 +67,7 @@ class OrdonnancementColonnesTbl extends AbstractMigration
- protected function after()
+ public function after()
{
$console = $this->manager->getOseAdmin()->getConsole();
diff --git a/admin/migration/v18Divers.php b/admin/migration/v18Divers.php
index 9d2a529cf9..d4b2d05d73 100644
--- a/admin/migration/v18Divers.php
+++ b/admin/migration/v18Divers.php
@@ -6,9 +6,6 @@
class v18Divers extends AbstractMigration
{
- protected $contexte = self::CONTEXTE_PRE;
-
-
public function description(): string
{
@@ -24,17 +21,6 @@ class v18Divers extends AbstractMigration
- public function action(string $contexte)
- {
- if ($contexte == self::CONTEXTE_PRE) {
- $this->before();
- } else {
- $this->after();
- }
- }
-
-
-
public function before()
{
$bdd = $this->manager->getBdd();
diff --git a/admin/migration/v18Plafonds.php b/admin/migration/v18Plafonds.php
index b01cf17619..f4a030e46d 100644
--- a/admin/migration/v18Plafonds.php
+++ b/admin/migration/v18Plafonds.php
@@ -6,9 +6,6 @@
class v18Plafonds extends AbstractMigration
{
- protected $contexte = self::CONTEXTE_ALL;
-
-
public function description(): string
{
@@ -24,17 +21,6 @@ class v18Plafonds extends AbstractMigration
- public function action(string $contexte)
- {
- if ($contexte == self::CONTEXTE_PRE) {
- $this->before();
- } else {
- $this->after();
- }
- }
-
-
-
public function before()
{
$bdd = $this->manager->getBdd();
@@ -78,7 +64,7 @@ class v18Plafonds extends AbstractMigration
- protected function after()
+ public function after()
{
$c = $this->manager->getOseAdmin()->getConsole();
try {
diff --git a/admin/migration/v18Statuts.php b/admin/migration/v18Statuts.php
index 07ef5a7acb..dddf93970e 100644
--- a/admin/migration/v18Statuts.php
+++ b/admin/migration/v18Statuts.php
@@ -6,9 +6,6 @@
class v18Statuts extends AbstractMigration
{
- protected $contexte = self::CONTEXTE_ALL;
-
-
public function description(): string
{
@@ -24,17 +21,6 @@ class v18Statuts extends AbstractMigration
- public function action(string $contexte)
- {
- if ($contexte == self::CONTEXTE_PRE) {
- $this->before();
- } else {
- $this->after();
- }
- }
-
-
-
public function before()
{
$bdd = $this->manager->getBdd();
@@ -122,7 +108,7 @@ class v18Statuts extends AbstractMigration
- protected function after()
+ public function after()
{
$bdd = $this->manager->getBdd();
$c = $this->manager->getOseAdmin()->getConsole();
diff --git a/admin/src/AbstractMigration.php b/admin/src/AbstractMigration.php
index 27a4cb9b9e..dc2832ad4f 100644
--- a/admin/src/AbstractMigration.php
+++ b/admin/src/AbstractMigration.php
@@ -6,14 +6,6 @@
abstract class AbstractMigration
{
- CONST CONTEXTE_PRE = 'pre';
- CONST CONTEXTE_POST = 'post';
- CONST CONTEXTE_ALL = 'all';
-
- /**
- * @var string
- */
- protected $contexte = self::CONTEXTE_POST;
/**
* @var MigrationManager
@@ -42,7 +34,11 @@ abstract class AbstractMigration
abstract public function utile(): bool;
+ /*
+ Ajouter uniquement si nécessaire :
+ - une méthode publique before() qui s'exécutera AVANT la mise à jour
+ - une méthode publique after() qui s'exécutera APRES la mise à jour
- abstract public function action(string $contexte);
+ */
}
diff --git a/admin/src/MigrationManager.php b/admin/src/MigrationManager.php
index f28c630149..dc54d8cb55 100644
--- a/admin/src/MigrationManager.php
+++ b/admin/src/MigrationManager.php
@@ -258,11 +258,16 @@ class MigrationManager
if (
$migration
&& $migration instanceof AbstractMigration
- && ($contexte == $migration->getContexte() || AbstractMigration::CONTEXTE_ALL == $migration->getContexte())
+ && (method_exists($migration, $contexte))
) {
- $console->print("[$contexte MIGRATION] " . $migration->description() . ' ... ');
+ $traducs = [
+ 'before' => 'AVANT',
+ 'after' => 'APRES',
+ ];
+ $contexteLib = $traducs[$contexte] ?? $contexte;
+ $console->print("[$contexteLib MIGRATION] " . $migration->description() . ' ... ');
try {
- $migration->action($contexte);
+ $migration->$contexte();
$console->println('OK', $console::COLOR_GREEN);
} catch (\Throwable $e) {
$console->println('Erreur : ' . $e->getMessage(), $console::COLOR_RED);
--
GitLab