From 5db38a1419ac0c1d21c3ebf47d70e2e3392089d9 Mon Sep 17 00:00:00 2001
From: Cam Saul <cammsaul@gmail.com>
Date: Mon, 16 Jul 2018 12:17:32 -0700
Subject: [PATCH] Fix syncing of Mongo tables w/ all-NULL columns :wrench: [ci
 drivers]

---
 src/metabase/driver.clj                    |  3 ++-
 src/metabase/driver/mongo.clj              |  2 +-
 src/metabase/sync/interface.clj            |  2 +-
 src/metabase/sync/sync_metadata/fields.clj |  2 +-
 test/metabase/driver/mongo_test.clj        | 18 ++++++++++++++++++
 5 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index 6abe8089620..bdf893ba773 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -392,7 +392,8 @@
              [clojure.lang.IPersistentMap    :type/Dictionary]
              [clojure.lang.IPersistentVector :type/Array]
              [org.bson.types.ObjectId        :type/MongoBSONID]
-             [org.postgresql.util.PGobject   :type/*]])
+             [org.postgresql.util.PGobject   :type/*]
+             [nil                            :type/*]]) ; all-NULL columns in DBs like Mongo w/o explicit types
       (log/warn (trs "Don''t know how to map class ''{0}'' to a Field base_type, falling back to :type/*." klass))
       :type/*))
 
diff --git a/src/metabase/driver/mongo.clj b/src/metabase/driver/mongo.clj
index 45f504ea3bc..c886e27b1d6 100644
--- a/src/metabase/driver/mongo.clj
+++ b/src/metabase/driver/mongo.clj
@@ -116,7 +116,7 @@
 (defn- describe-table-field [field-kw field-info]
   (let [most-common-object-type (most-common-object-type (vec (:types field-info)))]
     (cond-> {:name          (name field-kw)
-             :database-type (.getName most-common-object-type)
+             :database-type (some-> most-common-object-type .getName)
              :base-type     (driver/class->base-type most-common-object-type)}
       (= :_id field-kw)           (assoc :pk? true)
       (:special-types field-info) (assoc :special-type (->> (vec (:special-types field-info))
diff --git a/src/metabase/sync/interface.clj b/src/metabase/sync/interface.clj
index 1ee51060d55..ade235b04f5 100644
--- a/src/metabase/sync/interface.clj
+++ b/src/metabase/sync/interface.clj
@@ -22,7 +22,7 @@
 (def TableMetadataField
   "Schema for a given Field as provided in `describe-table`."
   {:name                           su/NonBlankString
-   :database-type                  su/NonBlankString
+   :database-type                  (s/maybe su/NonBlankString) ; blank if the Field is all NULL & untyped, i.e. in Mongo
    :base-type                      su/FieldType
    (s/optional-key :special-type)  (s/maybe su/FieldType)
    (s/optional-key :pk?)           s/Bool
diff --git a/src/metabase/sync/sync_metadata/fields.clj b/src/metabase/sync/sync_metadata/fields.clj
index 1aa22346f01..9f4fd5e12ba 100644
--- a/src/metabase/sync/sync_metadata/fields.clj
+++ b/src/metabase/sync/sync_metadata/fields.clj
@@ -74,7 +74,7 @@
         {:table_id      (u/get-id table)
          :name          field-name
          :display_name  (humanization/name->human-readable-name field-name)
-         :database_type database-type
+         :database_type (or database-type "NULL") ; placeholder for Fields w/ no type info (e.g. Mongo) & all NULL
          :base_type     base-type
          :special_type  (special-type field)
          :parent_id     parent-id}))))
diff --git a/test/metabase/driver/mongo_test.clj b/test/metabase/driver/mongo_test.clj
index 4a346543db6..7c4b6fb1cd6 100644
--- a/test/metabase/driver/mongo_test.clj
+++ b/test/metabase/driver/mongo_test.clj
@@ -122,6 +122,24 @@
               :pk?           true}}}
   (driver/describe-table (MongoDriver.) (data/db) (Table (data/id :venues))))
 
+;; Make sure that all-NULL columns work are synced correctly (#6875)
+(i/def-database-definition ^:private all-null-columns
+  [["bird_species"
+     [{:field-name "name", :base-type :type/Text}
+      {:field-name "favorite_snack", :base-type :type/Text}]
+     [["House Finch" nil]
+      ["Mourning Dove" nil]]]])
+
+(datasets/expect-with-engine :mongo
+  [{:name "_id",            :database_type "java.lang.Long",   :base_type :type/Integer, :special_type :type/PK}
+   {:name "favorite_snack", :database_type "NULL",             :base_type :type/*,       :special_type nil}
+   {:name "name",           :database_type "java.lang.String", :base_type :type/Text,    :special_type :type/Name}]
+  (data/dataset metabase.driver.mongo-test/all-null-columns
+    (map (partial into {})
+         (db/select [Field :name :database_type :base_type :special_type]
+           :table_id (data/id :bird_species)
+           {:order-by [:name]}))))
+
 
 ;;; table-rows-sample
 (datasets/expect-with-engine :mongo
-- 
GitLab