Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

에러 날 경우 Exception 발생 #12

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/automigrate/core.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
(ns automigrate.core
"Public interface for lib's users."
(:gen-class)
(:require [clojure.spec.alpha :as s]
(:require [automigrate.fields :as fields]
[automigrate.help :as automigrate-help]
[automigrate.migrations :as migrations]
[automigrate.util.file :as file-util]
[automigrate.util.spec :as spec-util]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[clojure.tools.cli :as cli]
[slingshot.slingshot :refer [try+]]
[automigrate.migrations :as migrations]
[automigrate.util.spec :as spec-util]
[automigrate.util.file :as file-util]
[automigrate.errors :as errors]
[automigrate.help :as automigrate-help]
[automigrate.fields :as fields])
[slingshot.support :refer [rethrow]])
(:refer-clojure :exclude [list]))


Expand Down Expand Up @@ -106,13 +106,12 @@
(let [args* (spec-util/conform args-spec (or args {}))]
(f args*))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(file-util/prn-err e) ; type invalid는 모두 여기서 prn-err
(.flush *out*)
(rethrow))
(catch Object e
(let [message (or (ex-message e) (str e))]
(-> {:title "UNEXPECTED ERROR"
:message message}
(errors/custom-error->error-report)
(file-util/prn-err))))))
(.flush *out*)
(rethrow))))


; Public interface
Expand Down Expand Up @@ -247,3 +246,11 @@ Available options:
"explain" (explain (parse-opts-or-throw-err args cli-options-explain))
"help" (help (parse-opts-or-throw-err args cli-options-help))
(println "ERROR: command does not exist.")))


(comment
(make {})

(migrate {:jdbc-url "jdbc:postgresql://localhost:5433/farmmoa?user=developer&password=postgrespasswor"})

:rcf)
65 changes: 37 additions & 28 deletions src/automigrate/migrations.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@
"Module for applying changes to migrations and db.
Also contains tools for inspection of db state by migrations
and state of migrations itself."
(:require [next.jdbc :as jdbc]
[clojure.spec.alpha :as s]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.set :as set]
[clojure.pprint :as pprint]
[slingshot.slingshot :refer [throw+ try+]]
[differ.core :as differ]
[weavejester.dependency :as dep]
[automigrate.actions :as actions]
[automigrate.models :as models]
[automigrate.fields :as fields]
(:require [automigrate.actions :as actions]
[automigrate.errors :as errors]
[automigrate.sql :as sql]
[automigrate.fields :as fields]
[automigrate.models :as models]
[automigrate.schema :as schema]
[automigrate.util.file :as file-util]
[automigrate.sql :as sql]
[automigrate.util.db :as db-util]
[automigrate.util.file :as file-util]
[automigrate.util.model :as model-util]
[automigrate.util.spec :as spec-util]
[automigrate.util.model :as model-util])
[clojure.java.io :as io]
[clojure.pprint :as pprint]
[clojure.set :as set]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[differ.core :as differ]
[next.jdbc :as jdbc]
[slingshot.slingshot :refer [throw+ try+]]
[slingshot.support :refer [rethrow]]
[weavejester.dependency :as dep])
(:import [java.io FileNotFoundException]))


Expand Down Expand Up @@ -721,34 +722,38 @@
(print-action-names next-migration))
(println "There are no changes in models."))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(rethrow))
(catch #(contains? #{::models/missing-referenced-model
::models/missing-referenced-field
::models/referenced-field-is-not-unique
::models/fk-fields-have-different-types} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))
(file-util/prn-err))
(rethrow))
(catch [:reason ::dep/circular-dependency] e
(-> {:title "MIGRATION ERROR"
:message (format (str "Circular dependency between two migration actions: \n %s\nand\n %s\n\n"
"Please split actions by different migrations.")
(pr-str (:dependency e))
(pr-str (:node e)))}
(errors/custom-error->error-report)
(file-util/prn-err)))
(file-util/prn-err))
(rethrow))
(catch FileNotFoundException e
(-> {:title "ERROR"
:message (format "Missing file:\n\n %s" (ex-message e))}
(errors/custom-error->error-report)
(file-util/prn-err)))
(file-util/prn-err))
(rethrow))
(catch IllegalArgumentException e
(-> {:title "ERROR"
:message (str (format "%s\n\nMissing resource file error. " (ex-message e))
"Please, check, if models.edn exists and resources dir\n"
"is included to source paths in `deps.edn` or `project.clj`.")}
(errors/custom-error->error-report)
(file-util/prn-err)))))
(file-util/prn-err))
(rethrow))))


(defmethod make-migration EMPTY-SQL-MIGRATION-TYPE
Expand All @@ -772,12 +777,13 @@
(spit migration-file-name-full-path SQL-MIGRATION-TEMPLATE)
(println (str "Created migration: " migration-file-name-full-path)))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(rethrow))
(catch #(contains? #{::missing-migration-name
::duplicated-migration-numbers} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))))
(file-util/prn-err))
(rethrow))))


(defn- get-migration-by-number
Expand Down Expand Up @@ -869,12 +875,13 @@
:migration-type (get-migration-type file-name)
:explain-format explain-format})))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(rethrow))
(catch #(contains? #{::no-migration-by-number
::duplicated-migration-numbers} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))))
(file-util/prn-err))
(rethrow))))


(defn- already-migrated
Expand Down Expand Up @@ -1002,12 +1009,13 @@
(delete-migration! db migration-name migrations-table))))
(println "Nothing to migrate.")))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(rethrow))
(catch #(contains? #{::duplicated-migration-numbers
::invalid-target-migration-number} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))))
(file-util/prn-err))
(rethrow))))


