aboutsummaryrefslogtreecommitdiffstats
path: root/core/migrations.php
blob: 1ddeefd9192f8efe257391edbe99738600ecca1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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'));

	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!');
}