aleeusgr / hs-clojure

Other
0 stars 0 forks source link

Set up CI/CD pipeline #2

Open aleeusgr opened 4 months ago

aleeusgr commented 4 months ago

Test Data

Use a test database or a in-memory database like jdbc:h2:mem: to isolate tests from the production database. You can also use a library like testcontainers to spin up a test database instance.

aleeusgr commented 3 months ago

To use an in-memory database instead of the current one, you can use the jdbc:hsqldb subprotocol in your db.clj file. Here's an example of how you can modify your db.clj file to use an in-memory database:

(ns hs-clojure.db
  (:require [clojure.java.jdbc :as jdbc]
            [jdbc.pool.c3p0 :as pool]))

(def spec
  (pool/make-datasource-spec
   {:subprotocol "hsqldb"
    :subname "mem:hs_clojure"
    :user "sa"
    :password ""}))

(defn create-patients-table []
  (jdbc/with-db-transaction [tx spec] 
    (jdbc/db-do-commands tx 
                         "CREATE TABLE IF NOT EXISTS patients (
                          id bigserial PRIMARY KEY,
                          name varchar NOT NULL,
                          sex varchar NOT NULL,
                          date_of_birth date NOT NULL,
                          address text NOT NULL,
                          social_security_number varchar(11) NOT NULL,
                          created_at timestamp NOT NULL default current_timestamp)")))

(create-patients-table)

In this example, we're using the hsqldb subprotocol, which is a Java SQL database engine that can run in-memory. The subname is set to "mem:hs_clojure", which tells HSQLDB to create an in-memory database.

Note that you'll need to add the HSQLDB dependency to your project.clj file:

(defproject hs-clojure "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.10.1"]
                 [org.clojure/java.jdbc "0.7.11"]
                 [jdbc/c3p0 "0.9.5.2"]
                 [org.hsqldb/hsqldb "2.5.0"]]
  :main ^:skip-aot hs-clojure.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

With these changes, your application will use an in-memory database instead of the PostgreSQL database.

aleeusgr commented 3 months ago

To add database configuration to your CI action, you can use the services feature of GitHub Actions. Here's an updated version of your CI action file:

name: Clojure CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_USER: mydatabase
          POSTGRES_PASSWORD: mypassword
          POSTGRES_DB: mydatabase
        ports:
          - 5432:5432

    steps:
    - uses: actions/checkout@v4
    - name: Install dependencies
      run: lein deps
    - name: Run tests
      run: lein test
    - name: Set environment variables
      run: |
        export DATABASE_URL=postgres://mydatabase:mypassword@localhost:5432/mydatabase
        export DATABASE_USER=mydatabase
        export DATABASE_PASSWORD=mypassword
        export DATABASE_NAME=mydatabase
    - name: Run database migrations
      run: lein migrate

Here's what's changed:

Note that you'll need to update your lein configuration to use the environment variables we've set. You may also need to adjust the DATABASE_URL format to match your Clojure application's database configuration.

With these changes, your CI action should now be able to connect to a PostgreSQL database and run tests and migrations.