Skip to content

Commit

Permalink
implement 'sql-file' change type
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarshantanu committed Jan 28, 2014
1 parent 3433c3d commit b057d11
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
49 changes: 42 additions & 7 deletions src/clj_liquibase/change.clj
Expand Up @@ -30,7 +30,7 @@
;; Architectural Refactorings
CreateIndexChange DropIndexChange
;; Custom Refactorings
RawSQLChange)
RawSQLChange SQLFileChange)
(liquibase.statement DatabaseFunction)
(liquibase.util ISODateFormat)))

Expand Down Expand Up @@ -1189,10 +1189,12 @@
;; Modifying Generated SQL
;; TODO

;; Custom SQL

;; Custom SQL - RawSQLChange

(defn ^RawSQLChange sql
"Allows execution of arbitrary SQL. This change can be used when existing
changes are either don't exist, are not flexible enough, or buggy. (RawSQLChange).
"Return a Change instance that executes arbitrary SQL (RawSQLChange). May be
useful when desired change types don't exist, or are buggy/inflexible.
See also:
http://www.liquibase.org/documentation/changes/sql"
[sql
Expand All @@ -1212,14 +1214,47 @@
(doto change
(.setSql sql))
(if comment (.setComment change comment))
(if dbms (.setDbms change dbms))
(if dbms (.setDbms change (if (or (coll? dbms) (seq? dbms))
(mu/comma-sep-str dbms)
dbms)))
(if end-delimiter (.setEndDelimiter change end-delimiter))
(if split-statements (.setSplitStatements change split-statements))
(if strip-comments (.setStripComments change strip-comments))
change))

;; Custom SQL File .SQLFileChange
;; TODO

;; Custom SQL File - SQLFileChange

(defn ^SQLFileChange sql-file
"Return a Change instance that executes SQL after reading it from a file
(SQLFileChange). Useful to integrate with legacy projects having SQL files for
DDL, or to decouple DDL from the project in general.
See also:
http://www.liquibase.org/documentation/changes/sql_file"
[sql-filepath
& {:keys [dbms
encoding
end-delimiter
split-statements
strip-comments
] :as opt}] {:post (instance? SQLFileChange %)
:pre [(mu/verify-opt #{:dbms
:encoding
:end-delimiter
:split-statements
:strip-comments} opt)]}
(let [change (SQLFileChange.)]
(doto change
(.setPath sql-filepath))
(if dbms (.setDbms change (if (or (coll? dbms) (seq? dbms))
(mu/comma-sep-str dbms)
dbms)))
(if encoding (.setEncoding change encoding))
(if end-delimiter (.setEndDelimiter change end-delimiter))
(if split-statements (.setSplitStatements change split-statements))
(if strip-comments (.setStripComments change strip-comments))
change))


;; Custom Refactoring Class
;; TODO
Expand Down
40 changes: 29 additions & 11 deletions test/clj_liquibase/test_change.clj
Expand Up @@ -24,7 +24,7 @@
;; Architectural Refactorings
CreateIndexChange DropIndexChange
;; Custom Refactorings
RawSQLChange)
RawSQLChange SQLFileChange)
(liquibase.statement DatabaseFunction)
(liquibase.util ISODateFormat))
(:use clj-liquibase.test-util)
Expand Down Expand Up @@ -529,15 +529,34 @@
with-schema-name
with-schema)))


;; ----- Custom Refactorings -----


(deftest test-sql
(testing "sql"
(test-change RawSQLChange
(partial change/sql "SELECT * FROM DATABASECHAGELOG")
["With :comment value" :comment "This is a comment"]
["With :dbms value" :dbms "h2"]
["With :end-delimiter value" :end-delimiter ";"]
["With :split-statements value" :split-statements true]
["With :strip-comments value" :strip-comments true])))
(partial change/sql "SELECT * FROM DATABASECHAGELOG")
["With :comment value" :comment "This is a comment"]
["With :dbms value" :dbms "h2"]
["With :end-delimiter value" :end-delimiter ";"]
["With :split-statements value" :split-statements true]
["With :strip-comments value" :strip-comments true])))


(deftest test-sql-file
(testing "sql-file"
(test-change SQLFileChange
(partial change/sql-file "test-sql-file.sql")
["With :dbms value" :dbms "h2"]
["With :encoding value" :encoding "utf-8"]
["With :end-delimiter value" :end-delimiter ";"]
["With :split-statements value" :split-statements true]
["With :strip-comments value" :strip-comments true])))


;; ***** Run the tests *****


(defn test-ns-hook []
;; ----- Structural Refactorings -----
Expand Down Expand Up @@ -581,7 +600,6 @@
;; ----- Architectural Refactorings -----
(test-create-index)
(test-drop-index)
(test-drop-default-value)
(test-drop-default-value)
(test-drop-default-value)
(test-drop-default-value))
;; ----- Custom Refactorings -----
(test-sql)
(test-sql-file))

0 comments on commit b057d11

Please sign in to comment.