aboutsummaryrefslogtreecommitdiffstats
path: root/core/migrations.php
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-06-24 21:09:24 +0200
committerThomas Lange <code@nerdmind.de>2021-06-24 21:09:24 +0200
commitd69d7e82b8bbb567668c935ace848c7dcf750b08 (patch)
treedfad6bc5ce73262e958c92a3c3875c619c28ffc8 /core/migrations.php
parent55ae320e7cfd710f3ea0f295c880619217db2220 (diff)
downloadblog-d69d7e82b8bbb567668c935ace848c7dcf750b08.tar.gz
blog-d69d7e82b8bbb567668c935ace848c7dcf750b08.tar.xz
blog-d69d7e82b8bbb567668c935ace848c7dcf750b08.zip
Implement database schema Migrator
This commit implements the new database schema Migrator which keeps track of the on-disk schema and the schema used by the codebase. It tries to makes future database schema upgrades user-friendlier.
Diffstat (limited to 'core/migrations.php')
-rw-r--r--core/migrations.php43
1 files changed, 43 insertions, 0 deletions
diff --git a/core/migrations.php b/core/migrations.php
new file mode 100644
index 0000000..f2c9714
--- /dev/null
+++ b/core/migrations.php
@@ -0,0 +1,43 @@
+<?php
+#===============================================================================
+# Get Migrator singleton
+#===============================================================================
+$Migrator = Application::getMigrator();
+
+#===============================================================================
+# Check for outstanding database schema migrations
+#===============================================================================
+if($Migrator->isMigrationNeeded()) {
+ @session_start();
+
+ Application::set('TEMPLATE.NAME', Application::get('ADMIN.TEMPLATE'));
+ Application::set('TEMPLATE.LANG', Application::get('ADMIN.LANGUAGE'));
+ Application::getLanguage(TRUE); // Force recreation of Language object
+
+ if(HTTP::issetPOST(['token' => Application::getSecurityToken()], 'run')) {
+ if(!$migrated = $Migrator->runMigrations()) {
+ Application::exit('CONFUSED: No migrations were performed?!');
+ }
+ }
+
+ $Template = Template\Factory::build('migration');
+ $Template->set('MIGRATION', [
+ 'LIST' => $Migrator->getMigrations(),
+ 'SUCCESSFUL' => $migrated ?? [],
+ 'SCHEMA_VERSION' => [
+ 'DATABASE' => $Migrator->getVersionFromTable(),
+ 'CODEBASE' => $Migrator::CURRENT_SCHEMA_VERSION
+ ],
+ ]);
+
+ Application::exit($Template);
+}
+
+#===============================================================================
+# Check for an unsupported downgrade attempt
+#===============================================================================
+else if($Migrator->isDowngradeAttempt()) {
+ throw new Exception('MIGRATOR: The schema version used by *your* database is
+ higher than the schema version defined in the codebase. It is officially
+ not supported to automatically downgrade the database schema version!');
+}