*/
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;
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);
from audits a
where
a.created_at >= #{start} and a.created_at < #{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 >= #{start} and created_at < #{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 >= #{start} and created_at < #{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
(