You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SchemaMigration.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.jpa.entity;
  21. import javax.persistence.*;
  22. import java.sql.Connection;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;
  26. @Entity
  27. @Table(name = SchemaMigration.TABLE_NAME, uniqueConstraints = {@UniqueConstraint(columnNames = {"version"})})
  28. public class SchemaMigration {
  29. public final static int VERSION_UNKNOWN = -1;
  30. public static final int LAST_VERSION = 140;
  31. public final static String TABLE_NAME = "schema_migrations";
  32. @Id
  33. @Column(name = "version", updatable = true)
  34. private String version;
  35. public String getVersion() {
  36. return version;
  37. }
  38. public void setVersion(String s) {
  39. this.version = s;
  40. }
  41. public void setVersion(int i) {
  42. this.version = String.valueOf(i);
  43. }
  44. public static int getCurrentVersion(Connection connection) {
  45. Statement stmt = null;
  46. ResultSet rs = null;
  47. int version = VERSION_UNKNOWN;
  48. try {
  49. stmt = connection.createStatement();
  50. rs = stmt.executeQuery("SELECT version FROM " + SchemaMigration.TABLE_NAME);
  51. while (rs.next()) {
  52. int i = Integer.parseInt(rs.getString(1));
  53. if (i > version) {
  54. version = i;
  55. }
  56. }
  57. } catch (SQLException e) {
  58. // ignore
  59. } finally {
  60. close(rs);
  61. close(stmt);
  62. }
  63. return version;
  64. }
  65. private static void close(ResultSet rs) {
  66. if (rs != null) {
  67. try {
  68. rs.close();
  69. } catch (SQLException e) {
  70. // why does close() throw a checked-exception ???
  71. }
  72. }
  73. }
  74. private static void close(Statement st) {
  75. if (st != null) {
  76. try {
  77. st.close();
  78. } catch (SQLException e) {
  79. // why does close() throw a checked-exception ???
  80. }
  81. }
  82. }
  83. }