]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5471 refactor db migration 1315 in Java
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 12 Dec 2016 09:25:58 +0000 (10:25 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 12 Dec 2016 12:05:20 +0000 (13:05 +0100)
22 files changed:
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1315_rename_table_properties2_to_properties.rb
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v60/CreateTemporaryIndicesFor1211.java
sonar-db/src/main/java/org/sonar/db/version/v61/RenameTableProperties2ToProperties.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
sonar-db/src/test/java/org/sonar/db/version/v56/RemoveUselessIndexesOnGroupRolesTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/AddIndexOnComponentUuidOfMeasuresTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/AddUniqueIndexOnUuidOfSnapshotsTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/CreatePermTemplatesCharacteristicsTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/CreateTemporaryIndicesFor1211Test.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexDuplicationsIndexSidFromDuplicationsIndexTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexProjectsRootIdFromProjectsTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexProjectsUuidFromProjectsTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/DropResourceIndexRidFromResourceIndexTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/DropSnapshotProjectIdFromSnapshotsTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/DropTemporaryIndicesOf1210Test.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/v60/MakeComponentUuidAndAnalysisUuidNotNullOnDuplicationsIndexTest.java
sonar-db/src/test/java/org/sonar/db/version/v60/MakeComponentUuidColumnsNotNullOnSnapshotsTest.java
sonar-db/src/test/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnProjectsTest.java
sonar-db/src/test/java/org/sonar/db/version/v60/MakeUuidColumnsNotNullOnResourceIndexTest.java
sonar-db/src/test/java/org/sonar/db/version/v60/RecreateIndexProjectsUuidFromProjectsTest.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v60/CreatePermTemplatesCharacteristicsTest/empty.sql [new file with mode: 0644]

index 0241e47f304de1b9ae5ff848870d7158f3a3e5b3..e82bc5bc19df7e1ca6890af6c46ee8e56cacc12c 100644 (file)
 class RenameTableProperties2ToProperties < ActiveRecord::Migration
 
   def self.up
-    drop_index_quietly :properties2, :properties2_key
-    rename_table_quietly :properties2, :properties
-    add_varchar_index :properties, :prop_key, :name => 'properties_key'
-  end
-
-  private
-
-  def self.rename_table_quietly(oldName, newName)
-    begin
-      rename_table oldName, newName
-    rescue
-      #ignore
-    end
+    execute_java_migration('org.sonar.db.version.v61.RenameTableProperties2ToProperties')
   end
 
 end
index d687d760a976fd6bb9c92e12b5d26512db42e096..01c2f6c9b1d89130ed40e349f084bb2581da5bf3 100644 (file)
@@ -177,6 +177,7 @@ import org.sonar.db.version.v61.DropTableActivities;
 import org.sonar.db.version.v61.DropTableProperties;
 import org.sonar.db.version.v61.PopulateTableProperties2;
 import org.sonar.db.version.v61.RemoveViewsDefinitionFromProperties;
+import org.sonar.db.version.v61.RenameTableProperties2ToProperties;
 import org.sonar.db.version.v61.ShrinkModuleUuidPathOfProjects;
 import org.sonar.db.version.v62.AddIsRootColumnOnTableUsers;
 import org.sonar.db.version.v62.AddOrganizationUuidToGroupRoles;
@@ -402,6 +403,7 @@ public class MigrationStepModule extends Module {
       CreateTableRuleRepositories.class,
       DropTableActivities.class,
       DropTableProperties.class,
+      RenameTableProperties2ToProperties.class,
 
       // 6.2
       CreateTableOrganizations.class,
index b1e561fee51fef58292788adf6fe7fbd3ca34077..24d13ddcf5da9736b2ddb1f79ef9e96b95ae29b9 100644 (file)
@@ -28,8 +28,8 @@ import static org.sonar.db.version.IntegerColumnDef.newIntegerColumnDefBuilder;
 
 public class CreateTemporaryIndicesFor1211 extends DdlChange {
 
-  public static final String INDEX_ON_CE_ACTIVITY = "ce_activity_snapshot_id";
-  public static final String INDEX_ON_DUPLICATIONS_INDEX = "dup_index_psid";
+  static final String INDEX_ON_CE_ACTIVITY = "ce_activity_snapshot_id";
+  static final String INDEX_ON_DUPLICATIONS_INDEX = "dup_index_psid";
 
   public CreateTemporaryIndicesFor1211(Database db) {
     super(db);
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/RenameTableProperties2ToProperties.java b/sonar-db/src/main/java/org/sonar/db/version/v61/RenameTableProperties2ToProperties.java
new file mode 100644 (file)
index 0000000..5a1d3e2
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v61;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.version.CreateIndexBuilder;
+import org.sonar.db.version.DdlChange;
+import org.sonar.db.version.DropIndexBuilder;
+import org.sonar.db.version.RenameTableBuilder;
+
+import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class RenameTableProperties2ToProperties extends DdlChange {
+
+  public RenameTableProperties2ToProperties(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new DropIndexBuilder(getDialect())
+      .setTable("properties2")
+      .setName("properties2_key")
+      .build());
+
+    context.execute(new RenameTableBuilder(getDialect())
+      .setName("properties2")
+      .setNewName("properties")
+      .build());
+
+    context.execute(new CreateIndexBuilder(getDialect())
+      .setTable("properties")
+      .setName("properties_key")
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("prop_key").setLimit(512).setIsNullable(false).build())
+      .build());
+  }
+
+}
index 615bade9e167ac82f581b3f4e38dc114fc6ed2fb..2b59ac8c770bce51fc2f500b880655b212bcde9b 100644 (file)
@@ -29,6 +29,6 @@ public class MigrationStepModuleTest {
   public void verify_count_of_added_MigrationStep_types() {
     ComponentContainer container = new ComponentContainer();
     new MigrationStepModule().configure(container);
-    assertThat(container.size()).isEqualTo(187);
+    assertThat(container.size()).isEqualTo(188);
   }
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v56/RemoveUselessIndexesOnGroupRolesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v56/RemoveUselessIndexesOnGroupRolesTest.java
new file mode 100644 (file)
index 0000000..ff0ea40
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v56;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class RemoveUselessIndexesOnGroupRolesTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    // Some databases have unique names of indexes, so table name is not declared
+    // when dropping an index ("drop index <index name>"). Because of that MySQL is
+    // used in the test so that the table name can also be verified
+    when(db.getDialect()).thenReturn(new MySql());
+  }
+
+  @Test
+  public void remove_two_indices_from_group_roles() throws Exception {
+    RemoveUselessIndexesOnGroupRoles underTest = new RemoveUselessIndexesOnGroupRoles(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("DROP INDEX group_roles_group ON group_roles"));
+    verify(context).execute(asList("DROP INDEX group_roles_role ON group_roles"));
+    verifyNoMoreInteractions(context);
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/AddIndexOnComponentUuidOfMeasuresTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/AddIndexOnComponentUuidOfMeasuresTest.java
new file mode 100644 (file)
index 0000000..25b00a1
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+
+public class AddIndexOnComponentUuidOfMeasuresTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Test
+  public void add_index_to_measures() throws Exception {
+    when(db.getDialect()).thenReturn(new H2());
+    AddIndexOnComponentUuidOfMeasures underTest = new AddIndexOnComponentUuidOfMeasures(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("CREATE INDEX measures_component_uuid ON project_measures (component_uuid)"));
+    verifyNoMoreInteractions(context);
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/AddUniqueIndexOnUuidOfSnapshotsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/AddUniqueIndexOnUuidOfSnapshotsTest.java
new file mode 100644 (file)
index 0000000..848b704
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class AddUniqueIndexOnUuidOfSnapshotsTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Test
+  public void create_unique_index() throws Exception {
+    when(db.getDialect()).thenReturn(new H2());
+    AddUniqueIndexOnUuidOfSnapshots underTest = new AddUniqueIndexOnUuidOfSnapshots(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("CREATE UNIQUE INDEX analyses_uuid ON snapshots (uuid)"));
+    verifyNoMoreInteractions(context);
+  }
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/CreatePermTemplatesCharacteristicsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/CreatePermTemplatesCharacteristicsTest.java
new file mode 100644 (file)
index 0000000..7285f40
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class CreatePermTemplatesCharacteristicsTest {
+
+  private static final String TABLE_NAME = "perm_tpl_characteristics";
+
+  @Rule
+  public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, CreatePermTemplatesCharacteristicsTest.class, "empty.sql");
+
+  private CreatePermTemplatesCharacteristics underTest = new CreatePermTemplatesCharacteristics(dbTester.database());
+
+  @Test
+  public void creates_table_and_index() throws SQLException {
+    underTest.execute();
+
+    assertThat(dbTester.countRowsOfTable(TABLE_NAME)).isEqualTo(0);
+    dbTester.assertColumnDefinition(TABLE_NAME, "id", Types.INTEGER, null, false);
+    dbTester.assertPrimaryKey(TABLE_NAME, "pk_" + TABLE_NAME, "id");
+    dbTester.assertColumnDefinition(TABLE_NAME, "template_id", Types.INTEGER, null);
+    dbTester.assertColumnDefinition(TABLE_NAME, "permission_key", Types.VARCHAR, 64, false);
+    dbTester.assertColumnDefinition(TABLE_NAME, "with_project_creator", Types.BOOLEAN, null, false);
+    dbTester.assertColumnDefinition(TABLE_NAME, "created_at", Types.BIGINT, null, false);
+    dbTester.assertColumnDefinition(TABLE_NAME, "updated_at", Types.BIGINT, null, false);
+
+    dbTester.assertUniqueIndex(TABLE_NAME, "uniq_perm_tpl_charac", "template_id", "permission_key");
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/CreateTemporaryIndicesFor1211Test.java b/sonar-db/src/test/java/org/sonar/db/version/v60/CreateTemporaryIndicesFor1211Test.java
new file mode 100644 (file)
index 0000000..15fafcc
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+
+public class CreateTemporaryIndicesFor1211Test {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    when(db.getDialect()).thenReturn(new H2());
+  }
+
+  @Test
+  public void create_two_indices() throws Exception {
+    CreateTemporaryIndicesFor1211 underTest = new CreateTemporaryIndicesFor1211(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("CREATE INDEX ce_activity_snapshot_id ON ce_activity (snapshot_id)"));
+    verify(context).execute(asList("CREATE INDEX dup_index_psid ON duplications_index (project_snapshot_id)"));
+    verifyNoMoreInteractions(context);
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexDuplicationsIndexSidFromDuplicationsIndexTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexDuplicationsIndexSidFromDuplicationsIndexTest.java
new file mode 100644 (file)
index 0000000..182b6da
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+
+public class DropIndexDuplicationsIndexSidFromDuplicationsIndexTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    // Some databases have unique names of indexes, so table name is not declared
+    // when dropping an index ("drop index <index name>"). Because of that MySQL is
+    // used in the test so that the table name can also be verified
+    when(db.getDialect()).thenReturn(new MySql());
+  }
+
+  @Test
+  public void drop_index() throws Exception {
+    DropIndexDuplicationsIndexSidFromDuplicationsIndex underTest = new DropIndexDuplicationsIndexSidFromDuplicationsIndex(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("DROP INDEX duplications_index_sid ON duplications_index"));
+    verifyNoMoreInteractions(context);
+  }
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexProjectsRootIdFromProjectsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexProjectsRootIdFromProjectsTest.java
new file mode 100644 (file)
index 0000000..3aa0a24
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+
+public class DropIndexProjectsRootIdFromProjectsTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    // Some databases have unique names of indexes, so table name is not declared
+    // when dropping an index ("drop index <index name>"). Because of that MySQL is
+    // used in the test so that the table name can also be verified
+    when(db.getDialect()).thenReturn(new MySql());
+  }
+
+  @Test
+  public void drop_index() throws Exception {
+    DropIndexProjectsRootIdFromProjects underTest = new DropIndexProjectsRootIdFromProjects(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("DROP INDEX projects_root_id ON projects"));
+    verifyNoMoreInteractions(context);
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexProjectsUuidFromProjectsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/DropIndexProjectsUuidFromProjectsTest.java
new file mode 100644 (file)
index 0000000..c28cd60
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class DropIndexProjectsUuidFromProjectsTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    // Some databases have unique names of indexes, so table name is not declared
+    // when dropping an index ("drop index <index name>"). Because of that MySQL is
+    // used in the test so that the table name can also be verified
+    when(db.getDialect()).thenReturn(new MySql());
+  }
+
+  @Test
+  public void drop_index() throws Exception {
+    DropIndexProjectsUuidFromProjects underTest = new DropIndexProjectsUuidFromProjects(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("DROP INDEX projects_uuid ON projects"));
+    verifyNoMoreInteractions(context);
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/DropResourceIndexRidFromResourceIndexTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/DropResourceIndexRidFromResourceIndexTest.java
new file mode 100644 (file)
index 0000000..7032d2e
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class DropResourceIndexRidFromResourceIndexTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    // Some databases have unique names of indexes, so table name is not declared
+    // when dropping an index ("drop index <index name>"). Because of that MySQL is
+    // used in the test so that the table name can also be verified
+    when(db.getDialect()).thenReturn(new MySql());
+  }
+
+  @Test
+  public void drop_index_from_resource_index() throws Exception {
+    DropResourceIndexRidFromResourceIndex underTest = new DropResourceIndexRidFromResourceIndex(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("DROP INDEX resource_index_rid ON resource_index"));
+    verifyNoMoreInteractions(context);
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/DropSnapshotProjectIdFromSnapshotsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/DropSnapshotProjectIdFromSnapshotsTest.java
new file mode 100644 (file)
index 0000000..3c09a3c
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+// FIXME migration is badly named
+public class DropSnapshotProjectIdFromSnapshotsTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    // Some databases have unique names of indexes, so table name is not declared
+    // when dropping an index ("drop index <index name>"). Because of that MySQL is
+    // used in the test so that the table name can also be verified
+    when(db.getDialect()).thenReturn(new MySql());
+  }
+
+  @Test
+  public void drop_two_indices_from_snapshots() throws Exception {
+    DropSnapshotProjectIdFromSnapshots underTest = new DropSnapshotProjectIdFromSnapshots(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("DROP INDEX snapshot_project_id ON snapshots"));
+    verify(context).execute(asList("DROP INDEX snapshots_root_project_id ON snapshots"));
+    verifyNoMoreInteractions(context);
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/DropTemporaryIndicesOf1210Test.java b/sonar-db/src/test/java/org/sonar/db/version/v60/DropTemporaryIndicesOf1210Test.java
new file mode 100644 (file)
index 0000000..4c98c65
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+
+public class DropTemporaryIndicesOf1210Test {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    // Some databases have unique names of indexes, so table name is not declared
+    // when dropping an index ("drop index <index name>"). Because of that MySQL is
+    // used in the test so that the table name can also be verified
+    when(db.getDialect()).thenReturn(new MySql());
+  }
+
+  @Test
+  public void drop_two_indices() throws Exception {
+    DropTemporaryIndicesOf1210 underTest = new DropTemporaryIndicesOf1210(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("DROP INDEX ce_activity_snapshot_id ON ce_activity"));
+    verify(context).execute(asList("DROP INDEX dup_index_psid ON duplications_index"));
+    verifyNoMoreInteractions(context);
+  }
+
+}
index 203c2f578f626882cc6e9accedef52c2e7d22435..8002ccaff6949a7b1119b2731e4c842659ab06ef 100644 (file)
@@ -47,6 +47,7 @@ public class MakeComponentUuidAndAnalysisUuidNotNullOnDuplicationsIndexTest {
     underTest.execute();
 
     verifyColumnDefinitions();
+    verifyIndex();
   }
 
   @Test
@@ -57,6 +58,7 @@ public class MakeComponentUuidAndAnalysisUuidNotNullOnDuplicationsIndexTest {
     underTest.execute();
 
     verifyColumnDefinitions();
+    verifyIndex();
     assertThat(idsOfRowsInDuplicationsIndex()).containsOnly(1L, 2L);
   }
 
@@ -85,6 +87,10 @@ public class MakeComponentUuidAndAnalysisUuidNotNullOnDuplicationsIndexTest {
     db.assertColumnDefinition("duplications_index", "analysis_uuid", Types.VARCHAR, 50, false);
   }
 
+  private void verifyIndex() {
+    db.assertIndex("duplications_index", "duplication_analysis_component", "analysis_uuid", "component_uuid");
+  }
+
   private List<Long> idsOfRowsInDuplicationsIndex() {
     return db.select("select ID from duplications_index").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
   }
index 2263e9e38661e00944b511adc48fd2059fab542a..c900360deb1f066594444b3dc5e2e1cea3cdc06a 100644 (file)
@@ -46,6 +46,7 @@ public class MakeComponentUuidColumnsNotNullOnSnapshotsTest {
     underTest.execute();
 
     verifyColumnDefinitions();
+    verifyIndices();
   }
 
   @Test
@@ -56,6 +57,7 @@ public class MakeComponentUuidColumnsNotNullOnSnapshotsTest {
     underTest.execute();
 
     verifyColumnDefinitions();
+    verifyIndices();
   }
 
   @Test
@@ -73,6 +75,11 @@ public class MakeComponentUuidColumnsNotNullOnSnapshotsTest {
     db.assertColumnDefinition(SNAPSHOTS_TABLE, "root_component_uuid", Types.VARCHAR, 50, false);
   }
 
+  private void verifyIndices() {
+    db.assertIndex(SNAPSHOTS_TABLE, "snapshot_component", "component_uuid");
+    db.assertIndex(SNAPSHOTS_TABLE, "snapshot_root_component", "root_component_uuid");
+  }
+
   private void insertSnapshots(long id, boolean hasComponentUiid, boolean hasRootComponentUuid) {
     db.executeInsert(
       SNAPSHOTS_TABLE,
index a232ff295b4ae556cd9e88181f7f8ffc51386281..6c504f4d32f86aff8e8fc7830648dcc3352a6577 100644 (file)
@@ -46,6 +46,7 @@ public class MakeUuidColumnsNotNullOnProjectsTest {
     underTest.execute();
 
     verifyColumnDefinitions();
+    verifyIndex();
   }
 
   @Test
@@ -56,6 +57,7 @@ public class MakeUuidColumnsNotNullOnProjectsTest {
     underTest.execute();
 
     verifyColumnDefinitions();
+    verifyIndex();
   }
 
   @Test
@@ -83,6 +85,10 @@ public class MakeUuidColumnsNotNullOnProjectsTest {
     db.assertColumnDefinition(PROJECTS_TABLE, "root_uuid", Types.VARCHAR, 50, false);
   }
 
+  private void verifyIndex() {
+    db.assertIndex(PROJECTS_TABLE, "projects_root_uuid", "root_uuid");
+  }
+
   private String insertComponent(long id, boolean hasUuid, boolean hasRootUuid) {
     String uuid = "uuid_" + id;
     db.executeInsert(
index f60e3085763b635ed9969b3eabe44a3a06f2f03f..1181ea3f7a89924b33976e66c9b00ad817f445bc 100644 (file)
@@ -44,6 +44,7 @@ public class MakeUuidColumnsNotNullOnResourceIndexTest {
     underTest.execute();
 
     verifyColumnDefinitions();
+    verifyIndex();
   }
 
   @Test
@@ -54,6 +55,7 @@ public class MakeUuidColumnsNotNullOnResourceIndexTest {
     underTest.execute();
 
     verifyColumnDefinitions();
+    verifyIndex();
   }
 
   @Test
@@ -71,6 +73,10 @@ public class MakeUuidColumnsNotNullOnResourceIndexTest {
     db.assertColumnDefinition("resource_index", "root_component_uuid", Types.VARCHAR, 50, false);
   }
 
+  private void verifyIndex() {
+    db.assertIndex("resource_index", "resource_index_component", "component_uuid");
+  }
+
   private void insertResourceIndex(long id, boolean hasComponentUiid, boolean hasRootComponentUuid) {
     db.executeInsert(
         "resource_index",
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/RecreateIndexProjectsUuidFromProjectsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/RecreateIndexProjectsUuidFromProjectsTest.java
new file mode 100644 (file)
index 0000000..61ff740
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v60;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.version.DdlChange;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class RecreateIndexProjectsUuidFromProjectsTest {
+
+  private Database db = mock(Database.class, Mockito.RETURNS_DEEP_STUBS);
+  private DdlChange.Context context = mock(DdlChange.Context.class);
+
+  @Before
+  public void setUp() {
+    when(db.getDialect()).thenReturn(new H2());
+  }
+
+  @Test
+  public void create_index() throws Exception {
+    RecreateIndexProjectsUuidFromProjects underTest = new RecreateIndexProjectsUuidFromProjects(db);
+
+    underTest.execute(context);
+
+    verify(context).execute(asList("CREATE INDEX projects_uuid ON projects (uuid)"));
+    verifyNoMoreInteractions(context);
+  }
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v60/CreatePermTemplatesCharacteristicsTest/empty.sql b/sonar-db/src/test/resources/org/sonar/db/version/v60/CreatePermTemplatesCharacteristicsTest/empty.sql
new file mode 100644 (file)
index 0000000..e69de29