Skip to content
Snippets Groups Projects
Commit 35ccc43e authored by Cam Saül's avatar Cam Saül Committed by GitHub
Browse files

Merge pull request #3406 from metabase/fix-field-type-wonkiness

Fix field type wonkiness
parents f13be5c6 1fca900a
Branches
Tags
No related merge requests found
(ns metabase.db.migrations
"Clojure-land data migration definitions and fns for running them."
(:require [clojure.tools.logging :as log]
(:require [clojure.string :as s]
[clojure.tools.logging :as log]
(metabase [config :as config]
[db :as db]
[driver :as driver]
......@@ -258,10 +259,28 @@
(doseq [[_ t] old-base-type->new-type]
(assert (isa? (keyword t) :type/*))))
(defmigration migrate-base-types
;; migrate all of the old base + special types to the new ones.
;; This also takes care of any types that are already correct other than the fact that they're missing :type/ in the front.
;; This was a bug that existed for a bit in 0.20.0-SNAPSHOT but has since been corrected
(defmigration migrate-field-types
(doseq [[old-type new-type] old-special-type->new-type]
(db/update-where! 'Field {:special_type old-type}
;; migrate things like :timestamp_milliseconds -> :type/UNIXTimestampMilliseconds
(db/update-where! 'Field {:%lower.special_type (s/lower-case old-type)}
:special_type new-type)
;; migrate things like :UNIXTimestampMilliseconds -> :type/UNIXTimestampMilliseconds
(db/update-where! 'Field {:special_type (name (keyword new-type))}
:special_type new-type))
(doseq [[old-type new-type] old-base-type->new-type]
(db/update-where! 'Field {:base_type old-type}
;; migrate things like :DateTimeField -> :type/DateTime
(db/update-where! 'Field {:%lower.base_type (s/lower-case old-type)}
:base_type new-type)
;; migrate things like :DateTime -> :type/DateTime
(db/update-where! 'Field {:base_type (name (keyword new-type))}
:base_type new-type)))
;; if there were invalid field types in the database anywhere fix those so the new stricter validation logic doesn't blow up
(defmigration fix-invalid-field-types
(db/update-where! 'Field {:base_type [:not-like "type/%"]}
:base_type "type/*")
(db/update-where! 'Field {:special_type [:not-like "type/%"]}
:special_type nil))
......@@ -26,19 +26,22 @@
(i/defentity Field :metabase_field)
(defn- assert-valid-special-type [{special-type :special_type}]
(defn- check-valid-types [{base-type :base_type, special-type :special_type}]
(when base-type
(assert (isa? (keyword base-type) :type/*)
(str "Invalid base type: " base-type)))
(when special-type
(assert (isa? (keyword special-type) :type/*)
(str "Invalid special type: " special-type))))
(defn- pre-insert [field]
(assert-valid-special-type field)
(check-valid-types field)
(let [defaults {:display_name (humanization/name->human-readable-name (:name field))}]
(merge defaults field)))
(defn- pre-update [field]
(u/prog1 field
(assert-valid-special-type field)))
(check-valid-types field)))
(defn- pre-cascade-delete [{:keys [id]}]
(db/cascade-delete! Field :parent_id id)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment