Skip to content

Commit

Permalink
#30275 Adding unique = true conditiong to the retrieve unique values …
Browse files Browse the repository at this point in the history
…query
  • Loading branch information
freddyDOTCMS committed Oct 10, 2024
1 parent e64edd3 commit f0517fe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,13 @@ public class Task241007CreateUniqueFieldsTable implements StartupTask {
" INNER JOIN structure ON structure.inode = contentlet.structure_inode" +
" INNER JOIN field ON structure.inode = field.structure_inode" +
" INNER JOIN identifier ON contentlet.identifier = identifier.id" +
" WHERE jsonb_extract_path_text(contentlet_as_json->'fields', field.velocity_var_name) IS NOT NULL" +
" WHERE jsonb_extract_path_text(contentlet_as_json->'fields', field.velocity_var_name) IS NOT NULL AND " +
" field.unique_ = true " +
" GROUP BY structure.inode," +
" field.velocity_var_name ," +
" contentlet.language_id," +
" identifier.host_inode," +
" jsonb_extract_path_text(contentlet_as_json -> 'fields', field.velocity_var_name)::jsonb ->> 'value'";

private static final String TESTING_QUERY = "SELECT structure.inode AS content_type_id," +
" field.velocity_var_name AS field_var_name," +
" contentlet.language_id AS language_id," +
" identifier.host_inode AS host_id," +
" jsonb_extract_path_text(contentlet_as_json -> 'fields', field.velocity_var_name)::jsonb ->> 'value' AS field_value," +
" ARRAY_AGG(contentlet.identifier) AS contentlet_identifier" +
" FROM contentlet" +
" INNER JOIN structure ON structure.inode = contentlet.structure_inode" +
" INNER JOIN field ON structure.inode = field.structure_inode" +
" INNER JOIN identifier ON contentlet.identifier = identifier.id" +
" WHERE jsonb_extract_path_text(contentlet_as_json->'fields', field.velocity_var_name) IS NOT NULL AND" +
" jsonb_extract_path_text(contentlet_as_json -> 'fields', field.velocity_var_name)::jsonb ->> 'value' = 'test.jpg'" +
" GROUP BY structure.inode," +
" field.velocity_var_name ," +
" contentlet.language_id," +
" identifier.host_inode," +
" jsonb_extract_path_text(contentlet_as_json -> 'fields', field.velocity_var_name)::jsonb ->> 'value'";

private static final String INSERT_UNIQUE_FIELDS_QUERY = "INSERT INTO unique_fields(unique_key_val, supporting_values) VALUES(?, ?)";

@Override
Expand Down Expand Up @@ -174,10 +156,9 @@ private void populate() throws DotDataException, SQLException {
}

try {

insertUniqueFieldsRegister(params);
} catch (DotDataException e) {
throw new DotRuntimeException(new DotConnect().setSQL(TESTING_QUERY).loadObjectResults().toString());
throw new DotRuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,59 @@ public void populateWhenExistsDuplicatedValues() throws DotDataException, NoSuch
checkSupportingValues(uniqueValuesResult.get(0), contentType, uniqueField, contentlet_1, contentlet_2);
}


/**
* Method to test: {@link Task241007CreateUniqueFieldsTable#executeUpgrade()}
* When: Run the method and already exists Contentlet with duplicated values for not unique fields
* Should: do nothing really
*/
@Test
public void populateWhenExistsDuplicatedValuesButNotUniqueField() throws DotDataException, NoSuchAlgorithmException, IOException, DotSecurityException {
final Field titleField = new FieldDataGen().type(TextField.class).name("title").next();
final Field uniqueField = new FieldDataGen().type(TextField.class).name("unique").next();

final ContentType contentType = new ContentTypeDataGen().field(titleField).field(uniqueField).nextPersisted();
final String uniqueValue = "Unique_" + System.currentTimeMillis();

final Contentlet contentlet_1 = new ContentletDataGen(contentType)
.setProperty(titleField.variable(), "Title_1_" + System.currentTimeMillis())
.setProperty(uniqueField.variable(), uniqueValue)
.nextPersisted();

final Contentlet contentlet_2 = new ContentletDataGen(contentType)
.setProperty(titleField.variable(), "Title_2_" + System.currentTimeMillis())
.setProperty(uniqueField.variable(), uniqueValue)
.nextPersisted();

final ImmutableTextField uniqueFieldUpdated = ImmutableTextField.builder()
.from(uniqueField)
.contentTypeId(contentType.id())
.build();

APILocator.getContentTypeFieldAPI().save(uniqueFieldUpdated, APILocator.systemUser());

final Task241007CreateUniqueFieldsTable task241007CreateUniqueFieldsTable = new Task241007CreateUniqueFieldsTable();

assertTrue(task241007CreateUniqueFieldsTable.forceRun());
task241007CreateUniqueFieldsTable.executeUpgrade();
assertFalse(task241007CreateUniqueFieldsTable.forceRun());

final List<Map<String, Object>> results = new DotConnect().setSQL("SELECT * from unique_fields").loadObjectResults();

if (!results.isEmpty()) {
final String valueToHash_1 = getHash(contentType, uniqueField, contentlet_1);

final List<Map<String, Object>> uniqueValuesResult = results.stream()
.filter(result -> result.get("unique_key_val").equals(valueToHash_1))
.collect(Collectors.toList());

assertTrue(uniqueValuesResult.isEmpty());
} else {
assertTrue(true);
}
assertFalse(results.isEmpty());
}

/**
* Method to test: {@link Task241007CreateUniqueFieldsTable#executeUpgrade()}
* When: Run the method and already exists Contentlet with Unique field and uniquePerSite enabled
Expand Down

0 comments on commit f0517fe

Please sign in to comment.