Fix and improve performance for migration from 0.8 to 0.9

This commit is contained in:
Olivier PEREZ 2015-02-26 10:44:49 +01:00
parent ca8be9443b
commit d30b6d6f7b
6 changed files with 69 additions and 23 deletions

View File

@ -80,10 +80,10 @@ foreach ($migrations as $migration) {
$migration->execute($pdo);
if ($insertStmt->execute([$className])) {
$countSucceeded++;
$success[] = $className;
$success[] = $migration->description();
} else {
$countFailed++;
$fail[] = $className;
$fail[] = $migration->description();
}
} else {
$countSkipped++;
@ -100,6 +100,7 @@ $smarty->assign('countSucceeded', $countSucceeded);
$smarty->assign('countFailed', $countFailed);
$smarty->assign('countSkipped', $countSkipped);
$smarty->assign('countTotal', $countTotal);
$smarty->assign('time', $total_time = round((microtime(true)-$_SERVER['REQUEST_TIME_FLOAT']), 4));
$smarty->assign('title', _('Migration'));

View File

@ -8,6 +8,15 @@ class From_0_0_to_0_8_Migration implements Migration {
function __construct() {
}
/**
* This method should describe in english what is the purpose of the migration class.
*
* @return string The description of the migration class
*/
function description() {
return "First installation of the Framadate application (v0.8)";
}
/**
* This method could check if the execute method should be called.
* It is called before the execute method.

View File

@ -13,6 +13,15 @@ class From_0_8_to_0_9_Migration implements Migration {
function __construct() {
}
/**
* This method should describe in english what is the purpose of the migration class.
*
* @return string The description of the migration class
*/
function description() {
return "From 0.8 to 0.9";
}
/**
* This method could check if the execute method should be called.
* It is called before the execute method.
@ -37,16 +46,16 @@ class From_0_8_to_0_9_Migration implements Migration {
*/
function execute(\PDO $pdo) {
$this->createPollTable($pdo);
$this->migrateFromSondageToPoll($pdo);
$this->createSlotTable($pdo);
$this->migrateFromSujetStudsToSlot($pdo);
$this->createCommentTable($pdo);
$this->migrateFromCommentsToComment($pdo);
$this->createSlotTable($pdo);
$this->createVoteTable($pdo);
$pdo->beginTransaction();
$this->migrateFromSondageToPoll($pdo);
$this->migrateFromCommentsToComment($pdo);
$this->migrateFromSujetStudsToSlot($pdo);
$this->migrateFromUserStudsToVote($pdo);
$pdo->commit();
$this->dropOldTables($pdo);
@ -124,7 +133,7 @@ CREATE TABLE IF NOT EXISTS `' . Utils::table('slot') . '` (
$prepared = $pdo->prepare('INSERT INTO ' . Utils::table('slot') . ' (`poll_id`, `title`, `moments`) VALUE (?,?,?)');
foreach ($slots as $slot) {
$prepared->execute([$slot->poll_id, $slot->title, $slot->moments]);
$prepared->execute([$slot->poll_id, $slot->title, !empty($slot->moments) ? $slot->moments : null]);
}
}
@ -181,18 +190,26 @@ INSERT INTO `' . Utils::table('vote') . '`
private function transformSujetToSlot($sujet) {
$slots = [];
$ex = explode(',', $sujet->sujet);
$isDatePoll = strpos($sujet->sujet, '@');
$lastSlot = null;
foreach ($ex as $atomicSlot) {
$values = explode('@', $atomicSlot);
if ($lastSlot == null || $lastSlot->title !== $values[0]) {
$lastSlot = new \stdClass();
$lastSlot->poll_id = $sujet->id_sondage;
$lastSlot->title = $values[0];
$lastSlot->moments = count($values) == 2 ? $values[1] : null;
$slots[] = $lastSlot;
} else {
$lastSlot->moments .= ',' . $values[1];
if ($isDatePoll === false) { // Classic poll
$slot = new \stdClass();
$slot->poll_id = $sujet->id_sondage;
$slot->title = $atomicSlot;
$slots[] = $slot;
} else { // Date poll
$values = explode('@', $atomicSlot);
if ($lastSlot == null || $lastSlot->title !== $values[0]) {
$lastSlot = new \stdClass();
$lastSlot->poll_id = $sujet->id_sondage;
$lastSlot->title = $values[0];
$lastSlot->moments = count($values) == 2 ? $values[1] : '-';
$slots[] = $lastSlot;
} else {
$lastSlot->moments .= ',' . (count($values) == 2 ? $values[1] : '-');
}
}
}
@ -205,5 +222,4 @@ INSERT INTO `' . Utils::table('vote') . '`
$pdo->exec('DROP TABLE `user_studs`');
$pdo->exec('DROP TABLE `sondage`');
}
}

View File

@ -13,6 +13,15 @@ class From_0_9_to_0_9_1_Migration implements Migration {
function __construct() {
}
/**
* This method should describe in english what is the purpose of the migration class.
*
* @return string The description of the migration class
*/
function description() {
return "From 0.9 to 0.9.1";
}
/**
* This method could check if the execute method should be called.
* It is called before the execute method.
@ -24,7 +33,7 @@ class From_0_9_to_0_9_1_Migration implements Migration {
$stmt = $pdo->query('SHOW TABLES');
$tables = $stmt->fetchAll(\PDO::FETCH_COLUMN);
// Check if tables of v0.8 are presents
// Check if tables of v0.9 are presents
$diff = array_diff([Utils::table('poll'), Utils::table('slot'), Utils::table('vote'), Utils::table('comment')], $tables);
return count($diff) === 0;
}

View File

@ -3,12 +3,19 @@ namespace Framadate\Migration;
interface Migration {
/**
* This method should describe in english what is the purpose of the migration class.
*
* @return string The description of the migration class
*/
function description();
/**
* 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.
* @return bool true if the Migration should be executed
*/
function preCondition(\PDO $pdo);
@ -16,7 +23,7 @@ interface Migration {
* This methode is called only one time in the migration page.
*
* @param \PDO $pdo The connection to database
* @return bool true is the execution succeeded
* @return bool true if the execution succeeded
*/
function execute(\PDO $pdo);

View File

@ -31,5 +31,9 @@
{/foreach}
</ul>
</div>
<div class="col-xs-12 well well-sm">
{_('Page generated in')} {$time} {_('secondes')}
</div>
</div>
{/block}