From 82599634f9cfe4e96b4d1b7781d642440432ac78 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 15 Sep 2016 11:48:38 +0200 Subject: [PATCH] SONAR-8025 rewrite create table QPROFILE_CHANGES in java --- .../1316_create_table_qprofile_changes.rb | 10 +-- .../sonar/db/version/MigrationStepModule.java | 2 + .../v61/CopyActivitiesToQprofileChanges.java | 2 +- .../v61/CreateTableQprofileChanges.java | 49 ++++++++++++++ .../qualityprofile/QProfileChangeMapper.xml | 4 +- .../org/sonar/db/version/schema-h2.ddl | 2 +- .../qualityprofile/QProfileChangeDaoTest.java | 6 +- .../db/version/MigrationStepModuleTest.java | 2 +- .../CopyActivitiesToQprofileChangesTest.java | 4 +- .../v61/CreateTableQprofileChangesTest.java | 65 +++++++++++++++++++ .../schema.sql | 2 +- .../CreateTableQprofileChangesTest/empty.sql | 0 12 files changed, 128 insertions(+), 20 deletions(-) create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableQprofileChanges.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableQprofileChangesTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableQprofileChangesTest/empty.sql diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1316_create_table_qprofile_changes.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1316_create_table_qprofile_changes.rb index 779def82761..d02129dcad6 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1316_create_table_qprofile_changes.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1316_create_table_qprofile_changes.rb @@ -24,14 +24,6 @@ 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 diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java index 97126ec1658..967f3b6ae0d 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java +++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java @@ -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); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChanges.java b/sonar-db/src/main/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChanges.java index e3707462147..fe863cd629c 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChanges.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChanges.java @@ -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 index 00000000000..f7e4178f59d --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableQprofileChanges.java @@ -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 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); + } +} diff --git a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml index d2a3b3b6490..7b9c275a44d 100644 --- a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/QProfileChangeMapper.xml @@ -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 diff --git a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl index 293743b8b8a..563fecd28e6 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -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" ( diff --git a/sonar-db/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java b/sonar-db/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java index 80fb2ce73d4..65e3dae8948 100644 --- a/sonar-db/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java @@ -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 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 + "'"); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index e8c5df62134..37ac7916425 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -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); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java index d4eacf9aacb..022de4b64af 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java @@ -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 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 index 00000000000..362e9cd96e4 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableQprofileChangesTest.java @@ -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(); + } +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql index 1ad0dd1d259..8f8df334379 100644 --- a/sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql +++ b/sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql @@ -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 index 00000000000..e69de29bb2d -- 2.39.5