source: trunk/Packages/Common/Update.php@ 90

Last change on this file since 90 was 90, checked in by chronos, 6 years ago
  • Modified: Updated Common package files.
  • Modified: Updated link to project website.
File size: 1.9 KB
Line 
1<?php
2
3class UpdateManager
4{
5 var $Revision;
6 var $Trace;
7 var $VersionTable;
8 /* @var Database */
9 var $Database;
10 var $InstallMethod;
11
12 function __construct()
13 {
14 $this->Revision = 0;
15 $this->Trace = array();
16 $this->VersionTable = 'SystemVersion';
17 $this->InstallMethod = 'FullInstall';
18 $this->InsertSampleDataMethod = 'InsertSampleData';
19 }
20
21 function GetDbVersion()
22 {
23 $DbResult = $this->Database->select($this->VersionTable, '*', 'Id=1');
24 $Version = $DbResult->fetch_assoc();
25 return($Version['Revision']);
26 }
27
28 function IsInstalled()
29 {
30 $DbResult = $this->Database->query('SHOW TABLES LIKE "'.$this->VersionTable.'"');
31 return($DbResult->num_rows > 0);
32 }
33
34 function IsUpToDate()
35 {
36 return($this->Revision <= $this->GetDbVersion());
37 }
38
39 function Upgrade()
40 {
41 $DbRevision = $this->GetDbVersion();
42 $Output = 'Počáteční revize databáze: '.$DbRevision.'<br/>';
43 while($this->Revision > $DbRevision)
44 {
45 if(!array_key_exists($DbRevision, $this->Trace))
46 die('Missing upgrade trace for revision '.$DbRevision);
47 $TraceItem = $this->Trace[$DbRevision];
48 $Output .= 'Aktualizace na verzi '.$TraceItem['Revision'].':<br/>';
49 // Show applied SQL queries immediatelly
50 echo($Output);
51 $Output = '';
52 $RevUpdate = $TraceItem['Function'];
53 $RevUpdate($this);
54 $DbRevision = $TraceItem['Revision'];
55 $this->Database->query('UPDATE `'.$this->VersionTable.'` SET `Revision`= '.
56 $TraceItem['Revision'].' WHERE `Id`=1');
57 }
58 return($Output);
59 }
60
61 function Install()
62 {
63 $InstallMethod = $this->InstallMethod;
64 $InstallMethod($this);
65 $this->Update();
66 }
67
68 function Uninstall()
69 {
70
71 }
72
73 function InsertSampleData()
74 {
75 $InstallMethod = $this->InsertSampleDataMethod;
76 $InstallMethod($this);
77 }
78
79 function Execute($Query)
80 {
81 echo($Query.';<br/>');
82 flush();
83 return($this->Database->query($Query));
84 }
85}
Note: See TracBrowser for help on using the repository browser.