Skip to content

Commit

Permalink
Merge pull request #1326 from /issues/1308-oracle-distinct
Browse files Browse the repository at this point in the history
Fix #1308: Operation list fails on Oracle (backport)
  • Loading branch information
banterCZ authored Feb 22, 2024
2 parents d65f448 + e7b7282 commit 85fb714
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* Database repository for the operations.
*
* @author Petr Dvorak, [email protected]
* @implSpec Oracle does not support {@code DISTINCT} on {@code CLOB} so subselects have to be used.
*/
@Repository
public interface OperationRepository extends CrudRepository<OperationEntity, String> {
Expand All @@ -48,32 +49,40 @@ public interface OperationRepository extends CrudRepository<OperationEntity, Str
Optional<OperationEntity> findOperation(String operationId);

@Query("""
SELECT DISTINCT o FROM OperationEntity o INNER JOIN o.applications a
WHERE o.userId = :userId
AND a.id in :applicationIds
AND (:activationId IS NULL OR o.activationId IS NULL OR o.activationId = :activationId)
AND (:activationFlags IS NULL OR o.activationFlag IS NULL OR o.activationFlag IN :activationFlags)
ORDER BY o.timestampCreated DESC
""")
SELECT o FROM OperationEntity o WHERE o.id IN (SELECT o.id FROM OperationEntity o INNER JOIN o.applications a
WHERE o.userId = :userId
AND a.id in :applicationIds
AND (:activationId IS NULL OR o.activationId IS NULL OR o.activationId = :activationId)
AND (:activationFlags IS NULL OR o.activationFlag IS NULL OR o.activationFlag IN :activationFlags))
ORDER BY o.timestampCreated DESC
""")
Stream<OperationEntity> findAllOperationsForUser(String userId, List<String> applicationIds, String activationId, List<String> activationFlags, final Pageable pageable);

@Query("""
SELECT DISTINCT o FROM OperationEntity o INNER JOIN o.applications a
WHERE o.userId = :userId
AND a.id IN :applicationIds
AND o.status = io.getlime.security.powerauth.app.server.database.model.enumeration.OperationStatusDo.PENDING
AND (:activationId IS NULL OR o.activationId IS NULL OR o.activationId = :activationId)
AND (:activationFlags IS NULL OR o.activationFlag IS NULL OR o.activationFlag IN :activationFlags)
ORDER BY o.timestampCreated DESC
""")
SELECT o FROM OperationEntity o WHERE o.id IN (SELECT o.id FROM OperationEntity o INNER JOIN o.applications a
WHERE o.userId = :userId
AND a.id IN :applicationIds
AND o.status = io.getlime.security.powerauth.app.server.database.model.enumeration.OperationStatusDo.PENDING
AND (:activationId IS NULL OR o.activationId IS NULL OR o.activationId = :activationId)
AND (:activationFlags IS NULL OR o.activationFlag IS NULL OR o.activationFlag IN :activationFlags))
ORDER BY o.timestampCreated DESC
""")
Stream<OperationEntity> findPendingOperationsForUser(String userId, List<String> applicationIds, String activationId, List<String> activationFlags, final Pageable pageable);

@Query("SELECT DISTINCT o FROM OperationEntity o INNER JOIN o.applications a WHERE o.externalId = :externalId AND a.id IN :applicationIds ORDER BY o.timestampCreated DESC")
@Query("""
SELECT o FROM OperationEntity o WHERE o.id IN (SELECT o.id FROM OperationEntity o INNER JOIN o.applications a
WHERE o.externalId = :externalId
AND a.id IN :applicationIds)
ORDER BY o.timestampCreated DESC
""")
Stream<OperationEntity> findOperationsByExternalId(String externalId, List<String> applicationIds, final Pageable pageable);

