Skip to content

Commit

Permalink
SONAR-6119 Move module UUID path migration, include self on parents
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont committed Feb 13, 2015
1 parent c8ead95 commit 33ad213
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 41 deletions.
Expand Up @@ -21,18 +21,17 @@
package org.sonar.server.db.migrations.v51; package org.sonar.server.db.migrations.v51;


import org.sonar.core.persistence.Database; import org.sonar.core.persistence.Database;
import org.sonar.server.db.migrations.BaseDataChange; import org.sonar.server.db.migrations.*;
import org.sonar.server.db.migrations.MassUpdate;
import org.sonar.server.db.migrations.MassUpdate.Handler; import org.sonar.server.db.migrations.MassUpdate.Handler;
import org.sonar.server.db.migrations.Select.Row; import org.sonar.server.db.migrations.Select.Row;
import org.sonar.server.db.migrations.SqlStatement;


import javax.annotation.Nullable; import javax.annotation.Nullable;


import java.sql.SQLException; import java.sql.SQLException;


/** /**
* SONAR-6054 * SONAR-6054
* SONAR-6119
*/ */
public class UpdateProjectsModuleUuidPath extends BaseDataChange { public class UpdateProjectsModuleUuidPath extends BaseDataChange {


Expand All @@ -44,37 +43,65 @@ public UpdateProjectsModuleUuidPath(Database db) {


@Override @Override
public void execute(Context context) throws SQLException { public void execute(Context context) throws SQLException {
MassUpdate update = context.prepareMassUpdate().rowPluralName("projects"); MassUpdate update = context.prepareMassUpdate().rowPluralName("components");
update.select("SELECT p.id, p.module_uuid_path FROM projects p"); update.select("SELECT p.id, p.module_uuid_path, p.uuid, p.scope, p.qualifier FROM projects p");
update.update("UPDATE projects SET module_uuid_path=? WHERE id=?"); update.update("UPDATE projects SET module_uuid_path=? WHERE id=?");
update.execute(new Handler() { update.execute(new ModuleUuidPathUpdateHandler());
@Override
public boolean handle(Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1);
String moduleUuidPath = row.getString(2);
if (needUpdate(moduleUuidPath)) {
update.setString(1, newModuleUuidPath(moduleUuidPath));
update.setLong(2, id);
return true;
}
return false;
}
});
} }


private static boolean needUpdate(@Nullable String moduleUuidPath) { private static final class ModuleUuidPathUpdateHandler implements Handler {
return moduleUuidPath == null || !(moduleUuidPath.startsWith(SEP) && moduleUuidPath.endsWith(SEP)); @Override
} public boolean handle(Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1);
String moduleUuidPath = row.getString(2);
String uuid = row.getString(3);
String scope = row.getString(4);
String qualifier = row.getString(5);

boolean needUpdate = false;
String newModuleUuidPath = moduleUuidPath;

if (needUpdateForSeparators(moduleUuidPath)) {
newModuleUuidPath = newModuleUuidPathWithSeparators(moduleUuidPath);
needUpdate = true;
}

if (needUpdateToIncludeItself(newModuleUuidPath, uuid, scope, qualifier)) {
newModuleUuidPath = newModuleUuidPathIncludingItself(newModuleUuidPath, uuid);
needUpdate = true;
}

if (needUpdate) {
update.setString(1, newModuleUuidPath);
update.setLong(2, id);
}
return needUpdate;
}

private static boolean needUpdateForSeparators(@Nullable String moduleUuidPath) {
return moduleUuidPath == null || !(moduleUuidPath.startsWith(SEP) && moduleUuidPath.endsWith(SEP));
}


private static String newModuleUuidPath(@Nullable String oldModuleUuidPath) { private static String newModuleUuidPathWithSeparators(@Nullable String oldModuleUuidPath) {
if (oldModuleUuidPath == null || oldModuleUuidPath.isEmpty()) { if (oldModuleUuidPath == null || oldModuleUuidPath.isEmpty()) {
return SEP; return SEP;
} else { } else {
StringBuilder newModuleUuidPath = new StringBuilder(oldModuleUuidPath); StringBuilder newModuleUuidPath = new StringBuilder(oldModuleUuidPath);
newModuleUuidPath.insert(0, SEP); newModuleUuidPath.insert(0, SEP);
newModuleUuidPath.append(SEP);
return newModuleUuidPath.toString();
}
}

private static boolean needUpdateToIncludeItself(String moduleUuidPath, @Nullable String uuid, @Nullable String scope, @Nullable String qualifier) {
return "PRJ".equals(scope) && !("DEV_PRJ".equals(qualifier)) && !(moduleUuidPath.contains(uuid));
}

private static String newModuleUuidPathIncludingItself(String moduleUuidPath, String uuid) {
StringBuilder newModuleUuidPath = new StringBuilder(moduleUuidPath);
newModuleUuidPath.append(uuid);
newModuleUuidPath.append(SEP); newModuleUuidPath.append(SEP);
return newModuleUuidPath.toString(); return newModuleUuidPath.toString();
} }
} }

} }
Expand Up @@ -2,21 +2,21 @@


