diff --git a/src/main/scala/plugin/Plugin.scala b/src/main/scala/plugin/Plugin.scala index 48ae955..3c9fe4a 100644 --- a/src/main/scala/plugin/Plugin.scala +++ b/src/main/scala/plugin/Plugin.scala @@ -7,7 +7,7 @@ val pluginId: String val pluginName: String val description: String - val versions: List[Version] + val versions: Seq[Version] def initialize(registry: PluginRegistry): Unit diff --git a/src/main/scala/servlet/AutoUpdateListener.scala b/src/main/scala/servlet/AutoUpdateListener.scala index 07edf7e..241eafe 100644 --- a/src/main/scala/servlet/AutoUpdateListener.scala +++ b/src/main/scala/servlet/AutoUpdateListener.scala @@ -11,43 +11,10 @@ import util.JDBCUtil._ import org.eclipse.jgit.api.Git import util.Directory +import util.{Version, Versions} import plugin._ object AutoUpdate { - - /** - * Version of GitBucket - * - * @param majorVersion the major version - * @param minorVersion the minor version - */ - case class Version(majorVersion: Int, minorVersion: Int){ - - private val logger = LoggerFactory.getLogger(classOf[servlet.AutoUpdate.Version]) - - /** - * Execute update/MAJOR_MINOR.sql to update schema to this version. - * If corresponding SQL file does not exist, this method do nothing. - */ - def update(conn: Connection): Unit = { - val sqlPath = s"update/${majorVersion}_${minorVersion}.sql" - - using(Thread.currentThread.getContextClassLoader.getResourceAsStream(sqlPath)){ in => - if(in != null){ - val sql = IOUtils.toString(in, "UTF-8") - using(conn.createStatement()){ stmt => - logger.debug(sqlPath + "=" + sql) - stmt.executeUpdate(sql) - } - } - } - } - - /** - * MAJOR.MINOR - */ - val versionString = s"${majorVersion}.${minorVersion}" - } /** * The history of versions. A head of this sequence is the current BitBucket version. @@ -55,8 +22,8 @@ val versions = Seq( new Version(2, 8), new Version(2, 7) { - override def update(conn: Connection): Unit = { - super.update(conn) + override def update(conn: Connection, cl: ClassLoader): Unit = { + super.update(conn, cl) conn.select("SELECT * FROM REPOSITORY"){ rs => // Rename attached files directory from /issues to /comments val userName = rs.getString("USER_NAME") @@ -94,8 +61,8 @@ new Version(2, 5), new Version(2, 4), new Version(2, 3) { - override def update(conn: Connection): Unit = { - super.update(conn) + override def update(conn: Connection, cl: ClassLoader): Unit = { + super.update(conn, cl) conn.select("SELECT ACTIVITY_ID, ADDITIONAL_INFO FROM ACTIVITY WHERE ACTIVITY_TYPE='push'"){ rs => val curInfo = rs.getString("ADDITIONAL_INFO") val newInfo = curInfo.split("\n").filter(_ matches "^[0-9a-z]{40}:.*").mkString("\n") @@ -112,13 +79,13 @@ new Version(2, 2), new Version(2, 1), new Version(2, 0){ - override def update(conn: Connection): Unit = { + override def update(conn: Connection, cl: ClassLoader): Unit = { import eu.medsea.mimeutil.{MimeUtil2, MimeType} val mimeUtil = new MimeUtil2() mimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector") - super.update(conn) + super.update(conn, cl) conn.select("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY"){ rs => defining(Directory.getAttachedDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME"))){ dir => if(dir.exists && dir.isDirectory){ @@ -146,8 +113,8 @@ Version(1, 5), Version(1, 4), new Version(1, 3){ - override def update(conn: Connection): Unit = { - super.update(conn) + override def update(conn: Connection, cl: ClassLoader): Unit = { + super.update(conn, cl) // Fix wiki repository configuration conn.select("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY"){ rs => using(Git.open(getWikiRepositoryDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME")))){ git => @@ -214,32 +181,13 @@ val context = event.getServletContext context.setInitParameter("db.url", s"jdbc:h2:${DatabaseHome};MVCC=true") - // Migration defining(getConnection(event.getServletContext)){ conn => + // Migration logger.debug("Start schema update") - try { - defining(getCurrentVersion()){ currentVersion => - if(currentVersion == headVersion){ - logger.debug("No update") - } else if(!versions.contains(currentVersion)){ - logger.warn(s"Skip migration because ${currentVersion.versionString} is illegal version.") - } else { - versions.takeWhile(_ != currentVersion).reverse.foreach(_.update(conn)) - FileUtils.writeStringToFile(versionFile, headVersion.versionString, "UTF-8") - logger.debug(s"Updated from ${currentVersion.versionString} to ${headVersion.versionString}") - } - } - } catch { - case ex: Throwable => { - logger.error("Failed to schema update", ex) - ex.printStackTrace() - conn.rollback() - } + Versions.update(conn, headVersion, getCurrentVersion(), versions, Thread.currentThread.getContextClassLoader){ conn => + FileUtils.writeStringToFile(versionFile, headVersion.versionString, "UTF-8") } - logger.debug("End schema update") - // Load plugins - PluginRegistry.initialize(conn) } diff --git a/src/main/scala/util/Version.scala b/src/main/scala/util/Version.scala index 00d4854..fed74a8 100644 --- a/src/main/scala/util/Version.scala +++ b/src/main/scala/util/Version.scala @@ -40,7 +40,7 @@ private val logger = LoggerFactory.getLogger(Versions.getClass) - def update(conn: Connection, headVersion: Version, currentVersion: Version, versions: List[Version], cl: ClassLoader) + def update(conn: Connection, headVersion: Version, currentVersion: Version, versions: Seq[Version], cl: ClassLoader) (save: Connection => Unit): Unit = { logger.debug("Start schema update") try {