diff --git a/app/classes/Framadate/Migration/From_0_0_to_0_8_Migration.php b/app/classes/Framadate/Migration/From_0_0_to_0_8_Migration.php index 121e389..dbdb59e 100644 --- a/app/classes/Framadate/Migration/From_0_0_to_0_8_Migration.php +++ b/app/classes/Framadate/Migration/From_0_0_to_0_8_Migration.php @@ -1,11 +1,29 @@ query('SHOW TABLES'); + $tables = $stmt->fetchAll(\PDO::FETCH_COLUMN); + + // Check if there is no tables but the MIGRATION_TABLE one + $diff = array_diff($tables, [Utils::table(MIGRATION_TABLE)]); + return count($diff) === 0; + } + /** * This methode is called only one time in the migration page. * diff --git a/app/classes/Framadate/Migration/From_0_8_to_0_9_Migration.php b/app/classes/Framadate/Migration/From_0_8_to_0_9_Migration.php index 33f1185..ea69991 100644 --- a/app/classes/Framadate/Migration/From_0_8_to_0_9_Migration.php +++ b/app/classes/Framadate/Migration/From_0_8_to_0_9_Migration.php @@ -13,6 +13,22 @@ class From_0_8_to_0_9_Migration implements Migration { function __construct() { } + /** + * This method could check if the execute method should be called. + * It is called before the execute method. + * + * @param \PDO $pdo The connection to database + * @return bool true is the Migration should be executed. + */ + function preCondition(\PDO $pdo) { + $stmt = $pdo->query('SHOW TABLES'); + $tables = $stmt->fetchAll(\PDO::FETCH_COLUMN); + + // Check if tables of v0.8 are presents + $diff = array_diff(['sondage', 'sujet_studs', 'comments', 'user_studs'], $tables); + return count($diff) === 0; + } + /** * This methode is called only one time in the migration page. * diff --git a/app/classes/Framadate/Migration/Migration.php b/app/classes/Framadate/Migration/Migration.php index e6d0eb9..b68362b 100644 --- a/app/classes/Framadate/Migration/Migration.php +++ b/app/classes/Framadate/Migration/Migration.php @@ -3,6 +3,15 @@ namespace Framadate\Migration; interface Migration { + /** + * This method could check if the execute method should be called. + * It is called before the execute method. + * + * @param \PDO $pdo The connection to database + * @return bool true is the Migration should be executed. + */ + function preCondition(\PDO $pdo); + /** * This methode is called only one time in the migration page. * diff --git a/migration.php b/migration.php index 70d7c6d..2617b84 100644 --- a/migration.php +++ b/migration.php @@ -57,7 +57,7 @@ foreach ($migrations as $migration) { $executed = $selectStmt->rowCount(); $selectStmt->closeCursor(); - if (!$executed) { + if (!$executed && $migration->preCondition($pdo)) { $migration->execute($pdo); if ($insertStmt->execute([$className])) { $countSucceeded++;