Skip to content

Commit fba767d

Browse files
Error handling for json schema version mismatch. Throw
IllegalStateException when json schema version does not match.
1 parent b0c7682 commit fba767d

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/main/java/com/oracle/nosql/spring/data/core/NosqlTemplateBase.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ protected boolean doCheckExistingTable(NosqlEntityInformation<?, ?> entityInform
196196
final String primaryField = "primaryKey";
197197
final String ttlField = "ttl";
198198
final String identityField = "identity";
199+
final String schemaVersionField = "json_version";
200+
final int supportedSchemaVersion = 1;
199201

200202
List<String> errors = new ArrayList<>();
201203
try {
@@ -213,6 +215,22 @@ protected boolean doCheckExistingTable(NosqlEntityInformation<?, ?> entityInform
213215
new JsonOptions().setMaintainInsertionOrder(true)).
214216
asMap();
215217

218+
int currentSchemaVersion = tableSchema.get(schemaVersionField).
219+
getInt();
220+
221+
// If JSON schema version mismatch throw error
222+
if (currentSchemaVersion != supportedSchemaVersion) {
223+
String msg = String.format("Could not validate schema of the " +
224+
"table %s in the database and entity %s : " +
225+
"json schema version mismatch. " +
226+
"Expected version is %s but version is %s",
227+
entityInformation.getTableName(),
228+
entityInformation.getJavaType().getName(),
229+
currentSchemaVersion,
230+
supportedSchemaVersion);
231+
throw new IllegalStateException(msg);
232+
}
233+
216234
ArrayValue tableColumns = tableSchema.get(colField).asArray();
217235
ArrayValue tableShardKeys =
218236
tableSchema.get(shardField).asArray();
@@ -327,11 +345,13 @@ protected boolean doCheckExistingTable(NosqlEntityInformation<?, ?> entityInform
327345
"the TTL of the entity " +
328346
entityInformation.getJavaType().getName());
329347
}
330-
} catch (NullPointerException npe) {
331-
LOG.warn("Error while checking DDLs of table and entity " + npe.getMessage());
332-
if (LOG.isDebugEnabled()) {
333-
npe.printStackTrace();
334-
}
348+
} catch (NullPointerException | ClassCastException ex) {
349+
// something is wrong in parsing json schema
350+
String msg = String.format("Table %s json schema is not " +
351+
"compatible with schema version %s",
352+
entityInformation.getTableName(),
353+
supportedSchemaVersion);
354+
throw new IllegalStateException(msg, ex);
335355
}
336356

337357
if (!errors.isEmpty()) {

0 commit comments

Comments
 (0)