Skip to content

Commit

Permalink
SONAR-5183 timezones - migrate issues table from timestamp to long
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Feb 9, 2015
1 parent 2e0239d commit 5879a09
Show file tree
Hide file tree
Showing 70 changed files with 885 additions and 538 deletions.
Expand Up @@ -85,6 +85,7 @@ public interface DatabaseMigrations {
FeedAnalysisReportsLongDates.class,
UpdateProjectsModuleUuidPath.class,
FeedIssueComponentUuids.class,
FeedSnapshotsLongDates.class
FeedSnapshotsLongDates.class,
FeedIssuesLongDates.class
);
}
Expand Up @@ -94,6 +94,7 @@ public static String getString(ResultSet rs, int columnIndex) throws SQLExceptio
return rs.wasNull() ? null : s;
}

@CheckForNull
public static Date getDate(ResultSet rs, int columnIndex) throws SQLException {
Timestamp t = rs.getTimestamp(columnIndex);
return rs.wasNull() ? null : new Date(t.getTime());
Expand Down
@@ -0,0 +1,65 @@
/*
* 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;

public class FeedIssuesLongDates extends BaseDataChange {

private final System2 system2;

public FeedIssuesLongDates(Database db, System2 system2) {
super(db);
this.system2 = system2;
}

@Override
public void execute(Context context) throws SQLException {
final long now = system2.now();
MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate
.select("SELECT i.issue_creation_date, i.issue_update_date, i.issue_close_date, i.id FROM issues i WHERE issue_creation_date_ms IS NULL");
massUpdate
.update("UPDATE issues SET issue_creation_date_ms=?, issue_update_date_ms=?, issue_close_date_ms=? WHERE id=?");
massUpdate.rowPluralName("issues");
massUpdate.execute(new MassUpdate.Handler() {
@Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
for (int i = 1; i <= 3; i++) {
update.setLong(i, row.getDate(i) == null ? null : Math.min(now, row.getDate(i).getTime()));
}

Long id = row.getLong(4);
update.setLong(4, id);

return true;
}
});
}

}
Expand Up @@ -38,6 +38,9 @@
import java.sql.SQLException;
import java.util.Date;

import static org.sonar.api.utils.DateUtils.longToDate;
import static org.sonar.server.db.migrations.SqlUtil.getLong;

/**
* Scrolls over table ISSUES and reads documents to populate
* the issues index
Expand Down Expand Up @@ -87,6 +90,10 @@ class IssueResultSetIterator extends ResultSetIterator<IssueDoc> {

private static final Splitter TAGS_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();

private IssueResultSetIterator(PreparedStatement stmt) throws SQLException {
super(stmt);
}

static IssueResultSetIterator create(DbClient dbClient, Connection connection, long afterDate) {
try {
String sql = afterDate > 0L ? SQL_AFTER_DATE : SQL_ALL;
Expand All @@ -100,8 +107,16 @@ static IssueResultSetIterator create(DbClient dbClient, Connection connection, l
}
}

private IssueResultSetIterator(PreparedStatement stmt) throws SQLException {
super(stmt);
@CheckForNull
private static String extractDirPath(@Nullable String filePath) {
if (filePath != null) {
int lastSlashIndex = CharMatcher.anyOf("/").lastIndexIn(filePath);
if (lastSlashIndex > 0) {
return filePath.substring(0, lastSlashIndex);
}
return "/";
}
return null;
}

@Override
Expand All @@ -124,12 +139,12 @@ protected IssueDoc read(ResultSet rs) throws SQLException {
doc.setResolution(rs.getString(10));
doc.setSeverity(rs.getString(11));
doc.setStatus(rs.getString(12));
doc.setDebt(SqlUtil.getLong(rs, 13));
doc.setDebt(getLong(rs, 13));
doc.setReporter(rs.getString(14));
doc.setAuthorLogin(rs.getString(15));
doc.setFuncCloseDate(SqlUtil.getDate(rs, 16));
doc.setFuncCreationDate(SqlUtil.getDate(rs, 17));
doc.setFuncUpdateDate(SqlUtil.getDate(rs, 18));
doc.setFuncCloseDate(longToDate(getLong(rs, 16)));
doc.setFuncCreationDate(longToDate(getLong(rs, 17)));
doc.setFuncUpdateDate(longToDate(getLong(rs, 18)));
String ruleRepo = rs.getString(19);
String ruleKey = rs.getString(20);
doc.setRuleKey(RuleKey.of(ruleRepo, ruleKey).toString());
Expand All @@ -143,16 +158,4 @@ protected IssueDoc read(ResultSet rs) throws SQLException {
doc.setTags(ImmutableList.copyOf(TAGS_SPLITTER.split(tags == null ? "" : tags)));
return doc;
}

@CheckForNull
private static String extractDirPath(@Nullable String filePath) {
if (filePath != null) {
int lastSlashIndex = CharMatcher.anyOf("/").lastIndexIn(filePath);
if (lastSlashIndex > 0) {
return filePath.substring(0, lastSlashIndex);
}
return "/";
}
return null;
}
}
@@ -0,0 +1,81 @@
/*
* 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;
import static org.sonar.api.utils.DateUtils.parseDate;

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

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

DatabaseMigration migration = new FeedIssuesLongDates(db.database(), System2.INSTANCE);
migration.execute();

int count = db
.countSql("select count(*) from issues where " +
"issue_creation_date_ms is not null " +
"and issue_update_date_ms is not null " +
"and issue_close_date_ms is not null");
assertThat(count).isEqualTo(2);
}

@Test
public void take_now_if_date_in_the_future() throws Exception {
db.prepareDbUnit(getClass(), "before.xml");
System2 system2 = mock(System2.class);
when(system2.now()).thenReturn(0L);

DatabaseMigration migration = new FeedIssuesLongDates(db.database(), mock(System2.class));
migration.execute();

int count = db
.countSql("select count(*) from issues where " +
"issue_creation_date_ms = 0");
assertThat(count).isEqualTo(1);
}

@Test
public void take_snapshot_date_if_in_the_past() throws Exception {
db.prepareDbUnit(getClass(), "before.xml");
long snapshotTime = parseDate("2014-09-25").getTime();

DatabaseMigration migration = new FeedIssuesLongDates(db.database(), System2.INSTANCE);
migration.execute();

int count = db
.countSql("select count(*) from issues where " +
"issue_creation_date_ms=" + snapshotTime);
assertThat(count).isEqualTo(1);
}
}
Expand Up @@ -28,9 +28,9 @@
assignee="john"
author_login="[null]"
issue_attributes="[null]"
issue_creation_date="2013-04-16"
issue_update_date="2013-04-16"
issue_close_date="2013-04-16"
issue_creation_date="1366063200000"
issue_update_date="1366063200000"
issue_close_date="1366063200000"
created_at="1400000000000"
updated_at="[null]"
/>
Expand Down
Expand Up @@ -31,9 +31,9 @@
assignee="john"
author_login="[null]"
issue_attributes="[null]"
issue_creation_date="2013-04-16"
issue_update_date="2013-04-16"
issue_close_date="2013-04-16"
issue_creation_date="1366063200000"
issue_update_date="1366063200000"
issue_close_date="1366063200000"
created_at="1400000000000"
updated_at="[null]"
/>
Expand Down
@@ -0,0 +1,34 @@
<dataset>
<!-- new migration -->
<issues
id="1"
issue_creation_date="2014-09-25"
issue_creation_date_ms="[null]"
issue_update_date="2014-09-25"
issue_update_date_ms="[null]"
issue_close_date="2014-09-25"
issue_close_date_ms="[null]"
/>

<!-- re-entrant migration - ignore the issues that are already fed with new dates -->
<issues
id="2"
issue_creation_date="2014-09-25"
issue_creation_date_ms="1500000000"
issue_update_date="2014-09-25"
issue_update_date_ms="1500000000"
issue_close_date="2014-09-25"
issue_close_date_ms="1500000000"
/>

<!-- NULL dates -->
<issues
id="3"
issue_creation_date="[null]"
issue_creation_date_ms="[null]"
issue_update_date="[null]"
issue_update_date_ms="[null]"
issue_close_date="[null]"
issue_close_date_ms="[null]"
/>
</dataset>
@@ -0,0 +1,9 @@
CREATE TABLE "ISSUES" (
"ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
"ISSUE_CREATION_DATE" TIMESTAMP,
"ISSUE_CREATION_DATE_MS" BIGINT,
"ISSUE_CLOSE_DATE" TIMESTAMP,
"ISSUE_CLOSE_DATE_MS" BIGINT,
"ISSUE_UPDATE_DATE" TIMESTAMP,
"ISSUE_UPDATE_DATE_MS" BIGINT,
);
Expand Up @@ -16,9 +16,9 @@
issue_attributes="foo=bar"
tags="[null]"
action_plan_key="[null]"
issue_creation_date="2013-05-18"
issue_update_date="2013-05-18"
issue_close_date="2013-05-18"
issue_creation_date="1368828000000"
issue_update_date="1368828000000"
issue_close_date="1368828000000"
/>

<issue_changes id="1" kee="FGHIJ" issue_key="ABCDE" change_type="comment" user_login="emmerik"
Expand Down
Expand Up @@ -21,9 +21,9 @@
issue_attributes="foo=bar"
tags="[null]"
action_plan_key="[null]"
issue_creation_date="2013-05-18 00:00:00.0"
issue_update_date="2013-05-18 00:00:00.0"
issue_close_date="2013-05-18 00:00:00.0"
issue_creation_date="1368828000000"
issue_update_date="1368828000000"
issue_close_date="1368828000000"
/>

<issue_changes id="1" kee="FGHIJ" issue_key="ABCDE" change_type="comment" user_login="emmerik"
Expand Down
Expand Up @@ -27,8 +27,8 @@
reporter="emmerik"
issue_attributes="foo=bar"
action_plan_key="[null]"
issue_creation_date="2010-01-01"
issue_update_date="2010-02-02"
issue_creation_date="1262300400000"
issue_update_date="1265065200000"
issue_close_date="[null]"
/>
</dataset>
Expand Up @@ -19,9 +19,9 @@
action_plan_key="AP-1"
author_login="karadoc"
issue_attributes="JIRA=FOO-1234"
issue_creation_date="2013-04-16"
issue_update_date="2013-04-16"
issue_close_date="2013-04-16"
issue_creation_date="1366063200000"
issue_update_date="1366063200000"
issue_close_date="1366063200000"
created_at="1400000000000"
updated_at="1400000000000"
/>
Expand Down
Expand Up @@ -18,9 +18,9 @@
assignee="perceval"
author_login="karadoc"
issue_attributes="JIRA=FOO-1234"
issue_creation_date="2013-04-16"
issue_update_date="2013-04-16"
issue_close_date="2013-04-16"
issue_creation_date="1366063200000"
issue_update_date="1366063200000"
issue_close_date="1366063200000"
created_at="1400000000000"
updated_at="1450000000000"
/>
Expand Down
Expand Up @@ -19,9 +19,9 @@
assignee="karadoc"
issue_attributes="JIRA=FOO-1234"
tags="[null]"
issue_creation_date="2013-05-18"
issue_update_date="2013-05-19"
issue_close_date="2013-05-20"
issue_creation_date="1368828000000"
issue_update_date="1368914400000"
issue_close_date="1369000800000"
created_at="1400000000000"
updated_at="1450000000000"
action_plan_key="current_sprint"
Expand Down

0 comments on commit 5879a09

Please sign in to comment.