Skip to content
Snippets Groups Projects
Unverified Commit 23ef0e20 authored by Chris Truter's avatar Chris Truter Committed by GitHub
Browse files

Be explicit about truncating characters versus bytes (#44876)

parent 074c78de
No related branches found
No related tags found
No related merge requests found
......@@ -958,13 +958,13 @@
nil)
(defmulti table-name-length-limit
"Return the maximum number of characters allowed in a table name, or `nil` if there is no limit."
"Return the maximum number of bytes allowed in a table name, or `nil` if there is no limit."
{:changelog-test/ignore true, :added "0.47.0", :arglists '([driver])}
dispatch-on-initialized-driver
:hierarchy #'hierarchy)
(defmulti column-name-length-limit
"Return the maximum number of characters allowed in a column name, or `nil` if there is no limit."
"Return the maximum number of bytes allowed in a column name, or `nil` if there is no limit."
{:changelog-test/ignore true, :added "0.49.19", :arglists '([driver])}
dispatch-on-initialized-driver
:hierarchy #'hierarchy)
......
......@@ -41,22 +41,22 @@
(set! *warn-on-reflection* true)
(def ^:private max-field-name-length
"This tracks the size of the metabase_field.name field"
(def ^:private max-field-name-bytes
"This tracks the size of the metabase_field.name field, in bytes."
254)
(def ^:private min-safe (fnil min Long/MAX_VALUE Long/MAX_VALUE))
(defn- max-column-bytes [driver]
(let [column-limit (some-> driver driver/column-name-length-limit)]
(min-safe column-limit max-field-name-length)))
(min-safe column-limit max-field-name-bytes)))
(defn- normalize-column-name
[driver raw-name]
(if (str/blank? raw-name)
"unnamed_column"
(u/slugify (str/trim raw-name)
;; since normalized names contain only ASCII characters, we can conflate bytes and length here.
;; since slugified names contain only ASCII characters, we can conflate bytes and length here.
{:max-length (max-column-bytes driver)})))
(def auto-pk-column-name
......@@ -108,6 +108,8 @@
[driver table-name]
(let [time-format "_yyyyMMddHHmmss"
slugified-name (or (u/slugify table-name) "")
;; since both the time-format and the slugified-name contain only ASCII characters, we can behave as if
;; [[driver/table-name-length-limit]] were defining a length in characters.
max-length (- (driver/table-name-length-limit driver) (count time-format))
acceptable-length (min (count slugified-name) max-length)
truncated-name-without-time (subs slugified-name 0 acceptable-length)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment