Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change analysis_reports timestamp columns to bigint/long
- Loading branch information
Showing
41 changed files
with
477 additions
and
167 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
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
77 changes: 77 additions & 0 deletions
77
...server/src/main/java/org/sonar/server/db/migrations/v51/FeedAnalysisReportsLongDates.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 | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} |
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
53 changes: 53 additions & 0 deletions
53
...er/src/test/java/org/sonar/server/db/migrations/v51/FeedAnalysisReportsLongDatesTest.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 | 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 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); | ||
} | ||
} |
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.