]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8025 rewrite create table QPROFILE_CHANGES in java
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 15 Sep 2016 09:48:38 +0000 (11:48 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 16 Sep 2016 10:22:12 +0000 (12:22 +0200)
12 files changed:
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1316_create_table_qprofile_changes.rb
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChanges.java
sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableQprofileChanges.java [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml
sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl
sonar-db/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java
sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableQprofileChangesTest.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql
sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableQprofileChangesTest/empty.sql [new file with mode: 0644]

index 779def82761fea77b01873d255111be780e5e073..d02129dcad669c70e678479b0b97ec547374c106 100644 (file)
 class CreateTableQprofileChanges < ActiveRecord::Migration
 
   def self.up
-    create_table 'qprofile_changes', :id => false do |t|
-      t.column 'kee', :string, :limit => 40, :null => false
-      t.column 'qprofile_key', :string, :limit => 255, :null => false
-      t.column 'change_type', :string, :limit => 20, :null => false
-      t.column 'created_at', :big_integer, :null => false
-      t.column 'user_login', :string, :limit => 255, :null => true
-      t.column 'data', :text, :null => true
-    end
-    add_primary_key 'qprofile_changes', 'kee'
+    execute_java_migration('org.sonar.db.version.v61.CreateTableQprofileChanges')
   end
 end
index 97126ec1658b7db45cd75fb6b8743d4c148e7fab..967f3b6ae0d338834eaa0c6712bef657e9fa0d2f 100644 (file)
@@ -150,6 +150,7 @@ import org.sonar.db.version.v61.CopyActivitiesToQprofileChanges;
 import org.sonar.db.version.v61.CreateTableCeTaskInput;
 import org.sonar.db.version.v61.CreateTableInternalProperties;
 import org.sonar.db.version.v61.CreateTableProperties2;
+import org.sonar.db.version.v61.CreateTableQprofileChanges;
 import org.sonar.db.version.v61.CreateTableScannerContext;
 import org.sonar.db.version.v61.DeleteProjectDashboards;
 import org.sonar.db.version.v61.DeleteReportsFromCeQueue;
@@ -332,6 +333,7 @@ public class MigrationStepModule extends Module {
       CreateTableProperties2.class,
       PopulateTableProperties2.class,
       RemoveViewsDefinitionFromProperties.class,
+      CreateTableQprofileChanges.class,
       CopyActivitiesToQprofileChanges.class);
   }
 }
index e37074621471cd47ed86ce9e82a0b49efdf7d411..fe863cd629c22cc6cef91c63fb76c7efa883c76c 100644 (file)
@@ -46,7 +46,7 @@ public class CopyActivitiesToQprofileChanges extends BaseDataChange {
         "and qc.kee is null")
       .setString(1, "QPROFILE");
 
