2014-12-31 01:33:56 +01:00
|
|
|
<?php
|
2015-01-01 01:18:49 +01:00
|
|
|
use Framadate\Migration\From_0_0_to_0_8_Migration;
|
2014-12-31 01:33:56 +01:00
|
|
|
use Framadate\Migration\From_0_8_to_0_9_Migration;
|
|
|
|
use Framadate\Migration\Migration;
|
|
|
|
use Framadate\Utils;
|
|
|
|
|
2015-01-07 14:01:08 +01:00
|
|
|
include_once __DIR__ . '/../app/inc/init.php';
|
2014-12-31 01:33:56 +01:00
|
|
|
|
|
|
|
// List a Migration sub classes to execute
|
|
|
|
$migrations = [
|
2015-01-01 01:18:49 +01:00
|
|
|
new From_0_0_to_0_8_Migration(),
|
2014-12-31 01:33:56 +01:00
|
|
|
new From_0_8_to_0_9_Migration()
|
|
|
|
];
|
2014-12-31 14:02:36 +01:00
|
|
|
// ---------------------------------------
|
2014-12-31 01:33:56 +01:00
|
|
|
|
|
|
|
// Check if MIGRATION_TABLE already exists
|
|
|
|
$tables = $connect->allTables();
|
|
|
|
$pdo = $connect->getPDO();
|
2014-12-31 15:19:15 +01:00
|
|
|
$prefixedMigrationTable = Utils::table(MIGRATION_TABLE);
|
2014-12-31 01:33:56 +01:00
|
|
|
|
2014-12-31 15:19:15 +01:00
|
|
|
if (!in_array($prefixedMigrationTable, $tables)) {
|
2014-12-31 01:33:56 +01:00
|
|
|
$pdo->exec('
|
2014-12-31 15:19:15 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS `' . $prefixedMigrationTable . '` (
|
2014-12-31 01:33:56 +01:00
|
|
|
`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;');
|
|
|
|
|
2014-12-31 15:19:15 +01:00
|
|
|
output('Table ' . $prefixedMigrationTable . ' created.');
|
2014-12-31 01:33:56 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 15:19:15 +01:00
|
|
|
$selectStmt = $pdo->prepare('SELECT id FROM ' . $prefixedMigrationTable . ' WHERE name=?');
|
|
|
|
$insertStmt = $pdo->prepare('INSERT INTO ' . $prefixedMigrationTable . ' (name) VALUES (?)');
|
2014-12-31 14:02:36 +01:00
|
|
|
$countSucceeded = 0;
|
|
|
|
$countFailed = 0;
|
|
|
|
$countSkipped = 0;
|
2014-12-31 01:33:56 +01:00
|
|
|
|
|
|
|
// Loop on every Migration sub classes
|
2015-01-07 14:01:08 +01:00
|
|
|
$success = [];
|
|
|
|
$fail = [];
|
2014-12-31 01:33:56 +01:00
|
|
|
foreach ($migrations as $migration) {
|
|
|
|
$className = get_class($migration);
|
|
|
|
|
|
|
|
// Check if $className is a Migration sub class
|
|
|
|
if (!$migration instanceof Migration) {
|
2015-01-07 14:01:08 +01:00
|
|
|
$smarty->assign('error', 'The class ' . $className . ' is not a sub class of Framadate\\Migration\\Migration.');
|
|
|
|
$smarty->display('error.tpl');
|
2014-12-31 01:33:56 +01:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the Migration is already executed
|
|
|
|
$selectStmt->execute([$className]);
|
|
|
|
$executed = $selectStmt->rowCount();
|
|
|
|
$selectStmt->closeCursor();
|
|
|
|
|
2015-01-02 09:08:07 +01:00
|
|
|
if (!$executed && $migration->preCondition($pdo)) {
|
2014-12-31 01:33:56 +01:00
|
|
|
$migration->execute($pdo);
|
2014-12-31 14:02:36 +01:00
|
|
|
if ($insertStmt->execute([$className])) {
|
|
|
|
$countSucceeded++;
|
2015-01-07 14:01:08 +01:00
|
|
|
$success[] = $className;
|
2014-12-31 14:02:36 +01:00
|
|
|
} else {
|
|
|
|
$countFailed++;
|
2015-01-07 14:01:08 +01:00
|
|
|
$fail[] = $className;
|
2014-12-31 14:02:36 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$countSkipped++;
|
2014-12-31 01:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-12-31 14:02:36 +01:00
|
|
|
|
|
|
|
$countTotal = $countSucceeded + $countFailed + $countSkipped;
|
|
|
|
|
2015-01-07 14:01:08 +01:00
|
|
|
$smarty->assign('success', $success);
|
|
|
|
$smarty->assign('fail', $fail);
|
|
|
|
|
|
|
|
$smarty->assign('countSucceeded', $countSucceeded);
|
|
|
|
$smarty->assign('countFailed', $countFailed);
|
|
|
|
$smarty->assign('countSkipped', $countSkipped);
|
|
|
|
$smarty->assign('countTotal', $countTotal);
|
|
|
|
|
|
|
|
$smarty->assign('title', _('Migration'));
|
|
|
|
|
|
|
|
$smarty->display('admin/migration.tpl');
|