From 4bc59b3d23d257875f01de2ecdadd42127ab9859 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 13 Sep 2016 15:19:12 +0200 Subject: [PATCH] SONAR-8025 rewrite create table PROPERTIES2 in java --- .../migrate/1312_create_table_properties_2.rb | 10 +-- .../sonar/db/version/MigrationStepModule.java | 2 + .../version/v61/CreateTableProperties2.java | 57 ++++++++++++++++ .../db/version/MigrationStepModuleTest.java | 2 +- .../v61/CreateTableProperties2Test.java | 68 +++++++++++++++++++ .../v61/CreateTableProperties2Test/empty.sql | 0 6 files changed, 129 insertions(+), 10 deletions(-) create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableProperties2.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableProperties2Test.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableProperties2Test/empty.sql diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1312_create_table_properties_2.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1312_create_table_properties_2.rb index dc8456c9924..5c5513bafcd 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1312_create_table_properties_2.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1312_create_table_properties_2.rb @@ -24,15 +24,7 @@ class CreateTableProperties2 < ActiveRecord::Migration def self.up - create_table 'properties2' do |t| - t.column 'prop_key', :string, :limit => 512, :null => false - t.column :resource_id, :integer, :null => true - t.column :user_id, :integer, :null => true - t.column 'is_empty', :boolean, :null => false - t.column :text_value, :string, :limit => 4000, :null => true - t.column :clob_value, :text, :null => true - t.column 'created_at', :big_integer, :null => false - end + execute_java_migration('org.sonar.db.version.v61.CreateTableProperties2') add_varchar_index :properties2, :prop_key, :name => 'properties2_key' 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 744b4389c2e..97126ec1658 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 @@ -149,6 +149,7 @@ import org.sonar.db.version.v61.AddErrorColumnsToCeActivity; 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.CreateTableScannerContext; import org.sonar.db.version.v61.DeleteProjectDashboards; import org.sonar.db.version.v61.DeleteReportsFromCeQueue; @@ -328,6 +329,7 @@ public class MigrationStepModule extends Module { AddErrorColumnsToCeActivity.class, CreateTableScannerContext.class, CreateTableInternalProperties.class, + CreateTableProperties2.class, PopulateTableProperties2.class, RemoveViewsDefinitionFromProperties.class, CopyActivitiesToQprofileChanges.class); diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableProperties2.java b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableProperties2.java new file mode 100644 index 00000000000..bd8a690ead1 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableProperties2.java @@ -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.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.BooleanColumnDef.newBooleanColumnDefBuilder; +import static org.sonar.db.version.ClobColumnDef.newClobColumnDefBuilder; +import static org.sonar.db.version.CreateTableBuilder.ColumnFlag.AUTO_INCREMENT; +import static org.sonar.db.version.IntegerColumnDef.newIntegerColumnDefBuilder; +import static org.sonar.db.version.VarcharColumnDef.MAX_SIZE; +import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateTableProperties2 extends DdlChange { + public CreateTableProperties2(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + List stmts = new CreateTableBuilder(getDialect(), "properties2") + .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) + .addColumn(newVarcharColumnDefBuilder().setColumnName("prop_key").setLimit(512).setIsNullable(false).build()) + .addColumn(newBigIntegerColumnDefBuilder().setColumnName("resource_id").setIsNullable(true).build()) + .addColumn(newBigIntegerColumnDefBuilder().setColumnName("user_id").setIsNullable(true).build()) + .addColumn(newBooleanColumnDefBuilder().setColumnName("is_empty").setIsNullable(false).build()) + .addColumn(newVarcharColumnDefBuilder().setColumnName("text_value").setLimit(MAX_SIZE).setIsNullable(true).build()) + .addColumn(newClobColumnDefBuilder().setColumnName("clob_value").setIsNullable(true).build()) + .addColumn(newBigIntegerColumnDefBuilder().setColumnName("created_at").setIsNullable(false).build()) + // table with be renamed to properties in following migration, use final constraint name right away + .withPkConstraintName("pk_properties") + .build(); + context.execute(stmts); + } +} 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 657cd62dc11..e8c5df62134 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(138); + assertThat(container.size()).isEqualTo(139); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableProperties2Test.java b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableProperties2Test.java new file mode 100644 index 00000000000..223b84ff0e5 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableProperties2Test.java @@ -0,0 +1,68 @@ +/* + * 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 CreateTableProperties2Test { + private static final String TABLE_PROPERTIES_2 = "properties2"; + + @Rule + public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, CreateTableProperties2Test.class, "empty.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private CreateTableProperties2 underTest = new CreateTableProperties2(dbTester.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + assertThat(dbTester.countRowsOfTable(TABLE_PROPERTIES_2)).isEqualTo(0); + + dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "id", Types.INTEGER, null, false); + dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "prop_key", Types.VARCHAR, 512, false); + dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "resource_id", Types.BIGINT, null, true); + dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "user_id", Types.BIGINT, null, true); + dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "is_empty", Types.BOOLEAN, null, false); + dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "text_value", Types.VARCHAR, 4000, true); + dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "clob_value", Types.CLOB, null, true); + dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "created_at", Types.BIGINT, null, false); + dbTester.assertPrimaryKey(TABLE_PROPERTIES_2, "pk_properties", "id"); + } + + @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/CreateTableProperties2Test/empty.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableProperties2Test/empty.sql new file mode 100644 index 00000000000..e69de29bb2d -- 2.39.5