diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2020-04-20 15:24:55 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-05-25 20:05:19 +0000 |
commit | d94ddd98de1e2b201d3e6183e925bc9ce2c5ff24 (patch) | |
tree | 6033c91d9f2085270e60bc9397a0ee06271ff5dc /server/sonar-db-migration | |
parent | 1b0ad62a22856594b69cce5cfd480cf01d7e46e2 (diff) | |
download | sonarqube-d94ddd98de1e2b201d3e6183e925bc9ce2c5ff24.tar.gz sonarqube-d94ddd98de1e2b201d3e6183e925bc9ce2c5ff24.zip |
SONAR-13221 File sources
Diffstat (limited to 'server/sonar-db-migration')
18 files changed, 696 insertions, 0 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTable.java new file mode 100644 index 00000000000..7f290eccd1b --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTable.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DdlChange; +import org.sonar.server.platform.db.migration.version.v83.util.AddPrimaryKeyBuilder; + +public class AddPrimaryKeyOnUuidColumnOfFileSourcesTable extends DdlChange { + + public AddPrimaryKeyOnUuidColumnOfFileSourcesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddPrimaryKeyBuilder("file_sources", "uuid").build()); + } + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTable.java new file mode 100644 index 00000000000..79b6bd695fb --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTable.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.AddUuidColumnToTable; + +public class AddUuidColumnToFileSourcesTable extends AddUuidColumnToTable { + private static final String TABLE = "file_sources"; + + public AddUuidColumnToFileSourcesTable(Database db) { + super(db, TABLE); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTable.java new file mode 100644 index 00000000000..741bf99a0e3 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTable.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.DropIdColumn; + +public class DropIdColumnOfFileSourcesTable extends DropIdColumn { + private static final String TABLE = "file_sources"; + + public DropIdColumnOfFileSourcesTable(Database db) { + super(db, TABLE); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTable.java new file mode 100644 index 00000000000..8b03c71be40 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTable.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.DropPrimaryKeyOnIdColumn; +import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator; + +public class DropPrimaryKeyOnIdColumnOfFileSourcesTable extends DropPrimaryKeyOnIdColumn { + private static final String TABLE_NAME = "file_sources"; + + public DropPrimaryKeyOnIdColumnOfFileSourcesTable(Database db, DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator) { + super(db, dropPrimaryKeySqlGenerator, TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullable.java new file mode 100644 index 00000000000..efd6b87eeb3 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullable.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class MakeFileSourcesUuidColumnNotNullable extends DdlChange { + private static final String TABLE = "file_sources"; + + private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setIsNullable(false) + .setDefaultValue(null) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build(); + + public MakeFileSourcesUuidColumnNotNullable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE) + .updateColumn(uuidColumnDefinition) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuid.java new file mode 100644 index 00000000000..b292f040284 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuid.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class PopulateFileSourcesUuid extends DataChange { + + private final UuidFactory uuidFactory; + + public PopulateFileSourcesUuid(Database db, UuidFactory uuidFactory) { + super(db); + this.uuidFactory = uuidFactory; + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + + massUpdate.select("select id from file_sources where uuid is null order by id asc"); + massUpdate.update("update file_sources set uuid = ? where id = ?"); + + massUpdate.execute((row, update) -> { + update.setString(1, uuidFactory.create()); + update.setLong(2, row.getLong(1)); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.java new file mode 100644 index 00000000000..d74453a6c9e --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.class, "schema.sql"); + + private DdlChange underTest = new AddPrimaryKeyOnUuidColumnOfFileSourcesTable(db.database()); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertPrimaryKey("file_sources", "pk_file_sources", "uuid"); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest.java new file mode 100644 index 00000000000..d8739496423 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest.java @@ -0,0 +1,69 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AddUuidColumnToFileSourcesTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddUuidColumnToFileSourcesTableTest.class, "schema.sql"); + + private DdlChange underTest = new AddUuidColumnToFileSourcesTable(db.database()); + + private UuidFactoryFast uuidFactory = UuidFactoryFast.getInstance(); + + @Before + public void setup() { + insertFileSources(1L); + insertFileSources(2L); + insertFileSources(3L); + } + + @Test + public void add_uuid_column() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("file_sources", "uuid", Types.VARCHAR, 40, true); + + assertThat(db.countRowsOfTable("file_sources")) + .isEqualTo(3); + } + + private void insertFileSources(Long id) { + db.executeInsert("file_sources", + "id", id, + "project_uuid", uuidFactory.create(), + "file_uuid", uuidFactory.create(), + "line_count", id + 1, + "created_at", id + 2, + "updated_at", id + 3); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest.java new file mode 100644 index 00000000000..b0132fb007b --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DropIdColumnOfFileSourcesTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropIdColumnOfFileSourcesTableTest.class, "schema.sql"); + + private DdlChange underTest = new DropIdColumnOfFileSourcesTable(db.database()); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertColumnDoesNotExist("file_sources", "id"); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.java new file mode 100644 index 00000000000..7fcf0f3c7d3 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; +import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator; +import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DropPrimaryKeyOnIdColumnOfFileSourcesTableTest { + + private static final String TABLE_NAME = "file_sources"; + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.class, "schema.sql"); + + private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database())); + + private DdlChange underTest = new DropPrimaryKeyOnIdColumnOfFileSourcesTable(db.database(), dropPrimaryKeySqlGenerator); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertNoPrimaryKey(TABLE_NAME); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest.java new file mode 100644 index 00000000000..111984bc7f1 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; + +import static java.sql.Types.VARCHAR; + +public class MakeFileSourcesUuidColumnNotNullableTest { + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MakeFileSourcesUuidColumnNotNullableTest.class, "schema.sql"); + + private MigrationStep underTest = new MakeFileSourcesUuidColumnNotNullable(db.database()); + + @Test + public void uuid_column_is_not_nullable() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("file_sources", "uuid", VARCHAR, null, false); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest.java new file mode 100644 index 00000000000..dde090da5bc --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest.java @@ -0,0 +1,84 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import java.util.Objects; +import java.util.stream.Collectors; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateFileSourcesUuidTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(PopulateFileSourcesUuidTest.class, "schema.sql"); + + private UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + private DataChange underTest = new PopulateFileSourcesUuid(db.database(), uuidFactory); + + @Test + public void populate_uuids() throws SQLException { + insertFileSources(1L); + insertFileSources(2L); + insertFileSources(3L); + + underTest.execute(); + + verifyUuidsAreNotNull(); + } + + @Test + public void migration_is_reentrant() throws SQLException { + insertFileSources(1L); + insertFileSources(2L); + insertFileSources(3L); + + underTest.execute(); + // re-entrant + underTest.execute(); + + verifyUuidsAreNotNull(); + } + + private void verifyUuidsAreNotNull() { + assertThat(db.select("select uuid from file_sources") + .stream() + .map(row -> row.get("UUID")) + .filter(Objects::isNull) + .collect(Collectors.toList())).isEmpty(); + } + + private void insertFileSources(Long id) { + db.executeInsert("file_sources", + "id", id, + "uuid", uuidFactory.create(), + "project_uuid", uuidFactory.create(), + "file_uuid", uuidFactory.create(), + "line_count", id + 1, + "created_at", id + 2, + "updated_at", id + 3); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest/schema.sql new file mode 100644 index 00000000000..dfb2d817bc7 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest/schema.sql @@ -0,0 +1,18 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest/schema.sql new file mode 100644 index 00000000000..179847ee2ce --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest/schema.sql @@ -0,0 +1,18 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest/schema.sql new file mode 100644 index 00000000000..3dab0132b33 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest/schema.sql new file mode 100644 index 00000000000..a6a2a841f83 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest/schema.sql new file mode 100644 index 00000000000..559418594f8 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest/schema.sql new file mode 100644 index 00000000000..559418594f8 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); |