ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/iblocaldb/gui/IBXUpgradeDatabaseDlg.pas
Revision: 345
Committed: Mon Aug 23 14:22:29 2021 UTC (2 years, 8 months ago) by tony
Content type: text/x-pascal
File size: 7528 byte(s)
Log Message:
Merged into public release

File Contents

# Content
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 FSectionHeaderTemplate: string;
63 FArchiveStub: string;
64 procedure DoUpdate(Data: PtrInt);
65 function CurrentDBVersionNo: integer;
66 public
67 { public declarations }
68 SuccessfulCompletion: boolean;
69 constructor Create(theOwner: TComponent); override;
70 destructor Destroy; override;
71 procedure Add2Log(Msg: string);
72 property OnGetDatabaseVersionNo: TGetDatabaseVersionNo read FOnGetDatabaseVersionNo
73 write FOnGetDatabaseVersionNo;
74 property OnUpgradeStepCompleted: TNotifyEvent read FOnUpgradeStepCompleted write FOnUpgradeStepCompleted;
75 end;
76
77
78 var
79 UpgradeDatabaseDlg: TUpgradeDatabaseDlg;
80
81 function RunUpgradeDatabase(aDatabase: TIBDatabase;
82 aBackupService: TIBXServerSideBackupService;
83 UpgradeConf: TUpgradeConfFile;
84 SectionHeaderTemplate: string;
85 ArchiveStub: string;
86 TargetVersionNo: integer;
87 aOnGetDatabaseVersionNo: TGetDatabaseVersionNo;
88 aOnUpgradeStepCompleted: TNotifyEvent): boolean;
89
90
91 implementation
92
93 {$R *.lfm}
94
95 uses IBXViewLogDig, IBXSaveDatabaseDlg;
96
97 resourcestring
98 sNoUpgradeConf = 'An Upgrade Conf file must be provided';
99 sUpdateMsg = 'Applying Update from %s';
100 sUpdateStarted = '%s Update Started';
101 sUpdateEnded = '%s Update Completed';
102 sUpdateFailed = 'Update Failed - %s';
103
104 function RunUpgradeDatabase(aDatabase: TIBDatabase;
105 aBackupService: TIBXServerSideBackupService; UpgradeConf: TUpgradeConfFile;
106 SectionHeaderTemplate: string; ArchiveStub: string; TargetVersionNo: integer;
107 aOnGetDatabaseVersionNo: TGetDatabaseVersionNo;
108 aOnUpgradeStepCompleted: TNotifyEvent): boolean;
109 begin
110 if not assigned(UpgradeConf) then
111 raise Exception.Create(sNoUpgradeConf);
112
113 with TUpgradeDatabaseDlg.Create(Application) do
114 try
115 FTargetVersionNo := TargetVersionNo;
116 FBackupService := aBackupService;
117 FUpgradeConf := UpgradeConf;
118 FSectionHeaderTemplate := SectionHeaderTemplate;
119 FArchiveStub := ArchiveStub;
120 IBXScript.Database := aDatabase;
121 UpdateTransaction.DefaultDatabase := aDatabase;
122 IBXScript.GetParamValue := @UpgradeConf.GetParamValue;
123 OnGetDatabaseVersionNo := aOnGetDatabaseVersionNo;
124 OnUpgradeStepCompleted := aOnUpgradeStepCompleted;
125 Result := ShowModal = mrOK;
126 finally
127 Free
128 end;
129 end;
130
131 { TUpgradeDatabaseDlg }
132
133 procedure TUpgradeDatabaseDlg.FormShow(Sender: TObject);
134 begin
135 ProgressBar1.Position := 0;
136 Status.Caption := '';
137 FUpgradeLog.Clear;
138 Application.QueueAsyncCall(@DoUpdate,0);
139 end;
140
141 procedure TUpgradeDatabaseDlg.HandleCompletionEvent(Sender: TObject);
142 begin
143 Timer1.Enabled := false;
144 if not SuccessfulCompletion then
145 begin
146 ShowViewLogDlg(FUpgradeLog);
147 ModalResult := mrCancel
148 end
149 else
150 ModalResult := mrOK;
151 end;
152
153 procedure TUpgradeDatabaseDlg.IBXScriptLogProc(Sender: TObject; Msg: string);
154 begin
155 Add2Log(Msg);
156 end;
157
158 procedure TUpgradeDatabaseDlg.IBXScriptProgressEvent(Sender: TObject;
159 Reset: boolean; value: integer);
160 begin
161 if Reset then
162 begin
163 with ProgressBar1 do
164 begin
165 Position := 0;
166 Max := value;
167 end;
168 end;
169 ProgressBar1.StepIt;
170 Application.ProcessMessages;
171 end;
172
173 procedure TUpgradeDatabaseDlg.Add2Log(Msg: string);
174 begin
175 FUpgradeLog.Add(Msg);
176 end;
177
178 procedure TUpgradeDatabaseDlg.DoUpdate(Data: PtrInt);
179 var UpdateAvailable: boolean;
180 UpgradeInfo: TUpgradeInfo;
181 DBArchive: string;
182 LastVersionNo: integer;
183 CurVersionNo: integer;
184 begin
185 SuccessfulCompletion := true;
186 try
187 CurVersionNo := CurrentDBVersionNo;
188 repeat
189 if CurVersionNo >= FTargetVersionNo then break;
190 LastVersionNo := CurVersionNo;
191 UpdateAvailable := FUpgradeConf.GetUpgradeInfo(FSectionHeaderTemplate,CurVersionNo+1,UpgradeInfo);
192 if UpdateAvailable then
193 begin
194 if UpgradeInfo.BackupDB and (FBackupService <> nil) then
195 begin
196 CreateDir(ExtractFileDir(FArchiveStub));
197 DBArchive := FArchiveStub + '.' + IntToStr(CurrentDBVersionNo) + '.gbk';
198 SaveDatabaseToArchive(FBackupService,DBArchive);
199 end;
200 Add2Log(UpgradeInfo.UserMessage);
201 Status.Caption := UpgradeInfo.UserMessage;
202 Application.ProcessMessages;
203 Add2Log(Format(sUpdateMsg,[UpgradeInfo.UpdateSQLFile]));
204 Add2Log(Format(sUpdateStarted,[DateTimeToStr(Now)]));
205 if not IBXScript.PerformUpdate(UpgradeInfo.UpdateSQLFile,true) then
206 begin
207 Add2Log(Format(sUpdateFailed,[DateTimeToStr(Now)]));
208 SuccessfulCompletion := false;
209 UpdateTransaction.Rollback;
210 break;
211 end;
212 UpdateTransaction.Commit;
213 Add2Log(Format(sUpdateEnded,[DateTimeToStr(Now)]));
214 if assigned(FOnUpgradeStepCompleted) then
215 OnUpgradeStepCompleted(self);
216 end;
217 CurVersionNo := CurrentDBVersionNo;
218 until not UpdateAvailable or (LastVersionNo = CurVersionNo);
219 except on E:Exception do
220 begin
221 SuccessfulCompletion := false;
222 Add2Log(E.Message);
223 end;
224 end;
225 Timer1.Enabled := true;
226 end;
227
228 function TUpgradeDatabaseDlg.CurrentDBVersionNo: integer;
229 begin
230 if assigned(FOnGetDatabaseVersionNo) then
231 OnGetDatabaseVersionNo(self,Result)
232 else
233 Result := 0;
234 end;
235
236
237 constructor TUpgradeDatabaseDlg.Create(theOwner: TComponent);
238 begin
239 inherited Create(theOwner);
240 FUpgradeLog := TStringList.Create;
241 end;
242
243 destructor TUpgradeDatabaseDlg.Destroy;
244 begin
245 if assigned(FUpgradeLog) then
246 FUpgradeLog.Free;
247 inherited Destroy;
248 end;
249
250 end.
251