001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 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.jpa.entity; 021 022 import org.apache.commons.lang.builder.ReflectionToStringBuilder; 023 import org.apache.commons.lang.builder.ToStringStyle; 024 025 import javax.persistence.*; 026 import java.sql.Connection; 027 import java.sql.ResultSet; 028 import java.sql.SQLException; 029 import java.sql.Statement; 030 031 @Entity 032 @Table(name = SchemaMigration.TABLE_NAME, uniqueConstraints = {@UniqueConstraint(columnNames = {"version"})}) 033 public class SchemaMigration { 034 035 public final static int VERSION_UNKNOWN = -1; 036 037 /* 038 039 IMPORTANT : HOW TO ADD A DATABASE MIGRATION : 040 - set the following constant LAST_VERSION 041 - add the activerecord migration script into sonar-server/src/main/webapp/WEB-INF/db/migrate 042 - complete the Derby DDL file used for unit tests : sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl 043 044 */ 045 public static final int LAST_VERSION = 222; 046 047 public final static String TABLE_NAME = "schema_migrations"; 048 049 @Id 050 @Column(name = "version", updatable = true) 051 private String version; 052 053 public String getVersion() { 054 return version; 055 } 056 057 public void setVersion(String s) { 058 this.version = s; 059 } 060 061 public void setVersion(int i) { 062 this.version = String.valueOf(i); 063 } 064 065 public static int getCurrentVersion(Connection connection) { 066 Statement stmt = null; 067 ResultSet rs = null; 068 int version = VERSION_UNKNOWN; 069 try { 070 stmt = connection.createStatement(); 071 rs = stmt.executeQuery("SELECT version FROM " + SchemaMigration.TABLE_NAME); 072 while (rs.next()) { 073 int i = Integer.parseInt(rs.getString(1)); 074 if (i > version) { 075 version = i; 076 } 077 } 078 } catch (SQLException e) { 079 // ignore 080 } finally { 081 close(rs); 082 close(stmt); 083 } 084 085 return version; 086 } 087 088 private static void close(ResultSet rs) { 089 if (rs != null) { 090 try { 091 rs.close(); 092 } catch (SQLException e) { 093 // why does close() throw a checked-exception ??? 094 } 095 } 096 } 097 098 private static void close(Statement st) { 099 if (st != null) { 100 try { 101 st.close(); 102 } catch (SQLException e) { 103 // why does close() throw a checked-exception ??? 104 } 105 } 106 } 107 108 @Override 109 public String toString() { 110 return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); 111 } 112 }