@Query("SELECT DISTINCT o FROM OperationEntity o " +
"WHERE o.timestampExpires < :timestamp AND o.status = io.getlime.security.powerauth.app.server.database.model.enumeration.OperationStatusDo.PENDING " +
"ORDER BY o.timestampCreated")
@Query("""
SELECT o FROM OperationEntity o
WHERE o.timestampExpires < :timestamp
AND o.status = io.getlime.security.powerauth.app.server.database.model.enumeration.OperationStatusDo.PENDING
ORDER BY o.timestampCreated
""")
Stream<OperationEntity> findExpiredPendingOperations(Date timestamp);

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
@Sql
class OperationRepositoryTest {

private static final String userId = "testUser";
private static final List<String> applicationIds = Arrays.asList("PA_Tests", "PA_Tests2");
private static final String activationId1 = "e43a5dec-afea-4a10-a80b-b2183399f16b";
private static final String activationId2 = "68c5ca56-b419-4653-949f-49061a4be886";
private static final Pageable pageable = PageRequest.of(0, 10);
private static final String USER_ID = "testUser";
private static final List<String> APPLICATION_IDS = Arrays.asList("PA_Tests", "PA_Tests2", "PA_Tests3");
private static final String ACTIVATION_ID1 = "e43a5dec-afea-4a10-a80b-b2183399f16b";
private static final String ACTIVATION_ID2 = "68c5ca56-b419-4653-949f-49061a4be886";
private static final Pageable PAGEABLE = PageRequest.of(0, 10);

@Autowired
private OperationRepository operationRepository;
Expand All @@ -74,13 +74,13 @@ void testFindOperationById() {
@Test
void testFindOperationsWithActivationIdFilter() {
final List<OperationEntity> operations1 = operationRepository.
findAllOperationsForUser(userId, applicationIds, activationId1, null, pageable).toList();
findAllOperationsForUser(USER_ID, APPLICATION_IDS, ACTIVATION_ID1, null, PAGEABLE).toList();

assertNotNull(operations1);
assertEquals(3, operations1.size());

final List<OperationEntity> operations2 = operationRepository.
findAllOperationsForUser(userId, applicationIds, activationId2, null, pageable).toList();
findAllOperationsForUser(USER_ID, APPLICATION_IDS, ACTIVATION_ID2, null, PAGEABLE).toList();

assertNotNull(operations2);
assertEquals(4, operations2.size());
Expand All @@ -92,23 +92,23 @@ void testFindOperationsWithActivationIdFilter() {
*/
@Test
void testFindOperationsWithActivationFlagFilter() {
final List<String> activationFlags1 = activationRepository.findActivationWithoutLock(activationId1).getFlags();
final List<String> activationFlags2 = activationRepository.findActivationWithoutLock(activationId2).getFlags();
final List<String> activationFlags1 = activationRepository.findActivationWithoutLock(ACTIVATION_ID1).getFlags();
final List<String> activationFlags2 = activationRepository.findActivationWithoutLock(ACTIVATION_ID2).getFlags();
final List<String> nonExistingFlags = List.of("NOT_EXISTING");
final List<OperationEntity> operations1 = operationRepository.
findAllOperationsForUser(userId, applicationIds, null, activationFlags1, pageable).toList();
findAllOperationsForUser(USER_ID, APPLICATION_IDS, null, activationFlags1, PAGEABLE).toList();

assertNotNull(operations1);
assertEquals(6, operations1.size());

final List<OperationEntity> operations2 = operationRepository.
findAllOperationsForUser(userId, applicationIds, null, activationFlags2, pageable).toList();
findAllOperationsForUser(USER_ID, APPLICATION_IDS, null, activationFlags2, PAGEABLE).toList();

assertNotNull(operations2);
assertEquals(5, operations2.size());

final List<OperationEntity> operations3 = operationRepository.
findAllOperationsForUser(userId, applicationIds, null, nonExistingFlags, pageable).toList();
findAllOperationsForUser(USER_ID, APPLICATION_IDS, null, nonExistingFlags, PAGEABLE).toList();

assertNotNull(operations3);
assertEquals(2, operations3.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
INSERT INTO pa_application (id, name, roles)
VALUES (21, 'PA_Tests', '[ "ROLE3", "ROLE4" ]');
VALUES (21, 'PA_Tests', '[ "ROLE3", "ROLE4" ]'),
(22, 'PA_Tests2', '[ "ROLE_ADMIN" ]');

INSERT INTO pa_master_keypair (id, application_id, master_key_private_base64, master_key_public_base64, name, timestamp_created) VALUES
(21, 21, 'KdcJHQAT/BBF+26uBGNhGC0GQ93ncTx7V6kusNA8AdE=', 'BP8ZZ0LjiwRCQPob3NFwF9pPDLhxCjnPNmENzayEeeGCiDdk0gl3UzUhYk9ntMg18LZdhpvYnprZ8mk/71WlQqo=', 'PA_Tests Default Keypair', '2022-06-07 09:13:27.599000');
(21, 21, 'KdcJHQAT/BBF+26uBGNhGC0GQ93ncTx7V6kusNA8AdE=', 'BP8ZZ0LjiwRCQPob3NFwF9pPDLhxCjnPNmENzayEeeGCiDdk0gl3UzUhYk9ntMg18LZdhpvYnprZ8mk/71WlQqo=', 'PA_Tests Default Keypair', '2022-06-07 09:13:27.599000'),
(22, 22, 'ALMo+ZbnbelAs7oL5FsD8IocDC15nITu9+RO1xvfWFaY', 'BEr2c6ikVz6fQ2FL7SXcl1vxcksGB+/EbNbI1Ma7al/1jr455+b6f9dKhAupA3WIqcCRx6qpw9CoWdSVEbVZxzY=', 'PA_Tests2 Default Keypair', '2022-06-07 09:13:27.599000');

INSERT INTO pa_activation (activation_id, application_id, user_id, activation_name, activation_code, activation_status, activation_otp, activation_otp_validation, blocked_reason, counter, ctr_data, device_public_key_base64, extras, platform, device_info, flags, failed_attempts, max_failed_attempts, server_private_key_base64, server_private_key_encryption, server_public_key_base64, timestamp_activation_expire, timestamp_created, timestamp_last_used, timestamp_last_change, master_keypair_id, version)
VALUES ('e43a5dec-afea-4a10-a80b-b2183399f16b', 21, 'testUser', 'test v4', 'PXSNR-E2B46-7TY3G-TMR2Q', 3, null, 0, null, 0, 'D5XibWWPCv+nOOfcdfnUGQ==', 'BF3Sc/vqg8Zk70Y8rbT45xzAIxblGoWgLqknCHuNj7f6QFBNi2UnLbG7yMqf2eWShhyBJdu9zqx7DG2qzlqhbBE=', null, 'unknown', 'backend-tests', '[ "test-flag1", "test-flag2", "test-flag3" ]', 0, 1, 'PUz/He8+RFoOPS1NG6Gw3TDXIQ/DnS1skNBOQWzXX60=', 0, 'BPHJ4N90NUuLDq92FJUPcaKZOMad1KH2HrwQEN9DB5ST5fiJU4baYF1VlK1JHglnnN1miL3/Qb6IyW3YSMBySYM=', '2023-04-03 14:04:06.015000', '2023-04-03 13:59:06.015000', '2023-04-03 13:59:16.293000', '2023-04-03 13:59:16.343000', 21, 3);
Expand Down Expand Up @@ -30,8 +32,9 @@ VALUES ('2067b5d1-1c50-43eb-99df-847830e4807a', 'testUser', null, null, 'login',

insert into pa_operation_application (application_id, operation_id)
VALUES (21, '0f038bac-6c94-45eb-b3a9-f92e809e8ea4'),
(21, '70702b41-7b5a-4a6d-9bc2-e99949c2df53'),
(21, 'f451bcb7-9d76-42d6-97a6-76a9ecaf25813'),
(21, 'b6e41a83-6357-4670-ac4c-1f7dcaf2aa9e'),
(21, 'e708e33e-e1cb-48e2-9b51-521a3d205330'),
(21, '2067b5d1-1c50-43eb-99df-847830e4807a');
(21, '70702b41-7b5a-4a6d-9bc2-e99949c2df53'),
(21, 'f451bcb7-9d76-42d6-97a6-76a9ecaf25813'),
(21, 'b6e41a83-6357-4670-ac4c-1f7dcaf2aa9e'),
(21, 'e708e33e-e1cb-48e2-9b51-521a3d205330'),
(21, '2067b5d1-1c50-43eb-99df-847830e4807a'),
(22, '0f038bac-6c94-45eb-b3a9-f92e809e8ea4');

0 comments on commit 85fb714

Please sign in to comment.