]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10691 Introduce ES drop in DB migration
authorEric Hartmann <hartmann.eric@gmail.com>
Fri, 25 May 2018 13:56:30 +0000 (15:56 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 12 Jun 2018 18:20:59 +0000 (20:20 +0200)
14 files changed:
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/es/ElasticsearchClient.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/es/package-info.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/SelectImpl.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/ElasticsearchChangeTest.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/es/IndexCreator.java
server/sonar-server/src/main/java/org/sonar/server/es/SimpleEsClientImpl.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/es/metadata/EsDbCompatibility.java
server/sonar-server/src/main/java/org/sonar/server/es/metadata/EsDbCompatibilityImpl.java
server/sonar-server/src/main/java/org/sonar/server/es/metadata/MetadataIndex.java
server/sonar-server/src/test/java/org/sonar/server/es/IndexCreatorTest.java
server/sonar-server/src/test/java/org/sonar/server/es/SimpleEsClientImplTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/es/TestEsDbCompatibility.java
server/sonar-server/src/test/java/org/sonar/server/es/metadata/EsDbCompatibilityImplTest.java
server/sonar-server/src/test/java/org/sonar/server/es/metadata/MetadataIndexTest.java

diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/es/ElasticsearchClient.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/es/ElasticsearchClient.java
new file mode 100644 (file)
index 0000000..677118f
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.es;
+
+public interface ElasticsearchClient {
+
+  /**
+   * This method is reentrant and does not fail if indexName or otherIndexNames does not exist
+   */
+  void deleteIndexes(String... otherIndexNames);
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/es/package-info.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/es/package-info.java
new file mode 100644 (file)
index 0000000..a3ef5bf
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.server.platform.db.migration.es;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
index aed210f2175e3548954d7a160522f18a070e15f0..3a7a91c29bd3f02f40fba792d83695e8b9a3c040 100644 (file)
@@ -28,7 +28,7 @@ import java.util.List;
 import org.apache.commons.dbutils.DbUtils;
 import org.sonar.db.Database;
 
-public class SelectImpl extends BaseSqlStatement<Select>implements Select {
+public class SelectImpl extends BaseSqlStatement<Select> implements Select {
 
   private SelectImpl(PreparedStatement pstmt) {
     super(pstmt);
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/ElasticsearchChangeTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/ElasticsearchChangeTest.java
new file mode 100644 (file)
index 0000000..7b1c145
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.step;
+
+import java.sql.SQLException;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.sonar.server.platform.db.migration.es.ElasticsearchClient;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class ElasticsearchChangeTest {
+
+  private ElasticsearchClient client = mock(ElasticsearchClient.class);
+
+  @Test
+  public void deleteIndice_call() throws SQLException {
+    ArgumentCaptor<String> indices = ArgumentCaptor.forClass(String.class);
+
+    new ElasticsearchChange(client) {
+      @Override
+      protected void execute(Context context) throws SQLException {
+        context.deleteIndice("a", "b", "c");
+      }
+    }.execute();
+
+    verify(client, times(1)).deleteIndice(indices.capture());
+    assertThat(indices.getAllValues()).containsExactly("a", "b", "c");
+  }
+}
index ac295a599befd79db45b9f5bf4d71f45689bbe72..54d9aa3520160fd9fff3487203adc9ae1a3a194c 100644 (file)
@@ -48,7 +48,6 @@ import org.sonar.server.es.metadata.MetadataIndexDefinition;
 public class IndexCreator implements Startable {
 
   private static final Logger LOGGER = Loggers.get(IndexCreator.class);
-  private static final String PROPERY_DISABLE_CHECK = "sonar.search.disableDropOnDbMigration";
 
   private final MetadataIndexDefinition metadataIndexDefinition;
   private final MetadataIndex metadataIndex;
@@ -151,20 +150,12 @@ public class IndexCreator implements Startable {
   }
 
   private void checkDbCompatibility(Collection<Index> definitions) {
-    boolean disabledCheck = configuration.getBoolean(PROPERY_DISABLE_CHECK).orElse(false);
-    if (disabledCheck) {
-      LOGGER.warn("Automatic drop of search indices in turned off (see property " + PROPERY_DISABLE_CHECK + ")");
-    }
-
     List<String> existingIndices = loadExistingIndicesExceptMetadata(definitions);
-    if (!disabledCheck && !existingIndices.isEmpty()) {
+    if (!existingIndices.isEmpty()) {
       boolean delete = false;
       if (!esDbCompatibility.hasSameDbVendor()) {
         LOGGER.info("Delete Elasticsearch indices (DB vendor changed)");
         delete = true;
-      } else if (!esDbCompatibility.hasSameDbSchemaVersion()) {
-        LOGGER.info("Delete Elasticsearch indices (DB schema changed)");
-        delete = true;
       }
       if (delete) {
         existingIndices.forEach(this::deleteIndex);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/SimpleEsClientImpl.java b/server/sonar-server/src/main/java/org/sonar/server/es/SimpleEsClientImpl.java
new file mode 100644 (file)
index 0000000..47b32e4
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.es;
+
+import java.util.Arrays;
+import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
+import org.elasticsearch.client.Client;
+import org.sonar.server.platform.db.migration.es.ElasticsearchClient;
+
+import static java.util.Objects.requireNonNull;
+
+public class SimpleEsClientImpl implements ElasticsearchClient {
+  private final Client nativeClient;
+
+  public SimpleEsClientImpl(Client nativeClient) {
+    this.nativeClient = requireNonNull(nativeClient);
+  }
+
+  /**
+   * This method is reentrant and does not fail if indexName or otherIndexNames does not exist
+   */
+  public void deleteIndexes(String... indexNames) {
+    if (indexNames.length == 0) {
+      return;
+    }
+
+    GetMappingsResponse getMappingsResponse = nativeClient.admin().indices().prepareGetMappings("_all").get();
+    String[] allIndexes = getMappingsResponse.mappings().keys().toArray(String.class);
+    String[] intersection = intersection(indexNames, allIndexes);
+    nativeClient.admin().indices().prepareDelete(intersection).get();
+  }
+
+  private String[] intersection(String[] a, String[] b) {
+    return Arrays.stream(a)
+      .distinct()
+      .filter(x -> Arrays.stream(b).anyMatch(y -> y != null && y.equals(x)))
+      .toArray(String[]::new);
+  }
+}
index e8ccdcc75c1554b4be5df1c2a8608da564086430..8bdfa30130640374132066a41b74b6b1bfca8d29 100644 (file)
@@ -32,13 +32,6 @@ public interface EsDbCompatibility {
    */
   boolean hasSameDbVendor();
 
-  /**
-   * Whether the effective DB schema version equals the version
-   * registered in Elasticsearch metadata.
-   * Return {@code false} if at least one of the values is absent
-   */
-  boolean hasSameDbSchemaVersion();
-
   /**
    * Stores in Elasticsearch the metadata about database
    */
index 3d969ae83ef39e8af157d46fce4a141200a0a0a2..dde74dee7e7a543ab9a401d760895eae9c8b2c9b 100644 (file)
  */
 package org.sonar.server.es.metadata;
 
-import java.util.Objects;
 import java.util.Optional;
 import org.sonar.db.DbClient;
-import org.sonar.server.platform.db.migration.history.MigrationHistory;
 
 public class EsDbCompatibilityImpl implements EsDbCompatibility {
 
   private final DbClient dbClient;
   private final MetadataIndex metadataIndex;
-  private final MigrationHistory dbMigrationHistory;
 
-  public EsDbCompatibilityImpl(DbClient dbClient, MetadataIndex metadataIndex, MigrationHistory dbMigrationHistory) {
+  public EsDbCompatibilityImpl(DbClient dbClient, MetadataIndex metadataIndex) {
     this.dbClient = dbClient;
     this.metadataIndex = metadataIndex;
-    this.dbMigrationHistory = dbMigrationHistory;
   }
 
   @Override
@@ -42,28 +38,12 @@ public class EsDbCompatibilityImpl implements EsDbCompatibility {
     return registeredDbVendor.isPresent() && registeredDbVendor.get().equals(getDbVendor());
   }
 
-  @Override
-  public boolean hasSameDbSchemaVersion() {
-    Optional<Long> registeredVersion = metadataIndex.getDbSchemaVersion();
-    if (!registeredVersion.isPresent()) {
-      return false;
-    }
-    return getDbSchemaVersion()
-      .filter(effectiveVersion -> Objects.equals(registeredVersion.get(), effectiveVersion))
-      .isPresent();
-  }
-
   @Override
   public void markAsCompatible() {
-    metadataIndex.setDbMetadata(getDbVendor(), getDbSchemaVersion()
-      .orElseThrow(() -> new IllegalStateException("DB schema version is not present in database")));
+    metadataIndex.setDbMetadata(getDbVendor());
   }
 
   private String getDbVendor() {
     return dbClient.getDatabase().getDialect().getId();
   }
-
-  private Optional<Long> getDbSchemaVersion() {
-    return dbMigrationHistory.getLastMigrationNumber();
-  }
 }
index c503cb9d5666d8a8a97f6abcc2fe8eda10202d0d..150fc4f84dd26f3cdc685df40df25b994f2b264d 100644 (file)
@@ -31,7 +31,6 @@ import static org.sonar.server.es.DefaultIndexSettings.REFRESH_IMMEDIATE;
 public class MetadataIndex {
 
   private static final String DB_VENDOR_KEY = "dbVendor";
-  private static final String DB_SCHEMA_VERSION_KEY = "dbSchemaVersion";
 
   private final EsClient esClient;
 
@@ -67,13 +66,8 @@ public class MetadataIndex {
     return getMetadata(DB_VENDOR_KEY);
   }
 
-  public Optional<Long> getDbSchemaVersion() {
-    return getMetadata(DB_SCHEMA_VERSION_KEY).map(Long::parseLong);
-  }
-
-  public void setDbMetadata(String vendor, long schemaVersion) {
+  public void setDbMetadata(String vendor) {
     setMetadata(DB_VENDOR_KEY, vendor);
-    setMetadata(DB_SCHEMA_VERSION_KEY, String.valueOf(schemaVersion));
   }
 
   private Optional<String> getMetadata(String id) {
index 5c5aeb3fa8d0f4cb3a359486a4ea64ef868af3a4..2046d6d17b59e91a3442be983d5044688fa96560 100644 (file)
@@ -151,17 +151,10 @@ public class IndexCreatorTest {
       c -> c.setHasSameDbVendor(false));
   }
 
-  @Test
-  public void delete_existing_indices_if_db_schema_changed() {
-    testDeleteOnDbChange(LOG_DB_SCHEMA_CHANGED,
-      c -> c.setHasSameDbSchemaVersion(false));
-  }
-
   @Test
   public void do_not_check_db_compatibility_on_fresh_es() {
     // supposed to be ignored
     esDbCompatibility.setHasSameDbVendor(false);
-    esDbCompatibility.setHasSameDbSchemaVersion(false);
 
     startNewCreator(new FakeIndexDefinition());
 
@@ -172,28 +165,6 @@ public class IndexCreatorTest {
       .contains("Create type metadatas/metadata");
   }
 
-  @Test
-  public void do_not_check_db_compatibility_if_disabled_by_configuration() {
-    settings.setProperty("sonar.search.disableDropOnDbMigration", true);
-
-    // initial startup, automatic drop may be ignored because indices do not exist
-    startNewCreator(new FakeIndexDefinition());
-    logTester.clear();
-
-    // second startup, automatic drop can be disabled only by configuration
-
-    // supposed to be ignored
-    esDbCompatibility.setHasSameDbVendor(false);
-    esDbCompatibility.setHasSameDbSchemaVersion(false);
-
-    startNewCreator(new FakeIndexDefinition());
-
-    assertThat(logTester.logs(LoggerLevel.INFO))
-      .doesNotContain(LOG_DB_VENDOR_CHANGED)
-      .doesNotContain(LOG_DB_SCHEMA_CHANGED);
-    assertThat(logTester.logs(LoggerLevel.WARN)).contains("Automatic drop of search indices in turned off (see property sonar.search.disableDropOnDbMigration)");
-  }
-
   private void testDeleteOnDbChange(String expectedLog, Consumer<TestEsDbCompatibility> afterFirstStart) {
     startNewCreator(new FakeIndexDefinition());
     assertThat(logTester.logs(LoggerLevel.INFO))
diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/SimpleEsClientImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/SimpleEsClientImplTest.java
new file mode 100644 (file)
index 0000000..1818be2
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.es;
+
+import org.elasticsearch.client.Client;
+import org.elasticsearch.cluster.metadata.IndexMetaData;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.config.internal.MapSettings;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.sonar.server.es.NewIndex.SettingsConfiguration.newBuilder;
+
+public class SimpleEsClientImplTest {
+  @Rule
+  public EsTester es = EsTester.createCustom(new FakeIndexDefinition(),
+    new SimpleIndexDefinition("a"),
+    new SimpleIndexDefinition("b"),
+    new SimpleIndexDefinition("c"));
+
+  private Client client = es.client().nativeClient();
+  private SimpleEsClientImpl underTest = new SimpleEsClientImpl(client);
+
+  @Test
+  public void call_without_arguments_does_not_generate_an_elasticsearch_call() {
+    Client client = mock(Client.class);
+    SimpleEsClientImpl underTest = new SimpleEsClientImpl(client);
+    underTest.deleteIndexes();
+
+    verify(client, never()).admin();
+  }
+
+  @Test
+  public void delete_known_indice_must_delete_the_index() {
+    underTest.deleteIndexes("fakes");
+
+    assertThat(es.client().nativeClient().admin().indices().prepareGetMappings().get().mappings().get("fakes")).isNull();
+  }
+
+  @Test
+  public void delete_unknown_indice_must_delete_all_existing_indexes() {
+    underTest.deleteIndexes("a", "xxx", "c");
+
+    assertThat(es.client().nativeClient().admin().indices().prepareGetMappings().get().mappings().get("a")).isNull();
+    assertThat(es.client().nativeClient().admin().indices().prepareGetMappings().get().mappings().get("c")).isNull();
+  }
+
+  public class SimpleIndexDefinition implements IndexDefinition {
+    private final String indexName;
+
+    public SimpleIndexDefinition(String indexName) {
+      this.indexName = indexName;
+    }
+
+    @Override
+    public void define(IndexDefinitionContext context) {
+      NewIndex index = context.create(indexName, newBuilder(new MapSettings().asConfig()).build());
+      index.getSettings().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0);
+      index.getSettings().put("index.refresh_interval", "-1");
+    }
+  }
+}
index eae6331e9118fc011fb3b19b1b11fa46ff2030ea..16d5cc07fce7e8ce2954fa6e631d58831b453f83 100644 (file)
@@ -24,28 +24,17 @@ import org.sonar.server.es.metadata.EsDbCompatibility;
 public class TestEsDbCompatibility implements EsDbCompatibility {
 
   private boolean hasSameDbVendor = true;
-  private boolean hasSameDbSchemaVersion = true;
 
   public TestEsDbCompatibility setHasSameDbVendor(boolean b) {
     this.hasSameDbVendor = b;
     return this;
   }
 
-  public TestEsDbCompatibility setHasSameDbSchemaVersion(boolean b) {
-    this.hasSameDbSchemaVersion = b;
-    return this;
-  }
-
   @Override
   public boolean hasSameDbVendor() {
     return hasSameDbVendor;
   }
 
-  @Override
-  public boolean hasSameDbSchemaVersion() {
-    return hasSameDbSchemaVersion;
-  }
-
   @Override
   public void markAsCompatible() {
 
index a610b6a63a8f275f06924664f43b6fc863eff152..e4849d93299c6aea43cc112d493974e9b9fc3f6b 100644 (file)
@@ -19,8 +19,6 @@
  */
 package org.sonar.server.es.metadata;
 
-import java.util.Optional;
-import javax.annotation.Nullable;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -28,8 +26,6 @@ import org.mockito.Mockito;
 import org.sonar.db.DbClient;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.es.FakeIndexDefinition;
-import org.sonar.server.platform.db.migration.history.MigrationHistory;
-
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -40,111 +36,65 @@ public class EsDbCompatibilityImplTest {
   @Rule
   public EsTester es = EsTester.createCustom(new MetadataIndexDefinitionBridge(), new FakeIndexDefinition());
   private DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
-  private MigrationHistory migrationHistory = mock(MigrationHistory.class);
   private MetadataIndex metadataIndex = new MetadataIndex(es.client());
-  private EsDbCompatibilityImpl underTest = new EsDbCompatibilityImpl(dbClient, metadataIndex, migrationHistory);
+  private EsDbCompatibilityImpl underTest = new EsDbCompatibilityImpl(dbClient, metadataIndex);
 
   @Test
   public void hasSameDbVendor_is_true_if_values_match() {
-    prepareDb("mysql", 1_800L);
-    prepareEs("mysql", 2_000L);
+    prepareDb("mysql");
+    prepareEs("mysql");
 
     assertThat(underTest.hasSameDbVendor()).isTrue();
   }
 
   @Test
   public void hasSameDbVendor_is_false_if_values_dont_match() {
-    prepareDb("mysql", 1_800L);
-    prepareEs("postgres", 1_800L);
+    prepareDb("mysql");
+    prepareEs("postgres");
 
     assertThat(underTest.hasSameDbVendor()).isFalse();
   }
 
   @Test
   public void hasSameDbVendor_is_false_if_value_is_absent_from_es() {
-    prepareDb("mysql", 1_800L);
+    prepareDb("mysql");
 
     assertThat(underTest.hasSameDbVendor()).isFalse();
   }
 
-  @Test
-  public void hasSameDbSchemaVersion_is_true_if_values_match() {
-    prepareDb("mysql", 1_800L);
-    prepareEs("postgres", 1_800L);
-
-    assertThat(underTest.hasSameDbSchemaVersion()).isTrue();
-  }
-
-  @Test
-  public void hasSameDbSchemaVersion_is_false_if_values_dont_match() {
-    prepareDb("mysql", 1_800L);
-    prepareEs("postgres", 2_000L);
-
-    assertThat(underTest.hasSameDbSchemaVersion()).isFalse();
-  }
-
-  @Test
-  public void hasSameDbSchemaVersion_is_false_if_value_is_absent_from_db() {
-    prepareDb("mysql", null);
-    prepareEs("postgres", 1_800L);
-
-    assertThat(underTest.hasSameDbSchemaVersion()).isFalse();
-  }
-
-  @Test
-  public void hasSameDbSchemaVersion_is_false_if_value_is_absent_from_es() {
-    prepareDb("mysql", 1_800L);
-
-    assertThat(underTest.hasSameDbSchemaVersion()).isFalse();
-  }
-
   @Test
   public void store_db_metadata_in_es() {
-    prepareDb("mysql", 1_800L);
+    prepareDb("mysql");
 
     underTest.markAsCompatible();
 
     assertThat(metadataIndex.getDbVendor()).hasValue("mysql");
-    assertThat(metadataIndex.getDbSchemaVersion()).hasValue(1_800L);
   }
 
   @Test
   public void store_updates_db_metadata_in_es() {
-    prepareEs("mysql", 1_800L);
-    prepareDb("postgres", 2_000L);
+    prepareEs("mysql");
+    prepareDb("postgres");
 
     underTest.markAsCompatible();
 
     assertThat(metadataIndex.getDbVendor()).hasValue("postgres");
-    assertThat(metadataIndex.getDbSchemaVersion()).hasValue(2_000L);
-  }
-
-  @Test
-  public void store_throws_ISE_if_metadata_cant_be_loaded_from_db() {
-    prepareDb("postgres", null);
-
-    expectedException.expect(IllegalStateException.class);
-    expectedException.expectMessage("DB schema version is not present in database");
-
-    underTest.markAsCompatible();
   }
 
   @Test
   public void store_marks_es_as_compatible_with_db() {
-    prepareDb("postgres", 1_800L);
+    prepareDb("postgres");
 
     underTest.markAsCompatible();
 
-    assertThat(underTest.hasSameDbSchemaVersion()).isTrue();
     assertThat(underTest.hasSameDbVendor()).isTrue();
   }
 
-  private void prepareDb(String dbVendor, @Nullable Long dbSchemaVersion) {
+  private void prepareDb(String dbVendor) {
     when(dbClient.getDatabase().getDialect().getId()).thenReturn(dbVendor);
-    when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.ofNullable(dbSchemaVersion));
   }
 
-  private void prepareEs(String dbVendor, long dbSchemaVersion) {
-    metadataIndex.setDbMetadata(dbVendor, dbSchemaVersion);
+  private void prepareEs(String dbVendor) {
+    metadataIndex.setDbMetadata(dbVendor);
   }
 }
index 845cf8eda38e31ebead55cc3817bff68c7ea29ee..9cfc0c14852a98cf866a5a5bd3af0017e8d93110 100644 (file)
@@ -63,14 +63,12 @@ public class MetadataIndexTest {
   @Test
   public void database_metadata_are_empty_if_absent_from_index() {
     assertThat(underTest.getDbVendor()).isNotPresent();
-    assertThat(underTest.getDbSchemaVersion()).isNotPresent();
   }
 
   @Test
   public void database_metadata_are_present_from_index() {
-    underTest.setDbMetadata("postgres", 1_800);
+    underTest.setDbMetadata("postgres");
 
     assertThat(underTest.getDbVendor()).hasValue("postgres");
-    assertThat(underTest.getDbSchemaVersion()).hasValue(1_800L);
   }
 }