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     */
020    package org.sonar.core.persistence;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import org.apache.ibatis.session.SqlSession;
024    import org.sonar.api.BatchComponent;
025    import org.sonar.api.ServerComponent;
026    
027    import java.util.Collections;
028    import java.util.List;
029    
030    /**
031     * @since 3.0
032     */
033    public class DatabaseVersion implements BatchComponent, ServerComponent {
034    
035      public static final int LAST_VERSION = 287;
036    
037      public static enum Status {
038        UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL
039      }
040    
041      private MyBatis mybatis;
042    
043      public DatabaseVersion(MyBatis mybatis) {
044        this.mybatis = mybatis;
045      }
046    
047      public Integer getVersion() {
048        SqlSession session = mybatis.openSession();
049        try {
050          List<Integer> versions = session.getMapper(SchemaMigrationMapper.class).selectVersions();
051          if (!versions.isEmpty()) {
052            Collections.sort(versions);
053            return versions.get(versions.size() - 1);
054          }
055          return null;
056        } catch (RuntimeException e) {
057          // The table SCHEMA_MIGRATIONS does not exist.
058          // Ignore this exception -> it will be created by Ruby on Rails migrations.
059          return null;
060    
061        } finally {
062          MyBatis.closeQuietly(session);
063        }
064      }
065    
066      public Status getStatus() {
067        return getStatus(getVersion(), LAST_VERSION);
068      }
069    
070      @VisibleForTesting
071      static Status getStatus(Integer currentVersion, int lastVersion) {
072        Status status = Status.FRESH_INSTALL;
073        if (currentVersion != null) {
074          if (currentVersion == lastVersion) {
075            status = Status.UP_TO_DATE;
076          } else if (currentVersion > lastVersion) {
077            status = Status.REQUIRES_DOWNGRADE;
078          } else {
079            status = Status.REQUIRES_UPGRADE;
080          }
081        }
082        return status;
083      }
084    }