-    massUpdate.update("insert into qprofile_changes (kee, qprofile_key, created_at, user_login, change_type, data) values (?,?,?,?,?,?)");
+    massUpdate.update("insert into qprofile_changes (kee, qprofile_key, created_at, user_login, change_type, change_data) values (?,?,?,?,?,?)");
     massUpdate.execute((row, update) -> {
       String key = row.getString(1);
       String profileKey = row.getString(2);
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableQprofileChanges.java b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableQprofileChanges.java
new file mode 100644 (file)
index 0000000..f7e4178
--- /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.v61;
+
+import java.sql.SQLException;
+import java.util.List;
+import org.sonar.db.Database;
+import org.sonar.db.version.CreateTableBuilder;
+import org.sonar.db.version.DdlChange;
+
+import static org.sonar.db.version.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
+import static org.sonar.db.version.ClobColumnDef.newClobColumnDefBuilder;
+import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class CreateTableQprofileChanges extends DdlChange {
+  public CreateTableQprofileChanges(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    List<String> stmts = new CreateTableBuilder(getDialect(), "qprofile_changes")
+      .addPkColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(40).setIsNullable(false).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("qprofile_key").setLimit(255).setIsNullable(false).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("change_type").setLimit(20).setIsNullable(false).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("user_login").setLimit(255).setIsNullable(true).build())
+      .addColumn(newClobColumnDefBuilder().setColumnName("change_data").setIsNullable(true).build())
+      .addColumn(newBigIntegerColumnDefBuilder().setColumnName("created_at").setIsNullable(false).build())
+      .build();
+    context.execute(stmts);
+  }
+}
index d2a3b3b649040c9940448872289eccc676f16e61..7b9c275a44d517036ed8a99fcd3d28a9240fbc60 100644 (file)
@@ -11,7 +11,7 @@
     created_at,
     user_login,
     change_type,
-    data
+    change_data
     ) values (
     #{key,jdbcType=VARCHAR},
     #{profileKey,jdbcType=VARCHAR},
@@ -55,7 +55,7 @@
     created_at as createdAt,
     user_login as login,
     change_type as changeType,
-    data
+    change_data as data
   </sql>
 
   <sql id="sqlSelectByQuery">
index 293743b8b8a8854d0a088acb27b51b2fdb207649..563fecd28e6522daa5d6efe2cb5fdef47dedc0fc 100644 (file)
@@ -501,7 +501,7 @@ CREATE TABLE "QPROFILE_CHANGES" (
   "CHANGE_TYPE" VARCHAR(20) NOT NULL,
   "CREATED_AT" BIGINT NOT NULL,
   "USER_LOGIN" VARCHAR(255),
-  "DATA" CLOB
+  "CHANGE_DATA" CLOB
 );
 
 CREATE TABLE "FILE_SOURCES" (
index 80fb2ce73d4c59a8d6b2ea5bbd2ac210e92c8bc0..65e3dae8948780e58d0d4acecb6fafb9ee4bde20 100644 (file)
@@ -66,7 +66,7 @@ public class QProfileChangeDaoTest {
     assertThat(row.get("createdAt")).isEqualTo(A_DATE);
     assertThat(row.get("login")).isEqualTo(login);
     assertThat(row.get("changeType")).isEqualTo(type);
-    assertThat(row.get("data")).isEqualTo(data);
+    assertThat(row.get("changeData")).isEqualTo(data);
   }
 
   /**
@@ -84,7 +84,7 @@ public class QProfileChangeDaoTest {
     assertThat(row.get("createdAt")).isEqualTo(A_DATE);
     assertThat(row.get("changeType")).isEqualTo("ACTIVATED");
     assertThat(row.get("login")).isNull();
-    assertThat(row.get("data")).isNull();
+    assertThat(row.get("changeData")).isNull();
   }
 
   @Test
@@ -207,7 +207,7 @@ public class QProfileChangeDaoTest {
 
   private Map<String, Object> selectChangeByKey(String key) {
     return dbTester.selectFirst(dbSession,
-      "select qprofile_key as \"qprofileKey\", created_at as \"createdAt\", user_login as \"login\", change_type as \"changeType\", data as \"data\" from qprofile_changes where kee='"
+      "select qprofile_key as \"qprofileKey\", created_at as \"createdAt\", user_login as \"login\", change_type as \"changeType\", change_data as \"changeData\" from qprofile_changes where kee='"
         + key + "'");
   }
 }
index e8c5df62134a4e536abbac66a49fb806ea06f1ea..37ac79164256ed7cbabccf8c7d5b52be58daab17 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(139);
+    assertThat(container.size()).isEqualTo(140);
   }
 }
index d4eacf9aacbf54b30f551a46ebb895e9ba780e98..022de4b64af6c10dc41315ba55703c08f1885f06 100644 (file)
@@ -66,7 +66,7 @@ public class CopyActivitiesToQprofileChangesTest {
     assertThat(change.get("createdAt")).isEqualTo(A_DATE);
     assertThat(change.get("login")).isEqualTo(login);
     assertThat(change.get("changeType")).isEqualTo(type);
-    assertThat(change.get("data")).isEqualTo(data);
+    assertThat(change.get("changeData")).isEqualTo(data);
   }
 
   /**
@@ -128,6 +128,6 @@ public class CopyActivitiesToQprofileChangesTest {
   }
 
   private Map<String, Object> selectChangeByKey(String key) {
-    return db.selectFirst("select qprofile_key as \"qprofileKey\", created_at as \"createdAt\", user_login as \"login\", change_type as \"changeType\", data as \"data\" from qprofile_changes where kee='" + key + "'");
+    return db.selectFirst("select qprofile_key as \"qprofileKey\", created_at as \"createdAt\", user_login as \"login\", change_type as \"changeType\", change_data as \"changeData\" from qprofile_changes where kee='" + key + "'");
   }
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableQprofileChangesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableQprofileChangesTest.java
new file mode 100644 (file)
index 0000000..362e9cd
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * 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 java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CreateTableQprofileChangesTest {
+  private static final String TABLE_QPROFILE_CHANGES = "qprofile_changes";
+
+  @Rule
+  public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, CreateTableQprofileChangesTest.class, "empty.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private CreateTableQprofileChanges underTest = new CreateTableQprofileChanges(dbTester.database());
+
+  @Test
+  public void creates_table_on_empty_db() throws SQLException {
+    underTest.execute();
+
+    assertThat(dbTester.countRowsOfTable(TABLE_QPROFILE_CHANGES)).isEqualTo(0);
+
+    dbTester.assertColumnDefinition(TABLE_QPROFILE_CHANGES, "kee", Types.VARCHAR, 40, false);
+    dbTester.assertColumnDefinition(TABLE_QPROFILE_CHANGES, "qprofile_key", Types.VARCHAR, 255, false);
+    dbTester.assertColumnDefinition(TABLE_QPROFILE_CHANGES, "change_type", Types.VARCHAR, 20, false);
+    dbTester.assertColumnDefinition(TABLE_QPROFILE_CHANGES, "user_login", Types.VARCHAR, 255, true);
+    dbTester.assertColumnDefinition(TABLE_QPROFILE_CHANGES, "change_data", Types.CLOB, null, true);
+    dbTester.assertColumnDefinition(TABLE_QPROFILE_CHANGES, "created_at", Types.BIGINT, null, false);
+    dbTester.assertPrimaryKey(TABLE_QPROFILE_CHANGES, "pk_" + TABLE_QPROFILE_CHANGES, "kee");
+  }
+
+  @Test
+  public void migration_is_not_reentrant() throws SQLException {
+    underTest.execute();
+
+    expectedException.expect(IllegalStateException.class);
+
+    underTest.execute();
+  }
+}
index 1ad0dd1d25947dfae1d49b50c0edfd57809fb6a7..8f8df334379139881d9aba451e7bd6698e3b7f28 100644 (file)
@@ -17,6 +17,6 @@ CREATE TABLE "QPROFILE_CHANGES" (
   "CHANGE_TYPE" VARCHAR(20) NOT NULL,
   "CREATED_AT" BIGINT NOT NULL,
   "USER_LOGIN" VARCHAR(255),
-  "DATA" CLOB
+  "CHANGE_DATA" CLOB
 );
 CREATE INDEX "QPROFILE_CHANGES_QPROFILE_KEY" ON "QPROFILE_CHANGES" ("QPROFILE_KEY");
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableQprofileChangesTest/empty.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableQprofileChangesTest/empty.sql
new file mode 100644 (file)
index 0000000..e69de29