<!-- root project --> <!-- root project -->
<projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts" deprecated_kee="org.struts:struts" <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts" deprecated_kee="org.struts:struts"
uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="." uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path=".ABCD."
description="the description" long_name="Apache Struts" description="the description" long_name="Apache Struts"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>


<!-- module --> <!-- module -->
<projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core" deprecated_kee="org.struts:struts-core" <projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core" deprecated_kee="org.struts:struts-core"
uuid="EFGH" project_uuid="ABCD" module_uuid="[null]" module_uuid_path=".ABCD." uuid="EFGH" project_uuid="ABCD" module_uuid="[null]" module_uuid_path=".ABCD.EFGH."
scope="PRJ" qualifier="BRC" long_name="Struts Core" scope="PRJ" qualifier="BRC" long_name="Struts Core"
description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>


<!-- sub module --> <!-- sub module, already has dots: only itself appended -->
<projects id="3" root_id="1" kee="org.struts:struts-data" name="Struts Data" deprecated_kee="org.struts:struts-data" <projects id="3" root_id="1" kee="org.struts:struts-data" name="Struts Data" deprecated_kee="org.struts:struts-data"
uuid="FGHI" project_uuid="ABCD" module_uuid="EFGH" module_uuid_path=".ABCD.EFGH." uuid="FGHI" project_uuid="ABCD" module_uuid="EFGH" module_uuid_path=".ABCD.EFGH.FGHI."
scope="PRJ" qualifier="BRC" long_name="Struts Data" scope="PRJ" qualifier="BRC" long_name="Struts Data"
description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>
Expand All @@ -38,11 +38,53 @@
enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts/RequestContext.java" enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts/RequestContext.java"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>


<!-- view -->
<projects id="6" root_id="[null]" scope="PRJ" qualifier="VW" kee="Teams" name="Teams" deprecated_kee="Teams"
uuid="MEAT" project_uuid="MEAT" module_uuid="[null]" module_uuid_path=".MEAT."
description="the description" long_name="Teams"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- sub-view -->
<projects id="7" root_id="6" scope="PRJ" qualifier="SVW" kee="Platform_Team" name="Platform Team" deprecated_kee="Platform_Team"
uuid="PLAT" project_uuid="MEAT" module_uuid="MEAT" module_uuid_path=".MEAT.PLAT."
description="the description" long_name="Platform Team"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- view technical project - unchanged -->
<projects id="8" root_id="6" scope="FIL" qualifier="TRK" kee="Platform_Team:sonarqube" name="SonarQube" deprecated_kee="Platform_Team:sonarqube"
uuid="SNQB" project_uuid="PLAT" module_uuid="PLAT" module_uuid_path=".MEAT.PLAT."
description="the description" long_name="Platform Team"
enabled="[true]" language="[null]" copy_resource_id="42" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- root project already has dots, appending itself -->
<projects id="9" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.sonar:sample" name="Sample" deprecated_kee="org.sonar:sample"
uuid="WDOT" project_uuid="WDOT" module_uuid="[null]" module_uuid_path=".WDOT."
description="the description" long_name="Sample"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- root project with module_uuid_path NULL --> <!-- root project with module_uuid_path NULL -->
<projects id="10" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.sonar:sample" name="Sample" deprecated_kee="org.sonar:sample" <projects id="10" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.sonar:sample" name="Sample" deprecated_kee="org.sonar:sample"
uuid="DCBA" project_uuid="DCBA" module_uuid="[null]" module_uuid_path="." uuid="DCBA" project_uuid="DCBA" module_uuid="[null]" module_uuid_path=".DCBA."
description="the description" long_name="Sample" description="the description" long_name="Sample"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>


