Migration: Add precondition on every Migration sub-classes

This commit is contained in:
Olivier Perez [a570709] 2015-01-02 09:08:07 +01:00
parent ca365ff348
commit 8f8956d70a
4 changed files with 44 additions and 1 deletions

View File

@ -1,11 +1,29 @@
<?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.
*

View File

@ -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.
*

View File

@ -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.
*

View File

@ -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++;