From 5f682d84a007b0abe561f63678bef61e8744ca24 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 16 Jun 2016 22:41:26 +0200 Subject: [PATCH] Fix Quality flaws in DB migrations --- .../org/sonar/db/version/v60/AddUuidColumnsToProjects.java | 2 +- .../org/sonar/db/version/v60/AddUuidColumnsToSnapshots.java | 5 +---- .../org/sonar/db/version/v60/DropIdColumnsFromSnapshots.java | 5 +---- .../db/version/v60/MakeUuidColumnsNotNullOnProjects.java | 2 +- .../db/version/v60/MakeUuidColumnsNotNullOnSnapshots.java | 5 +---- .../db/version/v60/PopulateUuidColumnsOfResourceIndex.java | 2 +- .../sonar/db/version/v60/PopulateUuidColumnsOfSnapshots.java | 2 +- .../sonar/db/version/v60/AddUuidColumnsToProjectsTest.java | 3 ++- .../sonar/db/version/v60/AddUuidColumnsToSnapshotsTest.java | 3 ++- .../sonar/db/version/v60/CleanOrphanRowsInSnapshotsTest.java | 3 ++- 10 files changed, 13 insertions(+), 19 deletions(-) diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/AddUuidColumnsToProjects.java b/sonar-db/src/main/java/org/sonar/db/version/v60/AddUuidColumnsToProjects.java index 9ebe7c5f5f2..7cda11cbee7 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/AddUuidColumnsToProjects.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/AddUuidColumnsToProjects.java @@ -24,12 +24,12 @@ import org.sonar.db.Database; import org.sonar.db.version.AddColumnsBuilder; import org.sonar.db.version.DdlChange; +import static org.sonar.db.version.VarcharColumnDef.UUID_VARCHAR_SIZE; import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; public class AddUuidColumnsToProjects extends DdlChange { private static final String TABLE_PROJECTS = "projects"; - private static final int UUID_VARCHAR_SIZE = 50; public AddUuidColumnsToProjects(Database db) { super(db); diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/AddUuidColumnsToSnapshots.java b/sonar-db/src/main/java/org/sonar/db/version/v60/AddUuidColumnsToSnapshots.java index 34dbbfd7f48..a9abdc76a9b 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/AddUuidColumnsToSnapshots.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/AddUuidColumnsToSnapshots.java @@ -31,16 +31,13 @@ public class AddUuidColumnsToSnapshots extends DdlChange { private static final String TABLE_SNAPSHOTS = "snapshots"; - private final Database db; - public AddUuidColumnsToSnapshots(Database db) { super(db); - this.db = db; } @Override public void execute(Context context) throws SQLException { - context.execute(new AddColumnsBuilder(db.getDialect(), TABLE_SNAPSHOTS) + context.execute(new AddColumnsBuilder(getDatabase().getDialect(), TABLE_SNAPSHOTS) .addColumn(newVarcharColumnDefBuilder().setColumnName("component_uuid").setLimit(UUID_VARCHAR_SIZE).setIsNullable(true).build()) .addColumn(newVarcharColumnDefBuilder().setColumnName("root_component_uuid").setLimit(UUID_VARCHAR_SIZE).setIsNullable(true).build()) .build()); diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/DropIdColumnsFromSnapshots.java b/sonar-db/src/main/java/org/sonar/db/version/v60/DropIdColumnsFromSnapshots.java index dbaa08c9c7e..b1ff5935d3b 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/DropIdColumnsFromSnapshots.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/DropIdColumnsFromSnapshots.java @@ -28,18 +28,15 @@ public class DropIdColumnsFromSnapshots extends DdlChange { private static final String TABLE_SNAPSHOTS = "snapshots"; - private final Database db; - public DropIdColumnsFromSnapshots(Database db) { super(db); - this.db = db; } @Override public void execute(Context context) throws SQLException { context.execute( new DropColumnsBuilder( - db.getDialect(), TABLE_SNAPSHOTS, + getDatabase().getDialect(), TABLE_SNAPSHOTS, "project_id", "root_project_id") .build()); } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnProjects.java b/sonar-db/src/main/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnProjects.java index 18152b30c26..2a6c87a1e2d 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnProjects.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnProjects.java @@ -24,12 +24,12 @@ import org.sonar.db.Database; import org.sonar.db.version.AlterColumnsBuilder; import org.sonar.db.version.DdlChange; +import static org.sonar.db.version.VarcharColumnDef.UUID_VARCHAR_SIZE; import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; public class MakeUuidColumnsNotNullOnProjects extends DdlChange { private static final String TABLE_PROJECTS = "projects"; - private static final int UUID_VARCHAR_SIZE = 50; public MakeUuidColumnsNotNullOnProjects(Database db) { super(db); diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnSnapshots.java b/sonar-db/src/main/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnSnapshots.java index 6bf5025cba5..c821e589aa2 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnSnapshots.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnSnapshots.java @@ -31,16 +31,13 @@ public class MakeUuidColumnsNotNullOnSnapshots extends DdlChange { private static final String TABLE_SNAPSHOTS = "snapshots"; - private final Database db; - public MakeUuidColumnsNotNullOnSnapshots(Database db) { super(db); - this.db = db; } @Override public void execute(Context context) throws SQLException { - context.execute(new AlterColumnsBuilder(db.getDialect(), TABLE_SNAPSHOTS) + context.execute(new AlterColumnsBuilder(getDatabase().getDialect(), TABLE_SNAPSHOTS) .updateColumn(newVarcharColumnDefBuilder().setColumnName("component_uuid").setLimit(UUID_VARCHAR_SIZE).setIsNullable(false).build()) .updateColumn(newVarcharColumnDefBuilder().setColumnName("root_component_uuid").setLimit(UUID_VARCHAR_SIZE).setIsNullable(false).build()) .build()); diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidColumnsOfResourceIndex.java b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidColumnsOfResourceIndex.java index e5be59b5f48..e7b423a6b08 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidColumnsOfResourceIndex.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidColumnsOfResourceIndex.java @@ -44,7 +44,7 @@ public class PopulateUuidColumnsOfResourceIndex extends BaseDataChange { populateUuidColumns(context, componentUuidById); } - private Map buildComponentUuidMap(Context context) throws SQLException { + private static Map buildComponentUuidMap(Context context) throws SQLException { Map componentUuidById = new HashMap<>(); context.prepareSelect("select distinct p.id, p.uuid from projects p" + " join resource_index ri1 on ri1.resource_id = p.id and ri1.component_uuid is null") diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidColumnsOfSnapshots.java b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidColumnsOfSnapshots.java index 146421d8200..4fb0fd2eda5 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidColumnsOfSnapshots.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidColumnsOfSnapshots.java @@ -44,7 +44,7 @@ public class PopulateUuidColumnsOfSnapshots extends BaseDataChange { populateUuidColumns(context, componentUuidById); } - private Map buildComponentUuidMap(Context context) throws SQLException { + private static Map buildComponentUuidMap(Context context) throws SQLException { Map componentUuidById = new HashMap<>(); context.prepareSelect("select distinct p.id, p.uuid from projects p" + " join snapshots sn1 on sn1.project_id = p.id and sn1.component_uuid is null") diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/AddUuidColumnsToProjectsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/AddUuidColumnsToProjectsTest.java index 6d8117e91d5..dabfef32ee9 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v60/AddUuidColumnsToProjectsTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v60/AddUuidColumnsToProjectsTest.java @@ -29,7 +29,8 @@ import org.sonar.db.DbTester; public class AddUuidColumnsToProjectsTest { - public static final String PROJECTS_TABLE = "projects"; + private static final String PROJECTS_TABLE = "projects"; + @Rule public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddUuidColumnsToProjectsTest.class, "old_projects.sql"); @Rule diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/AddUuidColumnsToSnapshotsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/AddUuidColumnsToSnapshotsTest.java index 17063d56218..ec76166493b 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v60/AddUuidColumnsToSnapshotsTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v60/AddUuidColumnsToSnapshotsTest.java @@ -31,7 +31,8 @@ import static java.lang.String.valueOf; public class AddUuidColumnsToSnapshotsTest { - public static final String SNAPSHOTS_TABLE = "snapshots"; + private static final String SNAPSHOTS_TABLE = "snapshots"; + @Rule public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddUuidColumnsToSnapshotsTest.class, "old_snapshots.sql"); @Rule diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/CleanOrphanRowsInSnapshotsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/CleanOrphanRowsInSnapshotsTest.java index 8e9d3e88d1d..eb0f683e629 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v60/CleanOrphanRowsInSnapshotsTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v60/CleanOrphanRowsInSnapshotsTest.java @@ -32,7 +32,8 @@ import static org.assertj.core.api.Assertions.assertThat; public class CleanOrphanRowsInSnapshotsTest { - public static final String SNAPSHOTS_TABLE = "snapshots"; + private static final String SNAPSHOTS_TABLE = "snapshots"; + @Rule public DbTester db = DbTester.createForSchema(System2.INSTANCE, CleanOrphanRowsInSnapshotsTest.class, "in_progress_snapshots_and_children_tables.sql"); -- 2.39.5