Back

init (clj)

(source)

function

(init config & [name])
Initialize the data store

Examples

migratus
(ns migratus.testcontainers.postgres
  "Integration tests for postgresql using testcontainers.org"
  {:authors ["Eugen Stan"]}
  (:require [clj-test-containers.core :as tc]
            [clojure.tools.logging :as log]
            [clojure.set :as set]
            [clojure.test :refer [deftest is testing]]
            [migratus.test.migration.sql :as test-sql]
            [migratus.core :as migratus]
            [next.jdbc :as jdbc]
            [next.jdbc.result-set :as rs]))

  (testing "Migrations are applied succesfully in PostgreSQL."
    (let [pg-container (tc/create pg-container-spec)
          initialized-pg-container (tc/start! pg-container)
          meta->table-names #(into #{} (map :pg_class/table_name %))]
      (Thread/sleep 1000)
      (let [ds (jdbc/get-datasource {:dbtype   "postgresql"
                                     :dbname   "postgres"
                                     :user     "postgres"
                                     :password "pw"
                                     :host     (:host initialized-pg-container)
                                     :port     (get (:mapped-ports initialized-pg-container) 5432)})
            config {:store :database
                    :migration-dir "migrations-postgres"
                    :init-script "init.sql"
                    :migration-table-name "foo_bar"
                    :db {:datasource ds}}]
        (is (= [] (test-sql/db-tables-and-views ds)) "db is empty before migrations")

        ;; init 
        (migratus/init config)
        (let [db-meta (test-sql/db-tables-and-views ds)
              table-names (meta->table-names db-meta)]
          (is (= #{"foo"} table-names) "db is initialized"))

      (tc/stop! initialized-pg-container))))
migratus
;;;; Copyright © 2011 Paul Stadig
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License"); you may not
;;;; use this file except in compliance with the License.  You may obtain a copy
;;;; of the License at
;;;;
;;;;   http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
;;;; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
;;;; License for the specific language governing permissions and limitations
;;;; under the License.
(ns migratus.test.database
  (:require [clojure.java.io :as io]
            [next.jdbc :as jdbc]
            [next.jdbc.result-set :as rs]
            [next.jdbc.quoted :as q]
            [next.jdbc.sql :as sql]
            [migratus.protocols :as proto]
            [migratus.core :as core]
            [clojure.test :refer :all]
            [migratus.database :refer :all :as db]
            [clojure.tools.logging :as log]
            [migratus.test.migration.edn :as test-edn]
            [migratus.test.migration.sql :as test-sql]
            [migratus.utils :as utils])
  (:import java.io.File
           java.sql.Connection
           java.util.jar.JarFile
           (java.util.concurrent CancellationException)))

(deftest test-find-init-script-resource
  (testing "finds init.sql under migrations/ in a JAR file"
    (let [jar-file    (JarFile. "test/migrations-jar/init-test.jar")
          init-script (find-init-script-resource "migrations/" jar-file "init.sql")]
      (is (not (nil? init-script)))
      (is (= "CREATE SCHEMA foo;\n" (slurp init-script))))))

(deftest test-init
  (testing "db init"
    (let [config (assoc config :init-script "init.sql")]
      (test-sql/reset-db)
      (let [store (proto/make-store config)]
        (proto/init store)
        (is (test-sql/verify-table-exists? config "foo")))
      (test-sql/reset-db)
      (let [store (proto/make-store (assoc config :init-in-transaction? false))]
        (proto/init store)
        (is (test-sql/verify-table-exists? config "foo"))))))

  (jdbc/execute! (:db config)
                 [(str "CREATE TABLE " (q/ansi "table")
                       "(id BIGINT UNIQUE NOT NULL, applied TIMESTAMP,
                        description VARCHAR(1024) )")])
  (run-test test-init)
  (run-test test-rollback-until-just-after)
  (run-test test-backing-out-bad-migration-no-tx)
clojure-garden/clojure-garden
(ns platform.system.database-migrator
  (:require
    [integrant.core :as ig]
    [migratus.core :as migratus]))


(defmethod ig/init-key :platform.system/database-migrator [_ config]
  (migratus/migrate config))