source: Common/Update.php

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