001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.core.persistence;
021
022import com.google.common.annotations.VisibleForTesting;
023import org.apache.ibatis.session.SqlSession;
024import org.sonar.api.BatchComponent;
025import org.sonar.api.CoreProperties;
026import org.sonar.api.ServerComponent;
027import org.sonar.core.properties.PropertiesMapper;
028import org.sonar.core.properties.PropertyDto;
029
030import java.util.Collections;
031import java.util.List;
032
033/**
034 * @since 3.0
035 */
036public class DatabaseVersion implements BatchComponent, ServerComponent {
037
038  public static final int LAST_VERSION = 306;
039
040  public static enum Status {
041    UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL
042  }
043
044  private MyBatis mybatis;
045
046  public DatabaseVersion(MyBatis mybatis) {
047    this.mybatis = mybatis;
048  }
049
050  public Integer getVersion() {
051    SqlSession session = mybatis.openSession();
052    try {
053      List<Integer> versions = session.getMapper(SchemaMigrationMapper.class).selectVersions();
054      if (!versions.isEmpty()) {
055        Collections.sort(versions);
056        return versions.get(versions.size() - 1);
057      }
058      return null;
059    } catch (RuntimeException e) {
060      // The table SCHEMA_MIGRATIONS does not exist.
061      // Ignore this exception -> it will be created by Ruby on Rails migrations.
062      return null;
063
064    } finally {
065      MyBatis.closeQuietly(session);
066    }
067  }
068
069  public String getSonarCoreId() {
070    SqlSession session = mybatis.openSession();
071    try {
072      PropertyDto serverIdProperty = session.getMapper(PropertiesMapper.class).selectByKey(new PropertyDto().setKey(CoreProperties.SERVER_ID));
073      // this property can't be NULL
074      return serverIdProperty.getValue();
075    } finally {
076      MyBatis.closeQuietly(session);
077    }
078  }
079
080  public Status getStatus() {
081    return getStatus(getVersion(), LAST_VERSION);
082  }
083
084  @VisibleForTesting
085  static Status getStatus(Integer currentVersion, int lastVersion) {
086    Status status = Status.FRESH_INSTALL;
087    if (currentVersion != null) {
088      if (currentVersion == lastVersion) {
089        status = Status.UP_TO_DATE;
090      } else if (currentVersion > lastVersion) {
091        status = Status.REQUIRES_DOWNGRADE;
092      } else {
093        status = Status.REQUIRES_UPGRADE;
094      }
095    }
096    return status;
097  }
098}