Skip to content

Commit

Permalink
change issue_changes timestamp columns to bigint/long
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Jan 12, 2015
1 parent 058fdeb commit 60ca04b
Show file tree
Hide file tree
Showing 28 changed files with 458 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.System2;
import org.sonar.batch.issue.IssueCache;
import org.sonar.batch.scan.LastLineHashes;
import org.sonar.batch.scan.filesystem.InputPathCache;
Expand Down Expand Up @@ -538,10 +539,10 @@ public void merge_matched_issue_on_manual_severity() throws Exception {

@Test
public void merge_issue_changelog_with_previous_changelog() throws Exception {
when(initialOpenIssues.selectChangelog("ABCDE")).thenReturn(newArrayList(new IssueChangeDto().setIssueKey("ABCD")));
when(initialOpenIssues.selectChangelog("ABCDE")).thenReturn(newArrayList(new IssueChangeDto().setIssueKey("ABCD").setCreatedAt(System2.INSTANCE.now())));

IssueDto previousIssue = new IssueDto().setKee("ABCDE").setResolution(null).setStatus("OPEN").setRuleKey("squid", "AvoidCycle")
.setLine(10).setMessage("Message").setEffortToFix(1.5).setDebt(1L);
.setLine(10).setMessage("Message").setEffortToFix(1.5).setDebt(1L).setCreatedAt(System2.INSTANCE.now());
DefaultIssue issue = new DefaultIssue();

IssueTrackingResult trackingResult = mock(IssueTrackingResult.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import org.sonar.server.db.migrations.v451.AddMissingCustomRuleParametersMigration;
import org.sonar.server.db.migrations.v451.DeleteUnescapedActivities;
import org.sonar.server.db.migrations.v50.*;
import org.sonar.server.db.migrations.v51.CopyScmAccountsFromAuthorsToUsers;
import org.sonar.server.db.migrations.v51.FeedIssueTags;
import org.sonar.server.db.migrations.v51.FeedUsersLongDates;
import org.sonar.server.db.migrations.v51.RenameComponentRelatedParamsInIssueFilters;
import org.sonar.server.db.migrations.v51.*;

import java.util.List;

Expand Down Expand Up @@ -83,6 +80,7 @@ public interface DatabaseMigrations {
FeedIssueTags.class,
FeedUsersLongDates.class,
RenameComponentRelatedParamsInIssueFilters.class,
CopyScmAccountsFromAuthorsToUsers.class
);
CopyScmAccountsFromAuthorsToUsers.class,
FeedIssueChangesLongDates.class
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 FeedIssueChangesLongDates extends BaseDataChange {

private final System2 system;

public FeedIssueChangesLongDates(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 i.created_at, i.updated_at, i.issue_change_creation_date_ms, i.id FROM issue_changes i WHERE created_at_ms IS NULL");
massUpdate.update("UPDATE issue_changes SET created_at_ms=?, updated_at_ms=?, issue_change_creation_date_ms=? WHERE id=?");
massUpdate.rowPluralName("issue_changes");
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 functionalCreatedAt = row.getDate(3);
Long id = row.getLong(4);

updateColumn(update, 1, createdAt);
updateColumn(update, 2, updatedAt);
if (functionalCreatedAt == null) {
update.setLong(3, null);
} else {
update.setLong(3, functionalCreatedAt.getTime());
}
update.setLong(4, 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()));
}
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.issue.internal.DefaultIssueComment;
import org.sonar.api.issue.internal.IssueChangeContext;
import org.sonar.api.utils.System2;
import org.sonar.core.issue.IssueUpdater;
import org.sonar.core.issue.db.IssueChangeDao;
import org.sonar.core.issue.db.IssueChangeDto;
Expand Down Expand Up @@ -143,7 +144,7 @@ public IssueComment editComment(String commentKey, String text, UserSession user
issueService.getByKey(comment.issueKey());

IssueChangeDto dto = IssueChangeDto.of(comment);
dto.setUpdatedAt(new Date());
dto.setUpdatedAt(System2.INSTANCE.now());
dto.setChangeData(text);
changeDao.update(dto);

Expand Down
Original file line number Diff line number Diff line change
@@ -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 FeedIssueChangesLongDatesTest {
@ClassRule
public static DbTester db = new DbTester().schema(FeedIssueChangesLongDatesTest.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 FeedIssueChangesLongDates(db.database(), system);
migration.execute();

int count = db.countSql("select count(*) from issue_changes 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 issue_changes where created_at_ms is not null and updated_at_ms is not null and issue_change_creation_date_ms is not null");
assertThat(countWithAllDateFieldsNull).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ public void issue_with_comment() throws Exception {
.setChangeData("*My comment*")
.setChangeType(IssueChangeDto.TYPE_COMMENT)
.setUserLogin("john")
.setCreatedAt(DateUtils.parseDate("2014-09-09")));
.setCreatedAt(DateUtils.parseDate("2014-09-09").getTime()));
tester.get(IssueChangeDao.class).insert(session,
new IssueChangeDto().setIssueKey(issue.getKey())
.setKey("COMMENT-ABCE")
.setChangeData("Another comment")
.setChangeType(IssueChangeDto.TYPE_COMMENT)
.setUserLogin("fabrice")
.setCreatedAt(DateUtils.parseDate("2014-09-10")));
.setCreatedAt(DateUtils.parseDate("2014-09-10").getTime()));
session.commit();
tester.get(IssueIndexer.class).indexAll();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<dataset>

<!-- new migration -->
<issue_changes id="1" kee="ABC-DEF" issue_key="ABC" user_login="[null]" change_type="[null]" change_data="[null]"
created_at="2013-05-18"
updated_at="2013-05-18"
issue_change_creation_date="2013-05-18"
created_at_ms="[null]"
updated_at_ms="[null]"
issue_change_creation_date_ms="[null]"/>
/>

<!-- re-entrant migration - ignore the issues that are already fed with new dates -->
<issue_changes id="2" kee="FGH-DEF" issue_key="FGH" user_login="[null]" change_type="[null]" change_data="[null]"
created_at="2013-05-18"
updated_at="2013-05-18"
issue_change_creation_date="2013-05-18"
created_at_ms="1500000000000"
updated_at_ms="1500000000000"
issue_change_creation_date_ms="1500000000000"/>

<!-- NULL dates -->
<issue_changes id="3" kee="MISSING-DEF" issue_key="MISSING" user_login="[null]" change_type="[null]"
change_data="[null]"
created_at="[null]"
updated_at="[null]"
issue_change_creation_date="[null]"
created_at_ms="[null]"
updated_at_ms="[null]"
issue_change_creation_date_ms="[null]"/>
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE "ISSUE_CHANGES" (
"ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
"KEE" VARCHAR(50),
"ISSUE_KEY" VARCHAR(50) NOT NULL,
"USER_LOGIN" VARCHAR(255),
"CHANGE_TYPE" VARCHAR(40),
"CHANGE_DATA" VARCHAR(16777215),
"CREATED_AT" TIMESTAMP,
"UPDATED_AT" TIMESTAMP,
"ISSUE_CHANGE_CREATION_DATE" TIMESTAMP,
"CREATED_AT_MS" BIGINT,
"UPDATED_AT_MS" BIGINT,
"ISSUE_CHANGE_CREATION_DATE_MS" BIGINT
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# 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.
#

#
# SonarQube 5.1
#
class AddIssueChangesLongDates < ActiveRecord::Migration

def self.up
add_column 'issue_changes', :created_at_ms, :big_integer, :null => true
add_column 'issue_changes', :updated_at_ms, :big_integer, :null => true
add_column 'issue_changes', :issue_change_creation_date_ms, :big_integer, :null => true
end
end

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# 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.
#

#
# SonarQube 5.1
#
class FeedIssueChangesLongDates < ActiveRecord::Migration

def self.up
execute_java_migration('org.sonar.server.db.migrations.v51.FeedIssueChangesLongDates')
end

end

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# 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.
#

#
# SonarQube 5.1
#
class RenameIssueChangesLongDates < ActiveRecord::Migration

def self.up
remove_column 'issue_changes', 'created_at'
remove_column 'issue_changes', 'updated_at'
remove_column 'issue_changes', 'issue_change_creation_date'
rename_column 'issue_changes', 'created_at_ms', 'created_at'
rename_column 'issue_changes', 'updated_at_ms', 'updated_at'
rename_column 'issue_changes', 'issue_change_creation_date_ms', 'issue_change_creation_date'
end
end

Loading

0 comments on commit 60ca04b

Please sign in to comment.