1 |
(* |
2 |
* IBX For Lazarus (Firebird Express) |
3 |
* |
4 |
* The contents of this file are subject to the Initial Developer's |
5 |
* Public License Version 1.0 (the "License"); you may not use this |
6 |
* file except in compliance with the License. You may obtain a copy |
7 |
* of the License here: |
8 |
* |
9 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl |
10 |
* |
11 |
* Software distributed under the License is distributed on an "AS |
12 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
13 |
* implied. See the License for the specific language governing rights |
14 |
* and limitations under the License. |
15 |
* |
16 |
* The Initial Developer of the Original Code is Tony Whyman. |
17 |
* |
18 |
* The Original Code is (C) 2014 Tony Whyman, MWA Software |
19 |
* (http://www.mwasoftware.co.uk). |
20 |
* |
21 |
* All Rights Reserved. |
22 |
* |
23 |
* Contributor(s): ______________________________________. |
24 |
* |
25 |
*) |
26 |
unit IBXUpgradeDatabaseDlg; |
27 |
|
28 |
{$mode objfpc}{$H+} |
29 |
|
30 |
interface |
31 |
|
32 |
uses |
33 |
Classes, SysUtils, FileUtil, Forms, Controls, Dialogs, StdCtrls, |
34 |
ComCtrls, ExtCtrls, IBDatabase, ibxscript, IBXUpgradeConfFile, IBXServices; |
35 |
|
36 |
type |
37 |
TGetDatabaseVersionNo = procedure (Sender: TObject; var VersionNo: integer) of object; |
38 |
|
39 |
{ TUpgradeDatabaseDlg } |
40 |
|
41 |
TUpgradeDatabaseDlg = class(TForm) |
42 |
UpdateTransaction: TIBTransaction; |
43 |
IBXScript: TIBXScript; |
44 |
Label1: TLabel; |
45 |
Panel1: TPanel; |
46 |
ProgressBar1: TProgressBar; |
47 |
Status: TLabel; |
48 |
Timer1: TTimer; |
49 |
procedure FormShow(Sender: TObject); |
50 |
procedure HandleCompletionEvent(Sender: TObject); |
51 |
procedure IBXScriptLogProc(Sender: TObject; Msg: string); |
52 |
procedure IBXScriptProgressEvent(Sender: TObject; Reset: boolean; |
53 |
value: integer); |
54 |
private |
55 |
FOnGetDatabaseVersionNo: TGetDatabaseVersionNo; |
56 |
FOnUpgradeStepCompleted: TNotifyEvent; |
57 |
FBackupService: TIBXServerSideBackupService; |
58 |
{ private declarations } |
59 |
FUpgradeLog: TStrings; |
60 |
FTargetVersionNo: integer; |
61 |
FUpgradeConf: TUpgradeConfFile; |
62 |
FArchiveStub: string; |
63 |
procedure DoUpdate(Data: PtrInt); |
64 |
function CurrentDBVersionNo: integer; |
65 |
public |
66 |
{ public declarations } |
67 |
SuccessfulCompletion: boolean; |
68 |
constructor Create(theOwner: TComponent); override; |
69 |
destructor Destroy; override; |
70 |
procedure Add2Log(Msg: string); |
71 |
property OnGetDatabaseVersionNo: TGetDatabaseVersionNo read FOnGetDatabaseVersionNo |
72 |
write FOnGetDatabaseVersionNo; |
73 |
property OnUpgradeStepCompleted: TNotifyEvent read FOnUpgradeStepCompleted write FOnUpgradeStepCompleted; |
74 |
end; |
75 |
|
76 |
|
77 |
var |
78 |
UpgradeDatabaseDlg: TUpgradeDatabaseDlg; |
79 |
|
80 |
function RunUpgradeDatabase(aDatabase: TIBDatabase; |
81 |
aBackupService: TIBXServerSideBackupService; |
82 |
UpgradeConf: TUpgradeConfFile; |
83 |
ArchiveStub: string; |
84 |
TargetVersionNo: integer; |
85 |
aOnGetDatabaseVersionNo: TGetDatabaseVersionNo; |
86 |
aOnUpgradeStepCompleted: TNotifyEvent): boolean; |
87 |
|
88 |
|
89 |
implementation |
90 |
|
91 |
{$R *.lfm} |
92 |
|
93 |
uses IBXViewLogDig, IBXSaveDatabaseDlg; |
94 |
|
95 |
resourcestring |
96 |
sNoUpgradeConf = 'An Upgrade Conf file must be provided'; |
97 |
sUpdateMsg = 'Applying Update from %s'; |
98 |
sUpdateStarted = '%s Update Started'; |
99 |
sUpdateEnded = '%s Update Completed'; |
100 |
sUpdateFailed = 'Update Failed - %s'; |
101 |
|
102 |
function RunUpgradeDatabase(aDatabase: TIBDatabase; |
103 |
aBackupService: TIBXServerSideBackupService; |
104 |
UpgradeConf: TUpgradeConfFile; ArchiveStub: string; TargetVersionNo: integer; |
105 |
aOnGetDatabaseVersionNo: TGetDatabaseVersionNo; |
106 |
aOnUpgradeStepCompleted: TNotifyEvent): boolean; |
107 |
begin |
108 |
if not assigned(UpgradeConf) then |
109 |
raise Exception.Create(sNoUpgradeConf); |
110 |
|
111 |
with TUpgradeDatabaseDlg.Create(Application) do |
112 |
try |
113 |
FTargetVersionNo := TargetVersionNo; |
114 |
FBackupService := aBackupService; |
115 |
FUpgradeConf := UpgradeConf; |
116 |
FArchiveStub := ArchiveStub; |
117 |
IBXScript.Database := aDatabase; |
118 |
UpdateTransaction.DefaultDatabase := aDatabase; |
119 |
IBXScript.GetParamValue := @UpgradeConf.GetParamValue; |
120 |
OnGetDatabaseVersionNo := aOnGetDatabaseVersionNo; |
121 |
OnUpgradeStepCompleted := aOnUpgradeStepCompleted; |
122 |
Result := ShowModal = mrOK; |
123 |
finally |
124 |
Free |
125 |
end; |
126 |
end; |
127 |
|
128 |
{ TUpgradeDatabaseDlg } |
129 |
|
130 |
procedure TUpgradeDatabaseDlg.FormShow(Sender: TObject); |
131 |
begin |
132 |
ProgressBar1.Position := 0; |
133 |
Status.Caption := ''; |
134 |
FUpgradeLog.Clear; |
135 |
Application.QueueAsyncCall(@DoUpdate,0); |
136 |
end; |
137 |
|
138 |
procedure TUpgradeDatabaseDlg.HandleCompletionEvent(Sender: TObject); |
139 |
begin |
140 |
Timer1.Enabled := false; |
141 |
if not SuccessfulCompletion then |
142 |
begin |
143 |
ShowViewLogDlg(FUpgradeLog); |
144 |
ModalResult := mrCancel |
145 |
end |
146 |
else |
147 |
ModalResult := mrOK; |
148 |
end; |
149 |
|
150 |
procedure TUpgradeDatabaseDlg.IBXScriptLogProc(Sender: TObject; Msg: string); |
151 |
begin |
152 |
Add2Log(Msg); |
153 |
end; |
154 |
|
155 |
procedure TUpgradeDatabaseDlg.IBXScriptProgressEvent(Sender: TObject; |
156 |
Reset: boolean; value: integer); |
157 |
begin |
158 |
if Reset then |
159 |
begin |
160 |
with ProgressBar1 do |
161 |
begin |
162 |
Position := 0; |
163 |
Max := value; |
164 |
end; |
165 |
end; |
166 |
ProgressBar1.StepIt; |
167 |
Application.ProcessMessages; |
168 |
end; |
169 |
|
170 |
procedure TUpgradeDatabaseDlg.Add2Log(Msg: string); |
171 |
begin |
172 |
FUpgradeLog.Add(Msg); |
173 |
end; |
174 |
|
175 |
procedure TUpgradeDatabaseDlg.DoUpdate(Data: PtrInt); |
176 |
var UpdateAvailable: boolean; |
177 |
UpgradeInfo: TUpgradeInfo; |
178 |
DBArchive: string; |
179 |
LastVersionNo: integer; |
180 |
CurVersionNo: integer; |
181 |
begin |
182 |
SuccessfulCompletion := true; |
183 |
try |
184 |
CurVersionNo := CurrentDBVersionNo; |
185 |
repeat |
186 |
if CurVersionNo >= FTargetVersionNo then break; |
187 |
LastVersionNo := CurVersionNo; |
188 |
UpdateAvailable := FUpgradeConf.GetUpgradeInfo(CurVersionNo+1,UpgradeInfo); |
189 |
if UpdateAvailable then |
190 |
begin |
191 |
if UpgradeInfo.BackupDB then |
192 |
begin |
193 |
CreateDir(ExtractFileDir(FArchiveStub)); |
194 |
DBArchive := FArchiveStub + '.' + IntToStr(CurrentDBVersionNo) + '.gbk'; |
195 |
SaveDatabaseToArchive(FBackupService,DBArchive); |
196 |
end; |
197 |
Add2Log(UpgradeInfo.UserMessage); |
198 |
Status.Caption := UpgradeInfo.UserMessage; |
199 |
Application.ProcessMessages; |
200 |
Add2Log(Format(sUpdateMsg,[UpgradeInfo.UpdateSQLFile])); |
201 |
Add2Log(Format(sUpdateStarted,[DateTimeToStr(Now)])); |
202 |
if not IBXScript.PerformUpdate(UpgradeInfo.UpdateSQLFile,true) then |
203 |
begin |
204 |
Add2Log(Format(sUpdateFailed,[DateTimeToStr(Now)])); |
205 |
SuccessfulCompletion := false; |
206 |
UpdateTransaction.Rollback; |
207 |
break; |
208 |
end; |
209 |
UpdateTransaction.Commit; |
210 |
Add2Log(Format(sUpdateEnded,[DateTimeToStr(Now)])); |
211 |
if assigned(FOnUpgradeStepCompleted) then |
212 |
OnUpgradeStepCompleted(self); |
213 |
end; |
214 |
CurVersionNo := CurrentDBVersionNo; |
215 |
until not UpdateAvailable or (LastVersionNo = CurVersionNo); |
216 |
except on E:Exception do |
217 |
begin |
218 |
SuccessfulCompletion := false; |
219 |
Add2Log(E.Message); |
220 |
end; |
221 |
end; |
222 |
Timer1.Enabled := true; |
223 |
end; |
224 |
|
225 |
function TUpgradeDatabaseDlg.CurrentDBVersionNo: integer; |
226 |
begin |
227 |
if assigned(FOnGetDatabaseVersionNo) then |
228 |
OnGetDatabaseVersionNo(self,Result) |
229 |
else |
230 |
Result := 0; |
231 |
end; |
232 |
|
233 |
|
234 |
constructor TUpgradeDatabaseDlg.Create(theOwner: TComponent); |
235 |
begin |
236 |
inherited Create(theOwner); |
237 |
FUpgradeLog := TStringList.Create; |
238 |
end; |
239 |
|
240 |
destructor TUpgradeDatabaseDlg.Destroy; |
241 |
begin |
242 |
if assigned(FUpgradeLog) then |
243 |
FUpgradeLog.Free; |
244 |
inherited Destroy; |
245 |
end; |
246 |
|
247 |
end. |
248 |
|