forked from craigrodway/printmaster
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupgrade.php
92 lines (71 loc) · 2.57 KB
/
upgrade.php
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/*
Copyright (C) 2013 Craig A Rodway.
This file is part of Print Master.
Print Master is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Print Master is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Print Master. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Database patch script for PrintMaster.
If this is the first time being run, the patch history table is created.
When it exists, it executes the required patch scripts in the ./db folder.
*/
// Include initialisation file
include_once('inc/core.php');
// Get DB schema so we can find out if the table exists.
$schema = new fSchema($db);
$tables = $schema->getTables();
// If table does not exist - create it and insert default entry.
if ( ! array_search('patch_history', $tables))
{
echo "Initialising patch history table...<br>";
$sql = "CREATE TABLE `patch_history` ( `num` smallint(5) unsigned NOT NULL) ENGINE='MyISAM' COLLATE 'utf8_unicode_ci'";
$db->query($sql);
$sql = "ALTER TABLE `patch_history` ADD PRIMARY KEY `num` (`num`)";
$db->query($sql);
$sql = "INSERT INTO `patch_history` SET `num` = 0";
$db->query($sql);
}
// Get the last patch level
$sql = "SELECT MAX(num) AS num FROM patch_history";
$row = $db->query($sql)->fetchRow();
$num = (int) $row['num'];
// Next patch to install
$next = $num + 1;
echo "Current patch level: $num.<br>";
// Get the highest patch level available
$patch_dir = new fDirectory(DOC_ROOT . '/db');
$patch_files = $patch_dir->scan('patch*.sql');
foreach ($patch_files as $file)
{
// Get patch number from filename
preg_match('/patch([0-9]+).sql/', $file->getName(), $matches);
$patch_num = $matches[1];
// If it's at the right level, run it.
if ($patch_num >= $next)
{
echo "Running " . $file->getName() . "... ";
$contents = $file->read();
try {
$db->query($contents);
$db->query("INSERT INTO patch_history SET num = %i ON DUPLICATE KEY UPDATE num = VALUES(num);", $patch_num);
echo "OK!<br>";
} catch (fException $e) {
echo "Error: " . $e->getMessage() . "<br>";
}
}
else
{
echo "Skipping " . $file->getName() . "...<br>";
}
}
echo '<br><br><a href="' . URL_ROOT . '">Continue to PrintMaster</a>';
/* End of file: ./upgrade.php */