mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 22:45:51 +01:00
Use util.Version for GitBucket migration
This commit is contained in:
@@ -7,7 +7,7 @@ trait Plugin {
|
|||||||
val pluginId: String
|
val pluginId: String
|
||||||
val pluginName: String
|
val pluginName: String
|
||||||
val description: String
|
val description: String
|
||||||
val versions: List[Version]
|
val versions: Seq[Version]
|
||||||
|
|
||||||
def initialize(registry: PluginRegistry): Unit
|
def initialize(registry: PluginRegistry): Unit
|
||||||
|
|
||||||
|
|||||||
@@ -11,52 +11,19 @@ import util.ControlUtil._
|
|||||||
import util.JDBCUtil._
|
import util.JDBCUtil._
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
import util.Directory
|
import util.Directory
|
||||||
|
import util.{Version, Versions}
|
||||||
import plugin._
|
import plugin._
|
||||||
|
|
||||||
object AutoUpdate {
|
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.
|
* The history of versions. A head of this sequence is the current BitBucket version.
|
||||||
*/
|
*/
|
||||||
val versions = Seq(
|
val versions = Seq(
|
||||||
new Version(2, 8),
|
new Version(2, 8),
|
||||||
new Version(2, 7) {
|
new Version(2, 7) {
|
||||||
override def update(conn: Connection): Unit = {
|
override def update(conn: Connection, cl: ClassLoader): Unit = {
|
||||||
super.update(conn)
|
super.update(conn, cl)
|
||||||
conn.select("SELECT * FROM REPOSITORY"){ rs =>
|
conn.select("SELECT * FROM REPOSITORY"){ rs =>
|
||||||
// Rename attached files directory from /issues to /comments
|
// Rename attached files directory from /issues to /comments
|
||||||
val userName = rs.getString("USER_NAME")
|
val userName = rs.getString("USER_NAME")
|
||||||
@@ -94,8 +61,8 @@ object AutoUpdate {
|
|||||||
new Version(2, 5),
|
new Version(2, 5),
|
||||||
new Version(2, 4),
|
new Version(2, 4),
|
||||||
new Version(2, 3) {
|
new Version(2, 3) {
|
||||||
override def update(conn: Connection): Unit = {
|
override def update(conn: Connection, cl: ClassLoader): Unit = {
|
||||||
super.update(conn)
|
super.update(conn, cl)
|
||||||
conn.select("SELECT ACTIVITY_ID, ADDITIONAL_INFO FROM ACTIVITY WHERE ACTIVITY_TYPE='push'"){ rs =>
|
conn.select("SELECT ACTIVITY_ID, ADDITIONAL_INFO FROM ACTIVITY WHERE ACTIVITY_TYPE='push'"){ rs =>
|
||||||
val curInfo = rs.getString("ADDITIONAL_INFO")
|
val curInfo = rs.getString("ADDITIONAL_INFO")
|
||||||
val newInfo = curInfo.split("\n").filter(_ matches "^[0-9a-z]{40}:.*").mkString("\n")
|
val newInfo = curInfo.split("\n").filter(_ matches "^[0-9a-z]{40}:.*").mkString("\n")
|
||||||
@@ -112,13 +79,13 @@ object AutoUpdate {
|
|||||||
new Version(2, 2),
|
new Version(2, 2),
|
||||||
new Version(2, 1),
|
new Version(2, 1),
|
||||||
new Version(2, 0){
|
new Version(2, 0){
|
||||||
override def update(conn: Connection): Unit = {
|
override def update(conn: Connection, cl: ClassLoader): Unit = {
|
||||||
import eu.medsea.mimeutil.{MimeUtil2, MimeType}
|
import eu.medsea.mimeutil.{MimeUtil2, MimeType}
|
||||||
|
|
||||||
val mimeUtil = new MimeUtil2()
|
val mimeUtil = new MimeUtil2()
|
||||||
mimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector")
|
mimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector")
|
||||||
|
|
||||||
super.update(conn)
|
super.update(conn, cl)
|
||||||
conn.select("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY"){ rs =>
|
conn.select("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY"){ rs =>
|
||||||
defining(Directory.getAttachedDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME"))){ dir =>
|
defining(Directory.getAttachedDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME"))){ dir =>
|
||||||
if(dir.exists && dir.isDirectory){
|
if(dir.exists && dir.isDirectory){
|
||||||
@@ -146,8 +113,8 @@ object AutoUpdate {
|
|||||||
Version(1, 5),
|
Version(1, 5),
|
||||||
Version(1, 4),
|
Version(1, 4),
|
||||||
new Version(1, 3){
|
new Version(1, 3){
|
||||||
override def update(conn: Connection): Unit = {
|
override def update(conn: Connection, cl: ClassLoader): Unit = {
|
||||||
super.update(conn)
|
super.update(conn, cl)
|
||||||
// Fix wiki repository configuration
|
// Fix wiki repository configuration
|
||||||
conn.select("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY"){ rs =>
|
conn.select("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY"){ rs =>
|
||||||
using(Git.open(getWikiRepositoryDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME")))){ git =>
|
using(Git.open(getWikiRepositoryDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME")))){ git =>
|
||||||
@@ -214,32 +181,13 @@ class AutoUpdateListener extends ServletContextListener {
|
|||||||
val context = event.getServletContext
|
val context = event.getServletContext
|
||||||
context.setInitParameter("db.url", s"jdbc:h2:${DatabaseHome};MVCC=true")
|
context.setInitParameter("db.url", s"jdbc:h2:${DatabaseHome};MVCC=true")
|
||||||
|
|
||||||
// Migration
|
|
||||||
defining(getConnection(event.getServletContext)){ conn =>
|
defining(getConnection(event.getServletContext)){ conn =>
|
||||||
|
// Migration
|
||||||
logger.debug("Start schema update")
|
logger.debug("Start schema update")
|
||||||
try {
|
Versions.update(conn, headVersion, getCurrentVersion(), versions, Thread.currentThread.getContextClassLoader){ conn =>
|
||||||
defining(getCurrentVersion()){ currentVersion =>
|
FileUtils.writeStringToFile(versionFile, headVersion.versionString, "UTF-8")
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
logger.debug("End schema update")
|
|
||||||
|
|
||||||
// Load plugins
|
// Load plugins
|
||||||
|
|
||||||
PluginRegistry.initialize(conn)
|
PluginRegistry.initialize(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ object Versions {
|
|||||||
|
|
||||||
private val logger = LoggerFactory.getLogger(Versions.getClass)
|
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 = {
|
(save: Connection => Unit): Unit = {
|
||||||
logger.debug("Start schema update")
|
logger.debug("Start schema update")
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user