-
Notifications
You must be signed in to change notification settings - Fork 3
/
H2MigrationTool.java
1118 lines (952 loc) · 46.4 KB
/
H2MigrationTool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2020 Andreas Reichel<andreas@manticore-projects.com>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/>.
*/
package com.manticore.h2;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FilenameUtils;
import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.*;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Andreas Reichel <andreas@manticore-projects.com>
*/
public class H2MigrationTool {
public static final Logger LOGGER = Logger.getLogger(H2MigrationTool.class.getName());
public static final Pattern VERSION_PATTERN = Pattern
.compile("([0-9]+)\\.([0-9]+)\\.([0-9]+)(-([a-z0-9]{9}))?", Pattern.CASE_INSENSITIVE);
private static final TreeSet<DriverRecord> DRIVER_RECORDS = new TreeSet<>();
public static Set<DriverRecord> getDriverRecords() {
return Collections.unmodifiableSet(DRIVER_RECORDS);
}
public final static javax.swing.filechooser.FileFilter H2_DATABASE_FILE_FILTER =
new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File file) {
String fileName = file.getName().toLowerCase();
return file.isDirectory() || fileName.endsWith(".mv.db")
|| fileName.endsWith(".h2.db");
}
@Override
public String getDescription() {
return "H2 Database Files";
}
};
public final static javax.swing.filechooser.FileFilter SQL_SCRIPT_FILE_FILTER =
new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File file) {
String fileName = file.getName().toLowerCase();
return file.isDirectory() || fileName.endsWith(".sql")
|| fileName.endsWith(".sql.gz")
|| fileName.endsWith(".sql.zip");
}
@Override
public String getDescription() {
return "SQL Script Files";
}
};
private final TreeSet<Hook> hooks = new TreeSet<>();
public static String getTempFolderName() {
String tempPath = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
return tempPath;
}
public static File getAbsoluteFile(String filename) {
String homePath = new File(System.getProperty("user.home")).toURI().getPath();
String modifiedFilename = filename.replaceFirst("~", Matcher.quoteReplacement(homePath));
modifiedFilename = modifiedFilename.replaceFirst("\\$\\{user.home}",
Matcher.quoteReplacement(homePath));
File f = new File(modifiedFilename);
if (!f.isAbsolute()) {
Path basePath = Paths.get("").toAbsolutePath();
Path resolvedPath = basePath.resolve(modifiedFilename);
Path absolutePath = resolvedPath.normalize();
f = absolutePath.toFile();
}
return f;
}
public static String getAbsoluteFileName(String filename) {
return getAbsoluteFile(filename).getAbsolutePath();
}
public static Collection<Path> findFilesInPathRecursively(Path parentPath, int depth,
String prefix, String suffix) throws IOException {
ArrayList<Path> fileNames = new ArrayList<>();
try (Stream<Path> paths = Files.find(parentPath, depth, (path, attr) -> {
if (attr.isRegularFile()) {
String pathName = path.getFileName().toString().toLowerCase();
return pathName.startsWith(prefix.toLowerCase())
&& pathName.endsWith(suffix.toLowerCase());
}
return false;
})) {
paths.sorted().collect(Collectors.toCollection(() -> fileNames));
}
return fileNames;
}
public static Collection<Path> findFilesInPathRecursively(Path parentPath, int depth,
FileFilter... fileFilters) throws IOException {
ArrayList<Path> fileNames = new ArrayList<>();
try (Stream<Path> paths = Files.find(parentPath, depth, (path, attr) -> {
try {
if (attr.isRegularFile()) {
for (FileFilter fileFilter : fileFilters) {
if (fileFilter.accept(path.toFile())) {
return true;
}
}
}
} catch (RuntimeException ex) {
LOGGER.log(Level.FINE, "Failed to traverse " + parentPath.toString(), ex);
}
return false;
})) {
paths.sorted().collect(Collectors.toCollection(() -> fileNames));
}
return fileNames;
}
public static Collection<Path> findH2Drivers(String pathName) throws IOException {
ArrayList<Path> fileNames = new ArrayList<>();
File folder = new File(pathName);
if (folder.exists() && folder.canRead() && folder.isDirectory()) {
fileNames.addAll(
findFilesInPathRecursively(Path.of(folder.toURI()), Integer.MAX_VALUE, "h2",
".jar"));
}
return fileNames;
}
public static Collection<Path> findH2Databases(String pathName, FileFilter... fileFilters)
throws IOException {
ArrayList<Path> fileNames = new ArrayList<>();
File folder = new File(pathName);
if (folder.exists() && folder.canRead() && folder.isDirectory()) {
fileNames.addAll(
findFilesInPathRecursively(Path.of(folder.toURI()), Integer.MAX_VALUE,
fileFilters));
}
return fileNames;
}
public static TreeSet<DriverRecord> readDriverRecords() throws Exception {
return readDriverRecords("");
}
public static TreeSet<DriverRecord> readDriverRecords(String resourceName) throws Exception {
Path myPath;
FileSystem fileSystem = null;
if (resourceName != null && resourceName.length() > 0) {
myPath = new File(resourceName).toPath();
} else {
URL resourceUrl = H2MigrationTool.class.getResource("/drivers");
assert resourceUrl != null;
URI resourceUri = resourceUrl.toURI();
if (resourceUri.getScheme().equals("jar")) {
fileSystem = FileSystems.newFileSystem(resourceUri,
Collections.emptyMap());
myPath = fileSystem.getPath("/drivers");
} else {
myPath = Paths.get(resourceUri);
}
}
for (Path path : findFilesInPathRecursively(myPath, 1, "h2", ".bin")) {
LOGGER.fine("Found H2 library " + path);
try {
URI resourceUri = path.toUri();
URL url = path.toUri().toURL();
// @todo: For any reason we can't load a Jar from inside a Jar
// so we have to extract a local copy first
// investigate, if the is a better solution, e. g. a special ClassLoader
if (resourceUri.getScheme().equals("jar")) {
String fileName = FilenameUtils.getName(url.getPath());
File tmpFile = new File(System.getProperty("java.io.tmpdir"), fileName);
tmpFile.deleteOnExit();
Files.copy(url.openStream(), tmpFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
url = tmpFile.toURI().toURL();
}
readDriverRecord(url);
} catch (RuntimeException ex) {
LOGGER.log(Level.SEVERE,
"Failed to load the driver " + path, ex);
}
}
LOGGER.fine("Driver Records loaded: " + DRIVER_RECORDS.size());
if (fileSystem != null) {
fileSystem.close();
}
return new TreeSet<>(DRIVER_RECORDS);
}
public static void readDriverRecord(Path path) {
if (path != null) {
try {
URL url = path.toUri().toURL();
readDriverRecord(url);
} catch (Exception ex) {
LOGGER.log(Level.SEVERE,
"Failed to load the driver " + path, ex);
}
}
}
public static void readDriverRecord(final URL url) {
try {
LOGGER.fine("Load Driver from: " + url.toExternalForm());
Properties properties = new Properties();
properties.setProperty("user", "sa");
properties.setProperty("password", "");
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
try (URLClassLoader loader = new URLClassLoader(
new URL[] {url},
ClassLoader.getPlatformClassLoader())) {
Class<?> classToLoad = loader.loadClass("org.h2.Driver");
Method method = classToLoad.getDeclaredMethod("load");
Driver driver = (Driver) method.invoke(null);
try (Connection conn = driver.connect("jdbc:h2:mem:test", properties)) {
// nothing for now
}
unloadDriver(driver);
return null;
}
});
Matcher matcher = VERSION_PATTERN.matcher(url.getFile());
if (matcher.find()) {
int majorVersion = Integer.parseInt(matcher.group(1));
int minorVersion = Integer.parseInt(matcher.group(2));
int patchId = Integer.parseInt(matcher.group(3));
String buildId = matcher.groupCount() == 5 ? matcher.group(5) : "";
DriverRecord driverRecord =
new DriverRecord(majorVersion, minorVersion, patchId, buildId, url);
DRIVER_RECORDS.add(driverRecord);
LOGGER.fine(driverRecord.toString());
}
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Failed to load the driver " + url.toString(), ex);
}
}
public static Driver loadDriver(String version) throws Exception {
return loadDriver("", version);
}
public static Driver loadDriver(String resourceStr, String version) throws Exception {
TreeSet<DriverRecord> driverRecords = H2MigrationTool.readDriverRecords(resourceStr);
DriverRecord driverRecord = getDriverRecord(driverRecords, version);
return loadDriver(driverRecord);
}
public static Driver loadDriver(TreeSet<DriverRecord> driverRecords, String version)
throws Exception {
DriverRecord driverRecord = getDriverRecord(driverRecords, version);
return loadDriver(driverRecord);
}
public static Driver loadDriver(DriverRecord driverRecord) throws PrivilegedActionException {
return AccessController.doPrivileged((PrivilegedExceptionAction<Driver>) () -> {
URLClassLoader loader =
new URLClassLoader(new URL[] {driverRecord.url},
ClassLoader.getPlatformClassLoader());
Class<?> classToLoad = loader.loadClass("org.h2.Driver");
Method method = classToLoad.getDeclaredMethod("load");
return (java.sql.Driver) method.invoke(null);
});
}
public static void unloadDriver(java.sql.Driver driver) {
try {
driver.getClass().getDeclaredMethod("unload").invoke(null);
if (driver.getClass().getClassLoader() instanceof URLClassLoader) {
((URLClassLoader) driver.getClass().getClassLoader()).close();
}
} catch (Exception ex) {
LOGGER.log(Level.FINE, "Failed to unload Driver " + driver.getMajorVersion() + "."
+ driver.getMinorVersion(), ex);
}
}
public static DriverRecord getDriverRecord(Set<DriverRecord> driverRecords,
int majorVersion,
int minorVersion, int patchId, String buildID) {
for (DriverRecord r : new TreeSet<>(driverRecords).descendingSet()) {
if (buildID == null || buildID.isEmpty()) {
if (r.majorVersion == majorVersion && r.minorVersion == minorVersion
&& r.patchId == patchId
&& (r.buildId == null || r.buildId.isEmpty())) {
return r;
}
} else if (r.majorVersion == majorVersion && r.minorVersion == minorVersion
&& r.patchId == patchId && r.buildId.equalsIgnoreCase(buildID)) {
return r;
}
}
return null;
}
public static DriverRecord getDriverRecord(Set<DriverRecord> driverRecords,
int majorVersion,
int minorVersion) {
for (DriverRecord r : new TreeSet<>(driverRecords).descendingSet()) {
if (r.majorVersion == majorVersion && r.minorVersion == minorVersion) {
return r;
}
}
return null;
}
public static DriverRecord getDriverRecord(Set<DriverRecord> driverRecords, String version)
throws Exception {
DriverRecord driverRecord;
Matcher matcher = VERSION_PATTERN.matcher(version);
if (matcher.find()) {
if (matcher.groupCount() == 2) {
int majorVersion = Integer.parseInt(matcher.group(1));
int minorVersion = Integer.parseInt(matcher.group(2));
driverRecord = getDriverRecord(driverRecords, majorVersion, minorVersion);
} else if (matcher.groupCount() == 3) {
int majorVersion = Integer.parseInt(matcher.group(1));
int minorVersion = Integer.parseInt(matcher.group(2));
int patchId = Integer.parseInt(matcher.group(3));
driverRecord =
getDriverRecord(driverRecords, majorVersion, minorVersion, patchId, "");
} else if (matcher.groupCount() == 5) {
int majorVersion = Integer.parseInt(matcher.group(1));
int minorVersion = Integer.parseInt(matcher.group(2));
int patchId = Integer.parseInt(matcher.group(3));
String buildId = matcher.group(5);
driverRecord = getDriverRecord(driverRecords, majorVersion, minorVersion, patchId,
buildId);
} else {
throw new Exception(
"The provided version " + version
+ " does not match the required format ###.###.###");
}
} else {
throw new Exception(
"The provided version " + version
+ " does not match the required format ###.###.###");
}
if (driverRecord == null) {
throw new Exception("No H2 driver found for requestion version " + version);
}
return driverRecord;
}
public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption("l", "lib-dir", true, "(Relative) Folder containing the H2 jar files.");
options.addOption("f", "version-from", true, "Old H2 version of the existing database.");
options.addOption("t", "version-to", true, "New H2 version to upgrade to.");
options.addOption("d", "db-file", true,
"The (relative) existing H2 database file (in the old format).");
options.addOption("u", "user", true, "The database username.");
options.addOption("p", "password", true, "The database password.");
options.addOption("s", "script-file", true, "The export script file.");
options.addOption("c", "compression", true, "The compression method [ZIP, GZIP]");
options.addOption(Option.builder("o").longOpt("options").hasArgs().valueSeparator(' ')
.desc("The upgrade options [QUIRKS_MODE VARIABLE_BINARY]").build());
options.addOption(null, "force", false, "Overwrite files and continue on failure.");
options.addOption("h", "help", false, "Show the help message.");
// create the parser
CommandLineParser parser = new DefaultParser();
try {
// parse the command line arguments
CommandLine line = parser.parse(options, args);
if (line.getOptions().length == 0 && !GraphicsEnvironment.isHeadless()) {
System.setProperty("awt.useSystemAAFontSettings", "lcd");
System.setProperty("swing.aatext", "true");
System.setProperty("prism.lcdtext", "true");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException ex) {
LOGGER.log(Level.SEVERE, "Error when setting the NIMBUS L&F", ex);
}
try {
H2MigrationTool.readDriverRecords();
H2MigrationUI frame = new H2MigrationUI();
frame.buildUI(true);
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Error when reading the H2 Database drivers",
ex);
}
}
});
return;
}
if (line.hasOption("help") || line.getOptions().length == 0) {
HelpFormatter formatter = new HelpFormatter();
formatter.setOptionComparator(null);
formatter.printHelp("java -jar H2MigrationTool.jar", options, true);
return;
} else if (!line.hasOption("db-file")) {
throw new Exception(
"Nothing to convert. Please define the Database to convert,\neither by providing the DB Name or the DB Folder.");
}
try {
String ressourceName =
line.hasOption("lib-dir")
? getAbsoluteFileName(line.getOptionValue("lib-dir"))
: null;
String versionFrom =
line.hasOption("version-from") ? line.getOptionValue("version-from") : null;
String versionTo =
line.hasOption("version-to") ? line.getOptionValue("version-to") : null;
String databaseFileName = line.getOptionValue("db-file");
databaseFileName = getAbsoluteFileName(databaseFileName);
String user = line.hasOption("user") ? line.getOptionValue("user") : "sa";
String password = line.hasOption("password") ? line.getOptionValue("password") : "";
String scriptFileName =
line.hasOption("script-file") ? line.getOptionValue("script-file") : "";
if (scriptFileName != null && scriptFileName.length() > 1) {
scriptFileName = getAbsoluteFileName(scriptFileName);
}
// "COMPRESSION ZIP";
String compression =
line.hasOption("compression")
? "COMPRESSION " + line.getOptionValue("compression")
: "";
// "VARIABLE_BINARY"
String upgradeOptions = "";
if (line.hasOption("options")) {
StringBuilder stringBuffer = new StringBuilder();
int i = 0;
for (String s : line.getOptionValues("options")) {
if (i > 0) {
stringBuffer.append(" ");
}
stringBuffer.append(s);
i++;
}
upgradeOptions = stringBuffer.toString();
}
boolean overwrite = line.hasOption("force");
boolean force = line.hasOption("force");
H2MigrationTool app = new H2MigrationTool();
H2MigrationTool.readDriverRecords(ressourceName);
if (versionFrom != null && versionFrom.length() > 1) {
app.migrate(versionFrom, versionTo, databaseFileName, user, password,
scriptFileName,
compression, upgradeOptions, overwrite, force, "");
} else {
app.migrateAuto(versionTo, databaseFileName, user, password, scriptFileName,
compression,
upgradeOptions, overwrite, force);
}
} catch (Exception ex) {
LOGGER.log(Level.FINE, "Failed to migrate the database.", ex);
throw new Exception("Failed to migrate the database.", ex);
}
} catch (ParseException ex) {
LOGGER.log(Level.FINE, "Parsing failed. Reason: " + ex.getMessage(), ex);
HelpFormatter formatter = new HelpFormatter();
formatter.setOptionComparator(null);
formatter.printHelp("java -jar H2MigrationTool.jar", options, true);
throw new Exception("Could not parse the Command Line Arguments.", ex);
}
}
private void readHooks(String versionFrom) {
hooks.clear();
}
private void executeCommands(Connection connection, List<String> commands) throws Exception {
Statement st = null;
try {
st = connection.createStatement();
for (String s : commands) {
st.executeUpdate(s);
}
st.close();
} finally {
if (st != null) {
try {
st.close();
} catch (SQLException ex) {
LOGGER.log(Level.SEVERE, "Failed to close statement.", ex);
}
}
}
}
private List<String> executeHooks(Connection connection, HookStage stage) {
ArrayList<String> commands = new ArrayList<>();
for (Hook hook : hooks) {
if (hook.stage.equals(stage)) {
LOGGER.info("Execute hook for stage " + stage.name());
Statement st = null;
try {
st = connection.createStatement();
boolean isResultSet = st.execute(hook.text);
if (isResultSet) {
ArrayList<String> cmds = new ArrayList<>();
ResultSet rs = st.getResultSet();
while (rs.next()) {
cmds.add(rs.getString(1));
}
rs.close();
if (hook.stage.equals(HookStage.EXPORT)) {
try {
executeCommands(connection, cmds);
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Hook " + hook.id + " failed.", ex);
}
} else {
commands.addAll(cmds);
}
}
st.close();
} catch (SQLException ex) {
LOGGER.log(Level.SEVERE, null, ex);
} finally {
if (st != null) {
try {
st.close();
} catch (SQLException ex) {
LOGGER.log(Level.SEVERE, "Failed to close statement.", ex);
}
}
}
}
}
return commands;
}
public DriverRecord getDriverRecord(String version) throws Exception {
return getDriverRecord(H2MigrationTool.DRIVER_RECORDS, version);
}
public ScriptResult writeScript(DriverRecord driverRecord, String databaseFileName,
String user,
String password, String scriptFileName, String options, String connectionParameters)
throws SQLException, ClassNotFoundException, NoSuchMethodException,
InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException,
PrivilegedActionException {
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
Driver driver = loadDriver(driverRecord);
try (Connection connection = driver.connect(
"jdbc:h2:" + databaseFileName + ";ACCESS_MODE_DATA=r"
+ Objects.requireNonNull(connectionParameters),
properties)) {
List<String> commands = executeHooks(connection, HookStage.IMPORT);
executeHooks(connection, HookStage.EXPORT);
// processScript(String url, String user, String password, String fileName, String
// options1,
// String options2) throws SQLException
if (driver.getMajorVersion() == 1 && driver.getMinorVersion() <= 3) {
String sql = String.format("SCRIPT TO '%s' %s", scriptFileName, options);
try (PreparedStatement st = connection.prepareStatement(sql)) {
st.execute();
return new ScriptResult(scriptFileName, commands);
}
} else {
URL url = driverRecord.url;
return AccessController
.doPrivileged((PrivilegedExceptionAction<ScriptResult>) () -> {
try (URLClassLoader loader =
new URLClassLoader(
new URL[] {driverRecord.url},
ClassLoader.getPlatformClassLoader())) {
Class<?>[] argClasses =
new Class<?>[] {Connection.class, String.class,
String.class, String.class};
Class<?> classToLoad =
Class.forName("org.h2.tools.Script", true, loader);
Method method =
classToLoad.getDeclaredMethod("process", argClasses);
Object instance =
classToLoad.getDeclaredConstructor().newInstance();
// Connection conn, String fileName, String options1, String
// options2
method.invoke(instance, connection, scriptFileName, "", options);
return new ScriptResult(scriptFileName, commands);
}
});
}
} finally {
unloadDriver(driver);
}
}
public ScriptResult writeRecoveryScript(DriverRecord driverRecord, String folderName,
String databaseFileName)
throws Exception {
String databaseName = "";
String scriptFileName;
if (databaseFileName.toLowerCase().endsWith(".mv.db")
|| databaseFileName.toLowerCase().endsWith(".h2.db")) {
databaseName =
databaseFileName.substring(0, databaseFileName.length() - ".mv.db".length());
LOGGER.info("Found H2 DB " + databaseName + " which will be recovered to SQL Script");
scriptFileName = new File(folderName, databaseName + ".sql").getCanonicalPath();
} else {
throw new Exception("The file " + databaseName
+ " does not seem to be a H2 database. Only *.h2.db and *.mv.db files are supported.");
}
final URL url = driverRecord.url;
final String finalDatabaseName = databaseName;
return AccessController.doPrivileged((PrivilegedExceptionAction<ScriptResult>) () -> {
try (URLClassLoader loader =
new URLClassLoader(new URL[] {driverRecord.url},
ClassLoader.getPlatformClassLoader())) {
Class<?> classToLoad = Class.forName("org.h2.tools.Recover", true, loader);
Class<?>[] argClasses = new Class<?>[] {String.class, String.class};
Method method = classToLoad.getDeclaredMethod("execute", argClasses);
Object instance = classToLoad.getDeclaredConstructor().newInstance();
// public static void execute(String dir, String db) throws SQLException
method.invoke(instance, folderName, finalDatabaseName);
return new ScriptResult(scriptFileName, new ArrayList<>());
}
});
}
private ScriptResult createFromScript(DriverRecord driverRecord, String databaseFileName,
String user, String password, String scriptFileName, String options,
List<String> commands,
boolean overwrite, String connectionParameters) throws Exception {
String modifiedDatabaseFileName = databaseFileName + "." + driverRecord.patchId
+ (!driverRecord.buildId.isEmpty() ? ("-" + driverRecord.buildId) : "");
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
Driver driver = loadDriver(driverRecord.getVersion());
File dbFile = new File(modifiedDatabaseFileName + ".mv.db");
if (dbFile.exists()) {
if (dbFile.isFile() && dbFile.canWrite() && overwrite) {
boolean delete = dbFile.delete();
} else if (dbFile.isFile() && !(dbFile.canWrite() && overwrite)) {
throw new Exception("The Database File " + dbFile
+ " exists already and should not be overwritten automatically.");
} else {
throw new Exception(
"The Database File " + dbFile
+ " points to an existing Folder or irregular .");
}
}
try (Connection connection =
driver.connect("jdbc:h2:" + modifiedDatabaseFileName + connectionParameters,
properties);
Statement stat = connection.createStatement()) {
final String sqlStr = String.format("RUNSCRIPT FROM '%s' %s", scriptFileName, options);
stat.executeUpdate(sqlStr);
executeCommands(connection, commands);
List<String> commands1 = executeHooks(connection, HookStage.INIT);
executeCommands(connection, commands1);
commands.addAll(commands1);
stat.executeUpdate("ANALYZE SAMPLE_SIZE 0");
stat.executeUpdate("SHUTDOWN COMPACT");
} finally {
unloadDriver(driver);
}
return new ScriptResult(scriptFileName, commands);
}
public ScriptResult migrate(String versionFrom, String versionTo, String databaseFileName,
String user, String password, String scriptFileName, String compression,
String upgradeOptions, boolean overwrite, boolean force, String connectionParameters)
throws Exception {
String modifiedDatabaseFileName = databaseFileName;
String modifiedScriptFileName = scriptFileName;
String modifiedCompression = compression;
ArrayList<String> commands = new ArrayList<>();
DriverRecord driverRecordFrom = getDriverRecord(versionFrom);
DriverRecord driverRecordTo = getDriverRecord(versionTo);
ScriptResult scriptResult = null;
boolean success = false;
if (modifiedDatabaseFileName.toLowerCase().endsWith(".mv.db")
|| modifiedDatabaseFileName.toLowerCase().endsWith(".h2.db")) {
modifiedDatabaseFileName =
modifiedDatabaseFileName.substring(0,
modifiedDatabaseFileName.length() - ".mv.db".length());
LOGGER.info(
"Found H2 DB " + modifiedDatabaseFileName
+ " which will be exported to SQL Script");
if (modifiedScriptFileName == null || modifiedScriptFileName.isEmpty()) {
modifiedScriptFileName = modifiedDatabaseFileName + ".sql";
}
if (modifiedCompression != null && modifiedCompression.endsWith("GZIP")
&& !modifiedScriptFileName.toLowerCase().endsWith(".gz")) {
modifiedScriptFileName = modifiedScriptFileName + ".gz";
} else if (modifiedCompression != null && modifiedCompression.endsWith("ZIP")
&& !modifiedScriptFileName.toLowerCase().endsWith(".zip")) {
modifiedScriptFileName = modifiedScriptFileName + ".zip";
}
readHooks(versionFrom);
try {
scriptResult =
writeScript(driverRecordFrom, modifiedDatabaseFileName, user, password,
modifiedScriptFileName, modifiedCompression,
Objects.requireNonNull(connectionParameters));
modifiedScriptFileName = scriptResult.scriptFileName;
commands.addAll(scriptResult.commands);
success = true;
LOGGER.info(
"Wrote " + driverRecordFrom + " database to script: "
+ modifiedScriptFileName);
} catch (Exception ex) {
throw new Exception(
"Failed to write " + driverRecordFrom + " database to script",
ex);
}
} else if (modifiedDatabaseFileName.toLowerCase().endsWith(".sql")) {
LOGGER.info(
"Found SQL Script " + modifiedDatabaseFileName
+ " which will be imported directly.");
modifiedCompression = "";
modifiedScriptFileName = modifiedDatabaseFileName;
modifiedDatabaseFileName =
modifiedDatabaseFileName.substring(0,
modifiedDatabaseFileName.length() - ".sql".length());
success = true;
} else if (modifiedDatabaseFileName.toLowerCase().endsWith(".sql.gz")) {
LOGGER.info(
"Found Compressed SQL Script " + modifiedDatabaseFileName
+ " which will be imported directly.");
modifiedCompression = "COMPRESSION GZIP";
modifiedScriptFileName = modifiedDatabaseFileName;
modifiedDatabaseFileName =
modifiedDatabaseFileName.substring(0,
modifiedDatabaseFileName.length() - ".sql.gz".length());
success = true;
} else if (modifiedDatabaseFileName.toLowerCase().endsWith(".sql.zip")) {
LOGGER.info(
"Found Compressed SQL Script " + modifiedDatabaseFileName
+ " which will be imported directly.");
modifiedCompression = "COMPRESSION ZIP";
modifiedScriptFileName = modifiedDatabaseFileName;
modifiedDatabaseFileName =
databaseFileName.substring(0,
modifiedDatabaseFileName.length() - ".sql.zip".length());
success = true;
} else {
LOGGER.warning("Can't process the file " + modifiedDatabaseFileName
+ ".\nOnly *.mv.db, *.sql, *.sql.gz or *.sql.zip files are supported.");
}
String options =
modifiedCompression != null && !modifiedCompression.isEmpty()
? modifiedCompression + " " + upgradeOptions
: upgradeOptions;
if (success) {
try {
scriptResult =
createFromScript(driverRecordTo, modifiedDatabaseFileName, user, password,
modifiedScriptFileName, options, commands, force,
Objects.requireNonNull(connectionParameters));
modifiedDatabaseFileName = modifiedDatabaseFileName + "." + driverRecordTo.patchId
+ (!driverRecordTo.buildId.isEmpty() ? ("-" + driverRecordTo.buildId) : "")
+ ".mv.db";
LOGGER.info("Created new " + driverRecordTo + " database: "
+ modifiedDatabaseFileName);
commands.addAll(scriptResult.commands);
} catch (Exception ex) {
throw new Exception(
"Failed to created new " + driverRecordTo + " database: "
+ modifiedDatabaseFileName,
ex);
}
}
return scriptResult;
}
public void migrateAuto(String databaseFileName) throws Exception {
migrateAuto(null, databaseFileName, "SA", "", null, "COMPRESSION ZIP", "VARIABLE_BINARY",
true,
true);
}
public void migrateAuto(String versionTo, String databaseFileName, String user, String password,
String scriptFileName, String compression, String upgradeOptions, boolean overwrite,
boolean force) throws Exception {
ArrayList<String> databaseNames = new ArrayList<>();
String modifiedDatabaseFileName = databaseFileName;
String modifiedScriptFileName = scriptFileName;
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
String filename = name.toLowerCase();
return filename.endsWith(".mv.db");
}
};
File folder = new File(modifiedDatabaseFileName);
if (folder.isDirectory()) {
LOGGER.info("Will convert all H2 databases in folder " + folder.getAbsolutePath());
File[] files = folder.listFiles(filenameFilter);
if (files != null) {
for (File f : files) {
String fileName = f.getCanonicalPath();
fileName = fileName.substring(0, fileName.length() - ".mv.db".length());
databaseNames.add(fileName);
LOGGER.info("added DB: " + fileName);
}
}
} else {
if (modifiedDatabaseFileName.toLowerCase().endsWith(".mv.db")) {
modifiedDatabaseFileName =
modifiedDatabaseFileName.substring(0,
modifiedDatabaseFileName.length() - ".mv.db".length());
LOGGER.info("trimmed DB name to: " + modifiedDatabaseFileName);
} else if (modifiedDatabaseFileName.toLowerCase().endsWith(".h2.db")) {
modifiedDatabaseFileName =
modifiedDatabaseFileName.substring(0,
modifiedDatabaseFileName.length() - ".h2.db".length());
LOGGER.info("trimmed DB name to: " + modifiedDatabaseFileName);
}
databaseNames.add(modifiedDatabaseFileName);
}
if (DRIVER_RECORDS.isEmpty()) {
throw new Exception(
"No H2 libraries found and loaded yet. Please define, where to load the H2 libraries from.");
}
ArrayList<String> commands = new ArrayList<>();
DriverRecord firstDriverRecordFrom = DRIVER_RECORDS.last(); // getDriverRecord(1, 4);
DriverRecord driverRecordTo =
versionTo != null && versionTo.length() > 1 ? getDriverRecord(versionTo)
: DRIVER_RECORDS.last();
for (String databaseName : databaseNames) {
if (modifiedScriptFileName == null || modifiedScriptFileName.isEmpty()) {
modifiedScriptFileName = databaseName + ".sql";
}
if (compression != null && compression.endsWith("GZIP")
&& !modifiedScriptFileName.toLowerCase().endsWith(".gz")) {
modifiedScriptFileName = modifiedScriptFileName + ".gz";
} else if (compression != null && compression.endsWith("ZIP")
&& !modifiedScriptFileName.toLowerCase().endsWith(".zip")) {
modifiedScriptFileName = modifiedScriptFileName + ".zip";