import org.sonar.server.db.migrations.Select;
import org.sonar.server.db.migrations.SqlStatement;
+import javax.annotation.Nullable;
+
import java.sql.SQLException;
import java.util.Date;
final Date now = new Date(system.now());
MassUpdate massUpdate = context.prepareMassUpdate();
- massUpdate.select("SELECT f.id, f.data FROM issue_filters f");
+ massUpdate.select("SELECT f.id, f.data FROM issue_filters f WHERE f.data like '%componentRoots=%'");
massUpdate.update("UPDATE issue_filters SET data=?, updated_at=? WHERE id=?");
massUpdate.execute(new MassUpdate.Handler() {
@Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1);
String data = row.getString(2);
- if (!data.contains(OLD_COMPONENT_ROOTS_FIELDS)) {
+ if (data == null) {
return false;
}
update.setString(1, convertData(context, data));
String field = fields[i];
if (field.contains(OLD_COMPONENT_ROOTS_FIELDS)) {
String[] componentRootValues = field.split("=");
- if (componentRootValues.length == 2) {
- newFields.append(convertField(context, componentRootValues[1]));
- } else {
- newFields.append(NEW_COMPONENT_ROOTS_FIELDS + "=");
- }
+ append(context, newFields, componentRootValues.length == 2 ? componentRootValues[1] : null);
} else {
newFields.append(field);
}
return newFields.toString();
}
- private String convertField(Context context, String projectKey) throws SQLException {
- String projectUuid = context.prepareSelect("SELECT p.uuid FROM projects p WHERE p.kee=?").setString(1, projectKey).get(Select.STRING_READER);
- return NEW_COMPONENT_ROOTS_FIELDS + "=" + projectUuid;
+ private void append(Context context, StringBuilder newFields, @Nullable String projectKey) throws SQLException {
+ if (projectKey != null) {
+ String projectUuid = context.prepareSelect("SELECT p.uuid FROM projects p WHERE p.kee=?").setString(1, projectKey).get(Select.STRING_READER);
+ if (projectUuid != null) {
+ newFields.append(NEW_COMPONENT_ROOTS_FIELDS + "=" + projectUuid);
+ }
+ }
}
}
@Test
public void execute() throws Exception {
- db.prepareDbUnit(getClass(), "before.xml");
+ db.prepareDbUnit(getClass(), "execute.xml");
migration.execute();
- db.assertDbUnit(getClass(), "after.xml", "issue_filters");
+ db.assertDbUnit(getClass(), "execute-result.xml", "issue_filters");
}
-}
\ No newline at end of file
+ @Test
+ public void not_migrate_already_migrated_data() throws Exception {
+ db.prepareDbUnit(getClass(), "not_migrate_already_migrated_data.xml");
+
+ migration.execute();
+
+ db.assertDbUnit(getClass(), "not_migrate_already_migrated_data-result.xml", "issue_filters");
+ }
+
+}
+++ /dev/null
-<dataset>
-
- <issue_filters
- id="1"
- name="Struts Issues"
- user_login="stephane"
- shared="[true]"
- description="All issues of Struts"
- data="componentRootUuids=ABCD"
- created_at="2013-06-10"
- updated_at="2014-10-29" />
-
- <issue_filters
- id="2"
- name="Open issues"
- user_login="michael"
- shared="[false]"
- description="All open issues"
- data="statuses=OPEN"
- created_at="2013-06-10"
- updated_at="2013-06-10" />
-
- <issue_filters
- id="3"
- name="Sonar Open issues"
- user_login="michael"
- shared="[true]"
- description="All open issues on Sonar"
- data="statuses=CLOSED|componentRootUuids=ABCD|resolution=FIXED"
- created_at="2013-06-10"
- updated_at="2014-10-29" />
-
- <issue_filters
- id="4"
- name="Bad component roots fields"
- user_login="michael"
- shared="[true]"
- description="All open issues on Sonar"
- data="statuses=CLOSED|componentRootUuids=|resolution=FIXED"
- created_at="2013-06-10"
- updated_at="2014-10-29" />
-
-</dataset>
+++ /dev/null
-<dataset>
-
- <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
- uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="[null]"
- description="the description" long_name="Apache Struts"
- enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]"
- created_at="2014-06-18" authorization_updated_at="2014-06-18" />
-
- <issue_filters
- id="1"
- name="Struts Issues"
- user_login="stephane"
- shared="[true]"
- description="All issues of Struts"
- data="componentRoots=org.struts:struts"
- created_at="2013-06-10"
- updated_at="2013-06-10" />
-
- <issue_filters
- id="2"
- name="Open issues"
- user_login="michael"
- shared="[false]"
- description="All open issues"
- data="statuses=OPEN"
- created_at="2013-06-10"
- updated_at="2013-06-10" />
-
- <issue_filters
- id="3"
- name="Sonar Open issues"
- user_login="michael"
- shared="[true]"
- description="All open issues on Sonar"
- data="statuses=CLOSED|componentRoots=org.struts:struts|resolution=FIXED"
- created_at="2013-06-10"
- updated_at="2013-06-10" />
-
- <issue_filters
- id="4"
- name="Bad component roots fields"
- user_login="michael"
- shared="[true]"
- description="All open issues on Sonar"
- data="statuses=CLOSED|componentRoots=|resolution=FIXED"
- created_at="2013-06-10"
- updated_at="2013-06-10" />
-
-</dataset>
--- /dev/null
+<dataset>
+
+ <issue_filters
+ id="1"
+ name="Struts Issues"
+ user_login="stephane"
+ shared="[true]"
+ description="All issues of Struts"
+ data="componentRootUuids=ABCD"
+ created_at="2013-06-10"
+ updated_at="2014-10-29" />
+
+ <issue_filters
+ id="2"
+ name="Open issues"
+ user_login="michael"
+ shared="[false]"
+ description="All open issues"
+ data="statuses=OPEN"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="3"
+ name="Sonar Open issues"
+ user_login="michael"
+ shared="[true]"
+ description="All open issues on Sonar"
+ data="statuses=CLOSED|componentRootUuids=ABCD|resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2014-10-29" />
+
+ <issue_filters
+ id="4"
+ name="Bad component roots fields"
+ user_login="michael"
+ shared="[true]"
+ description="Bad component roots fields"
+ data="statuses=CLOSED||resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2014-10-29" />
+
+ <issue_filters
+ id="5"
+ name="Linked on not existing file"
+ user_login="michael"
+ shared="[true]"
+ description="Linked on not existing file"
+ data="statuses=CLOSED||resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2014-10-29" />
+
+ <issue_filters
+ id="6"
+ name="Empty data"
+ user_login="michael"
+ shared="[true]"
+ description="Empty data"
+ data="[null]"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="[null]"
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]"
+ created_at="2014-06-18" authorization_updated_at="2014-06-18" />
+
+ <issue_filters
+ id="1"
+ name="Struts Issues"
+ user_login="stephane"
+ shared="[true]"
+ description="All issues of Struts"
+ data="componentRoots=org.struts:struts"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="2"
+ name="Open issues"
+ user_login="michael"
+ shared="[false]"
+ description="All open issues"
+ data="statuses=OPEN"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="3"
+ name="Sonar Open issues"
+ user_login="michael"
+ shared="[true]"
+ description="All open issues on Sonar"
+ data="statuses=CLOSED|componentRoots=org.struts:struts|resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="4"
+ name="Bad component roots fields"
+ user_login="michael"
+ shared="[true]"
+ description="Bad component roots fields"
+ data="statuses=CLOSED|componentRoots=|resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="5"
+ name="Linked on not existing file"
+ user_login="michael"
+ shared="[true]"
+ description="Linked on not existing file"
+ data="statuses=CLOSED|componentRoots=unknown|resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="6"
+ name="Empty data"
+ user_login="michael"
+ shared="[true]"
+ description="Empty data"
+ data="[null]"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <issue_filters
+ id="1"
+ name="Struts Issues"
+ user_login="stephane"
+ shared="[true]"
+ description="All issues of Struts"
+ data="componentRootUuids=ABCD"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="2"
+ name="Open issues"
+ user_login="michael"
+ shared="[false]"
+ description="All open issues"
+ data="statuses=OPEN"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="3"
+ name="Sonar Open issues"
+ user_login="michael"
+ shared="[true]"
+ description="All open issues on Sonar"
+ data="statuses=CLOSED|componentRootUuids=ABCD|resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="4"
+ name="Bad component roots fields"
+ user_login="michael"
+ shared="[true]"
+ description="All open issues on Sonar"
+ data="statuses=CLOSED|componentRootUuids=|resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="[null]"
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]"
+ created_at="2014-06-18" authorization_updated_at="2014-06-18" />
+
+ <issue_filters
+ id="1"
+ name="Struts Issues"
+ user_login="stephane"
+ shared="[true]"
+ description="All issues of Struts"
+ data="componentRootUuids=ABCD"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="2"
+ name="Open issues"
+ user_login="michael"
+ shared="[false]"
+ description="All open issues"
+ data="statuses=OPEN"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="3"
+ name="Sonar Open issues"
+ user_login="michael"
+ shared="[true]"
+ description="All open issues on Sonar"
+ data="statuses=CLOSED|componentRootUuids=ABCD|resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+ <issue_filters
+ id="4"
+ name="Bad component roots fields"
+ user_login="michael"
+ shared="[true]"
+ description="All open issues on Sonar"
+ data="statuses=CLOSED|componentRootUuids=|resolution=FIXED"
+ created_at="2013-06-10"
+ updated_at="2013-06-10" />
+
+</dataset>