-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-5183 timezones - migrate issues table from timestamp to long
- Loading branch information
Showing
70 changed files
with
885 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...er/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedIssuesLongDates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; | |||
} | |||
}); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...onar-server/src/test/java/org/sonar/server/db/migrations/v51/FeedIssuesLongDatesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
.../src/test/resources/org/sonar/server/db/migrations/v51/FeedIssuesLongDatesTest/before.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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> |
9 changes: 9 additions & 0 deletions
9
.../src/test/resources/org/sonar/server/db/migrations/v51/FeedIssuesLongDatesTest/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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, | |||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.