                    :or {full-sync? true}}]
   (binding [qp/*disable-qp-logging* true]
     (driver/sync-in-context driver @(:db table) (fn []
-                                                  ;; TODO: create/update/fetch table
                                                   (sync-database-active-tables! driver [table] :analyze? full-sync?)
                                                   (events/publish-event :table-sync {:table_id (:id table)})))))
 



(defn- sync-database-active-tables!
  "Sync active tables by running each of the sync table steps.
   Note that we want to completely finish each step for *all* tables before starting the next, since they depend on the results of the previous step.
   (e.g., `sync-table-fks!` can't run until all tables have finished `sync-table-active-fields-and-pks!`, since creating `ForeignKeys` to `Fields` of *other*
   Tables can't take place before they exist."
  [driver active-tables & {:keys [analyze?]
                           :or {analyze? true}}]
  (let [active-tables (sort-by :name active-tables)]
    ;; Do a first pass which does the bulk of the work
    (let [tables-count          (count active-tables)
          finished-tables-count (atom 0)]
      (doseq [table active-tables]
        (sync-database-active-table! driver table :analyze? analyze?)

        (swap! finished-tables-count inc)
        (log/debug (u/format-color 'magenta "%s Synced table '%s'." (sync-progress-meter-string @finished-tables-count tables-count) (:name table)))))

    ;; Second pass to sync FKs, which must take place after all other table info is in place
    (when (contains? (driver/features driver) :foreign-keys)
      (doseq [table active-tables]
        (u/try-apply sync-table-fks! driver table)))))


;; ## sync-table steps.

;; ### 1) update-table-display-name!

(defn- update-table-display-name!
  "Update the display_name of TABLE if it doesn't exist."
  [table]
  {:pre [(integer? (:id table))]}
  (try
    (when (nil? (:display_name table))
      (upd Table (:id table) :display_name (common/name->human-readable-name (:name table))))
    (catch Throwable e
      (log/error (u/format-color 'red "Unable to update display_name for %s: %s" (:name table) (.getMessage e))))))


;; ### 2) sync-table-active-fields-and-pks!

(def ^{:arglists '([field-def])}
infer-field-special-type

                             [table :refer [Table]])
            [metabase.util :as u]))

(declare mark-category-field-or-update-field-values!
         mark-json-field!
         mark-no-preview-display-field!
         mark-url-field!
