Skip to content

Commit

Permalink
Fix schema case insensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
crisptrutski committed May 28, 2024
1 parent 5c3da72 commit edadd2d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/macaw/collect.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
(subs s 1 (dec (count s))))

(defn normalize-reference
"Normalize a schema, table, column, etc references so that we can match them regardless of syntactic differences."
"Normalize a schema, table, column, etc. references so that we can match them regardless of syntactic differences."
[s {:keys [case-insensitive? quotes-preserve-case?]}]
(when s
(let [quoted (quoted? s)
Expand Down
16 changes: 10 additions & 6 deletions src/macaw/rewrite.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@
[]))))

(defn- rename-table
[updated-nodes table-renames schema-renames known-tables ^Table t opts]
[updated-nodes table-renames schema-renames known-tables opts ^Table t _ctx]
(when-let [rename (u/find-relevant table-renames (get known-tables t) [:table :schema])]
(vswap! updated-nodes conj [t rename])
(.setName t (val rename)))
(let [schema-name (collect/normalize-reference (.getSchemaName t) opts)]
(let [raw-schema-name (.getSchemaName t)
schema-name (collect/normalize-reference raw-schema-name opts)]
(when-let [schema-rename (find schema-renames schema-name)]
(vswap! updated-nodes conj [schema-name schema-rename])
(vswap! updated-nodes conj [raw-schema-name schema-rename])
(.setSchemaName t (val schema-rename)))))

(defn- rename-column
Expand Down Expand Up @@ -110,13 +111,16 @@
comps (collect/query->components parsed-ast (assoc opts :with-instance true))
columns (index-by-instances (:columns comps))
tables (index-by-instances (:tables comps))
schemas (index-by-instances (:schemas comps))
;; execute rename
updated-nodes (volatile! [])
rename-table* (partial rename-table updated-nodes table-renames schema-renames tables opts)
rename-column* (partial rename-column updated-nodes column-renames columns)
res (-> parsed-ast
(mw/walk-query
{:table (partial rename-table updated-nodes table-renames schema-renames tables)
:column-qualifier (partial rename-table updated-nodes table-renames schema-renames tables)
:column (partial rename-column updated-nodes column-renames columns)})
{:table rename-table*
:column-qualifier rename-table*
:column rename-column*})
(update-query @updated-nodes sql opts))]
(alert-unused! @updated-nodes renames)
res))
9 changes: 8 additions & 1 deletion test/macaw/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@
(m/replace-names "SELECT DOGS.BaRk FROM dOGS"
{:tables {{:table "dogs"} "cats"}
:columns {{:table "dogs" :column "bark"} "meow"}}
:case-insensitive? true))))
{:case-insensitive? true})))

(is (= "SELECT meow FROM private.cats"
(m/replace-names "SELECT bark FROM PUBLIC.dogs"
{:schemas {"public" "private"}
:tables {{:schema "public" :table "dogs"} "cats"}
:columns {{:schema "public" :table "dogs" :column "bark"} "meow"}}
{:case-insensitive? true}))))

(def ^:private heavily-quoted-query-mixed-case
"SELECT RAW, \"Foo\", \"dong\".\"bAr\", `ding`.`dong`.`feE` FROM `ding`.dong")
Expand Down

0 comments on commit edadd2d

Please sign in to comment.