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