(defn- get-already-migrated-migrations
Expand Down Expand Up @@ -1040,10 +1048,11 @@
(println (format "[%s] %s" sign file-name))))
(println "Migrations not found.")))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(rethrow))
(catch #(contains? #{::duplicated-migration-numbers
::no-migrations-table
::unexpected-db-error} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))))
(file-util/prn-err))
(rethrow))))
6 changes: 3 additions & 3 deletions src/automigrate/util/file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[clojure.java.io :as io]
[clojure.string :as str]
[resauce.core :as resauce])
(:import [java.nio.file Paths]
[com.github.vertical_blank.sqlformatter SqlFormatter]
[com.github.vertical_blank.sqlformatter.languages Dialect]))
(:import [com.github.vertical_blank.sqlformatter SqlFormatter]
[com.github.vertical_blank.sqlformatter.languages Dialect]
[java.nio.file Paths]))


(def DEFAULT-ZERO-COUNT 4)
Expand Down
37 changes: 14 additions & 23 deletions test/automigrate/comment_on_column.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns automigrate.comment-on-column
(:require [clojure.test :refer :all]
(:require [automigrate.testing-config :as config]
[automigrate.testing-util :as test-util]
[automigrate.testing-config :as config]))
[clojure.test :refer :all]))


(use-fixtures :each
Expand Down Expand Up @@ -390,28 +390,19 @@

(deftest test-commment-on-column-errors
(testing "comment can't be integer"
(is (= (str "-- MODEL ERROR -------------------------------------\n\n"
"Option :comment of field :account/thing should be string.\n\n"
" {:comment 1}\n\n")
(with-out-str
(test-util/make-migration!
{:existing-models
{:account {:fields [[:thing :interval {:comment 1}]]}}})))))
(is (thrown-with-msg? Exception #"-- MODEL ERROR -------------------------------------\\n\\nOption :comment of field :account/thing should be string.\\n\\n \{:comment 1\}\\n"
(with-out-str (test-util/make-migration!
{:existing-models
{:account {:fields [[:thing :interval {:comment 1}]]}}})))))

(testing "comment can't be nil"
(is (= (str "-- MODEL ERROR -------------------------------------\n\n"
"Option :comment of field :account/thing should be string.\n\n"
" {:comment nil}\n\n")
(with-out-str
(test-util/make-migration!
{:existing-models
{:account {:fields [[:thing :interval {:comment nil}]]}}})))))
(is (thrown-with-msg? Exception #"-- MODEL ERROR -------------------------------------\\n\\nOption :comment of field :account/thing should be string.\\n\\n \{:comment nil\}\\n"
(with-out-str (test-util/make-migration!
{:existing-models
{:account {:fields [[:thing :interval {:comment nil}]]}}})))))

(testing "comment can't be an empty string"
(is (= (str "-- MODEL ERROR -------------------------------------\n\n"
"Option :comment of field :account/thing should be string.\n\n"
" {:comment \"\"}\n\n")
(with-out-str
(test-util/make-migration!
{:existing-models
{:account {:fields [[:thing :interval {:comment ""}]]}}}))))))
(is (thrown-with-msg? Exception #"-- MODEL ERROR -------------------------------------\\n\\nOption :comment of field :account/thing should be string.\\n\\n \{:comment \\\"\\\"\}\\n"
(with-out-str (test-util/make-migration!
{:existing-models
{:account {:fields [[:thing :interval {:comment ""}]]}}}))))))
28 changes: 11 additions & 17 deletions test/automigrate/constraints_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns automigrate.constraints-test
(:require [clojure.test :refer :all]
(:require [automigrate.testing-config :as config]
[automigrate.testing-util :as test-util]
[automigrate.testing-config :as config]))
[clojure.test :refer :all]))


(use-fixtures :each
Expand Down Expand Up @@ -632,28 +632,22 @@
(let [params {:existing-models
{:account
{:fields [[:thing :integer {:check nil}]]}}}]
(is (= (str "-- MODEL ERROR -------------------------------------\n\n"
"Option :check of field :account/thing should be a not empty vector.\n\n"
" {:check nil}\n\n")
(with-out-str
(test-util/make-migration! params))))))
(is (thrown-with-msg? Exception #"-- MODEL ERROR -------------------------------------\\n\\nOption :check of field :account/thing should be a not empty vector.\\n\\n \{:check nil\}\\n"
(with-out-str
(test-util/make-migration! params))))))

(testing "check can't be string"
(let [params {:existing-models
{:account
{:fields [[:thing :integer {:check "WRONG"}]]}}}]
(is (= (str "-- MODEL ERROR -------------------------------------\n\n"
"Option :check of field :account/thing should be a not empty vector.\n\n"
" {:check \"WRONG\"}\n\n")
(with-out-str
(test-util/make-migration! params))))))
(is (thrown-with-msg? Exception #"-- MODEL ERROR -------------------------------------\\n\\nOption :check of field :account/thing should be a not empty vector.\\n\\n \{:check \\\"WRONG\\\"\}\\n"
(with-out-str
(test-util/make-migration! params))))))

(testing "check can't be an empty vector"
(let [params {:existing-models
{:account
{:fields [[:thing :integer {:check []}]]}}}]
(is (= (str "-- MODEL ERROR -------------------------------------\n\n"
"Option :check of field :account/thing should be a not empty vector.\n\n"
" {:check []}\n\n")
(with-out-str
(test-util/make-migration! params)))))))
(is (thrown-with-msg? Exception #"-- MODEL ERROR -------------------------------------\\n\\nOption :check of field :account/thing should be a not empty vector.\\n\\n \{:check \[\]\}\\n"
(with-out-str
(test-util/make-migration! params)))))))
Loading