From 6e3a39859c1668dc1837d02e84f7fe701625da80 Mon Sep 17 00:00:00 2001 From: Bruno do Nascimento Maciel Date: Fri, 1 Nov 2024 16:36:00 -0300 Subject: [PATCH 1/8] initial --- project.clj | 4 +- resources/migrations/001.init.next.sql | 6 +++ .../integrant_components/postgresql.clj | 38 +++++++++++++++++++ .../integrant_components/postgresql_test.clj | 15 ++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 resources/migrations/001.init.next.sql create mode 100644 src/common_clj/integrant_components/postgresql.clj create mode 100644 test/integration/integration/integrant_components/postgresql_test.clj diff --git a/project.clj b/project.clj index c6e40051..6fdba678 100644 --- a/project.clj +++ b/project.clj @@ -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)] diff --git a/resources/migrations/001.init.next.sql b/resources/migrations/001.init.next.sql new file mode 100644 index 00000000..6761feaf --- /dev/null +++ b/resources/migrations/001.init.next.sql @@ -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 +); diff --git a/src/common_clj/integrant_components/postgresql.clj b/src/common_clj/integrant_components/postgresql.clj new file mode 100644 index 00000000..cb1bf259 --- /dev/null +++ b/src/common_clj/integrant_components/postgresql.clj @@ -0,0 +1,38 @@ +(ns common-clj.integrant-components.postgresql + (:require [integrant.core :as ig] + [pg.core :as pg] + [pg.migration.core :as mig] + [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) + connection (pg/connect postgresql-config)] + (mig/migrate-all postgresql-config) + connection)) + +(defmethod ig/halt-key! ::postgresql + [_ connection] + (log/info :stopping ::postgresql) + (pg/close connection)) + +(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)} + connection (pg/connect postgresql-config)] + (mig/migrate-all postgresql-config) + connection)) + +(defmethod ig/halt-key! ::postgresql-mock + [_ connection] + (log/info :stopping ::postgresql-mock) + (pg/close connection)) + diff --git a/test/integration/integration/integrant_components/postgresql_test.clj b/test/integration/integration/integrant_components/postgresql_test.clj new file mode 100644 index 00000000..574e8e53 --- /dev/null +++ b/test/integration/integration/integrant_components/postgresql_test.clj @@ -0,0 +1,15 @@ +(ns integration.integrant-components.postgresql-test + (:require [clojure.test :refer :all] + [common-clj.integrant-components.postgresql :as postgresql] + [integrant.core :as ig] + [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)] + #p system + (ig/halt! system)))) From 533c8dd801271e90b34a8868f527654b58a0ba34 Mon Sep 17 00:00:00 2001 From: Bruno do Nascimento Maciel Date: Fri, 1 Nov 2024 16:39:40 -0300 Subject: [PATCH 2/8] set java version --- .github/workflows/tests.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 62220294..7644e30e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,6 +9,10 @@ jobs: steps: - uses: actions/checkout@v2 + - uses: actions/setup-java@v4 + with: + distribution: 'zulu' # See 'Supported distributions' for available options + java-version: '21' - name: Install dependencies run: lein deps - name: Run tests From 03ad96a1c93dbb4597c232fd927387673a897b66 Mon Sep 17 00:00:00 2001 From: Bruno do Nascimento Maciel Date: Fri, 1 Nov 2024 16:48:48 -0300 Subject: [PATCH 3/8] debug --- .../integrant_components/postgresql_test.clj | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/integration/integration/integrant_components/postgresql_test.clj b/test/integration/integration/integrant_components/postgresql_test.clj index 574e8e53..6c66d285 100644 --- a/test/integration/integration/integrant_components/postgresql_test.clj +++ b/test/integration/integration/integrant_components/postgresql_test.clj @@ -1,7 +1,10 @@ (ns integration.integrant-components.postgresql-test - (:require [clojure.test :refer :all] + (:require [clojure.instant :as instant] + [clojure.test :refer :all] [common-clj.integrant-components.postgresql :as postgresql] [integrant.core :as ig] + [java-time.api :as jt] + [pg.core :as pg] [schema.test :as s])) (def postgresql-mock-config @@ -11,5 +14,8 @@ (s/deftest postgresql-mock-config-test (testing "That we can define endpoints" (let [system (ig/init postgresql-mock-config)] - #p system + #p (-> system + :common-clj.integrant-components.postgresql/postgresql-mock + (pg/execute "INSERT INTO pessoa (apelido, nome, nascimento) VALUES ($1, $2, $3)" + {:params ["brunão" "nascimento" (jt/local-date-time)]})) (ig/halt! system)))) From 664d51c5812a52d3abfb1e8f691cb3469189f1ca Mon Sep 17 00:00:00 2001 From: Bruno do Nascimento Maciel Date: Fri, 1 Nov 2024 17:25:14 -0300 Subject: [PATCH 4/8] now --- .../integrant_components/postgresql.clj | 18 ++++++++--------- .../integrant_components/postgresql_test.clj | 20 ++++++++++++------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/common_clj/integrant_components/postgresql.clj b/src/common_clj/integrant_components/postgresql.clj index cb1bf259..7138b48a 100644 --- a/src/common_clj/integrant_components/postgresql.clj +++ b/src/common_clj/integrant_components/postgresql.clj @@ -1,7 +1,7 @@ (ns common-clj.integrant-components.postgresql (:require [integrant.core :as ig] - [pg.core :as pg] [pg.migration.core :as mig] + [pg.pool :as pool] [taoensso.timbre :as log]) (:import (org.testcontainers.containers PostgreSQLContainer))) @@ -9,14 +9,14 @@ [_ {:keys [components]}] (log/info :starting ::postgresql) (let [postgresql-config (-> components :config :postgresql) - connection (pg/connect postgresql-config)] + pool (pool/pool postgresql-config)] (mig/migrate-all postgresql-config) - connection)) + pool)) (defmethod ig/halt-key! ::postgresql - [_ connection] + [_ pool] (log/info :stopping ::postgresql) - (pg/close connection)) + (pool/close pool)) (defmethod ig/init-key ::postgresql-mock [_ _] @@ -27,12 +27,12 @@ :user (.getUsername postgresql-container) :password (.getPassword postgresql-container) :database (.getDatabaseName postgresql-container)} - connection (pg/connect postgresql-config)] + pool (pool/pool postgresql-config)] (mig/migrate-all postgresql-config) - connection)) + pool)) (defmethod ig/halt-key! ::postgresql-mock - [_ connection] + [_ pool] (log/info :stopping ::postgresql-mock) - (pg/close connection)) + (pool/close pool)) diff --git a/test/integration/integration/integrant_components/postgresql_test.clj b/test/integration/integration/integrant_components/postgresql_test.clj index 6c66d285..0e02f7e0 100644 --- a/test/integration/integration/integrant_components/postgresql_test.clj +++ b/test/integration/integration/integrant_components/postgresql_test.clj @@ -1,10 +1,10 @@ (ns integration.integrant-components.postgresql-test - (:require [clojure.instant :as instant] - [clojure.test :refer :all] + (: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 @@ -13,9 +13,15 @@ (s/deftest postgresql-mock-config-test (testing "That we can define endpoints" - (let [system (ig/init postgresql-mock-config)] - #p (-> system - :common-clj.integrant-components.postgresql/postgresql-mock - (pg/execute "INSERT INTO pessoa (apelido, nome, nascimento) VALUES ($1, $2, $3)" - {:params ["brunão" "nascimento" (jt/local-date-time)]})) + (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)))) From cc1740b5c9d0fad707cfdf4b809d66a908553725 Mon Sep 17 00:00:00 2001 From: Bruno do Nascimento Maciel Date: Fri, 1 Nov 2024 17:29:56 -0300 Subject: [PATCH 5/8] proper release info --- CHANGELOG.md | 10 +++++++++- project.clj | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f821baf4..7a947a29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/project.clj b/project.clj index 6fdba678..f61e195b 100644 --- a/project.clj +++ b/project.clj @@ -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" From 81673515cf85d26789564e06b287b9c60d7bfc39 Mon Sep 17 00:00:00 2001 From: Bruno do Nascimento Maciel Date: Fri, 1 Nov 2024 17:46:42 -0300 Subject: [PATCH 6/8] add unit test --- .../integrant_components/postgresql.clj | 16 ++++++++++++++++ .../integrant_components/postgresql_test.clj | 1 - .../integrant_components/postgresql_test.clj | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/unit/common_clj/integrant_components/postgresql_test.clj diff --git a/src/common_clj/integrant_components/postgresql.clj b/src/common_clj/integrant_components/postgresql.clj index 7138b48a..ac257ba5 100644 --- a/src/common_clj/integrant_components/postgresql.clj +++ b/src/common_clj/integrant_components/postgresql.clj @@ -1,7 +1,9 @@ (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))) @@ -36,3 +38,17 @@ (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))) + + diff --git a/test/integration/integration/integrant_components/postgresql_test.clj b/test/integration/integration/integrant_components/postgresql_test.clj index 0e02f7e0..ad58f0cf 100644 --- a/test/integration/integration/integrant_components/postgresql_test.clj +++ b/test/integration/integration/integrant_components/postgresql_test.clj @@ -10,7 +10,6 @@ (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) diff --git a/test/unit/common_clj/integrant_components/postgresql_test.clj b/test/unit/common_clj/integrant_components/postgresql_test.clj new file mode 100644 index 00000000..75275e4b --- /dev/null +++ b/test/unit/common_clj/integrant_components/postgresql_test.clj @@ -0,0 +1,16 @@ +(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 (= nil + (pg/execute conn + "INSERT INTO pessoa (apelido, nome, nascimento) VALUES ($1, $2, $3) + returning *" + {:params ["brunão" "nascimento" now]})))))) From 5f5123e32e81aa50413312d6ba5069d69c62b7d5 Mon Sep 17 00:00:00 2001 From: Bruno do Nascimento Maciel Date: Fri, 1 Nov 2024 17:53:17 -0300 Subject: [PATCH 7/8] fix test --- test/unit/common_clj/integrant_components/postgresql_test.clj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/unit/common_clj/integrant_components/postgresql_test.clj b/test/unit/common_clj/integrant_components/postgresql_test.clj index 75275e4b..e998635a 100644 --- a/test/unit/common_clj/integrant_components/postgresql_test.clj +++ b/test/unit/common_clj/integrant_components/postgresql_test.clj @@ -9,7 +9,9 @@ (testing "That we can connect to a mocked postgresql container" (let [conn (postgresql/mocked-postgresql-conn) now (jt/local-date)] - (is (= nil + (is (= [{:nome "nascimento" + :apelido "brunão" + :nascimento now}] (pg/execute conn "INSERT INTO pessoa (apelido, nome, nascimento) VALUES ($1, $2, $3) returning *" From 0d7cddf12339617294579292e66a541b898f9521 Mon Sep 17 00:00:00 2001 From: Bruno do Nascimento Maciel Date: Fri, 1 Nov 2024 18:26:45 -0300 Subject: [PATCH 8/8] remove uselles coment --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7644e30e..af3b2672 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-java@v4 with: - distribution: 'zulu' # See 'Supported distributions' for available options + distribution: 'zulu' java-version: '21' - name: Install dependencies run: lein deps