summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreltrac <hi@eltr.ac>2026-10-23 14:38:14 +0800
committereltrac <hi@eltr.ac>2026-10-23 14:53:19 +0800
commit871ea1569fc1dc5900e3b8233fc9aede8245f15f (patch)
treef1a95e0c7683935a6d93a3628ca5acf2e4b47c3e /src
parentc7ddb958d7f1630a273006898f83e1d6dc54c83b (diff)
storage: migrate from SQLite to H2
add ADR-0002 about migrating to H2
Diffstat (limited to 'src')
-rw-r--r--src/weepinbell/core.clj6
-rw-r--r--src/weepinbell/migration.clj22
-rw-r--r--src/weepinbell/storage/h2.clj (renamed from src/weepinbell/storage/sqlite.clj)8
3 files changed, 18 insertions, 18 deletions
diff --git a/src/weepinbell/core.clj b/src/weepinbell/core.clj
index cb0033a..903d228 100644
--- a/src/weepinbell/core.clj
+++ b/src/weepinbell/core.clj
@@ -4,7 +4,7 @@
[weepinbell.config :refer [load-config]]
[weepinbell.webhook :as webhook]
[weepinbell.storage.edn-storage :as edn]
- [weepinbell.storage.sqlite :as sqlite]
+ [weepinbell.storage.h2 :as h2]
[weepinbell.storage.worker :as worker]
[clojure.core.async :refer [chan >!! <!!]]
[taoensso.timbre :as timbre]
@@ -32,7 +32,7 @@
data-path (:data-path config)
bare-store (case (:storage-type config)
:edn (edn/use-store data-path)
- :sqlite (sqlite/use-store data-path))
+ :h2 (h2/use-store data-path))
[stop-worker async-store] (worker/start bare-store)
;; Hooks, a map of callback functions that processor calls
;; on certain events. Can be used for webhooks and more.
@@ -85,6 +85,6 @@
(defn -main [& args]
(case (first args)
- "migrate-db" (apply m/edn->sqlite (rest args))
+ "migrate-db" (apply m/edn->h2 (rest args))
(start-engine)))
diff --git a/src/weepinbell/migration.clj b/src/weepinbell/migration.clj
index 4883b99..6579d84 100644
--- a/src/weepinbell/migration.clj
+++ b/src/weepinbell/migration.clj
@@ -1,26 +1,26 @@
(ns weepinbell.migration
- (:require [weepinbell.storage.sqlite :as sqlite]
+ (:require [weepinbell.storage.h2 :as h2]
[weepinbell.storage.edn-storage :as edns])
- (:import [org.sqlite SQLiteException]))
+ #_(:import [org.h2 SQLiteException]))
-(defn edn->sqlite
+(defn edn->h2
"Given a EDN file and a SQLite DB path, migrate data from that
edn file to SQLite DB. It appends data to DB so the destination
db can be empty or it can has some data."
- [edn-file sqlite-db]
- {:pre [(some? edn-file) (some? sqlite-db)]}
- (let [ds (sqlite/make-datasource sqlite-db)]
- (sqlite/create-tables ds)
+ [edn-file h2-db]
+ {:pre [(some? edn-file) (some? h2-db)]}
+ (let [ds (h2/make-datasource h2-db)]
+ (h2/create-tables ds)
(edns/with-webmentions edn-file
(fn [wms]
(doseq [wm wms]
(let [id (get-in wm [:webmention :id])]
(println "Appending webmention" id "to database")
(try
- (sqlite/insert-webmention ds wm)
- (catch SQLiteException e
- (println "Something wrong with data or database")
- (println (.getMessage e)))
+ (h2/insert-webmention ds wm)
+;; (catch SQLiteException e
+;; (println "Something wrong with data or database")
+;; (println (.getMessage e)))
(catch Exception e
(println "Something unexpected happened.")
(println (.getMessage e))))))))
diff --git a/src/weepinbell/storage/sqlite.clj b/src/weepinbell/storage/h2.clj
index 59feb37..b42b5ba 100644
--- a/src/weepinbell/storage/sqlite.clj
+++ b/src/weepinbell/storage/h2.clj
@@ -1,4 +1,4 @@
-(ns weepinbell.storage.sqlite
+(ns weepinbell.storage.h2
(:require [clojure.string :refer [blank? split]]
[next.jdbc :as jdbc]
[next.jdbc.result-set :refer [as-unqualified-lower-maps]]
@@ -55,7 +55,7 @@
(let [mf (:microformats data)
photo (get-in data [:extra :proxied-photo])
wm_result (sql/insert! ds :webmention (wm->schema data))
- wm_id (get wm_result (keyword "last_insert_rowid()"))]
+ wm_id (:id wm_result)]
(when mf
(sql/insert! ds :microformats (mf->schema wm_id data)))
(when photo
@@ -113,14 +113,14 @@
(defn make-db [db-file]
(io/make-parents db-file)
- {:dbtype "sqlite" :dbname db-file})
+ {:dbtype "h2" :dbname db-file})
(defn make-ds [db]
(jdbc/get-datasource db))
(defn make-datasource [db-file]
(jdbc/with-options (make-ds (make-db db-file))
{:builder-fn as-unqualified-lower-maps}))
-(def init-sql-script (io/resource "sql/sqlite_init.sql"))
+(def init-sql-script (io/resource "sql/h2_init.sql"))
(defn create-tables [ds]
(doseq [:let [statements (-> init-sql-script slurp (split #";"))]
statement (remove blank? statements)]