Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-9863 replace old "permanent server ID" by the new one
- Loading branch information
Simon Brandhof
committed
Oct 16, 2017
1 parent
36e1afc
commit 252fbe6
Showing
37 changed files
with
302 additions
and
832 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
64 changes: 64 additions & 0 deletions
64
.../main/java/org/sonar/server/platform/db/migration/version/v66/CopyDeprecatedServerId.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,64 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2017 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program 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. | ||
* | ||
* This program 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.platform.db.migration.version.v66; | ||
|
||
import java.sql.SQLException; | ||
import org.sonar.db.Database; | ||
import org.sonar.server.platform.db.migration.step.DataChange; | ||
import org.sonar.server.platform.db.migration.step.Select; | ||
|
||
public class CopyDeprecatedServerId extends DataChange { | ||
|
||
private static final String DEPRECATED_KEY = "sonar.server_id"; | ||
private static final String NEW_KEY = "sonar.core.id"; | ||
|
||
public CopyDeprecatedServerId(Database db) { | ||
super(db); | ||
} | ||
|
||
@Override | ||
protected void execute(Context context) throws SQLException { | ||
String deprecatedValue = context | ||
.prepareSelect("select text_value from properties where prop_key = '" + DEPRECATED_KEY + "'") | ||
.get(new Select.StringReader()); | ||
if (deprecatedValue != null && !deprecatedValue.isEmpty()) { | ||
deleteProperty(context, NEW_KEY); | ||
context.prepareUpsert("insert into properties" + | ||
" (prop_key, is_empty, text_value, created_at)" + | ||
" values " + | ||
" (?, ?, ?, ?)") | ||
.setString(1, NEW_KEY) | ||
.setBoolean(2, false) | ||
.setString(3, deprecatedValue) | ||
.setLong(4, System.currentTimeMillis()) | ||
.execute() | ||
.commit(); | ||
} | ||
deleteProperty(context, DEPRECATED_KEY); | ||
} | ||
|
||
private static void deleteProperty(Context context, String key) throws SQLException { | ||
context.prepareUpsert("delete from properties where prop_key = '" + key + "'") | ||
.execute() | ||
.commit(); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...gration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.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,33 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2017 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program 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. | ||
* | ||
* This program 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.platform.db.migration.version.v67; | ||
|
||
import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; | ||
import org.sonar.server.platform.db.migration.version.DbVersion; | ||
import org.sonar.server.platform.db.migration.version.v66.CopyDeprecatedServerId; | ||
|
||
public class DbVersion67 implements DbVersion { | ||
@Override | ||
public void addSteps(MigrationStepRegistry registry) { | ||
registry | ||
.add(1830, "Copy deprecated server ID", CopyDeprecatedServerId.class) | ||
; | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
...t/java/org/sonar/server/platform/db/migration/version/v67/CopyDeprecatedServerIdTest.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,91 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2017 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program 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. | ||
* | ||
* This program 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.platform.db.migration.version.v67; | ||
|
||
import java.sql.SQLException; | ||
import java.util.List; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.sonar.db.CoreDbTester; | ||
import org.sonar.server.platform.db.migration.version.v66.CopyDeprecatedServerId; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CopyDeprecatedServerIdTest { | ||
|
||
private static final String DEPRECATED_KEY = "sonar.server_id"; | ||
private static final String TARGET_KEY = "sonar.core.id"; | ||
|
||
@Rule | ||
public CoreDbTester db = CoreDbTester.createForSchema(CopyDeprecatedServerIdTest.class, "properties.sql"); | ||
|
||
private CopyDeprecatedServerId underTest = new CopyDeprecatedServerId(db.database()); | ||
|
||
@Test | ||
public void override_server_id_with_deprecated_value_if_present() throws SQLException { | ||
insertProperty(DEPRECATED_KEY, "foo"); | ||
insertProperty(TARGET_KEY, "bar"); | ||
|
||
underTest.execute(); | ||
|
||
assertThatTargetKeyHasValue("foo"); | ||
assertThatDeprecatedKeyDoesNotExist(); | ||
} | ||
|
||
@Test | ||
public void set_server_id_with_deprecated_value_if_present() throws SQLException { | ||
// the target property does not exist | ||
insertProperty(DEPRECATED_KEY, "foo"); | ||
|
||
underTest.execute(); | ||
|
||
assertThatTargetKeyHasValue("foo"); | ||
assertThatDeprecatedKeyDoesNotExist(); | ||
} | ||
|
||
@Test | ||
public void keep_existing_server_id_if_deprecated_value_if_absent() throws SQLException { | ||
insertProperty(TARGET_KEY, "foo"); | ||
|
||
underTest.execute(); | ||
|
||
assertThatTargetKeyHasValue("foo"); | ||
assertThatDeprecatedKeyDoesNotExist(); | ||
} | ||
|
||
private void assertThatTargetKeyHasValue(String expected) { | ||
String value = (String) db.selectFirst("SELECT TEXT_VALUE FROM PROPERTIES WHERE PROP_KEY = '" + TARGET_KEY + "'") | ||
.get("TEXT_VALUE"); | ||
assertThat(value).isEqualTo(expected); | ||
} | ||
|
||
private void assertThatDeprecatedKeyDoesNotExist() { | ||
List rows = db.select("SELECT * FROM PROPERTIES WHERE PROP_KEY = '" + DEPRECATED_KEY + "'"); | ||
assertThat(rows).isEmpty(); | ||
} | ||
|
||
public void insertProperty(String key, String value) { | ||
db.executeInsert( | ||
"properties", | ||
"prop_key", key, | ||
"is_empty", "false", | ||
"text_value", value); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ion/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.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,42 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2017 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program 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. | ||
* | ||
* This program 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.platform.db.migration.version.v67; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount; | ||
import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; | ||
|
||
public class DbVersion67Test { | ||
|
||
private DbVersion67 underTest = new DbVersion67(); | ||
|
||
@Test | ||
public void migrationNumber_starts_at_1830() { | ||
verifyMinimumMigrationNumber(underTest, 1830); | ||
} | ||
|
||
@Test | ||
public void verify_migration_count() { | ||
verifyMigrationCount(underTest, 1); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
.../sonar/server/platform/db/migration/version/v67/CopyDeprecatedServerIdTest/properties.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CREATE TABLE "PROPERTIES" ( | ||
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), | ||
"PROP_KEY" VARCHAR(512) NOT NULL, | ||
"RESOURCE_ID" INTEGER, | ||
"USER_ID" INTEGER, | ||
"IS_EMPTY" BOOLEAN NOT NULL, | ||
"TEXT_VALUE" VARCHAR(4000), | ||
"CLOB_VALUE" CLOB(2147483647), | ||
"CREATED_AT" BIGINT | ||
); | ||
CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES" ("PROP_KEY"); |
Oops, something went wrong.