Skip to content

Commit

Permalink
change analysis_reports timestamp columns to bigint/long
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Jan 13, 2015
1 parent 952ed40 commit b009b7b
Show file tree
Hide file tree
Showing 41 changed files with 477 additions and 167 deletions.
Expand Up @@ -21,13 +21,15 @@
package org.sonar.server.computation;

import com.google.common.collect.ImmutableMap;
import org.sonar.api.utils.DateUtils;
import org.sonar.core.activity.ActivityLog;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.computation.db.AnalysisReportDto;

import java.util.Map;

import static org.sonar.api.utils.DateUtils.formatDateTimeNullSafe;
import static org.sonar.api.utils.DateUtils.timeToDate;

public class AnalysisReportLog implements ActivityLog {

private static final String ACTION = "LOG_ANALYSIS_REPORT";
Expand All @@ -48,9 +50,9 @@ public Map<String, String> getDetails() {
.put("projectName", project.name())
.put("projectUuid", project.uuid())
.put("status", String.valueOf(report.getStatus()))
.put("submittedAt", DateUtils.formatDateTimeNullSafe(report.getCreatedAt()))
.put("startedAt", DateUtils.formatDateTimeNullSafe(report.getStartedAt()))
.put("finishedAt", DateUtils.formatDateTimeNullSafe(report.getFinishedAt()))
.put("submittedAt", formatDateTimeNullSafe(timeToDate(report.getCreatedAt())))
.put("startedAt", formatDateTimeNullSafe(timeToDate(report.getStartedAt())))
.put("finishedAt", formatDateTimeNullSafe(timeToDate(report.getFinishedAt())))
.build();
}

Expand Down
Expand Up @@ -32,7 +32,6 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -89,7 +88,7 @@ public void remove(AnalysisReportDto report) {

DbSession session = dbClient.openSession(false);
try {
report.setFinishedAt(new Date(system2.now()));
report.setFinishedAt(system2.now());
dbClient.analysisReportDao().delete(session, report.getId());
session.commit();
} finally {
Expand Down
Expand Up @@ -99,7 +99,7 @@ private ComponentDto findProject(AnalysisReportDto report, DbSession session) {
}

private void logActivity(DbSession session, AnalysisReportDto report, ComponentDto project) {
report.setFinishedAt(System2.INSTANCE.newDate());
report.setFinishedAt(System2.INSTANCE.now());
activityService.write(session, Activity.Type.ANALYSIS_REPORT, new AnalysisReportLog(report, project));
}
}
Expand Up @@ -37,12 +37,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.sql.*;
import java.util.List;

import static org.sonar.core.computation.db.AnalysisReportDto.Status.PENDING;
Expand All @@ -65,7 +60,7 @@ public AnalysisReportDao() {
* Update all rows with: STATUS='PENDING', STARTED_AT=NULL, UPDATED_AT={now}
*/
public void resetAllToPendingStatus(DbSession session) {
mapper(session).resetAllToPendingStatus(system2.newDate());
mapper(session).resetAllToPendingStatus(system2.now());
}

public void truncate(DbSession session) {
Expand Down Expand Up @@ -95,7 +90,7 @@ public AnalysisReportDto pop(DbSession session) {
@VisibleForTesting
AnalysisReportDto tryToPop(DbSession session, long reportId) {
AnalysisReportMapper mapper = mapper(session);
int nbOfReportBooked = mapper.updateWithBookingReport(reportId, system2.newDate(), PENDING, WORKING);
int nbOfReportBooked = mapper.updateWithBookingReport(reportId, system2.now(), PENDING, WORKING);
if (nbOfReportBooked == 0) {
return null;
}
Expand All @@ -110,8 +105,8 @@ public List<AnalysisReportDto> selectAll(DbSession session) {
}

public AnalysisReportDto insert(DbSession session, AnalysisReportDto report) {
report.setCreatedAt(system2.newDate());
report.setUpdatedAt(system2.newDate());
report.setCreatedAt(system2.now());
report.setUpdatedAt(system2.now());

Connection connection = session.getConnection();
PreparedStatement ps = null;
Expand All @@ -124,10 +119,10 @@ public AnalysisReportDto insert(DbSession session, AnalysisReportDto report) {
ps.setLong(2, report.getSnapshotId());
ps.setString(3, report.getStatus().toString());
setData(ps, 4, report.getData());
ps.setTimestamp(5, dateToTimestamp(report.getCreatedAt()));
ps.setTimestamp(6, dateToTimestamp(report.getUpdatedAt()));
ps.setTimestamp(7, dateToTimestamp(report.getStartedAt()));
ps.setTimestamp(8, dateToTimestamp(report.getFinishedAt()));
ps.setLong(5, report.getCreatedAt());
setLong(ps, 6, report.getUpdatedAt());
setLong(ps, 7, report.getStartedAt());
setLong(ps, 8, report.getFinishedAt());

ps.executeUpdate();
connection.commit();
Expand All @@ -140,6 +135,14 @@ public AnalysisReportDto insert(DbSession session, AnalysisReportDto report) {
return report;
}

private void setLong(PreparedStatement ps, int index, @Nullable Long time) throws SQLException {
if (time == null) {
ps.setNull(index, Types.BIGINT);
} else {
ps.setLong(index, time);
}
}

private void setData(PreparedStatement ps, int parameterIndex, @Nullable InputStream reportDataStream) throws IOException, SQLException {
if (reportDataStream == null) {
ps.setBytes(parameterIndex, null);
Expand Down Expand Up @@ -176,13 +179,6 @@ public void selectAndDecompressToDir(DbSession session, long id, File toDir) {
}
}

private Timestamp dateToTimestamp(@Nullable Date date) {
if (date == null) {
return null;
}
return new Timestamp(date.getTime());
}

public void delete(DbSession session, long id) {
mapper(session).delete(id);
}
Expand Down
Expand Up @@ -30,6 +30,8 @@

import java.util.List;

import static org.sonar.api.utils.DateUtils.timeToDate;

/**
* @since 5.0
*/
Expand Down Expand Up @@ -67,9 +69,9 @@ private void writeReports(List<AnalysisReportDto> reports, JsonWriter json) {
json.prop("key", report.getId());
json.prop("projectKey", report.getProjectKey());
json.prop("projectName", report.getProjectKey());
json.propDateTime("startedAt", report.getStartedAt());
json.propDateTime("finishedAt", report.getFinishedAt());
json.propDateTime("submittedAt", report.getCreatedAt());
json.propDateTime("startedAt", timeToDate(report.getStartedAt()));
json.propDateTime("finishedAt", timeToDate(report.getFinishedAt()));
json.propDateTime("submittedAt", timeToDate(report.getCreatedAt()));
json.prop("status", report.getStatus().toString());
json.endObject();
}
Expand Down
Expand Up @@ -81,6 +81,7 @@ public interface DatabaseMigrations {
FeedUsersLongDates.class,
RenameComponentRelatedParamsInIssueFilters.class,
CopyScmAccountsFromAuthorsToUsers.class,
FeedIssueChangesLongDates.class
FeedIssueChangesLongDates.class,
FeedAnalysisReportsLongDates.class
);
}
@@ -0,0 +1,77 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.db.migrations.v51;

import org.sonar.api.utils.System2;
import org.sonar.core.persistence.Database;
import org.sonar.server.db.migrations.BaseDataChange;
import org.sonar.server.db.migrations.MassUpdate;
import org.sonar.server.db.migrations.Select;
import org.sonar.server.db.migrations.SqlStatement;

import java.sql.SQLException;
import java.util.Date;

public class FeedAnalysisReportsLongDates extends BaseDataChange {

private final System2 system;

public FeedAnalysisReportsLongDates(Database db, System2 system) {
super(db);
this.system = system;
}

@Override
public void execute(Context context) throws SQLException {
final long now = system.now();

MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate.select("SELECT a.created_at, a.updated_at, a.started_at, a.finished_at, a.id FROM analysis_reports a WHERE created_at_ms IS NULL");
massUpdate.update("UPDATE analysis_reports SET created_at_ms=?, updated_at_ms=?, started_at_ms=?, finished_at_ms=? WHERE id=?");
massUpdate.rowPluralName("analysis_reports");
massUpdate.execute(new MassUpdate.Handler() {
@Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Date createdAt = row.getDate(1);
Date updatedAt = row.getDate(2);
Date startedAt = row.getDate(3);
Date finishedAt = row.getDate(4);
Long id = row.getLong(5);

updateColumn(update, 1, createdAt);
updateColumn(update, 2, updatedAt);
update.setLong(3, startedAt == null ? null : startedAt.getTime());
update.setLong(4, finishedAt == null ? null : finishedAt.getTime());
update.setLong(5, id);
return true;
}

private void updateColumn(SqlStatement update, int position, Date time) throws SQLException {
if (time == null) {
update.setLong(position, now);
} else {
update.setLong(position, Math.min(now, time.getTime()));
}
}
});
}

}
Expand Up @@ -68,10 +68,10 @@ public void insert_find_analysis_report_log() {
AnalysisReportDto report = AnalysisReportDto.newForTests(1L)
.setProjectKey("projectKey")
.setStatus(FAILED)
.setCreatedAt(DateUtils.parseDate("2014-10-15"))
.setUpdatedAt(DateUtils.parseDate("2014-10-16"))
.setStartedAt(DateUtils.parseDate("2014-10-17"))
.setFinishedAt(DateUtils.parseDate("2014-10-18"));
.setCreatedAt(DateUtils.parseDate("2014-10-15").getTime())
.setUpdatedAt(DateUtils.parseDate("2014-10-16").getTime())
.setStartedAt(DateUtils.parseDate("2014-10-17").getTime())
.setFinishedAt(DateUtils.parseDate("2014-10-18").getTime());
ComponentDto project = ComponentTesting.newProjectDto();

service.write(dbSession, ANALYSIS_REPORT, new AnalysisReportLog(report, project));
Expand Down
Expand Up @@ -27,7 +27,6 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.core.computation.db.AnalysisReportDto;
import org.sonar.core.persistence.DbSession;
Expand All @@ -42,6 +41,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.api.utils.DateUtils.parseDate;
import static org.sonar.core.computation.db.AnalysisReportDto.Status.PENDING;
import static org.sonar.core.computation.db.AnalysisReportDto.Status.WORKING;

Expand All @@ -67,8 +67,8 @@ public void before() {
this.system2 = mock(System2.class);
this.sut = new AnalysisReportDao(system2);

when(system2.now()).thenReturn(DateUtils.parseDate("2014-09-26").getTime());
when(system2.newDate()).thenReturn(DateUtils.parseDate("2014-09-26"));
when(system2.now()).thenReturn(parseDate("2014-09-26").getTime());
when(system2.newDate()).thenReturn(parseDate("2014-09-26"));
}

@After
Expand Down Expand Up @@ -168,10 +168,10 @@ public void getById_maps_all_the_fields_except_the_data() {
AnalysisReportDto report = sut.selectById(session, 1L);

assertThat(report.getProjectKey()).isEqualTo(DEFAULT_PROJECT_KEY);
assertThat(report.getCreatedAt()).isEqualTo(DateUtils.parseDate("2014-09-24"));
assertThat(report.getUpdatedAt()).isEqualTo(DateUtils.parseDate("2014-09-25"));
assertThat(report.getStartedAt()).isEqualTo(DateUtils.parseDate("2014-09-26"));
assertThat(report.getFinishedAt()).isEqualTo(DateUtils.parseDate("2014-09-27"));
assertThat(report.getCreatedAt()).isEqualTo(parseDate("2014-09-24").getTime());
assertThat(report.getUpdatedAt()).isEqualTo(parseDate("2014-09-25").getTime());
assertThat(report.getStartedAt()).isEqualTo(parseDate("2014-09-26").getTime());
assertThat(report.getFinishedAt()).isEqualTo(parseDate("2014-09-27").getTime());
assertThat(report.getStatus()).isEqualTo(WORKING);
assertThat(report.getData()).isNull();
assertThat(report.getKey()).isEqualTo("1");
Expand Down
Expand Up @@ -53,9 +53,9 @@ public void list_active_reports() throws Exception {
.setProjectKey("project-key")
.setStatus(PENDING)
.setData(null)
.setCreatedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"))
.setStartedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"))
.setFinishedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"));
.setCreatedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200").getTime())
.setStartedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200").getTime())
.setFinishedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200").getTime());
List<AnalysisReportDto> reports = Lists.newArrayList(report);
when(queue.all()).thenReturn(reports);

Expand Down
@@ -0,0 +1,53 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.db.migrations.v51;

import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.core.persistence.DbTester;
import org.sonar.server.db.migrations.DatabaseMigration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class FeedAnalysisReportsLongDatesTest {
@ClassRule
public static DbTester db = new DbTester().schema(FeedAnalysisReportsLongDatesTest.class, "schema.sql");

@Test
public void execute() throws Exception {
db.prepareDbUnit(getClass(), "before.xml");

System2 system = mock(System2.class);
when(system.now()).thenReturn(1500000000000L);
DatabaseMigration migration = new FeedAnalysisReportsLongDates(db.database(), system);
migration.execute();

int count = db.countSql("select count(*) from analysis_reports where created_at_ms is not null and updated_at_ms is not null");
assertThat(count).isEqualTo(3);

int countWithAllDateFieldsNull = db
.countSql("select count(*) from analysis_reports where created_at_ms is not null and updated_at_ms is not null and started_at_ms is not null and finished_at_ms is not null");
assertThat(countWithAllDateFieldsNull).isEqualTo(2);
}
}
Expand Up @@ -41,7 +41,7 @@
depth="1" scope="DIR" qualifier="PAC" created_at="2008-12-02" build_date="2011-09-29"
version="2.1-SNAPSHOT" path="1.2."/>

<!-- PROJECT_ID = 3 no last snapshot -->
<!-- PROJECT_ID = 3 - no last snapshot -->
<snapshots id="5" project_id="3" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="3"
status="P" islast="[false]" purge_status="1"
period1_mode="days1" period1_param="30" period1_date="2011-09-24"
Expand Down

0 comments on commit b009b7b

Please sign in to comment.