Skip to content

Commit b0c7682

Browse files
o. Create table only if not exist
1 parent 7d6cb06 commit b0c7682

File tree

3 files changed

+131
-120
lines changed

3 files changed

+131
-120
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ public String getTableName(Class<?> domainClass) {
9393
@Override
9494
public boolean createTableIfNotExists(
9595
NosqlEntityInformation<?, ?> entityInformation) {
96-
doCheckExistingTable(entityInformation);
97-
return doCreateTable(entityInformation);
96+
boolean isTableExist = doCheckExistingTable(entityInformation);
97+
// if table does not exist create
98+
return (!isTableExist) ? doCreateTable(entityInformation) : true;
9899
}
99100

100101
@SuppressWarnings("unchecked")

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

Lines changed: 124 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ protected boolean doCreateTable(NosqlEntityInformation<?, ?> entityInformation)
188188
return tableState == TableResult.State.ACTIVE;
189189
}
190190

191-
protected void doCheckExistingTable(NosqlEntityInformation<?, ?> entityInformation) {
191+
protected boolean doCheckExistingTable(NosqlEntityInformation<?, ?> entityInformation) {
192192
final String colField = "fields";
193193
final String colNameField = "name";
194194
final String colTypeField = "type";
@@ -201,131 +201,137 @@ protected void doCheckExistingTable(NosqlEntityInformation<?, ?> entityInformati
201201
try {
202202
TableResult tableResult = doGetTable(entityInformation);
203203

204+
// table does not exist return false
205+
if (tableResult == null) {
206+
return false;
207+
}
208+
204209
/* If table already exist in the database compare and throw error if
205210
mismatch*/
206-
if (tableResult != null) {
207-
MapValue tableSchema = JsonUtils.createValueFromJson(
208-
tableResult.getSchema(),
209-
new JsonOptions().setMaintainInsertionOrder(true)).
210-
asMap();
211-
212-
ArrayValue tableColumns = tableSchema.get(colField).asArray();
213-
ArrayValue tableShardKeys =
214-
tableSchema.get(shardField).asArray();
215-
ArrayValue tablePrimaryKeys =
216-
tableSchema.get(primaryField).asArray();
217-
218-
Map<String, String> tableShardMap = new LinkedHashMap<>();
219-
Map<String, String> tableNonShardMap = new LinkedHashMap<>();
220-
Map<String, String> tableOthersMap = new LinkedHashMap<>();
221-
222-
// extract table details into maps
223-
for (int i = 0; i < tableColumns.size(); i++) {
224-
MapValue column = tableColumns.get(i).asMap();
225-
String colName =
226-
column.getString(colNameField).toLowerCase();
227-
String columnType =
228-
column.getString(colTypeField).toLowerCase();
229-
230-
if (i < tableShardKeys.size()) {
231-
tableShardMap.put(colName, columnType);
232-
} else if (i < tablePrimaryKeys.size()) {
233-
tableNonShardMap.put(colName, columnType);
234-
} else {
235-
tableOthersMap.put(colName, columnType);
236-
}
211+
MapValue tableSchema = JsonUtils.createValueFromJson(
212+
tableResult.getSchema(),
213+
new JsonOptions().setMaintainInsertionOrder(true)).
214+
asMap();
215+
216+
ArrayValue tableColumns = tableSchema.get(colField).asArray();
217+
ArrayValue tableShardKeys =
218+
tableSchema.get(shardField).asArray();
219+
ArrayValue tablePrimaryKeys =
220+
tableSchema.get(primaryField).asArray();
221+
222+
Map<String, String> tableShardMap = new LinkedHashMap<>();
223+
Map<String, String> tableNonShardMap = new LinkedHashMap<>();
224+
Map<String, String> tableOthersMap = new LinkedHashMap<>();
225+
226+
// extract table details into maps
227+
for (int i = 0; i < tableColumns.size(); i++) {
228+
MapValue column = tableColumns.get(i).asMap();
229+
String colName =
230+
column.getString(colNameField).toLowerCase();
231+
String columnType =
232+
column.getString(colTypeField).toLowerCase();
233+
234+
if (i < tableShardKeys.size()) {
235+
tableShardMap.put(colName, columnType);
236+
} else if (i < tablePrimaryKeys.size()) {
237+
tableNonShardMap.put(colName, columnType);
238+
} else {
239+
tableOthersMap.put(colName, columnType);
237240
}
241+
}
238242

239-
// extract entity details into maps
240-
Map<String, FieldValue.Type> shardKeys = entityInformation.
241-
getShardKeys();
242-
Map<String, FieldValue.Type> nonShardKeys = entityInformation.
243-
getNonShardKeys();
244-
245-
Map<String, String> entityShardMap = new LinkedHashMap<>();
246-
shardKeys.forEach((k, v) -> entityShardMap.put(
247-
k.toLowerCase(), v.name().toLowerCase()));
248-
249-
Map<String, String> entityNonShardMap = new LinkedHashMap<>();
250-
nonShardKeys.forEach((k, v) -> entityNonShardMap.put(
251-
k.toLowerCase(), v.name().toLowerCase()));
252-
253-
Map<String, String> entityOthersMap = new LinkedHashMap<>();
254-
entityOthersMap.put(JSON_COLUMN.toLowerCase(), "json");
255-
256-
// convert maps to String
257-
String tableShards = "{" + tableShardMap.entrySet().stream()
258-
.map(e -> e.getKey() + " " + e.getValue()).
259-
collect(Collectors.joining(",")) + "}";
260-
String tableNonShards = "{" + tableNonShardMap.entrySet()
261-
.stream().map(e -> e.getKey() + " " + e.getValue()).
262-
collect(Collectors.joining(",")) + "}";
263-
String tableOthers = "{" + tableOthersMap.entrySet().stream()
264-
.map(e -> e.getKey() + " " + e.getValue()).
265-
collect(Collectors.joining(",")) + "}";
266-
267-
String entityShards = "{" + entityShardMap.entrySet().stream()
268-
.map(e -> e.getKey() + " " + e.getValue()).
269-
collect(Collectors.joining(",")) + "}";
270-
String entityNonShards = "{" + entityNonShardMap.entrySet()
271-
.stream().map(e -> e.getKey() + " " + e.getValue()).
272-
collect(Collectors.joining(",")) + "}";
273-
String entityOthers = "{" + entityOthersMap.entrySet().stream()
274-
.map(e -> e.getKey() + " " + e.getValue()).
275-
collect(Collectors.joining(",")) + "}";
276-
277-
String msg;
278-
// check shard keys and types match
279-
if (!tableShards.equals(entityShards)) {
280-
msg = String.format("Shard primary keys mismatch: " +
281-
"table=%s, entity=%s.", tableShards, entityShards);
282-
errors.add(msg);
283-
}
243+
// extract entity details into maps
244+
Map<String, FieldValue.Type> shardKeys = entityInformation.
245+
getShardKeys();
246+
Map<String, FieldValue.Type> nonShardKeys = entityInformation.
247+
getNonShardKeys();
248+
249+
Map<String, String> entityShardMap = new LinkedHashMap<>();
250+
shardKeys.forEach((k, v) -> entityShardMap.put(
251+
k.toLowerCase(), v.name().toLowerCase()));
252+
253+
Map<String, String> entityNonShardMap = new LinkedHashMap<>();
254+
nonShardKeys.forEach((k, v) -> entityNonShardMap.put(
255+
k.toLowerCase(), v.name().toLowerCase()));
256+
257+
Map<String, String> entityOthersMap = new LinkedHashMap<>();
258+
entityOthersMap.put(JSON_COLUMN.toLowerCase(), "json");
259+
260+
// convert maps to String
261+
String tableShards = "{" + tableShardMap.entrySet().stream()
262+
.map(e -> e.getKey() + " " + e.getValue()).
263+
collect(Collectors.joining(",")) + "}";
264+
String tableNonShards = "{" + tableNonShardMap.entrySet()
265+
.stream().map(e -> e.getKey() + " " + e.getValue()).
266+
collect(Collectors.joining(",")) + "}";
267+
String tableOthers = "{" + tableOthersMap.entrySet().stream()
268+
.map(e -> e.getKey() + " " + e.getValue()).
269+
collect(Collectors.joining(",")) + "}";
270+
271+
String entityShards = "{" + entityShardMap.entrySet().stream()
272+
.map(e -> e.getKey() + " " + e.getValue()).
273+
collect(Collectors.joining(",")) + "}";
274+
String entityNonShards = "{" + entityNonShardMap.entrySet()
275+
.stream().map(e -> e.getKey() + " " + e.getValue()).
276+
collect(Collectors.joining(",")) + "}";
277+
String entityOthers = "{" + entityOthersMap.entrySet().stream()
278+
.map(e -> e.getKey() + " " + e.getValue()).
279+
collect(Collectors.joining(",")) + "}";
280+
281+
String msg;
282+
// check shard keys and types match
283+
if (!tableShards.equals(entityShards)) {
284+
msg = String.format("Shard primary keys mismatch: " +
285+
"table=%s, entity=%s.", tableShards, entityShards);
286+
errors.add(msg);
287+
}
284288

285-
// check non-shard keys and types match
286-
if (!tableNonShards.equals(entityNonShards)) {
287-
msg = String.format("Non-shard primary keys mismatch: " +
288-
"table=%s, entity=%s.", tableNonShards,
289-
entityNonShards);
290-
errors.add(msg);
291-
}
289+
// check non-shard keys and types match
290+
if (!tableNonShards.equals(entityNonShards)) {
291+
msg = String.format("Non-shard primary keys mismatch: " +
292+
"table=%s, entity=%s.", tableNonShards,
293+
entityNonShards);
294+
errors.add(msg);
295+
}
292296

293-
// check non-primary keys and types match
294-
if (!tableOthers.equals(entityOthers)) {
295-
msg = String.format("Non-primary key columns mismatch:" +
296-
"table=%s, entity=%s.", tableOthers,
297-
entityOthers);
298-
errors.add(msg);
299-
}
297+
// check non-primary keys and types match
298+
if (!tableOthers.equals(entityOthers)) {
299+
msg = String.format("Non-primary key columns mismatch:" +
300+
"table=%s, entity=%s.", tableOthers,
301+
entityOthers);
302+
errors.add(msg);
303+
}
300304

301-
// check identity same
302-
FieldValue identity = tableSchema.get(identityField);
303-
if (identity != null && !entityInformation.isAutoGeneratedId()) {
304-
errors.add("Identity information mismatch.");
305+
// check identity same
306+
FieldValue identity = tableSchema.get(identityField);
307+
if (identity != null && !entityInformation.isAutoGeneratedId()) {
308+
errors.add("Identity information mismatch.");
305309

306-
} else if (identity == null && entityInformation.isAutoGeneratedId() &&
307-
entityInformation.getIdNosqlType() != FieldValue.Type.STRING) {
308-
errors.add("Identity information mismatch.");
309-
}
310+
} else if (identity == null && entityInformation.isAutoGeneratedId() &&
311+
entityInformation.getIdNosqlType() != FieldValue.Type.STRING) {
312+
errors.add("Identity information mismatch.");
313+
}
310314

311-
// TTL warning
312-
FieldValue ttlValue = tableSchema.get(ttlField);
313-
TimeToLive ttl = entityInformation.getTtl();
314-
// TTL is present in database but not in the entity
315-
if (ttlValue != null && ttl != null &&
316-
!ttl.toString().equalsIgnoreCase(ttlValue.getString())) {
317-
LOG.warn("TTL of the table in database is different from " +
318-
"the TTL of the entity " +
319-
entityInformation.getJavaType().getName());
320-
} else if (ttlValue == null && ttl != null && ttl.getValue() != 0) {
321-
// TTL is present in entity but not in the database
322-
LOG.warn("TTL of the table in database is different from " +
323-
"the TTL of the entity " +
324-
entityInformation.getJavaType().getName());
325-
}
315+
// TTL warning
316+
FieldValue ttlValue = tableSchema.get(ttlField);
317+
TimeToLive ttl = entityInformation.getTtl();
318+
// TTL is present in database but not in the entity
319+
if (ttlValue != null && ttl != null &&
320+
!ttl.toString().equalsIgnoreCase(ttlValue.getString())) {
321+
LOG.warn("TTL of the table in database is different from " +
322+
"the TTL of the entity " +
323+
entityInformation.getJavaType().getName());
324+
} else if (ttlValue == null && ttl != null && ttl.getValue() != 0) {
325+
// TTL is present in entity but not in the database
326+
LOG.warn("TTL of the table in database is different from " +
327+
"the TTL of the entity " +
328+
entityInformation.getJavaType().getName());
329+
}
330+
} catch (NullPointerException npe) {
331+
LOG.warn("Error while checking DDLs of table and entity " + npe.getMessage());
332+
if (LOG.isDebugEnabled()) {
333+
npe.printStackTrace();
326334
}
327-
} catch (Exception e) {
328-
LOG.warn("Error while checking DDLs of table and entity " + e.getMessage());
329335
}
330336

331337
if (!errors.isEmpty()) {
@@ -342,6 +348,8 @@ protected void doCheckExistingTable(NosqlEntityInformation<?, ?> entityInformati
342348
" annotation to use a different table.");
343349
throw new IllegalArgumentException(sb.toString());
344350
}
351+
// no mismatch between table and entity return true
352+
return true;
345353
}
346354

347355
protected DeleteResult doDelete(

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ public String getTableName(Class<?> domainClass) {
7878
public Mono<Boolean> createTableIfNotExists(
7979
NosqlEntityInformation<?, ?> entityInformation) {
8080
Assert.notNull(entityInformation, "Entity information should not be null");
81-
doCheckExistingTable(entityInformation);
82-
return Mono.just(doCreateTable(entityInformation));
81+
boolean isTableExist = doCheckExistingTable(entityInformation);
82+
// if table does not exist create
83+
return (!isTableExist) ? Mono.just(doCreateTable(entityInformation)) :
84+
Mono.just(true);
8385
}
8486

8587
/**

0 commit comments

Comments
 (0)