Move migration into separate file + add into admin panel

This fixes some rare bugs when database is no up-to-date and someone enters admin panel
This commit is contained in:
slawkens 2020-05-24 18:43:26 +02:00
parent d1c50f00a0
commit dbe83f8a74
3 changed files with 24 additions and 20 deletions

View File

@ -34,6 +34,7 @@ $hooks->load();
require SYSTEM . 'status.php'; require SYSTEM . 'status.php';
require SYSTEM . 'login.php'; require SYSTEM . 'login.php';
require SYSTEM . 'migrate.php';
require ADMIN . 'includes/functions.php'; require ADMIN . 'includes/functions.php';
$twig->addGlobal('config', $config); $twig->addGlobal('config', $config);

View File

@ -186,26 +186,7 @@ if(!$db->hasTable('myaac_account_actions')) {
throw new RuntimeException('Seems that the table <strong>myaac_account_actions</strong> of MyAAC doesn\'t exist in the database. This is a fatal error. You can try to reinstall MyAAC by visiting <a href="' . BASE_URL . 'install">this</a> url.'); throw new RuntimeException('Seems that the table <strong>myaac_account_actions</strong> of MyAAC doesn\'t exist in the database. This is a fatal error. You can try to reinstall MyAAC by visiting <a href="' . BASE_URL . 'install">this</a> url.');
} }
// database migrations require SYSTEM . 'migrate.php';
$tmp = '';
if(fetchDatabaseConfig('database_version', $tmp)) { // we got version
$tmp = (int)$tmp;
if($tmp < DATABASE_VERSION) { // import if older
$db->revalidateCache();
for($i = $tmp + 1; $i <= DATABASE_VERSION; $i++) {
require SYSTEM . 'migrations/' . $i . '.php';
updateDatabaseConfig('database_version', $i);
}
}
}
else { // register first version
registerDatabaseConfig('database_version', 0);
$db->revalidateCache();
for($i = 1; $i <= DATABASE_VERSION; $i++) {
require SYSTEM . 'migrations/' . $i . '.php';
updateDatabaseConfig('database_version', $i);
}
}
$hooks->trigger(HOOK_STARTUP); $hooks->trigger(HOOK_STARTUP);

22
system/migrate.php Normal file
View File

@ -0,0 +1,22 @@
<?php
// database migrations
$tmp = '';
if(fetchDatabaseConfig('database_version', $tmp)) { // we got version
$tmp = (int)$tmp;
if($tmp < DATABASE_VERSION) { // import if older
$db->revalidateCache();
for($i = $tmp + 1; $i <= DATABASE_VERSION; $i++) {
require SYSTEM . 'migrations/' . $i . '.php';
updateDatabaseConfig('database_version', $i);
}
}
}
else { // register first version
registerDatabaseConfig('database_version', 0);
$db->revalidateCache();
for($i = 1; $i <= DATABASE_VERSION; $i++) {
require SYSTEM . 'migrations/' . $i . '.php';
updateDatabaseConfig('database_version', $i);
}
}