Compare commits

...

1 Commits

Author SHA1 Message Date
akwizgran
580be7cfdc Log DB table names. 2022-06-08 15:10:11 +01:00
4 changed files with 33 additions and 5 deletions

View File

@@ -3,15 +3,16 @@ package org.briarproject.bramble.db;
class DatabaseTypes { class DatabaseTypes {
private final String hashType, secretType, binaryType; private final String hashType, secretType, binaryType;
private final String counterType, stringType; private final String counterType, stringType, schemaQuery;
public DatabaseTypes(String hashType, String secretType, String binaryType, DatabaseTypes(String hashType, String secretType, String binaryType,
String counterType, String stringType) { String counterType, String stringType, String schemaQuery) {
this.hashType = hashType; this.hashType = hashType;
this.secretType = secretType; this.secretType = secretType;
this.binaryType = binaryType; this.binaryType = binaryType;
this.counterType = counterType; this.counterType = counterType;
this.stringType = stringType; this.stringType = stringType;
this.schemaQuery = schemaQuery;
} }
/** /**
@@ -31,4 +32,8 @@ class DatabaseTypes {
s = s.replaceAll("_STRING", stringType); s = s.replaceAll("_STRING", stringType);
return s; return s;
} }
String getSchemaQuery() {
return schemaQuery;
}
} }

View File

@@ -41,8 +41,9 @@ class H2Database extends JdbcDatabase {
private static final String BINARY_TYPE = "BINARY"; private static final String BINARY_TYPE = "BINARY";
private static final String COUNTER_TYPE = "INT NOT NULL AUTO_INCREMENT"; private static final String COUNTER_TYPE = "INT NOT NULL AUTO_INCREMENT";
private static final String STRING_TYPE = "VARCHAR"; private static final String STRING_TYPE = "VARCHAR";
private static final String SCHEMA_QUERY = "SHOW TABLES";
private static final DatabaseTypes dbTypes = new DatabaseTypes(HASH_TYPE, private static final DatabaseTypes dbTypes = new DatabaseTypes(HASH_TYPE,
SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE); SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE, SCHEMA_QUERY);
private final DatabaseConfig config; private final DatabaseConfig config;
private final String url; private final String url;

View File

@@ -41,8 +41,11 @@ class HyperSqlDatabase extends JdbcDatabase {
private static final String COUNTER_TYPE = private static final String COUNTER_TYPE =
"INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1)"; "INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1)";
private static final String STRING_TYPE = "VARCHAR"; private static final String STRING_TYPE = "VARCHAR";
private static final String SCHEMA_QUERY =
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.SYSTEM_TABLES"
+ " WHERE TABLE_TYPE='TABLE'";
private static final DatabaseTypes dbTypes = new DatabaseTypes(HASH_TYPE, private static final DatabaseTypes dbTypes = new DatabaseTypes(HASH_TYPE,
SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE); SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE, SCHEMA_QUERY);
private final DatabaseConfig config; private final DatabaseConfig config;
private final String url; private final String url;

View File

@@ -405,6 +405,7 @@ abstract class JdbcDatabase implements Database<Connection> {
boolean compact; boolean compact;
Connection txn = startTransaction(); Connection txn = startTransaction();
try { try {
if (LOG.isLoggable(INFO)) logSchema(txn);
if (reopen) { if (reopen) {
Settings s = getSettings(txn, DB_SETTINGS_NAMESPACE); Settings s = getSettings(txn, DB_SETTINGS_NAMESPACE);
wasDirtyOnInitialisation = isDirty(s); wasDirtyOnInitialisation = isDirty(s);
@@ -447,6 +448,24 @@ abstract class JdbcDatabase implements Database<Connection> {
return wasDirtyOnInitialisation; return wasDirtyOnInitialisation;
} }
private void logSchema(Connection txn) throws DbException {
Statement s = null;
ResultSet rs = null;
try {
s = txn.createStatement();
rs = s.executeQuery(dbTypes.getSchemaQuery());
StringBuilder sb = new StringBuilder("Tables:");
while (rs.next()) sb.append('\n').append(rs.getString(1));
LOG.info(sb.toString());
rs.close();
s.close();
} catch (SQLException e) {
tryToClose(rs, LOG, WARNING);
tryToClose(s, LOG, WARNING);
throw new DbException(e);
}
}
/** /**
* Compares the schema version stored in the database with the schema * Compares the schema version stored in the database with the schema
* version used by the current code and applies any suitable migrations to * version used by the current code and applies any suitable migrations to