3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v60;
22 import java.sql.SQLException;
23 import java.sql.Types;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.db.CoreDbTester;
29 import static java.lang.String.valueOf;
31 public class MakeUuidColumnsNotNullOnResourceIndexTest {
34 public CoreDbTester db = CoreDbTester.createForSchema(MakeUuidColumnsNotNullOnResourceIndexTest.class,
35 "in_progress_resourceindex.sql");
37 public ExpectedException expectedException = ExpectedException.none();
39 private MakeUuidColumnsNotNullOnResourceIndex underTest = new MakeUuidColumnsNotNullOnResourceIndex(db.database());
42 public void migration_sets_uuid_columns_not_nullable_on_empty_table() throws SQLException {
45 verifyColumnDefinitions();
50 public void migration_sets_uuid_columns_not_nullable_on_populated_table() throws SQLException {
51 insertResourceIndex(1, true, true);
52 insertResourceIndex(2, true, true);
56 verifyColumnDefinitions();
61 public void migration_fails_if_some_uuid_columns_are_null() throws SQLException {
62 insertResourceIndex(1, false, true);
64 expectedException.expect(IllegalStateException.class);
65 expectedException.expectMessage("Fail to execute");
70 private void verifyColumnDefinitions() {
71 db.assertColumnDefinition("resource_index", "component_uuid", Types.VARCHAR, 50, false);
72 db.assertColumnDefinition("resource_index", "root_component_uuid", Types.VARCHAR, 50, false);
75 private void verifyIndex() {
76 db.assertIndex("resource_index", "resource_index_component", "component_uuid");
79 private void insertResourceIndex(long id, boolean hasComponentUiid, boolean hasRootComponentUuid) {
84 "POSITION", valueOf(id + 100),
85 "NAME_SIZE", valueOf(id + 1000),
86 "RESOURCE_ID", valueOf(id + 300),
87 "ROOT_PROJECT_ID", valueOf(id + 4000),
88 "QUALIFIER", "PROJECT");
90 if (hasComponentUiid) {
91 db.executeUpdateSql("update resource_index set COMPONENT_UUID=? where id=?", "uuid_" + id, valueOf(id));
93 if (hasRootComponentUuid) {
94 db.executeUpdateSql("update resource_index set ROOT_COMPONENT_UUID=? where id=?", "root_uuid_" + id, valueOf(id));