<!-- developer -->
<projects id="11" root_id="[null]" scope="PRJ" qualifier="DEV" kee="DEV:anakin.skywalker" name="Anakin Skywalker" deprecated_kee="DEV:anakin.skywalker"
uuid="VADR" project_uuid="VADR" module_uuid="[null]" module_uuid_path=".VADR."
description="the description" long_name="Anakin Skywalker"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="1" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- developer technical project, with dots - unchanged -->
<projects id="12" root_id="11" scope="PRJ" qualifier="DEV_PRJ" kee="DEV:anakin.skywalker:Executor" name="Executor Star Dreadnaught" deprecated_kee="DEV:anakin.skywalker:Executor"
uuid="EXCT" project_uuid="VADR" module_uuid="VADR" module_uuid_path=".VADR."
description="the description" long_name="Executor Star Dreadnaught"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

</dataset> </dataset>
Expand Up @@ -14,9 +14,9 @@
description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>


<!-- sub module --> <!-- sub module, already has dots: only itself appended -->
<projects id="3" root_id="1" kee="org.struts:struts-data" name="Struts Data" deprecated_kee="org.struts:struts-data" <projects id="3" root_id="1" kee="org.struts:struts-data" name="Struts Data" deprecated_kee="org.struts:struts-data"
uuid="FGHI" project_uuid="ABCD" module_uuid="EFGH" module_uuid_path="ABCD.EFGH" uuid="FGHI" project_uuid="ABCD" module_uuid="EFGH" module_uuid_path=".ABCD.EFGH."
scope="PRJ" qualifier="BRC" long_name="Struts Data" scope="PRJ" qualifier="BRC" long_name="Struts Data"
description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>
Expand All @@ -38,11 +38,53 @@
enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts/RequestContext.java" enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts/RequestContext.java"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>


<!-- view -->
<projects id="6" root_id="[null]" scope="PRJ" qualifier="VW" kee="Teams" name="Teams" deprecated_kee="Teams"
uuid="MEAT" project_uuid="MEAT" module_uuid="[null]" module_uuid_path=""
description="the description" long_name="Teams"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- sub-view -->
<projects id="7" root_id="6" scope="PRJ" qualifier="SVW" kee="Platform_Team" name="Platform Team" deprecated_kee="Platform_Team"
uuid="PLAT" project_uuid="MEAT" module_uuid="MEAT" module_uuid_path="MEAT"
description="the description" long_name="Platform Team"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- view technical project, already has dots - unchanged -->
<projects id="8" root_id="6" scope="FIL" qualifier="TRK" kee="Platform_Team:sonarqube" name="SonarQube" deprecated_kee="Platform_Team:sonarqube"
uuid="SNQB" project_uuid="PLAT" module_uuid="PLAT" module_uuid_path=".MEAT.PLAT."
description="the description" long_name="Platform Team"
enabled="[true]" language="[null]" copy_resource_id="42" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- root project already has dots, appending itself -->
<projects id="9" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.sonar:sample" name="Sample" deprecated_kee="org.sonar:sample"
uuid="WDOT" project_uuid="WDOT" module_uuid="[null]" module_uuid_path="."
description="the description" long_name="Sample"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- root project with module_uuid_path NULL --> <!-- root project with module_uuid_path NULL -->
<projects id="10" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.sonar:sample" name="Sample" deprecated_kee="org.sonar:sample" <projects id="10" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.sonar:sample" name="Sample" deprecated_kee="org.sonar:sample"
uuid="DCBA" project_uuid="DCBA" module_uuid="[null]" module_uuid_path="[null]" uuid="DCBA" project_uuid="DCBA" module_uuid="[null]" module_uuid_path="[null]"
description="the description" long_name="Sample" description="the description" long_name="Sample"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/> created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>


<!-- developer -->
<projects id="11" root_id="[null]" scope="PRJ" qualifier="DEV" kee="DEV:anakin.skywalker" name="Anakin Skywalker" deprecated_kee="DEV:anakin.skywalker"
uuid="VADR" project_uuid="VADR" module_uuid="[null]" module_uuid_path=""
description="the description" long_name="Anakin Skywalker"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="1" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

<!-- developer technical project, with dots - unchanged -->
<projects id="12" root_id="11" scope="PRJ" qualifier="DEV_PRJ" kee="DEV:anakin.skywalker:Executor" name="Executor Star Dreadnaught" deprecated_kee="DEV:anakin.skywalker:Executor"
uuid="EXCT" project_uuid="VADR" module_uuid="VADR" module_uuid_path=".VADR."
description="the description" long_name="Executor Star Dreadnaught"
enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02 13:58:00.00" authorization_updated_at="[null]"/>

</dataset> </dataset>

0 comments on commit 33ad213

Please sign in to comment.