Skip to content

Commit

Permalink
Merge pull request #193 from Flexiana/Add_seed!
Browse files Browse the repository at this point in the history
Add seed!
  • Loading branch information
g-krisztian authored Jan 17, 2022
2 parents 5751b7a + 0da9f5f commit 950f256
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
8 changes: 6 additions & 2 deletions doc/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
- [Config](#config)
- [Database](#database)
- [Database/main](#databasemain)
- [Database/seed](#databaseseed)
- [Database/core](#databasecore)
- [Database/postgresql](#databasepostgresql)
- [Database/sql](#databasesql)
- [Migrations](#migrations)
- [Interceptor](#interceptor)
- [Interceptor/core](#interceptorcore)
Expand Down Expand Up @@ -45,6 +44,11 @@ Database handling functions.

Migratus wrapper to get rid of lein migratus plugin. As well as support profile dependent configuration of migratus.

#### Database/seed

Migratus wrapper to support profile based separation of data seeding


#### Database/core

Start function, which gets the data source based on given configuration. Query executor functions and the db-access
Expand Down
36 changes: 36 additions & 0 deletions doc/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [Dependencies and configuration](#dependencies-and-configuration)
- [Database migration](#database-migration)
- [Database seed with data](#database-seed-with-data)
- [Interceptors typical use-case, and ordering](#interceptors-typical-use-case-and-ordering)
- [Defining new interceptors](#defining-new-interceptors)
- [Interceptor example](#interceptor-example)
Expand Down Expand Up @@ -96,6 +97,41 @@ lein migrate migrate

migratus will use the migrations from a folder, what is configured in `config/dev/config.edn`.

## Database seed with data

With extending migration configuration with `seeds-dir` and `seeds-table-name` you can use

```shell
lein seed create
lein seed migrate
lein seed reset
lein seed destroy
```

commands. Every defined profile can have a different seeds directory to have different dataset for different
environments. If you're using this method to seed your data, keep your eye on the database structure is already updated
when the seeding is happens.

Example for configuration:

```clojure
:framework.db.storage/migration {:store :database
:migration-dir "migrations"
:seeds-dir "dev_seeds"
:migration-table-name "migrations"
:seeds-table-name "seeds"}
```

Example of using it from application start
```clojure
(-> (config/config app-cfg)
...
db/connect
db/migrate!
seed/seed!
...)
```

## Interceptors typical use-case, and ordering

Typical use-case, and ordering looks like this:
Expand Down
9 changes: 5 additions & 4 deletions src/framework/db/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
"up"))

(defn -main [& args]
(let [[command param] args
(let [[command name type] args
[_ & ids] args
cfg (config/config)
db (:framework.db.storage/postgresql cfg)
config (assoc (:framework.db.storage/migration cfg) :db db)]
(log/debug config)
(if (str/blank? command)
(help)
(case (str/lower-case command)
"create" (migratus/create config param)
"create" (migratus/create config name (keyword type))
"destroy" (migratus/destroy config)
"down" (migratus/down config param)
"down" (apply migratus/down config (map #(Long/parseLong %) ids))
"init" (migratus/init config)
"migrate" (migratus/migrate config)
"reset" (migratus/reset config)
"rollback" (migratus/rollback config)
"up" (migratus/up config param)
"up" (apply migratus/up config (map #(Long/parseLong %) ids))
(help)))))
36 changes: 36 additions & 0 deletions src/framework/db/seed.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(ns framework.db.seed
(:require
[framework.config.core :as config]
[migratus.core :as migratus]
[next.jdbc :as jdbc]
[xiana.commons :refer [rename-key]]))

(defn seed!
([config]
(let [db-conf (if-let [datasource (some-> (get-in config [:framework.db.storage/postgresql :datasource])
jdbc/get-datasource)]
{:datasource datasource}
(:framework.db.storage/postgresql config))
mig-config (-> (assoc (:framework.db.storage/migration config) :db db-conf)
(rename-key :seeds-dir :migration-dir)
(rename-key :seeds-table-name :migration-table-name))]
(migratus/migrate mig-config))
config))

(def seed-config
(let [config (config/config)
db-conf (:framework.db.storage/postgresql config)]
(-> (assoc (:framework.db.storage/migration config) :db db-conf)
(rename-key :seeds-dir :migration-dir)
(rename-key :seeds-table-name :migration-table-name))))

(defn -main [& args]
(let [[command name type] args]
(if (and (:migration-dir seed-config) (:migration-table-name seed-config))
(case command
"create" (migratus/create seed-config name (keyword type))
"reset" (migratus/reset seed-config)
"destroy" (migratus/destroy seed-config)
"migrate" (migratus/migrate seed-config)
(println "You can 'create' 'reset' 'destroy' or 'migrate' your seed data"))
(println "No seed configuration found"))))
11 changes: 11 additions & 0 deletions test/framework/auth/hash_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@
(deftest test-assert-functionality
(let [fragment {:deps {:auth {:hash-algorithm :argon2}}}]
(is (thrown? java.lang.AssertionError (hash/make fragment password)))))

(deftest hash-behavior
(let [pwd "not_nil"
state {:deps {:auth {:hash-algorithm :bcrypt
:bcrypt-settings {:work-factor 11}}}}
hash1 (hash/make state pwd)
hash2 (hash/make state pwd)]
(is (false? (hash/check state hash1 hash2)))
(is (not= hash1 hash2))
(is (true? (and (hash/check state pwd hash1)
(hash/check state pwd hash2))))))

0 comments on commit 950f256

Please sign in to comment.