diff --git a/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task241007CreateUniqueFieldsTable.java b/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task241007CreateUniqueFieldsTable.java index 4d29572d162..e9b95e3eefc 100644 --- a/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task241007CreateUniqueFieldsTable.java +++ b/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task241007CreateUniqueFieldsTable.java @@ -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 @@ -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); } } diff --git a/dotcms-integration/src/test/java/com/dotmarketing/startup/runonce/Task241007CreateUniqueFieldsTableTest.java b/dotcms-integration/src/test/java/com/dotmarketing/startup/runonce/Task241007CreateUniqueFieldsTableTest.java index e0540e4d8c0..55e93ae8215 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/startup/runonce/Task241007CreateUniqueFieldsTableTest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/startup/runonce/Task241007CreateUniqueFieldsTableTest.java @@ -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> results = new DotConnect().setSQL("SELECT * from unique_fields").loadObjectResults(); + + if (!results.isEmpty()) { + final String valueToHash_1 = getHash(contentType, uniqueField, contentlet_1); + + final List> 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