Skip to content

Commit

Permalink
Merge pull request #152 from macielti/migrate-postgresql-to-integrant
Browse files Browse the repository at this point in the history
Migrate PostgreSQL component to Integrant
  • Loading branch information
macielti authored Nov 1, 2024
2 parents ca082c8 + 0d7cddf commit 03b99ee
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '21'
- name: Install dependencies
run: lein deps
- name: Run tests
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ of [keepachangelog.com](http://keepachangelog.com/).

## [Unreleased]

## [30.67.70] - 2024-11-01

### Added

- New `PostgreSQL` component (Integrant).

## [30.66.70] - 2024-10-10

### Added
Expand Down Expand Up @@ -984,7 +990,9 @@ of [keepachangelog.com](http://keepachangelog.com/).

- Add `loose-schema` function.

[Unreleased]: https://github.com/macielti/common-clj/compare/v30.66.70...HEAD
[Unreleased]: https://github.com/macielti/common-clj/compare/v30.67.70...HEAD

[30.67.70]: https://github.com/macielti/common-clj/compare/v30.66.70...v30.67.70

[30.66.70]: https://github.com/macielti/common-clj/compare/v30.65.70...v30.66.70

Expand Down
6 changes: 4 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject net.clojars.macielti/common-clj "30.66.70"
(defproject net.clojars.macielti/common-clj "30.67.70"
:description "Just common Clojure code that I use across projects"
:url "https://github.com/macielti/common-clj"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down Expand Up @@ -58,7 +58,9 @@
[com.fasterxml.jackson.core/jackson-annotations "2.18.0-rc1"]
[diehard "0.11.12"]
[overtone/at-at "1.3.58"]
[buddy/buddy-hashers "1.8.158"]]
[buddy/buddy-hashers "1.8.158"]
[com.github.igrishaev/pg2-core "0.1.18"]
[com.github.igrishaev/pg2-migration "0.1.18"]]

:injections [(require 'hashp.core)]

Expand Down
6 changes: 6 additions & 0 deletions resources/migrations/001.init.next.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS pessoa
(
apelido TEXT UNIQUE NOT NULL PRIMARY KEY,
nome TEXT NOT NULL,
nascimento DATE NOT NULL
);
54 changes: 54 additions & 0 deletions src/common_clj/integrant_components/postgresql.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(ns common-clj.integrant-components.postgresql
(:require [integrant.core :as ig]
[pg.core :as pg]
[pg.migration.core :as mig]
[pg.pool :as pool]
[schema.core :as s]
[taoensso.timbre :as log])
(:import (org.testcontainers.containers PostgreSQLContainer)))

(defmethod ig/init-key ::postgresql
[_ {:keys [components]}]
(log/info :starting ::postgresql)
(let [postgresql-config (-> components :config :postgresql)
pool (pool/pool postgresql-config)]
(mig/migrate-all postgresql-config)
pool))

(defmethod ig/halt-key! ::postgresql
[_ pool]
(log/info :stopping ::postgresql)
(pool/close pool))

(defmethod ig/init-key ::postgresql-mock
[_ _]
(log/info :starting ::postgresql-mock)
(let [postgresql-container (doto (PostgreSQLContainer. "postgres:16-alpine") .start)
postgresql-config {:host (.getHost postgresql-container)
:port (.getMappedPort postgresql-container PostgreSQLContainer/POSTGRESQL_PORT)
:user (.getUsername postgresql-container)
:password (.getPassword postgresql-container)
:database (.getDatabaseName postgresql-container)}
pool (pool/pool postgresql-config)]
(mig/migrate-all postgresql-config)
pool))

(defmethod ig/halt-key! ::postgresql-mock
[_ pool]
(log/info :stopping ::postgresql-mock)
(pool/close pool))

(s/defn mocked-postgresql-conn
"Intended to be used for unit testing"
[]
(let [postgresql-container (doto (PostgreSQLContainer. "postgres:16-alpine")
.start)
postgresql-config {:host (.getHost postgresql-container)
:port (.getMappedPort postgresql-container PostgreSQLContainer/POSTGRESQL_PORT)
:user (.getUsername postgresql-container)
:password (.getPassword postgresql-container)
:database (.getDatabaseName postgresql-container)}]
(mig/migrate-all postgresql-config)
(pg/connect postgresql-config)))


Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns integration.integrant-components.postgresql-test
(:require [clojure.test :refer :all]
[common-clj.integrant-components.postgresql :as postgresql]
[integrant.core :as ig]
[java-time.api :as jt]
[pg.core :as pg]
[pg.pool :as pool]
[schema.test :as s]))

(def postgresql-mock-config
{::postgresql/postgresql-mock {}})

(s/deftest postgresql-mock-config-test
(testing "That we can define endpoints"
(let [system (ig/init postgresql-mock-config)
now (jt/local-date)]
(is (= [{:apelido "brunão"
:nascimento now
:nome "nascimento"}]
(pool/with-connection
[conn (:common-clj.integrant-components.postgresql/postgresql-mock system)]
(pg/execute conn
"INSERT INTO pessoa (apelido, nome, nascimento) VALUES ($1, $2, $3)
returning *"
{:params ["brunão" "nascimento" now]}))))
(ig/halt! system))))
18 changes: 18 additions & 0 deletions test/unit/common_clj/integrant_components/postgresql_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns common-clj.integrant-components.postgresql-test
(:require [clojure.test :refer :all]
[common-clj.integrant-components.postgresql :as postgresql]
[java-time.api :as jt]
[pg.core :as pg]
[schema.test :as s]))

(s/deftest mocked-postgresql-conn-test
(testing "That we can connect to a mocked postgresql container"
(let [conn (postgresql/mocked-postgresql-conn)
now (jt/local-date)]
(is (= [{:nome "nascimento"
:apelido "brunão"
:nascimento now}]
(pg/execute conn
"INSERT INTO pessoa (apelido, nome, nascimento) VALUES ($1, $2, $3)
returning *"
{:params ["brunão" "nascimento" now]}))))))

0 comments on commit 03b99ee

Please sign in to comment.