migration.php 4.58 KB
Newer Older
1
<?php
2
3
4
5
6
7
/**
 * This software is governed by the CeCILL-B license. If a copy of this license
 * is not distributed with this file, you can obtain one at
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
 *
 * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
8
 * Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
9
10
11
12
13
14
15
16
17
18
19
 *
 * =============================
 *
 * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
 * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
 *
 * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
 * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
 */

Thomas Citharel's avatar
Thomas Citharel committed
20
use Framadate\Migration\AddColumn_hidden_In_poll_For_0_9;
21
22
23
use Framadate\Migration\AddColumn_receiveNewComments_For_0_9;
use Framadate\Migration\AddColumn_uniqId_In_vote_For_0_9;
use Framadate\Migration\AddColumn_ValueMax_In_poll_For_1_1;
Thomas Citharel's avatar
Thomas Citharel committed
24
25
26
use Framadate\Migration\AddColumns_password_hash_And_results_publicly_visible_In_poll_For_0_9;
use Framadate\Migration\Alter_Comment_table_adding_date;
use Framadate\Migration\Alter_Comment_table_for_name_length;
Thomas Citharel's avatar
Thomas Citharel committed
27
use Framadate\Migration\Fix_MySQL_No_Zero_Date;
Thomas Citharel's avatar
Thomas Citharel committed
28
29
use Framadate\Migration\From_0_0_to_0_8_Migration;
use Framadate\Migration\From_0_8_to_0_9_Migration;
30
use Framadate\Migration\Generate_uniqId_for_old_votes;
31
use Framadate\Migration\Increase_pollId_size;
32
use Framadate\Migration\Migration;
33
use Framadate\Migration\RPadVotes_from_0_8;
34
35
use Framadate\Utils;

36
include_once __DIR__ . '/../app/inc/init.php';
37

38
39
set_time_limit(300);

40
41
// List a Migration sub classes to execute
$migrations = [
42
    new From_0_0_to_0_8_Migration(),
43
    new From_0_8_to_0_9_Migration(),
Antonin's avatar
Antonin committed
44
    new AddColumn_receiveNewComments_For_0_9(),
45
46
    new AddColumn_uniqId_In_vote_For_0_9(),
    new AddColumn_hidden_In_poll_For_0_9(),
47
    new AddColumn_ValueMax_In_poll_For_1_1(),
48
    new Generate_uniqId_for_old_votes(),
49
    new RPadVotes_from_0_8(),
50
    new Alter_Comment_table_for_name_length(),
Antonin's avatar
Antonin committed
51
52
    new Alter_Comment_table_adding_date(),
    new AddColumns_password_hash_And_results_publicly_visible_In_poll_For_0_9(),
Thomas Citharel's avatar
Thomas Citharel committed
53
54
55
    new Increase_pollId_size(),
    new AddColumn_ValueMax_In_poll_For_1_1(),
    new Fix_MySQL_No_Zero_Date(),
56
];
57
// ---------------------------------------
58
59

// Check if MIGRATION_TABLE already exists
Thomas Citharel's avatar
Thomas Citharel committed
60
/** @var \Framadate\FramaDB $connect */
61
62
$tables = $connect->allTables();
$pdo = $connect->getPDO();
63
$prefixedMigrationTable = Utils::table(MIGRATION_TABLE);
64

65
if (!in_array($prefixedMigrationTable, $tables, true)) {
66
    $pdo->exec('
67
CREATE TABLE IF NOT EXISTS `' . $prefixedMigrationTable . '` (
68
69
70
71
72
73
74
75
76
  `id`   INT(11)  UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` TEXT              NOT NULL,
  `execute_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
)
  ENGINE = MyISAM
  DEFAULT CHARSET = utf8;');
}

77
78
$selectStmt = $pdo->prepare('SELECT id FROM `' . $prefixedMigrationTable . '` WHERE name=?');
$insertStmt = $pdo->prepare('INSERT INTO `' . $prefixedMigrationTable . '` (name) VALUES (?)');
79
80
81
$countSucceeded = 0;
$countFailed = 0;
$countSkipped = 0;
82
83

// Loop on every Migration sub classes
84
85
$success = [];
$fail = [];
86
87
88
89
90
foreach ($migrations as $migration) {
    $className = get_class($migration);

    // Check if $className is a Migration sub class
    if (!$migration instanceof Migration) {
91
92
        $smarty->assign('error', 'The class ' . $className . ' is not a sub class of Framadate\\Migration\\Migration.');
        $smarty->display('error.tpl');
93
94
95
96
97
98
99
100
        exit;
    }

    // Check if the Migration is already executed
    $selectStmt->execute([$className]);
    $executed = $selectStmt->rowCount();
    $selectStmt->closeCursor();

101
    if (!$executed && $migration->preCondition($pdo)) {
102
        $migration->execute($pdo);
103
104
        if ($insertStmt->execute([$className])) {
            $countSucceeded++;
105
            $success[] = $migration->description();
106
107
        } else {
            $countFailed++;
108
            $fail[] = $migration->description();
109
110
111
        }
    } else {
        $countSkipped++;
112
113
    }
}
114
115
116

$countTotal = $countSucceeded + $countFailed + $countSkipped;

117
118
119
120
121
122
123
$smarty->assign('success', $success);
$smarty->assign('fail', $fail);

$smarty->assign('countSucceeded', $countSucceeded);
$smarty->assign('countFailed', $countFailed);
$smarty->assign('countSkipped', $countSkipped);
$smarty->assign('countTotal', $countTotal);
124
$smarty->assign('time', $total_time = round((microtime(true)-$_SERVER['REQUEST_TIME_FLOAT']), 4));
125

126
$smarty->assign('title', __('Admin', 'Migration'));
127
128

$smarty->display('admin/migration.tpl');