Skip to content

Commit

Permalink
Fixes to DatabaseRestore and cleanup of SQLiteStore
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Dec 2, 2024
1 parent ed85d8e commit a33fede
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val developerURL: String = "https://matthicks.com"

name := projectName
ThisBuild / organization := org
ThisBuild / version := "0.17.0"
ThisBuild / version := "1.0.0-SNAPSHOT"
ThisBuild / scalaVersion := scala213
ThisBuild / crossScalaVersions := allScalaVersions
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation")
Expand Down Expand Up @@ -298,7 +298,7 @@ lazy val docs = project
.enablePlugins(MdocPlugin)
.settings(
mdocVariables := Map(
"VERSION" -> version.value
"VERSION" -> version.value
),
mdocOut := file(".")
)
7 changes: 6 additions & 1 deletion core/src/main/scala/lightdb/backup/DatabaseRestore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@ object DatabaseRestore {
f(collection) match {
case Some(source) =>
try {
scribe.info(s"Restoring ${collection.name}...")
if (truncate) collection.t.truncate()
val iterator = source
.getLines()
.map(s => JsonParser(s))
collection.t.json.insert(iterator)
val count = collection.t.json.insert(iterator)
scribe.info(s"Re-Indexing ${collection.name}...")
collection.reIndex()
scribe.info(s"Restored $count documents to ${collection.name}")
count
} finally {
source.close()
}
Expand Down
30 changes: 10 additions & 20 deletions sqlite/src/main/scala/lightdb/sql/SQLiteStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,17 @@ class SQLiteStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](val connect

object SQLiteStore extends StoreManager {
def singleConnectionManager(file: Option[Path]): ConnectionManager = {
val connection: Connection = {
val path = file match {
case Some(f) =>
val file = f.toFile
Option(file.getParentFile).foreach(_.mkdirs())
file.getCanonicalPath
case None => ":memory:"
}

val config = new SQLiteConfig
config.enableLoadExtension(true)
val uri = s"jdbc:sqlite:$path"
try {
val c = config.createConnection(uri)
c.setAutoCommit(false)
c
} catch {
case t: Throwable => throw new RuntimeException(s"Error establishing SQLite connection to $uri", t)
}
val path = file match {
case Some(f) =>
val file = f.toFile
Option(file.getParentFile).foreach(_.mkdirs())
file.getCanonicalPath
case None => ":memory:"
}
SingleConnectionManager(connection)

SingleConnectionManager(SQLConfig(
jdbcUrl = s"jdbc:sqlite:$path"
))
}

def apply[Doc <: Document[Doc], Model <: DocumentModel[Doc]](file: Option[Path], storeMode: StoreMode): SQLiteStore[Doc, Model] = {
Expand Down

0 comments on commit a33fede

Please sign in to comment.