source: tools/updater/Updater.php

Last change on this file was 4, checked in by chronos, 12 years ago
  • Přidáno: Zkušební třída pro automatické udržování verze struktury v databázi.
File size: 3.0 KB
Line 
1<?php
2
3include('Database.php');
4
5class DatabaseVersioning
6{
7 var $Version;
8 var $VersionInDb;
9 var $FullStructure;
10 var $Database;
11 var $Updates;
12
13 function __construct($Database)
14 {
15 $this->Database = $Database;
16 }
17
18 function Check()
19 {
20 ksort($this->Updates);
21 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemVersion` (
22 `Id` int(11) NOT NULL AUTO_INCREMENT,
23 `Version` varchar(255) NOT NULL,
24 `InstallTime` DATETIME NOT NULL,
25 PRIMARY KEY (`Id`)
26) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
27 $DbResult = $this->Database->query('SELECT `Version` FROM `SystemVersion` WHERE `Id`=1');
28 if($DbResult->num_rows > 0)
29 {
30 $DbRow = $DbResult->fetch_assoc();
31 $this->VersionInDb = $DbRow['Version'];
32 echo('DbVersion: '.$this->VersionInDb."\r\n");
33 $this->Update();
34 } else
35 {
36 $this->VersionInDb = key(array_slice($this->Updates, -1, 1, TRUE));
37 echo('Importing full structure'."\r\n");
38 echo('DbVersion: '.$this->VersionInDb."\r\n");
39 $this->Database->query($this->FullStructure);
40 $this->Database->insert('SystemVersion', array('Id' => 1, 'Version' => $this->VersionInDb,
41 'InstallTime' => 'NOW()'));
42 }
43 }
44
45 function Update()
46 {
47 $I = $this->VersionInDb;
48 $I++;
49 while(true)
50 {
51 if(array_key_exists($I, $this->Updates))
52 {
53 echo('Update '.$I.': '.$this->Updates[$I]."\r\n");
54 $this->Database->query($this->Updates[$I]);
55 $this->Database->update('SystemVersion', '`Id`=1', array('Version' => $I, 'InstallTime' => 'NOW()'));
56 $I++;
57 } else break;
58 }
59 }
60}
61
62class Application
63{
64 function CheckVersion()
65 {
66 $DatabaseVersioning = new DatabaseVersioning($this->Database);
67 $DatabaseVersioning->FullStructure = 'CREATE TABLE IF NOT EXISTS `Server` (
68 `Id` int(11) NOT NULL AUTO_INCREMENT,
69 `Name` varchar(255) NOT NULL,
70 `IpAddress` varchar(16) NOT NULL,
71 PRIMARY KEY (`Id`)
72) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
73
74CREATE TABLE IF NOT EXISTS `User` (
75 `Id` int(11) NOT NULL AUTO_INCREMENT,
76 `Name` varchar(255) NOT NULL,
77 `Password` varchar(255) NOT NULL,
78 `FullName` varchar(255) NOT NULL,
79 PRIMARY KEY (`Id`)
80) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;';
81 $DatabaseVersioning->Updates = array(
82 1 => 'CREATE TABLE `updater`.`Server` (
83`Id` INT NOT NULL AUTO_INCREMENT ,
84`Name` VARCHAR( 255 ) NOT NULL ,
85PRIMARY KEY ( `Id` )
86) ENGINE = InnoDB;',
87 2 => 'ALTER TABLE `Server` ADD `IpAddress` VARCHAR( 16 ) NOT NULL;',
88 3 => 'CREATE TABLE `updater`.`User` (
89`Id` INT NOT NULL AUTO_INCREMENT ,
90`Name` VARCHAR( 255 ) NOT NULL ,
91`Password` VARCHAR( 255 ) NOT NULL ,
92PRIMARY KEY ( `Id` )
93) ENGINE = InnoDB;',
94 4 => 'ALTER TABLE `User` ADD `FullName` VARCHAR( 255 ) NOT NULL;',
95 5 => 'ALTER TABLE `User` ADD `RegistrationTime` DATETIME NOT NULL;',
96);
97 $DatabaseVersioning->Check();
98 }
99}
100
101$Application = new Application();
102$Application->Database = new Database('localhost', 'updater', 'senoslama', 'updater');
103$Application->CheckVersion();
104
105?>
Note: See TracBrowser for help on using the repository browser.