Skip to content

Commit

Permalink
SONAR-9863 replace old "permanent server ID" by the new one
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof committed Oct 16, 2017
1 parent 36e1afc commit 252fbe6
Show file tree
Hide file tree
Showing 37 changed files with 302 additions and 832 deletions.
Expand Up @@ -145,7 +145,6 @@
import org.sonar.server.setting.DatabaseSettingLoader;
import org.sonar.server.setting.DatabaseSettingsEnabler;
import org.sonar.server.setting.ThreadLocalSettings;
import org.sonar.server.startup.LogServerId;
import org.sonar.server.test.index.TestIndexer;
import org.sonar.server.user.DefaultUserFinder;
import org.sonar.server.user.DeprecatedUserFinder;
Expand Down Expand Up @@ -442,7 +441,6 @@ private static void populateLevel4(ComponentContainer container, Props props) {

private static Object[] startupComponents() {
return new Object[] {
LogServerId.class,
ServerLifecycleNotifier.class,
PurgeCeActivities.class,
CeQueueCleaner.class
Expand Down
Expand Up @@ -146,7 +146,7 @@ public void real_start_without_cluster() throws IOException {
);
assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize(
CONTAINER_ITSELF
+ 12 // MigrationConfigurationModule
+ 13 // MigrationConfigurationModule
+ 17 // level 2
);
assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize(
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.sonar.server.platform.db.migration.version.v64.DbVersion64;
import org.sonar.server.platform.db.migration.version.v65.DbVersion65;
import org.sonar.server.platform.db.migration.version.v66.DbVersion66;
import org.sonar.server.platform.db.migration.version.v67.DbVersion67;

public class MigrationConfigurationModule extends Module {
@Override
Expand All @@ -47,6 +48,7 @@ protected void configureModule() {
DbVersion64.class,
DbVersion65.class,
DbVersion66.class,
DbVersion67.class,

// migration steps
MigrationStepRegistryImpl.class,
Expand Down
Expand Up @@ -144,17 +144,12 @@ public Long read(Row row) throws SQLException {
RowReader<Long> LONG_READER = new LongReader();

class StringReader implements RowReader<String> {
private StringReader() {
}

@Override
public String read(Row row) throws SQLException {
return row.getNullableString(1);
}
}

RowReader<String> STRING_READER = new StringReader();

@FunctionalInterface
interface RowHandler {
void handle(Row row) throws SQLException;
Expand Down
@@ -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();
}

}
@@ -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)
;
}
}
Expand Up @@ -17,25 +17,8 @@
* 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;
@ParametersAreNonnullByDefault
package org.sonar.server.platform.db.migration.version.v67;

import javax.annotation.concurrent.Immutable;
import javax.annotation.ParametersAreNonnullByDefault;

@Immutable
public final class ServerId {
private final String id;
private final boolean valid;

public ServerId(String id, boolean valid) {
this.id = id;
this.valid = valid;
}

public String getId() {
return id;
}

public boolean isValid() {
return valid;
}
}
Expand Up @@ -37,7 +37,7 @@ public void verify_component_count() {
assertThat(container.getPicoContainer().getComponentAdapters())
.hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER
// DbVersion classes
+ 9
+ 10
// Others
+ 3);
}
Expand Down
@@ -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);
}
}
@@ -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);
}

}
@@ -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");

0 comments on commit 252fbe6

Please sign in to comment.