Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft]add example sqlite #19

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples/mbrainz-sqlite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# About the mbrainz example

This example demonstrates how mbrainz datomic database works with the
plenish. To use this example, we need to prepare the Datomic database
and the empty sqlite database.

## Prepare the Datomic database
### Getting Datomic

Follow the directs in your [My Datomic](http://my.datomic.com) account to
download a [Datomic distribution](http://www.datomic.com/get-datomic.html) and
unzip it somewhere convenient.

Update `config/samples/dev-transactor-template.properties` with your license key
where you see`license=`.

Start the transactor:

cd datomic-pro-$VERSION
bin/transactor config/samples/dev-transactor-template.properties

### Getting the Data

Next download the
[subset of the mbrainz database](https://s3.amazonaws.com/mbrainz/datomic-mbrainz-1968-1973-backup-2017-07-20.tar)
covering the period 1968-1973 (which the Datomic team has
scientifically determined as being the most important period in the
history of recorded music):

wget https://s3.amazonaws.com/mbrainz/datomic-mbrainz-1968-1973-backup-2017-07-20.tar -O mbrainz.tar
tar -xvf mbrainz.tar

From the `datomic-pro-$VERSION` directory, [restore the backup](http://docs.datomic.com/on-prem/operation/backup.html#restoring):

# prints progress -- ~1,000 segments in restore
bin/datomic restore-db file://path/to/backup/mbrainz-1968-1973 datomic:dev://localhost:4334/mbrainz-1968-1973

## Prepare the empty sqlite database

1. Install sqlite CLI. Homebrew: `brew install sqlite`
2. rm /tmp/mbrainz.db

## Init the nREPL with Debug flag

```
export PLENISH_DEBUG=true && clj -M:dev:cider:sqlite:datomic-pro
```

## Results

After running the commands in `src/lambdaisland/mbrainz.clj`, the tables in sqlite are

33 changes: 33 additions & 0 deletions examples/mbrainz-sqlite/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{:paths ["src" "resources"]

:deps
{org.clojure/clojure {:mvn/version "1.11.1"}
com.cnuernber/charred {:mvn/version "1.028"}
com.github.seancorfield/next.jdbc {:mvn/version "1.3.874"}
com.github.seancorfield/honeysql {:mvn/version "2.4.1026"}
com.lambdaisland/plenish {:local/root "../../" }}

:mvn/repos
{"my.datomic.com" {:url "https://my.datomic.com/repo"}}

:aliases
{:dev
{:extra-paths ["dev"]
:extra-deps {com.lambdaisland/facai {:mvn/version "0.7.59-alpha"}
djblue/portal {:mvn/version "RELEASE"}}}

:test
{:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.82.1306"}}}

:sqlite
{:extra-deps {org.xerial/sqlite-jdbc {:mvn/version "3.41.2.1"}}}

:datomic-pro
{:extra-deps {com.datomic/datomic-pro {:mvn/version "1.0.6202"}}}

:datomic-cloud
{:extra-deps {com.datomic/client-cloud {:mvn/version "1.0.123"}}}

:datomic-free
{:extra-deps {com.datomic/datomic-free {:mvn/version "0.9.5703.21"}}}}}
Binary file added examples/mbrainz-sqlite/mbrainz.db
Binary file not shown.
19 changes: 19 additions & 0 deletions examples/mbrainz-sqlite/src/lambdaisland/mbrainz.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns lambdaisland.mbrainz
(:require
[datomic.api :as d]
[lambdaisland.plenish :as plenish]
[next.jdbc :as jdbc]
[next.jdbc.result-set :as rs]))

(def datomic-conn (d/connect "datomic:dev://localhost:4334/mbrainz-1968-1973"))
(def sqlite-conn (jdbc/get-datasource "jdbc:sqlite:/tmp/mbrainz.db"))

(def metaschema
{:tables {:release/name {}
:artist/name {}}})

(def initial-ctx (plenish/initial-ctx datomic-conn metaschema))

(def new-ctx (plenish/import-tx-range
initial-ctx datomic-conn sqlite-conn
(d/tx-range (d/log datomic-conn) nil nil)))