]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20592 add missing ORDER BY to audit download
authorPierre <pierre.guillot@sonarsource.com>
Thu, 28 Sep 2023 09:09:25 +0000 (11:09 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 29 Sep 2023 20:02:47 +0000 (20:02 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/audit/AuditDaoIT.java
server/sonar-db-dao/src/main/resources/org/sonar/db/audit/AuditMapper.xml

index 8d24f0a3d6f093b90f48f328627c895a00488ae9..f1c5c0a1fa3b341ef6ea7966ef32e83968926123 100644 (file)
  */
 package org.sonar.db.audit;
 
+import java.util.Collections;
 import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.LongStream;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.impl.utils.TestSystem2;
@@ -136,6 +139,19 @@ public class AuditDaoIT {
     assertThat(auditDto.getNewValue()).isEqualTo(EXCEEDED_LENGTH);
   }
 
+  @Test
+  public void selectByPeriodPaginated_whenRowsInAnyOrder_returnOrderedByCreatedAt(){
+    List<Long> createdAts = LongStream.range(1, 51).mapToObj(p -> p).collect(Collectors.toList());
+    Collections.shuffle(createdAts);
+    createdAts.stream().map(createdAt -> AuditTesting.newAuditDto(createdAt))
+      .forEach(auditDto -> testAuditDao.insert(dbSession, auditDto));
+
+    List<AuditDto> auditDtos = testAuditDao.selectByPeriodPaginated(dbSession, 1, 51, 1);
+
+    assertThat(auditDtos).hasSize(50);
+    assertThat(auditDtos).extracting(p -> p.getCreatedAt()).isSorted();
+  }
+
   private void prepareRowsWithDeterministicCreatedAt(int size) {
     for (int i = 1; i <= size; i++) {
       AuditDto auditDto = AuditTesting.newAuditDto(i);
index 550789850ebdedfae3fc98089b4f0379d3b48262..3aa93b67dba461a552b4d3c7c4e960def2e251ee 100644 (file)
     from audits a
     where
       a.created_at &gt;= #{start} and a.created_at &lt; #{end}
+    order by created_at asc
     offset #{pagination.offset,jdbcType=INTEGER} rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
   </select>
 
-  <select id="selectByPeriodPaginated" parameterType="map" resultType="org.sonar.db.audit.AuditDto" databaseId="mssql">
-    select <include refid="sqlColumns"/> from (
-      select
-        row_number() over(order by created_at, uuid) as row_number,
-          <include refid="sqlColumnsInInnerQuery" />
-        from audits
-        where created_at &gt;= #{start} and created_at &lt; #{end}
-    ) as a
-    where
-    a.row_number between (#{pagination.offset,jdbcType=INTEGER}+1) and #{pagination.endRowNumber,jdbcType=INTEGER}
-    order by a.row_number asc
-  </select>
-
-  <select id="selectByPeriodPaginated" parameterType="map" resultType="org.sonar.db.audit.AuditDto" databaseId="oracle">
-    select <include refid="sqlColumns"/> from (
-      select rownum as rn, t.* from (
-        select
-        <include refid="sqlColumnsInInnerQuery"/>
-        from audits
-        where created_at &gt;= #{start} and created_at &lt; #{end}
-      ) t
-    ) a
-    where
-      a.rn between (#{pagination.offset,jdbcType=INTEGER}+1) and #{pagination.endRowNumber,jdbcType=INTEGER}
-    order by a.rn asc
-  </select>
-
   <insert id="insert" parameterType="Map" useGeneratedKeys="false">
     INSERT INTO audits
     (