import com.thoughtworks.xstream.XStream;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.configuration.Property;
if (CollectionUtils.isNotEmpty(sonarConfig.getProperties())) {
for (Property xmlProperty : sonarConfig.getProperties()) {
- databaseSession.save(new Property(xmlProperty.getKey(), xmlProperty.getValue()));
+ String propKey = xmlProperty.getKey();
+ if (!CoreProperties.SERVER_ID.equals(propKey)) {
+ // "sonar.core.id" must never be restored, it is unique for a server and it created once at the 1rst server startup
+ databaseSession.save(new Property(propKey, xmlProperty.getValue()));
+ }
}
}
}
private void clearProperties() {
- // "sonar.core.*" properties should not be cleared, most notably "sonar.core.id" which is the unique key used to identify the server
- // and which is used by the batch to verify that it connects to the same DB as the remote server (see SONAR-3126).
- databaseSession.createQuery("delete " + FROM_GLOBAL_PROPERTIES + " and prop_key NOT LIKE 'sonar.core.%'").executeUpdate();
+ // "sonar.core.id" property should not be cleared, because it is the unique key used to identify the server
+ // and it is used by the batch to verify that it connects to the same DB as the remote server (see SONAR-3126).
+ databaseSession.createQuery("delete " + FROM_GLOBAL_PROPERTIES + " and prop_key != '" + CoreProperties.SERVER_ID + "'").executeUpdate();
}
public void configure(XStream xStream) {
assertTrue(CollectionUtils.isEqualCollection(sonarConfig.getProperties(), Arrays.asList(prop1, prop2)));
}
-
@Test
public void shouldExportAnArrayProperty() {
setupData("shouldExportAnArrayProperty");
checkTables("shouldImportProperties", "properties");
}
+ @Test
+ public void shouldNotImportSonarCoreIdProperty() {
+ setupData("shouldNotImportSonarCoreIdProperty");
+
+ Collection<Property> newProperties = Arrays.asList(new Property("sonar.core.id", "11111111"));
+ sonarConfig.setProperties(newProperties);
+
+ new PropertiesBackup(getSession()).importXml(sonarConfig);
+
+ checkTables("shouldNotImportSonarCoreIdProperty", "properties");
+ }
+
@Test
public void shouldImportMultilineProperties() throws Exception {
setupData("shouldImportMultilineProperties");
<properties id="4" prop_key="user_prop" text_value="[null]" resource_id="[null]" user_id="3"/>
<properties id="5" prop_key="favorite" text_value="[null]" resource_id="8" user_id="3"/>
- <!-- Sonar Core properties that were not purged -->
+ <!-- Sonar Core property that was not purged -->
<properties id="6" prop_key="sonar.core.id" text_value="123456" resource_id="[null]" user_id="[null]"/>
+
+ <!-- Sonar Core properties that were not purged -->
+ <!--
<properties id="7" prop_key="sonar.core.version" text_value="3.1" resource_id="[null]" user_id="[null]"/>
<properties id="8" prop_key="sonar.core.startTime" text_value="2012-04-19T11:01:45+0200" resource_id="[null]" user_id="[null]"/>
+ -->
<!-- New properties that were imported -->
<properties id="9" prop_key="key1" text_value="value1" resource_id="[null]" user_id="[null]"/>
<properties id="4" prop_key="user_prop" text_value="[null]" resource_id="[null]" user_id="3"/>
<properties id="5" prop_key="favorite" text_value="[null]" resource_id="8" user_id="3"/>
- <!-- Sonar Core properties that must not be purged -->
+ <!-- Sonar Core property that must not be purged -->
<properties id="6" prop_key="sonar.core.id" text_value="123456" resource_id="[null]" user_id="[null]"/>
+
+ <!-- Sonar Core properties that can be purged -->
<properties id="7" prop_key="sonar.core.version" text_value="3.1" resource_id="[null]" user_id="[null]"/>
<properties id="8" prop_key="sonar.core.startTime" text_value="2012-04-19T11:01:45+0200" resource_id="[null]" user_id="[null]"/>
--- /dev/null
+<dataset>
+ <!-- Only global property left, unchanged -->
+ <properties id="1" prop_key="sonar.core.id" text_value="123456" resource_id="[null]" user_id="[null]"/>
+
+ <!-- project properties -->
+ <properties id="5" prop_key="old2" text_value="value2" resource_id="3" user_id="[null]"/>
+
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <!-- global properties -->
+ <properties id="1" prop_key="sonar.core.id" text_value="123456" resource_id="[null]" user_id="[null]"/>
+ <properties id="2" prop_key="sonar.core.version" text_value="3.1" resource_id="[null]" user_id="[null]"/>
+ <properties id="3" prop_key="sonar.core.startTime" text_value="2012-04-19T11:01:45+0200" resource_id="[null]" user_id="[null]"/>
+ <properties id="4" prop_key="key1" text_value="oldvalue" resource_id="[null]" user_id="[null]"/>
+
+ <!-- project properties -->
+ <properties id="5" prop_key="old2" text_value="value2" resource_id="3" user_id="[null]"/>
+
+</dataset>
\ No newline at end of file