mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-18 03:10:39 +01:00
Data import is available
This commit is contained in:
@@ -6,6 +6,7 @@ import gitbucket.core.servlet.Database
|
||||
import gitbucket.core.util._
|
||||
import gitbucket.core.util.ControlUtil._
|
||||
import gitbucket.core.util.Directory._
|
||||
import gitbucket.core.util.Implicits._
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.eclipse.jgit.dircache.DirCache
|
||||
import org.eclipse.jgit.lib.{FileMode, Constants}
|
||||
@@ -79,11 +80,20 @@ class FileUploadController extends ScalatraServlet with FileUploadSupport with R
|
||||
post("/import") {
|
||||
session.get(Keys.Session.LoginAccount).collect { case loginAccount: Account if loginAccount.isAdmin =>
|
||||
execute({ (file, fileId) =>
|
||||
// TODO Import data here!!
|
||||
println("********")
|
||||
println(fileId)
|
||||
println(file.getName)
|
||||
println("********")
|
||||
using(file.getInputStream){ in =>
|
||||
import JDBCUtil._
|
||||
val sql = IOUtils.toString(in, "UTF-8")
|
||||
val conn = request2Session(request).conn
|
||||
conn.setAutoCommit(false)
|
||||
try {
|
||||
conn.update(sql)
|
||||
conn.commit()
|
||||
} catch {
|
||||
case e: Throwable =>
|
||||
conn.rollback()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}, _ => true)
|
||||
}
|
||||
redirect("/admin/data")
|
||||
|
||||
@@ -1,13 +1,28 @@
|
||||
package gitbucket.core.util
|
||||
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import Directory.DatabaseHome
|
||||
import java.io.File
|
||||
import Directory._
|
||||
import liquibase.database.AbstractJdbcDatabase
|
||||
import liquibase.database.core.{MySQLDatabase, H2Database}
|
||||
import org.apache.commons.io.FileUtils
|
||||
|
||||
object DatabaseConfig {
|
||||
|
||||
private lazy val config = ConfigFactory.load("database")
|
||||
private lazy val config = {
|
||||
val file = new File(GitBucketHome, "database.conf")
|
||||
if(!file.exists){
|
||||
FileUtils.write(file,
|
||||
"""db {
|
||||
| url = "jdbc:h2:${DatabaseHome};MVCC=true"
|
||||
| user = "sa"
|
||||
| password = "sa"
|
||||
|}
|
||||
|""".stripMargin, "UTF-8")
|
||||
}
|
||||
ConfigFactory.parseFile(file)
|
||||
}
|
||||
|
||||
private lazy val dbUrl = config.getString("db.url")
|
||||
|
||||
def url(directory: Option[String]): String =
|
||||
|
||||
@@ -68,6 +68,12 @@ object JDBCUtil {
|
||||
val dbMeta = conn.getMetaData
|
||||
val allTablesInDatabase = allTablesOrderByDependencies(dbMeta)
|
||||
|
||||
allTablesInDatabase.reverse.foreach { tableName =>
|
||||
if (targetTables.contains(tableName)) {
|
||||
out.write(s"DELETE FROM ${tableName};\n".getBytes("UTF-8"))
|
||||
}
|
||||
}
|
||||
|
||||
allTablesInDatabase.foreach { tableName =>
|
||||
if (targetTables.contains(tableName)) {
|
||||
val sb = new StringBuilder()
|
||||
|
||||
@@ -24,9 +24,16 @@
|
||||
<div class="panel-body">
|
||||
<form class="form form-horizontal" action="@path/upload/import" method="POST" enctype="multipart/form-data">
|
||||
<input type="file" name="file">
|
||||
<input type="submit" class="btn btn-success pull-right" value="Import">
|
||||
<input type="submit" class="btn btn-success pull-right" value="Import" id="import">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
<script>
|
||||
$(function(){
|
||||
$('#import').click(function(){
|
||||
return confirm('All existing data is deleted before importing.\nAre you sure?');
|
||||
})
|
||||
})
|
||||
</script>
|
||||
Reference in New Issue
Block a user