How to tell .db version without opening it

58 views
Skip to first unread message

Doug Merrett

unread,
May 22, 2024, 1:18:59 AMMay 22
to H2 Database
Hi, 

I am upgrading my app that uses H2 as the database.  Some people using it will not move to version 2 of my app and may go straight to version 3.  Since I am changing the H2 version with each release of my app, when I connect to an old version of the database, I get the 90048 error, however it does not tell me which version the database is.  At the moment, I am only on version 2 of my app, so the database had to be version 214, and therefore this worked:
  org.h2.tools.Upgrade.upgrade ("jdbc:h2:" + database, info, 214);  // Original version
however in version 3 of my app, how do I determine what version the database is before doing the upgrade command?  (ie, how do I know what to put in place of 214?)

Or do I need to?  Can I try the upgrade with 214 and if it fails, then try the upgrade with 224 (my current version)?

Thanks for your help.

Evgenij Ryazanov

unread,
May 22, 2024, 7:42:59 AMMay 22
to H2 Database
Hi!

mv.db files have two identical 4096-byte blocks in the beginning. You can read one of them, discard trailing bytes 0x00, and convert previous bytes to a string. This string contains comma-separated properties in the name:value format. You can check value of the property format.

1 was used by old historic H2 1.4 or H2 1.3. There is no reliable and easy way to determine a database version from their files. There is a CREATE BUILD 196 or something like it in the file, 196 means H2 1.4.196.
2 was used by H2 2.0/2.1. Because H2 2.1.214 can read all these files, you can safely pass 214 to Update utility.
3 is used by H2 2.2. H2 2.2.214 is able to read all these files and you don't need to run the Upgrade utility in this case.

Alternatively you can try to open the database file with the latest version of H2 and check error message, if any. H2 throws a specific error on an incompatible format, but in case of format 1 additional steps are still required.

Database files created by a non-release build of H2 should be used only with the same build of H2. For example, H2 compiled from the current sources also uses the same format 3, but its files aren't really compatible with H2 2.2.

Doug Merrett

unread,
May 22, 2024, 7:47:11 PMMay 22
to H2 Database
Thanks for the help Evgenij
Reply all
Reply to author
Forward
0 new messages