Debian Codegraph @

src/main/org/h2/tools/Upgrade.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.tools;
7
8 import java.io.BufferedInputStream;
9 import java.io.ByteArrayInputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.URL;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17 import java.nio.file.StandardCopyOption;
18 import java.security.MessageDigest;
19 import java.security.NoSuchAlgorithmException;
20 import java.sql.Connection;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Properties;
24 import java.util.UUID;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipInputStream;
27
28 import org.h2.engine.ConnectionInfo;
29 import org.h2.engine.Constants;
30 import org.h2.jdbc.JdbcConnection;
31 import org.h2.util.IOUtils;
32 import org.h2.util.StringUtils;
33
34 /**
35 * Upgrade utility.
36 */
37 public final class Upgrade {
38
39 private static final String[] CHECKSUMS = {
40 /* 1.2.120 */ "6fca37906aa3916ba609f47258c4abb4c749cd51aa28718a2339d9aa234a480c",
41 /* 1.2.121 */ "3233d38ee11e15243f66c98ad388da9f12cf038a203cf507415081e3329ac4f4",
42 /* 1.2.122 */ "7451e9f234f32fd9f07e4e5e682c0595806a803de656228a43887a525019ea74",
43 /* 1.2.123 */ "5a4dfaf211d32860623fdc5627f12a9cf8446b9cfabc742e7c0bad26835a8bb1",
44 /* 1.2.124 */ "f75efcaf9ccb91d94de920322c32328435e9705c19cc06b510c5f09c0a6245bf",
45 /* 1.2.125 */ "0ca368055dd72d539084c916642147780c944b90d98d2306da86814b174d1145",
46 /* 1.2.126 */ "4d9143f5b80f8878ca56edc383ae6d0a183a3b5879e83228dbacbe288007455c",
47 /* 1.2.127 */ "3df7aedd564cf61a464f4e95ec364eb7bb2b51d36863ed54edeb6ff2fed7b376",
48 /* 1.2.128 */ "7e8af7b5eca6334013fc024dab02e173a017b2d1c22c8481ed64a6af873d0819",
49 /* 1.2.129 */ "9a705009830ae80a368b1b66c8ba63071845fe25d8f6b0964aa14a3f31b46bdd",
50 /* 1.2.130 */ "8810d72867508b033a68830024e7fe7dd5a99e6f5bbb38c5a933aeb23badff00",
51 /* 1.2.131 */ "c8debc05829db1db2e6b6507a3f0561e1f72bd966d36f322bdf294baca29ed22",
52 /* 1.2.132 */ "75819d4adbf76d66af904e76b52b57afe26e9bc0e15aceed4e3c72cd7586b0d3",
53 /* 1.2.133 */ "c9ea3e95e77ae560322bca37d51601ae4b1d07ae90988af1e9fe1ceda80cd9ce",
54 /* 1.2.134 */ "1f4753d8d862d7d22d234625f617d3d7e91b73799c89b8a6036895f944a863eb",
55 /* 1.2.135 */ "eed53fcd3cf6e1159c90e57ef2b4cbd1fa3aff7a936988bb018af6fc17a2b6d9",
56 /* 1.2.136 */ "d3101d540ed004493952732d28bdf90a7968990bab7a2e04d16805469aa4eedd",
57 /* 1.2.137 */ "035dd78af874ada48339b90e8e4f1ffba0f32bb0fa37dec37ed523afa96a9c32",
58 /* 1.2.138 */ "1d03156b22b40812e39cca4d250eededfed4db8476bfbae78d60a24975cbe6d8",
59 /* 1.2.139 */ "8102cc96257d71caeff04f02c97020ae39268a32c1f0aa8fcdfda4e948ce48c8",
60 /* 1.2.140 */ "134ceafcae6ca661d8acd64c8e67d30f6ead609065dba9f6d3a0cde0d7bef6e3",
61 /* 1.2.141 */ "e453faccaaf7d8fe4eb8be744549c4a2395c7b3dcfcbc19173588c3756baff1e",
62 /* 1.2.142 */ "5973b4b467f1e0a69cf8c7b02d03d9dcadb4171d8a9635c85442a5829200e76f",
63 /* 1.2.143 */ "711cc225d8fe5325458c3947dda2093ef3a1cd4923e916082b27e87e41ca6735",
64 /* 1.2.144 */ "682f6997495a8389f4881b93cb8224685b9c6cbed487bcb445712402e52a4b80",
65 /* 1.2.145 */ "1407913cc6ba2f8c2928e8ad544c232273365d6eb66fdf84ec4213abf71449d5",
66 /* 1.3.146 */ "7756a89f10d5d5df23936bbb613de8b481e32d1099e5228968046fee62fee882",
67 /* 1.2.147 */ "2649d19db9eebbddc826029d236886dfece9404cd108ca590e82d3fd7d888278",
68 /* 1.3.148 */ "66f9389748f176c11c66c201a3737ebad0b1f4ace37cc2cd3da8962c92c72128",
69 /* 1.3.149 */ "7c3e3b93ffaf617393126870be7f8e1708bbe8e05b931c51c638a8cb03f79a36",
70 /* 1.3.150 */ "1d6dc1095d3d4b105a99034ab61ab5943c4dbb31551e7b244b403cb3c324964f",
71 /* 1.3.151 */ "8eabfde7cf64cedb7c25dc25ee7fe75a633c5cbeb18a1060da2045293fd53b14",
72 /* 1.3.152 */ "a9840c6024f8570ad3aa4d54388b4dd605640cb5ab163c444a123f7d4739aa09",
73 /* 1.3.153 */ "33d80491417eb117a0d64442dc3e60b78cf014ad099bb36a55d3835bb69e6248",
74 /* 1.3.154 */ "f153d03466acc00b66e699213fe092277e457502b5caf48c417ed3745f50eaac",
75 /* 1.3.155 */ "244b29d22939b43ecdcd3b0bfd279899df18e3af20a50241278b5b27bcf1a902",
76 /* 1.3.156 */ "070f9e4898044880e01232b269fea5285dbf7b814b7092701e755aa7d6941832",
77 /* 1.3.157 */ "4666d8f01c661054b973bc0f01f8b20f298d8e134e6fd26d78c74d43eeffd54e",
78 /* 1.3.158 */ "b0d95f18474beea619fcfba83f033e5702483457e0f0a1d1ffb4b757c5182582",
79 /* 1.3.159 */ "17aa5ced25f13f9adc2820e0ccc3010e3ce55944d10c9e2c0c631b77674d039b",
80 /* 1.3.160 */ "7fe66e211202733c52f02a328b55b30975287d9c509751bf87507e6227c6a2a7",
81 /* 1.3.161 */ "42e2ebbb7bdf29dd2de4ab16fc8fb511af6337d223afd66a5ee5fe183de05d57",
82 /* 1.3.162 */ "89e362f9525adf36d58487ff756ee93254bf92595a7098258a4c030e08e0742e",
83 /* 1.3.163 */ "1d1be843af365e8881e22732c8640e2b04c2821a0d7aa61d4152ac3f991bb735",
84 /* 1.3.164 */ "dbc88bb8cd8177b5f13b655d6afb525637129369422f0b7be0fe187950ea5132",
85 /* 1.3.165 */ "03f60ca37c0124fd2b9b177726396a51853ed0cade444e1674a090b73d341b08",
86 /* 1.3.166 */ "35103656071f1ffd1078b1a8c8028c9577297f31c5f8c7dcc845c7b4b6392619",
87 /* 1.3.167 */ "fa97521a2e72174485a96276bcf6f573d5e44ca6aba2f62de87b33b5bb0d4b91",
88 /* 1.3.168 */ "46d7ff55ccd910def16f9afd21d983f2eb2f9a6850fb501916f6673caebc2694",
89 /* 1.3.169 */ "0d99d51b8d7b8e94732d048438b9f555e031ecd52225613d7bea45290571886d",
90 /* 1.3.170 */ "0aca5eea86e8619e91ad61b82b77fb9d0e51e939c5603ab8da41be32c6f25664",
91 /* 1.3.171 */ "144d4ddb5d9f610b8b26809f1c65f442864cc55136325d3f02d7a93fb878a1db",
92 /* 1.3.172 */ "6ca30e38ccaa0c6f4264ef013327ef9ba5303f4be3d8fdbce0c3ae6451178c1e",
93 /* 1.3.173 */ "43908ee9db698cb335e2b85375d68a9d03d818869a0542b85d8d4e416619795b",
94 /* 1.3.174 */ "990b94cdfc89987281af4168fc2f6c9067be96a8533f5a6eb0f33da4d30d3e4b",
95 /* 1.3.175 */ "cc329a8742fb6e7168b00ebd0015816ff0d2462409add7c9d223826486de4691",
96 /* 1.3.176 */ "6ae3cc11a8bbaa5bd1d8494e62bccea4d354eaf042da468eac3bc5009fd33b67",
97 /* 1.4.177 */ "f281673f3248a4b5cb03fdc0cc39b944fe978366be959d0e8106fcc3197f4705",
98 /* 1.4.178 */ "da08fef0b2bc0ff8876f895e17605daf514405a064e3c2c11d2275a19d301be6",
99 /* 1.4.179 */ "2b76304ce4256ee9fd61156f9b6ef82c049ffdc8dc89af07fcf59e9532c7e7cd",
100 /* 1.4.180 */ "16428fd1e6a3e5baa8067c1c2e777e1e99af68c6ef3ff7fbbf1938937a048a82",
101 /* 1.4.181 */ "44673ff2834428fdb7f11dac3b9d679fb3039ea32194a69452971fdd7150a08b",
102 /* 1.4.182 */ "1025d0d70a4e899c41bc8fd7370cd3768826e78da91b66fd9357e44d03d79d30",
103 /* 1.4.183 */ "b3ff2ebe161976124965a9a841877ec4f6e913dbadcc31af27f1b99f6abd57e9",
104 /* 1.4.184 */ "9e47e14d5b4b9ead127b15a33b107ff06f0a7dd3f98b5d6c149e6ccae05dc0a2",
105 /* 1.4.185 */ "c4ac74be5971445e270bbd4344be58d9a06dc927223614217e5a87257a7edc03",
106 /* 1.4.186 */ "e3b7a39a2b45b61fa1521ef33b3ba676a5a9e1a397bc3ef4fb678d861a1b0ae4",
107 /* 1.4.187 */ "6204d0c206443681911fb9e04a3af5198b253b5627d16d8d8d79180d13319212",
108 /* 1.4.188 */ "11d6bff477f7ca392288f5f6d42ee61d0ccb63a34c99ba2d91710b2409673897",
109 /* 1.4.189 */ "c8dac03b66c8011cca4e44dcc7a8b1c8f8df769927c7672be1704e76f9ee7926",
110 /* 1.4.190 */ "23ba495a07bbbb3bd6c3084d10a96dad7a23741b8b6d64b213459a784195a98c",
111 /* 1.4.191 */ "e21ea665b74ec0115344b5afda5ec70ea27b528c3f103524e74c9854b1c4a284",
112 /* 1.4.192 */ "225b22e9857235c46c93861410b60b8c81c10dc8985f4faf188985ba5445126c",
113 /* 1.4.193 */ "b1cf34c64871014aa73580281cc464dfa72450d8860cc0752fc175e87edd6544",
114 /* 1.4.194 */ "b5b0c1836cead6831a50bd3e1b6c16fe6e583d4d2b7c4f41b4f838745c27cd01",
115 /* 1.4.195 */ "b99ea1f785c62b2a021664e72de696f8ea896f0da392a1c7baa3d4d47020b126",
116 /* 1.4.196 */ "0a05f4a0d5b85840148aadce63a423b5d3c36ef44756389b4faad08d2733faf5",
117 /* 1.4.197 */ "37f5216e14af2772930dff9b8734353f0a80e89ba3f33e065441de6537c5e842",
118 /* 1.4.198 */ "32dd6b149cb722aa4c2dd4d40a74a9cd41e32ac59a4e755a66e5753660d61d46",
119 /* 1.4.199 */ "3125a16743bc6b4cfbb61abba783203f1fb68230aa0fdc97898f796f99a5d42e",
120 /* 1.4.200 */ "3ad9ac4b6aae9cd9d3ac1c447465e1ed06019b851b893dd6a8d76ddb6d85bca6",
121 /* 2.0.202 */ "95090f0609aacb0ee339128ef04077145ef28320ee874ea2e33a692938da5b97",
122 /* 2.0.204 */ "712a616409580bd4ac7c10e48f2599cc32ba3a433a1804da619c3f0a5ef66a04",
123 /* 2.0.206 */ "3b9607c5673fd8b87e49e3ac46bd88fd3561e863dce673a35234e8b5708f3deb",
124 /* 2.0.208 */ null,
125 /* 2.1.210 */ "edc57299926297fd9315e04de75f8538c4cb5fe97fd3da2a1e5cee6a4c98b5cd",
126 /* 2.1.212 */ "db9284c6ff9bf3bc0087851edbd34563f1180df3ae87c67c5fe2203c0e67a536",
127 /* 2.1.214 */ "d623cdc0f61d218cf549a8d09f1c391ff91096116b22e2475475fce4fbe72bd0",
128 /* 2.1.216 */ null,
129 /* 2.1.218 */ null,
130 //
131 };
132
133 private static final String REPOSITORY = "https://repo1.maven.org/maven2";
134
135 /**
136 * Performs database upgrade from an older version of H2.
137 *
138 * @param url
139 * the JDBC connection URL
140 * @param info
141 * the connection properties ("user", "password", etc).
142 * @param version
143 * the old version of H2
144 * @return {@code true} on success, {@code false} if URL is a remote or
145 * in-memory URL
146 * @throws Exception
147 * on failure
148 */
149 public static boolean upgrade(String url, Properties info, int version) throws Exception {
150 Properties oldInfo = new Properties();
151 oldInfo.putAll(info);
152 Object password = info.get("password");
153 if (password instanceof char[]) {
154 oldInfo.put("password", ((char[]) password).clone());
155 }
156 ConnectionInfo ci = new ConnectionInfo(url, info, null, null);
157 if (!ci.isPersistent() || ci.isRemote()) {
158 return false;
159 }
160 String name = ci.getName();
161 String script = name + ".script.sql";
162 StringBuilder oldUrl = new StringBuilder("jdbc:h2:").append(name).append(";ACCESS_MODE_DATA=r");
163 copyProperty(ci, oldUrl, "FILE_LOCK");
164 copyProperty(ci, oldUrl, "MV_STORE");
165 String cipher = copyProperty(ci, oldUrl, "CIPHER");
166 String scriptCommandSuffix = cipher == null ? "" : " CIPHER AES PASSWORD '" + UUID.randomUUID() + "' --hide--";
167 java.sql.Driver driver = loadH2(version);
168 try (Connection conn = driver.connect(oldUrl.toString(), oldInfo)) {
169 conn.createStatement().execute(StringUtils.quoteStringSQL(new StringBuilder("SCRIPT TO "), script)
170 .append(scriptCommandSuffix).toString());
171 } finally {
172 unloadH2(driver);
173 }
174 rename(name, false);
175 try (JdbcConnection conn = new JdbcConnection(url, info, null, null, false)) {
176 StringBuilder builder = StringUtils.quoteStringSQL(new StringBuilder("RUNSCRIPT FROM "), script)
177 .append(scriptCommandSuffix);
178 if (version <= 200) {
179 builder.append(" FROM_1X");
180 }
181 conn.createStatement().execute(builder.toString());
182 } catch (Throwable t) {
183 rename(name, true);
184 throw t;
185 } finally {
186 Files.deleteIfExists(Paths.get(script));
187 }
188 return true;
189 }
190
191 private static void rename(String name, boolean back) throws IOException {
192 rename(name, Constants.SUFFIX_MV_FILE, back);
193 rename(name, ".lobs.db", back);
194 }
195
196 private static void rename(String name, String suffix, boolean back) throws IOException {
197 String source = name + suffix;
198 String target = source + ".bak";
199 if (back) {
200 String t = source;
201 source = target;
202 target = t;
203 }
204 Path p = Paths.get(source);
205 if (Files.exists(p)) {
206 Files.move(p, Paths.get(target), StandardCopyOption.ATOMIC_MOVE);
207 }
208 }
209
210 private static String copyProperty(ConnectionInfo ci, StringBuilder oldUrl, String name) {
211 try {
212 String value = ci.getProperty(name, null);
213 if (value != null) {
214 oldUrl.append(';').append(name).append('=').append(value);
215 }
216 return value;
217 } catch (Exception e) {
218 return null;
219 }
220 }
221
222 /**
223 * Loads the specified version of H2 in a separate class loader.
224 *
225 * @param version
226 * the version to load
227 * @return the driver of the specified version
228 * @throws IOException
229 * on I/O exception
230 * @throws ReflectiveOperationException
231 * on exception during initialization of the driver
232 */
233 public static java.sql.Driver loadH2(int version) throws IOException, ReflectiveOperationException {
234 String prefix;
235 if (version >= 201) {
236 if ((version & 1) != 0 || version > Constants.BUILD_ID) {
237 throw new IllegalArgumentException("version=" + version);
238 }
239 int major = version / 100;
240 int minor = version / 10 % 10;
241 prefix = new StringBuilder().append(major).append('.').append(minor).append('.').toString();
242 } else if (version >= 177) {
243 prefix = "1.4.";
244 } else if (version >= 146 && version != 147) {
245 prefix = "1.3.";
246 } else if (version >= 120) {
247 prefix = "1.2.";
248 } else {
249 throw new IllegalArgumentException("version=" + version);
250 }
251 String fullVersion = prefix + version;
252 byte[] data = downloadUsingMaven("com.h2database", "h2", fullVersion,
253 CHECKSUMS[version >= 202 ? (version >>> 1) - 20 : version - 120]);
254 ZipInputStream is = new ZipInputStream(new ByteArrayInputStream(data));
255 HashMap<String, byte[]> map = new HashMap<>(version >= 198 ? 2048 : 1024);
256 ByteArrayOutputStream baos = new ByteArrayOutputStream();
257 for (ZipEntry ze; (ze = is.getNextEntry()) != null;) {
258 if (ze.isDirectory()) {
259 continue;
260 }
261 IOUtils.copy(is, baos);
262 map.put(ze.getName(), baos.toByteArray());
263 baos.reset();
264 }
265 ClassLoader cl = new ClassLoader(null) {
266 @Override
267 protected Class<?> findClass(String name) throws ClassNotFoundException {
268 String resourceName = name.replace('.', '/') + ".class";
269 byte[] b = map.get(resourceName);
270 if (b == null) {
271 return ClassLoader.getSystemClassLoader().loadClass(name);
272 }
273 return defineClass(name, b, 0, b.length);
274 }
275
276 @Override
277 public InputStream getResourceAsStream(String name) {
278 byte[] b = map.get(name);
279 return b != null ? new ByteArrayInputStream(b) : null;
280 }
281 };
282 return (java.sql.Driver) cl.loadClass("org.h2.Driver").getDeclaredMethod("load").invoke(null);
283 }
284
285 /**
286 * Unloads the specified driver of H2.
287 *
288 * @param driver
289 * the driver to unload
290 * @throws ReflectiveOperationException
291 * on exception
292 */
293 public static void unloadH2(java.sql.Driver driver) throws ReflectiveOperationException {
294 driver.getClass().getDeclaredMethod("unload").invoke(null);
295 }
296
297 private static byte[] downloadUsingMaven(String group, String artifact, String version, String sha256Checksum)
298 throws IOException {
299 String repoFile = group.replace('.', '/') + '/' + artifact + '/' + version + '/' + artifact + '-' + version
300 + ".jar";
301 Path localMavenDir = Paths.get(System.getProperty("user.home") + "/.m2/repository");
302 if (Files.isDirectory(localMavenDir)) {
303 Path f = localMavenDir.resolve(repoFile);
304 if (!Files.exists(f)) {
305 try {
306 ArrayList<String> args = new ArrayList<>();
307 if (System.getProperty("os.name").toLowerCase().contains("windows")) {
308 args.add("cmd");
309 args.add("/C");
310 }
311 args.add("mvn");
312 args.add("org.apache.maven.plugins:maven-dependency-plugin:2.1:get");
313 args.add("-D" + "repoUrl=" + REPOSITORY);
314 args.add("-D" + "artifact=" + group + ':' + artifact + ':' + version);
315 exec(args);
316 } catch (RuntimeException e) {
317 System.out.println("Could not download using Maven: " + e.toString());
318 }
319 }
320 if (Files.exists(f)) {
321 return check(Files.readAllBytes(f), sha256Checksum, f.toAbsolutePath().toString());
322 }
323 }
324 return download(REPOSITORY + '/' + repoFile, sha256Checksum);
325 }
326
327 private static int exec(ArrayList<String> args) {
328 try {
329 ProcessBuilder pb = new ProcessBuilder();
330 pb.command(args.toArray(new String[0]));
331 pb.inheritIO();
332 Process p = pb.start();
333 p.waitFor();
334 return p.exitValue();
335 } catch (Exception e) {
336 throw new RuntimeException(e);
337 }
338 }
339
340 private static byte[] download(String fileURL, String sha256Checksum) {
341 ByteArrayOutputStream baos = new ByteArrayOutputStream();
342 try {
343 System.out.println("Downloading " + fileURL);
344 URL url = new URL(fileURL);
345 InputStream in = new BufferedInputStream(url.openStream());
346 long last = System.nanoTime();
347 int len = 0;
348 while (true) {
349 long now = System.nanoTime();
350 if (now - last > 1_000_000_000L) {
351 System.out.println("Downloaded " + len + " bytes");
352 last = now;
353 }
354 int x = in.read();
355 len++;
356 if (x < 0) {
357 break;
358 }
359 baos.write(x);
360 }
361 in.close();
362 } catch (IOException e) {
363 throw new RuntimeException("Error downloading " + fileURL, e);
364 }
365 return check(baos.toByteArray(), sha256Checksum, null);
366 }
367
368 private static byte[] check(byte[] data, String sha256Checksum, String checksummedFile) {
369 String got = getSHA256(data);
370 if (sha256Checksum == null) {
371 System.out.println('"' + got + '"');
372 } else {
373 if (!got.equals(sha256Checksum)) {
374 StringBuilder builder = new StringBuilder().append("SHA-256 checksum mismatch; got: ").append(got)
375 .append(" expected: ").append(sha256Checksum);
376 if (checksummedFile != null) {
377 builder.append(" for file ").append(checksummedFile);
378 }
379 throw new RuntimeException(builder.toString());
380 }
381 }
382 return data;
383 }
384
385 private static String getSHA256(byte[] data) {
386 try {
387 return StringUtils.convertBytesToHex(MessageDigest.getInstance("SHA-256").digest(data));
388 } catch (NoSuchAlgorithmException e) {
389 throw new RuntimeException(e);
390 }
391 }
392
393 private Upgrade() {
394 }
395
396 }