Skip to content
Snippets Groups Projects
Commit 8f8956d7 authored by Olivier Perez [a570709]'s avatar Olivier Perez [a570709]
Browse files

Migration: Add precondition on every Migration sub-classes

parent ca365ff3
No related branches found
No related tags found
No related merge requests found
<?php
namespace Framadate\Migration;
use Framadate\Utils;
class From_0_0_to_0_8_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 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.
*
......
......@@ -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.
*
......
......@@ -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.
*
......
......@@ -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++;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment