source: trunk/Packages/Common/Update.php

Last change on this file was 8, checked in by chronos, 18 months ago
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
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 public $InsertSampleDataMethod;
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(): ?int
22 {
23 $DbResult = $this->Database->select($this->VersionTable, '*', 'Id=1');
24 $Version = $DbResult->fetch_assoc();
25 return $Version['Revision'];
26 }
27
28 function IsInstalled(): bool
29 {
30 $DbResult = $this->Database->query('SHOW TABLES LIKE "'.$this->VersionTable.'"');
31 return $DbResult->num_rows > 0;
32 }
33
34 function IsUpToDate(): bool
35 {
36 return $this->Revision <= $this->GetDbVersion();
37 }
38
39 function Upgrade(): string
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(): void
62 {
63 $InstallMethod = $this->InstallMethod;
64 $InstallMethod($this);
65 }
66
67 function Uninstall(): void
68 {
69 }
70
71 function InsertSampleData(): void
72 {
73 $InstallMethod = $this->InsertSampleDataMethod;
74 $InstallMethod($this);
75 }
76
77 function Execute(string $Query): DatabaseResult
78 {
79 echo($Query.';<br/>');
80 flush();
81 return $this->Database->query($Query);
82 }
83}
Note: See TracBrowser for help on using the repository browser.