diff --git a/clojure/src/pgloader/ddl/common.clj b/clojure/src/pgloader/ddl/common.clj index 6a60ce86..a2c95e60 100644 --- a/clojure/src/pgloader/ddl/common.clj +++ b/clojure/src/pgloader/ddl/common.clj @@ -55,6 +55,7 @@ ;; Pass-through: native PostgreSQL types emitted by non-MySQL sources (= lower "uuid") "uuid" (= lower "xml") "xml" + (= lower "bytea") "bytea" ;; Pass-through array types from PostgreSQL sources (#1371) (str/ends-with? lower "[]") lower (str/starts-with? upper "BIGSERIAL") "bigserial" diff --git a/clojure/src/pgloader/source/sqlite.clj b/clojure/src/pgloader/source/sqlite.clj index b35e1fd2..ced698d7 100644 --- a/clojure/src/pgloader/source/sqlite.clj +++ b/clojure/src/pgloader/source/sqlite.clj @@ -138,6 +138,21 @@ :quote-ids name (str/lower-case name))) +(defn- jdbc-value->str + "Convert a raw JDBC value read from SQLite to a String for COPY TEXT. + BLOB values arrive as byte[]; str would give the JVM \"[B@hash\" form, so + they are emitted as PostgreSQL bytea hex (\\x...) instead (#1782)." + [v] + (when (some? v) + (if (bytes? v) + (let [ba ^bytes v + sb (StringBuilder. (+ 2 (* 2 (alength ba))))] + (.append sb "\\x") + (dotimes [i (alength ba)] + (.append sb (format "%02x" (bit-and (int (aget ba i)) 0xff)))) + (.toString sb)) + (str v)))) + (deftype SQLiteSource [^java.sql.Connection conn ^String db-path @@ -219,8 +234,7 @@ (if (<= i n-cols) (recur (unchecked-inc i) (conj! result - (let [v (.getObject ^ResultSet rs i)] - (when (some? v) (str v))))) + (jdbc-value->str (.getObject ^ResultSet rs i)))) (persistent! result))) (thisfn)))))))) @@ -243,8 +257,7 @@ (if (<= i n) (recur (inc i) (conj! result - (let [v (.getObject rs i)] - (when v (str v))))) + (jdbc-value->str (.getObject rs i)))) (persistent! result))) (thisfn))))))}) (catch Exception e diff --git a/clojure/test/pgloader/ddl_test.clj b/clojure/test/pgloader/ddl_test.clj index 9dceefdb..605d0d83 100644 --- a/clojure/test/pgloader/ddl_test.clj +++ b/clojure/test/pgloader/ddl_test.clj @@ -486,3 +486,13 @@ (is (= ["CREATE SCHEMA IF NOT EXISTS \"shopdb\";" "CREATE SCHEMA IF NOT EXISTS \"public\";"] (ddl/create-schemas-sql ["shopdb" "public" nil "shopdb"])))) + +(deftest test-column-def-bytea-passthrough + ;; SQLite catalog already yields PostgreSQL type names; "bytea" must not + ;; fall through to the MySQL mapper's text default (#1782). + (let [col {:column-name "b" + :column-type "bytea" + :is-nullable true + :extra "" + :column-default nil}] + (is (= " \"b\" bytea" (ddl/column-def col))))) diff --git a/clojure/test/pgloader/source/sqlite_test.clj b/clojure/test/pgloader/source/sqlite_test.clj index cd43c802..2a06f7f8 100644 --- a/clojure/test/pgloader/source/sqlite_test.clj +++ b/clojure/test/pgloader/source/sqlite_test.clj @@ -157,3 +157,16 @@ (is (= 2 (count mvs))) (is (nil? (:query (first mvs)))) (is (string? (:query (second mvs)))))))) + +(deftest jdbc-value->str-test + (testing "BLOB byte[] values become PostgreSQL bytea hex, not the JVM [B@ form (#1782)" + (is (= "\\xdeadbeef" + (#'sqlite/jdbc-value->str (byte-array [0xde 0xad 0xbe 0xef])))) + (is (= "\\x007f80ff" + (#'sqlite/jdbc-value->str (byte-array [0 127 -128 -1])))) + (is (= "\\x" (#'sqlite/jdbc-value->str (byte-array 0))))) + + (testing "other values keep their string form; nil stays nil" + (is (= "42" (#'sqlite/jdbc-value->str 42))) + (is (= "abc" (#'sqlite/jdbc-value->str "abc"))) + (is (nil? (#'sqlite/jdbc-value->str nil)))))