LCOV - code coverage report
Current view: top level - server/schema - NoteDbSchemaVersionCheck.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 31 80.6 %
Date: 2022-11-19 15:00:39 Functions: 7 7 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2018 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.schema;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.exceptions.StorageException;
      19             : import com.google.gerrit.extensions.events.LifecycleListener;
      20             : import com.google.gerrit.lifecycle.LifecycleModule;
      21             : import com.google.gerrit.server.config.GerritServerConfig;
      22             : import com.google.gerrit.server.config.SitePaths;
      23             : import com.google.inject.Inject;
      24             : import com.google.inject.Module;
      25             : import com.google.inject.ProvisionException;
      26             : import org.eclipse.jgit.lib.Config;
      27             : 
      28             : public class NoteDbSchemaVersionCheck implements LifecycleListener {
      29         139 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      30             : 
      31             :   public static Module module() {
      32         138 :     return new LifecycleModule() {
      33             :       @Override
      34             :       protected void configure() {
      35         138 :         listener().to(NoteDbSchemaVersionCheck.class);
      36         138 :       }
      37             :     };
      38             :   }
      39             : 
      40             :   private final NoteDbSchemaVersionManager versionManager;
      41             :   private final SitePaths sitePaths;
      42             :   private Config gerritConfig;
      43             : 
      44             :   @Inject
      45             :   NoteDbSchemaVersionCheck(
      46             :       NoteDbSchemaVersionManager versionManager,
      47             :       SitePaths sitePaths,
      48         139 :       @GerritServerConfig Config gerritConfig) {
      49         139 :     this.versionManager = versionManager;
      50         139 :     this.sitePaths = sitePaths;
      51         139 :     this.gerritConfig = gerritConfig;
      52         139 :   }
      53             : 
      54             :   @Override
      55             :   public void start() {
      56             :     try {
      57         139 :       int current = versionManager.read();
      58         139 :       if (current == 0) {
      59           0 :         throw new ProvisionException(
      60           0 :             String.format(
      61             :                 "Schema not yet initialized. Run init to initialize the schema:\n"
      62             :                     + "$ java -jar gerrit.war init -d %s",
      63           0 :                 sitePaths.site_path.toAbsolutePath()));
      64             :       }
      65         139 :       int expected = NoteDbSchemaVersions.LATEST;
      66             : 
      67         139 :       if (current > expected
      68           1 :           && gerritConfig.getBoolean("gerrit", "experimentalRollingUpgrade", false)) {
      69           1 :         logger.atWarning().log(
      70             :             "Gerrit has detected refs/meta/version %d different than the expected %d."
      71             :                 + "Bear in mind that this is supported ONLY for rolling upgrades to immediate next "
      72             :                 + "Gerrit version (e.g. v3.1 to v3.2). If this is not expected, remove gerrit.experimentalRollingUpgrade "
      73             :                 + "from $GERRIT_SITE/etc/gerrit.config and restart Gerrit."
      74             :                 + "Please note that gerrit.experimentalRollingUpgrade is intended to be used "
      75             :                 + "for the rolling upgrade phase only and should be disabled afterwards.",
      76             :             current, expected);
      77         139 :       } else if (current != expected) {
      78             :         String advice =
      79           1 :             current > expected
      80           1 :                 ? "Downgrade is not supported"
      81           1 :                 : String.format(
      82             :                     "Run init to upgrade:\n$ java -jar %s init -d %s",
      83           0 :                     sitePaths.gerrit_war.toAbsolutePath(), sitePaths.site_path.toAbsolutePath());
      84           1 :         throw new ProvisionException(
      85           1 :             String.format(
      86             :                 "Unsupported schema version %d; expected schema version %d. %s",
      87           1 :                 current, expected, advice));
      88             :       }
      89           0 :     } catch (StorageException e) {
      90           0 :       throw new ProvisionException("Failed to read NoteDb schema version", e);
      91         139 :     }
      92         139 :   }
      93             : 
      94             :   @Override
      95             :   public void stop() {
      96             :     // Do nothing.
      97         138 :   }
      98             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750