Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Store user-set Table and Field values in metabase_table and metabase_field again (#86150)

Alexander Polyankin45d755df83f5
diff --git a/.claude/agents/drivers-and-sync-backend-expert.md b/.claude/agents/drivers-and-sync-backend-expert.md
index 259ade17209..448903355e5 100644
--- a/.claude/agents/drivers-and-sync-backend-expert.md
+++ b/.claude/agents/drivers-and-sync-backend-expert.md
@@ -46,7 +46,6 @@ Sync (OSS, `src/metabase/sync/`, see its `README.md`):
Models (OSS):
- `metabase.warehouses.models.database`, `metabase.warehouses.db` - Database.
- `metabase.warehouse-schema.models.{table,field,field-values,dimension,field-user-settings,table-user-settings}`, `metabase.warehouse-schema.field-values.distinct-batch`, `metabase.warehouse-schema.db`.
-- `metabase.warehouse-schema-overlay.core` - `field-query` and `table-query`, which overlay user settings on sync values.
- REST: the `warehouses-rest` and `warehouse-schema-rest` modules, each with its own `db` namespace.
## Invariants and landmines
@@ -57,7 +56,7 @@ Models (OSS):
- Call `driver.u/supports?`, not `driver/database-supports?` directly. It caches, logs, and survives a slow or throwing driver.
- Read connection details through `metabase.driver.connection/effective-details`, not `(:details db)`. Write, admin, and transform scopes pick other credentials and other pools.
- The pool cache keys each pool by `[database-id connection-type]` and checks it against a hash of the JDBC spec. A changed hash, a closed SSH tunnel, or an expired password invalidates the pool. Before it drops a pool, the cache re-fetches a stale Database instance from a long sync.
-- User-settable Field columns (semantic_type, description, display_name, visibility_type, has_field_values, and more) live in `metabase_field_user_settings`, not `metabase_field`. Read through `warehouse-schema-overlay/field-query`. Sync passes `{:user-settings? false}` to see its own values. Do not write user edits into the sync-owned row.
+- `metabase_field` and `metabase_table` hold what users see. `metabase_field_user_settings` and `metabase_table_user_settings` record which values users set; user edits go through the settings models' write functions, which write both rows. Sync reads the settings row before each update and leaves user-set columns alone.
- Sync is destructive. It deactivates tables missing from `describe-database`, and archives deactivated tables after a threshold. A driver that returns too few tables hides content.
- Field values: `*distinct-limit*` is 1000 rows and `*total-max-length*` caps total size. A field over the limit stops being `auto-list`. Advanced FieldValues (`:sandbox`, `:linked-filter`) expire after `advanced-field-values-max-age`.
- Lazy loading: core code must not require an external driver namespace. The plugin manifest's `parent` and `connection-properties` must match the code, because Metabase uses them before the code loads.
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index 6bbfe27ec28..177c4a13e06 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -75,7 +75,6 @@
:metabase/prefer-with-dynamic-fn-redefs {:level :warning}
:metabase/t2-query-namespace {:level :warning}
:metabase/t2-implicit-pk {:level :warning}
- :metabase/table-or-field-query {:level :warning}
;; App-DB query hygiene: a value reaching a value slot unmarked, and a query written as
;; `:column value` pairs rather than a map. Off by default and enabled per namespace under
;; `:config-in-ns`; flip it on globally once every `db.clj` has been converted.
@@ -973,12 +972,7 @@
:name agent-exit-namespaces}]
:config-in-ns
- {metabase.app-db.custom-migrations
- {:linters
- ;; a migration reads the rows as they were when it ran; applying today's user settings would make it drift
- {:metabase/table-or-field-query {:level :off}}}
-
- test-namespaces
+ {test-namespaces
{:linters
{:inline-def {:level :off}
:missing-docstring {:level :off}
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 7ea5572bff9..67fc46b2107 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -135,7 +135,6 @@
settings
users
util
- warehouse-schema-overlay
warehouses}
:model-exports #{:model/Action :model/ImplicitAction :model/QueryAction}
:model-imports #{:model/Card
@@ -180,8 +179,7 @@
models
permissions
util
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-imports #{:model/Card
:model/Collection
:model/Dashboard
@@ -226,8 +224,7 @@
system
task
util
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-imports #{:model/CacheConfig
:model/Card
:model/Collection
@@ -237,6 +234,7 @@
:model/Database
:model/DatabaseRouter
:model/Document
+ :model/Field
:model/ModerationReview
:model/PermissionsGroup
:model/Pulse
@@ -415,8 +413,7 @@
task
task.history
tracing
- util
- warehouse-schema-overlay}
+ util}
:model-exports #{:model/AuditLog}
:model-imports #{:model/ApiKey
:model/Card
@@ -659,8 +656,7 @@
transforms.feature-gating
upload
util
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-exports #{:model/Collection}
:model-imports #{:model/Action
:model/Card
@@ -697,9 +693,8 @@
premium-features
queries
request
- util
- warehouse-schema-overlay}
- :model-imports #{:model/Card :model/Collection}}
+ util}
+ :model-imports #{:model/Card :model/Collection :model/Table}}
comments
{:team "UX West"
@@ -907,7 +902,6 @@
request
sync
util
- warehouse-schema-overlay
warehouses.schema}
:model-imports #{:model/Database
:model/Dimension
@@ -953,8 +947,7 @@
search
settings
staleness
- util
- warehouse-schema-overlay}
+ util}
:model-exports #{:model/Document}
:model-imports #{:model/Card
:model/Collection
@@ -1008,7 +1001,6 @@
util
util.classloader
util.config
- warehouse-schema-overlay
warehouses
warehouses.schema}
:model-imports #{:model/Database :model/Field :model/Table}}
@@ -1043,8 +1035,7 @@
metabot
permissions
premium-features
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-imports #{:model/Card
:model/Collection
:model/ConnectionImpersonation
@@ -1289,8 +1280,7 @@
lib.schema
models
settings
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Card
:model/Database
:model/Dimension
@@ -1339,8 +1329,7 @@
lib.schema
lib.types
settings
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Card :model/Field :model/Table}}
lib.schema
@@ -1461,7 +1450,6 @@
users
util
util.config
- warehouse-schema-overlay
warehouses.schema}
:model-imports #{:model/AuthIdentity
:model/Card
@@ -1522,13 +1510,9 @@
search
users
util
- warehouse-schema-overlay
warehouses.schema}
:model-exports #{:model/Measure}
- :model-imports #{:model/Collection
- :model/Table
- :model/TableUserSettings
- :model/User}}
+ :model-imports #{:model/Collection :model/Table :model/User}}
metabot
{:team "Metabot"
@@ -1601,7 +1585,6 @@
transforms
util
util.config
- warehouse-schema-overlay
warehouses
xrays}
:model-exports #{:model/AiUsageLog
@@ -1697,8 +1680,7 @@
sql-tools
startup
util
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-imports #{:model/Card
:model/Database
:model/Field
@@ -1761,8 +1743,7 @@
query-processor
request
server
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Card :model/Field :model/Measure}}
mfa
@@ -1818,8 +1799,7 @@
lib.schema
parameters.schema
util
- util.classloader
- warehouse-schema-overlay}
+ util.classloader}
:model-imports #{:model/Action
:model/Card
:model/Channel
@@ -1986,7 +1966,6 @@
query-processor
util
util.classloader
- warehouse-schema-overlay
warehouses.schema}
:model-imports #{:model/Card
:model/Dashboard
@@ -2024,7 +2003,6 @@
users
util
util.config
- warehouse-schema-overlay
warehouses
warehouses.schema
enterprise/permissions}
@@ -2053,8 +2031,7 @@
premium-features
request
settings
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Card
:model/Dashboard
:model/DataPermissions
@@ -2241,7 +2218,6 @@
util
util.config
util.graph
- warehouse-schema-overlay
warehouses.schema}
:model-exports #{:model/Card
:model/ParameterCard
@@ -2263,6 +2239,7 @@
:model/NativeQuerySnippet
:model/Notification
:model/Revision
+ :model/Table
:model/Timeline
:model/User}}
@@ -2290,8 +2267,7 @@
request
revisions
search
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Action
:model/Card
:model/CardBookmark
@@ -2315,8 +2291,7 @@
permissions
query-processor
request
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Card :model/Field :model/Table}}
query-processor
@@ -2397,8 +2372,7 @@
tracing
users
util
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-imports #{:model/Card
:model/DashboardCardSeries
:model/Database
@@ -2597,8 +2571,7 @@
tracing
transforms.feature-gating
util
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-imports #{:model/Card
:model/Collection
:model/Dashboard
@@ -2639,13 +2612,9 @@
search
users
util
- warehouse-schema-overlay
warehouses.schema}
:model-exports #{:model/Segment}
- :model-imports #{:model/Collection
- :model/Table
- :model/TableUserSettings
- :model/User}}
+ :model-imports #{:model/Collection :model/Table :model/User}}
segments.rest
{:team "Graphy"
@@ -2803,8 +2772,7 @@
lib.schema
settings
sql-tools.parsing
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Table}}
sql-tools.parsing
@@ -2912,13 +2880,14 @@
tracing
util
util.config
- warehouse-schema-overlay
warehouses
warehouses.schema}
:model-imports #{:model/Database
:model/Field
+ :model/FieldUserSettings
:model/FieldValues
- :model/Table}}
+ :model/Table
+ :model/TableUserSettings}}
sync.analyze
{:team "Graphy"
@@ -3090,8 +3059,7 @@
transforms.base
transforms.feature-gating
transforms.indexes
- util
- warehouse-schema-overlay}
+ util}
:model-exports #{:model/Transform
:model/TransformDagRun
:model/TransformJob
@@ -3137,8 +3105,7 @@
query-processor
sync
transforms.indexes
- util
- warehouse-schema-overlay}}
+ util}}
transforms.feature-gating
{:team "Graphy"
@@ -3217,7 +3184,6 @@
settings
sync
util
- warehouse-schema-overlay
warehouses.schema}
:model-imports #{:model/Card
:model/Database
@@ -3240,8 +3206,7 @@
models
settings
task
- util
- warehouse-schema-overlay}}
+ util}}
user-key-value
{:team "UX West"
@@ -3349,15 +3314,6 @@
:api #{metabase.graph.core}
:uses #{util}}
- warehouse-schema-overlay
- {:team "UX West"
- :api #{metabase.warehouse-schema-overlay.core}
- :uses #{util}
- :model-imports #{:model/Field
- :model/FieldUserSettings
- :model/Table
- :model/TableUserSettings}}
-
warehouses
{:team "UX West"
:module-exports #{warehouses.schema
@@ -3379,7 +3335,6 @@
settings
sync
util
- warehouse-schema-overlay
warehouses.secrets}
:model-exports #{:model/Database}
:model-imports #{:model/Action
@@ -3417,7 +3372,6 @@
upload
util
util.config
- warehouse-schema-overlay
warehouses
warehouses.schema
warehouses.secrets}
@@ -3443,7 +3397,8 @@
metabase.warehouse-schema.models.table
metabase.warehouse-schema.models.table-user-settings
metabase.warehouse-schema.schema
- metabase.warehouse-schema.table}
+ metabase.warehouse-schema.table
+ metabase.warehouse-schema.user-settings}
:uses #{api
app-db
audit-app
@@ -3465,7 +3420,6 @@
settings
sync.analyze
util
- warehouse-schema-overlay
warehouses}
:model-exports #{:model/Dimension
:model/Field
@@ -3509,7 +3463,6 @@
sync
upload
util
- warehouse-schema-overlay
warehouses.schema
xrays}
:model-imports #{:model/Collection
@@ -3557,7 +3510,6 @@
settings
sync.analyze
util
- warehouse-schema-overlay
warehouses.schema}
:model-imports #{:model/Card
:model/Collection
@@ -3586,8 +3538,7 @@
models
query-processor
sync
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Database
:model/Field
:model/FieldValues
@@ -3686,7 +3637,6 @@
util
util.classloader
util.config
- warehouse-schema-overlay
warehouses
warehouses.schema
enterprise/serialization}
@@ -3902,7 +3852,6 @@
system
util
util.graph
- warehouse-schema-overlay
enterprise/dependencies
enterprise/serialization}
:model-imports #{:model/Action
@@ -3926,11 +3875,11 @@
permissions
premium-features
util
- warehouse-schema-overlay
warehouses.schema}
:model-imports #{:model/AuditLog
:model/Collection
:model/Dimension
+ :model/Field
:model/Table
:model/User}}
@@ -3990,7 +3939,6 @@
transforms.base
util
util.graph
- warehouse-schema-overlay
enterprise/permissions.sandbox}
:model-imports #{:model/Action
:model/Card
@@ -4039,7 +3987,6 @@
task
transforms
util
- warehouse-schema-overlay
warehouses.schema
enterprise/dependencies}
:model-imports #{:model/Card
@@ -4076,8 +4023,7 @@
collections
lib.schema
premium-features
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Card
:model/Collection
:model/Dashboard
@@ -4188,7 +4134,6 @@
task
util
util.config
- warehouse-schema-overlay
enterprise/search.embeddings
enterprise/search.semantic}
:model-imports #{:model/Card
@@ -4246,7 +4191,6 @@
query-processor
remote-sync
util
- warehouse-schema-overlay
warehouses
enterprise/permissions.impersonation}
:model-imports #{:model/ApplicationPermissionsRevision
@@ -4267,9 +4211,8 @@
models
permissions
query-processor
- util
- warehouse-schema-overlay}
- :model-imports #{:model/Card :model/User}}
+ util}
+ :model-imports #{:model/Card :model/Table :model/User}}
enterprise/permissions.impersonation
{:team "Security Engineering"
@@ -4318,7 +4261,6 @@
users
users.tenants
util
- warehouse-schema-overlay
warehouses
warehouses.schema
enterprise/api}
@@ -4362,7 +4304,7 @@
task
task.history
util
- warehouse-schema-overlay
+ warehouses.schema
enterprise/serialization
enterprise/transforms.python}
:model-exports #{:model/RemoteSyncTask}
@@ -4422,7 +4364,6 @@
search.entity-retrieval
task
util
- warehouse-schema-overlay
enterprise/search.semantic}
:model-imports #{:model/Card
:model/Measure
@@ -4462,8 +4403,7 @@
task
tracing
util
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-imports #{:model/Collection :model/Dashboard :model/Table}}
enterprise/security-center
@@ -4683,8 +4623,7 @@
tracing
transforms
transforms.base
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Database
:model/Field
:model/Table
@@ -4713,8 +4652,7 @@
transforms
transforms.base
util
- util.config
- warehouse-schema-overlay}
+ util.config}
:model-exports #{:model/PythonLibrary}
:model-imports #{:model/Database
:model/Field
@@ -4731,8 +4669,7 @@
permissions
premium-features
upload
- util
- warehouse-schema-overlay}
+ util}
:model-imports #{:model/Database :model/Table}}
enterprise/warehouses
@@ -4744,7 +4681,6 @@
models
permissions
util
- warehouse-schema-overlay
warehouses.schema}
:model-imports #{:model/Database :model/Field :model/Table}}}
diff --git a/.clj-kondo/ratchets.edn b/.clj-kondo/ratchets.edn
index 7592f83afdb..854b7734ea9 100644
--- a/.clj-kondo/ratchets.edn
+++ b/.clj-kondo/ratchets.edn
@@ -94,7 +94,6 @@
:metabase/discourage-dynamic-vars 1
:metabase/prefer-with-dynamic-fn-redefs 4
:metabase/t2-query-namespace 2
- :metabase/table-or-field-query 1
:metabase/unsafe-app-db-query 1
:missing-docstring 1
:private-call 1
diff --git a/.clj-kondo/src/hooks/metabase/toucan/db_ns.clj b/.clj-kondo/src/hooks/metabase/toucan/db_ns.clj
index fb4a65b5fe3..09606e553c2 100644
--- a/.clj-kondo/src/hooks/metabase/toucan/db_ns.clj
+++ b/.clj-kondo/src/hooks/metabase/toucan/db_ns.clj
@@ -461,7 +461,6 @@
:message (format "Application database query calls like `%s` must live in metabase[-enterprise].<module>.db (or metabase.driver.<driver>.db) namespaces"
(hooks/sexpr fn-node))
:type :metabase/t2-query-namespace))))
- ((requiring-resolve 'hooks.metabase.warehouse-schema-overlay.table-or-field-query/lint-read) input)
(when (and ns
(db-namespace? (modules/config input) ns)
(not (test-file? filename))
diff --git a/.clj-kondo/src/hooks/metabase/warehouse_schema_overlay/table_or_field_query.clj b/.clj-kondo/src/hooks/metabase/warehouse_schema_overlay/table_or_field_query.clj
deleted file mode 100644
index 1aadebe573e..00000000000
--- a/.clj-kondo/src/hooks/metabase/warehouse_schema_overlay/table_or_field_query.clj
+++ /dev/null
@@ -1,52 +0,0 @@
-(ns hooks.metabase.warehouse-schema-overlay.table-or-field-query
- "Lint that a query over `:model/Field` or `:model/Table` names its source with `field-query`/`table-query` from
- `metabase.warehouse-schema-overlay.core`, or reads sync's values with `{:user-settings? false}`."
- (:require
- [clj-kondo.hooks-api :as hooks]))
-
-(def ^:private read-fn?
- "The Toucan 2 query functions that read rows. Writes are not affected: they go to `metabase_field` itself."
- #{"select" "select-one" "select-fn->fn" "select-fn->pk" "select-fn-reducible" "select-fn-set" "select-fn-vec"
- "select-one-fn" "select-one-pk" "select-pk->fn" "select-pks-set" "select-pks-vec" "select-reducible"
- "reducible-select" "reducible-query" "query" "query-one" "count" "exists?"})
-
-(def ^:private model->helper
- {":model/Field" "field-query"
- ":model/Table" "table-query"})
-
-(defn- test-file? [filename]
- (boolean (and filename (re-find #"(?:^|/)test/" filename))))
-
-(defn- tokens
- "Every token in `node`'s tree, as strings."
- [node]
- (tree-seq :children :children node))
-
-(defn- mentions?
- [node pred]
- (boolean (some (fn [n] (when-let [s (some-> (hooks/sexpr n) pr-str)] (pred s)))
- (filter (complement :children) (tokens node)))))
-
-(defn- writes?
- "Whether a `t2/query` form is really a write spelled as a query."
- [node]
- (mentions? node #{":update" ":delete-from" ":insert-into"}))
-
-(defn lint-read
- "Register a `:metabase/table-or-field-query` finding when a read of `:model/Field` or `:model/Table` does not name
- its source with the matching helper."
- [{:keys [node filename]}]
- (let [fn-node (first (:children node))
- fn-name (some-> (hooks/sexpr fn-node) name)]
- (when (and fn-name
- (read-fn? fn-name)
- (not (test-file? filename))
- (not (writes? node)))
- (doseq [[model helper] model->helper
- :when (mentions? node #(= % model))
- :when (not (mentions? node #(re-find (re-pattern (str "\\b" helper "$")) %)))]
- (hooks/reg-finding!
- (assoc (meta fn-node)
- :message (format "A read of %s must name its source: add {:from [(%s)]} (or {:user-settings? false} for sync's own values)"
- model helper)
- :type :metabase/table-or-field-query))))))
diff --git a/dev/test/metabase/core/table_or_field_raw_usage_test.clj b/dev/test/metabase/core/table_or_field_raw_usage_test.clj
deleted file mode 100644
index 21d8ca6a02e..00000000000
--- a/dev/test/metabase/core/table_or_field_raw_usage_test.clj
+++ /dev/null
@@ -1,188 +0,0 @@
-(ns metabase.core.table-or-field-raw-usage-test
- "Guards against hand-written Honey SQL that names `metabase_table`/`metabase_field` directly.
-
- `metabase.warehouse-schema-overlay.core/table-query` and `/field-query` are the source a query over
- `:model/Table`/`:model/Field` must read from, so that a user's Field values (`metabase_field_user_settings`) and a
- workspace's table remapping (`workspace_table_remapping`) are applied. The `:metabase/table-or-field-query` clj-kondo
- linter (`.clj-kondo/src/hooks/metabase/warehouse_schema_overlay/table_or_field_query.clj`) already catches this for
- `t2/select`-style calls naming `:model/Table`/`:model/Field`, but it only fires on those Toucan 2 query functions.
- Hand-written Honey SQL that instead names the raw table with the literal keyword `:metabase_table`/`:metabase_field`
- -- in a `:from` or a join -- sails right past it; clj-kondo has no `:discouraged-keyword` linter to catch a bare
- keyword literal irrespective of the function it's passed to (verified against the pinned 2026.07.24 build: only
- `:discouraged-var` and `:discouraged-namespace` exist, both keyed on a resolved var/namespace symbol, not a keyword
- value), and a hook can't fire on a keyword sitting in arbitrary data with no distinguishing enclosing call. Hence
- this repo-level test instead, in the style of [[metabase.core.modules-test]]."
- (:require
- [clojure.java.io :as io]
- [clojure.string :as str]
- [clojure.test :refer :all]))
-
-(set! *warn-on-reflection* true)
-
-(def ^:private scanned-roots
- "Where hand-written application-database Honey SQL lives. Driver modules are globbed since each has its own
- `src` directory. `.clj-kondo/` (which spells out both keywords in the hook this test backs up) is simply never
- one of these roots, so it needs no explicit exclusion."
- (into ["src" "enterprise/backend/src"]
- (comp (filter #(.isDirectory ^java.io.File %))
- (map #(str "modules/drivers/" (.getName ^java.io.File %) "/src"))
- (filter #(.isDirectory (io/file %))))
- (some-> (io/file "modules/drivers") .listFiles)))
-
-(def ^:private excluded-paths
- "Whole files/directories the scan never reports on, because a hit there isn't the linter's business:
-
- - `metabase.warehouse-schema-overlay.core` itself builds `table-query`/`field-query` by naming the raw tables --
- that's the one place allowed to.
- - `metabase.sync.*` is sync's own read/write path: sync is the thing that writes `metabase_table`/`metabase_field`,
- so it always wants the raw columns, and a separate change owns this namespace right now.
- - Liquibase/custom migrations run against whatever shape the app DB had at that migration's version, before (or
- long after) `field-query`/`table-query` existed; they are not reads a *current* user sees rendered anywhere."
- #{"src/metabase/warehouse_schema_overlay/core.clj"})
-
-(defn- excluded-dir?
- [path]
- (or (str/starts-with? path "src/metabase/sync/")
- (str/includes? path "custom_migrations")))
-
-(def ^:private allowed-raw-usages
- "Files where a `:metabase_table`/`:metabase_field` literal survives review: every hit in the file was read back and
- confirmed to select only columns `table-query`/`field-query` don't touch (an id, a count, a foreign key used purely
- to join or filter), or the file has its own structural reason to want the raw row. Note that a Table's
- `display_name`, `description`, `is_published` and `collection_id` are user values like a Field's -- a rationale
- resting on those being sync-owned no longer holds. Add an entry here only after
- checking every hit it covers -- see the check's failure message for the two helpers this is standing in for.
-
- Keep this in sync with what the scan actually finds: [[stale-allowed-raw-usage-test]] fails on an entry that no
- longer matches anything, same as clj-kondo's ignore ratchets flag a stale exemption."
- {"src/metabase/usage_metadata/db.clj"
- "raw-field-fingerprint selects only :fingerprint, a sync-owned column with no user override."
-
- "src/metabase/collections/children.clj"
- "the :model case joins metabase_table only for :is_upload, which is sync-owned."
-
- "src/metabase/queries/db.clj"
- "field-database-info-for-ids reads the Field's own raw :name (sync-owned, not user-settable); its Table join is
- already routed through table-query."
-
- "src/metabase/models/db.clj"
- "field-in-path-query is a nested id-only lookup used as a :where filter, not a value read."
-
- "src/metabase/warehouses_rest/db.clj"
- "delete-field-values-for-database! selects only :f.id inside a DELETE's subquery; database-usage-counts only
- counts rows. autocomplete-fields' Table join now goes through table-query."
-
- "src/metabase/parameters/db.clj"
- "both joins bring in the Table only to filter (.active, .db_id) or project its :id; neither selects
- :schema/:name."
-
- "enterprise/backend/src/metabase_enterprise/dependencies/db.clj"
- "the remaining hits select only :id, to build permission and dependency filters. The two that read a Table's
- display_name -- a user value -- now go through table-query."
-
- "enterprise/backend/src/metabase_enterprise/remote_sync/db.clj"
- "remote sync matches and tracks content by its actual physical (db, schema, table) location across instances --
- overlaying the workspace-remapped name would work against the one thing this module does."
-
- "enterprise/backend/src/metabase_enterprise/data_complexity_score/db.clj"
- "active-field-counts-by-table only counts active Fields per table; no Field/Table content column is read."})
-
-(defn- source-file?
- [^java.io.File file]
- (and (.isFile file)
- (let [name (.getName file)]
- (or (str/ends-with? name ".clj") (str/ends-with? name ".cljc")))))
-
-(defn- source-files
- []
- (for [root scanned-roots
- ^java.io.File file (file-seq (io/file root))
- :when (source-file? file)]
- (.getPath file)))
-
-(defn- strip-strings-and-comments
- "Blank out string-literal and line-comment content in `text`, keeping newlines so line numbers still line up.
- A naive keyword scan over raw source text would otherwise flag docstrings and comments that merely *mention*
- `:metabase_table`/`:metabase_field` (see e.g. `metabase.warehouse-schema.schema`'s column-list docstrings)."
- [^String text]
- (let [n (.length text)
- sb (StringBuilder. n)]
- (loop [i 0, in-str? false]
- (if (>= i n)
- (str sb)
- (let [c (.charAt text i)]
- (cond
- (and in-str? (= c \\) (< (inc i) n))
- (do (.append sb (if (= (.charAt text (inc i)) \newline) "\n " " "))
- (recur (+ i 2) true))
-
- (= c \")
- (do (.append sb \space)
- (recur (inc i) (not in-str?)))
-
- in-str?
- (do (.append sb (if (= c \newline) \newline \space))
- (recur (inc i) true))
-
- (= c \;)
- (let [nl (long (or (str/index-of text "\n" i) n))]
- (.append sb (apply str (repeat (- nl i) \space)))
- (recur nl false))
-
- :else
- (do (.append sb c)
- (recur (inc i) false))))))))
-
-(def ^:private raw-usage-pattern
- "A `:metabase_table`/`:metabase_field` keyword literal naming a table -- as opposed to `:metabase_field.column` or
- `:metabase_field/column`, which qualify a column of a source named elsewhere (often `table-query`/`field-query`'s
- own default alias, which is the raw table name)."
- #":metabase_(?:table|field)(?![./\w])")
-
-(defn- raw-usage-hits
- "Every `{:file :line :text}` where `path`'s code (not its strings or comments) uses the literal keyword to name a
- table, skipping `t2/table-name` definitions -- the canonical place the keyword has to be spelled out."
- [path]
- (let [text (slurp path)
- code-only (strip-strings-and-comments text)
- lines (str/split-lines text)
- code-lines (str/split-lines code-only)]
- (into []
- (keep (fn [[line-no orig code]]
- (when (and (re-find raw-usage-pattern code)
- (not (str/includes? orig "t2/table-name")))
- {:file path, :line line-no, :text (str/trim orig)})))
- (map vector (rest (range)) lines code-lines))))
-
-(defn- relative-path
- [path]
- (str/replace path #"^\./" ""))
-
-(defn- all-hits
- []
- (into []
- (mapcat raw-usage-hits)
- (remove excluded-dir? (map relative-path (source-files)))))
-
-(defn- excused?
- [file]
- (boolean (or (contains? excluded-paths file)
- (contains? allowed-raw-usages file))))
-
-(deftest no-raw-metabase-table-or-field-honey-sql-test
- (testing (str "Hand-written Honey SQL must not name `:metabase_table`/`:metabase_field` directly.\n"
- "Read from `(metabase.warehouse-schema-overlay.core/table-query)` / `(field-query)` instead"
- " (or add `{:alias ...}` for a join), or pass `{:user-settings? false}` for a deliberate raw read.\n"
- "If the hit really is fine as-is (only an id, a count, or a foreign key used to join/filter), review"
- " it and add it to `allowed-raw-usages` in this test with a one-line reason.")
- (doseq [{:keys [file line text]} (all-hits)]
- (testing (format "\n%s:%d\n %s" file line text)
- (is (excused? file) "unexpected raw metabase_table/metabase_field usage (see message above)")))))
-
-(deftest stale-allowed-raw-usage-test
- (testing "every allowed-raw-usages entry still matches at least one hit, so the allowlist doesn't accumulate cruft"
- (let [files-with-hits (into #{} (map :file) (all-hits))]
- (doseq [file (keys allowed-raw-usages)]
- (testing (format "\n%s" file)
- (is (contains? files-with-hits file)
- "no raw metabase_table/metabase_field usage found here anymore -- remove this allowlist entry"))))))
diff --git a/docs/developers-guide/api-changelog.md b/docs/developers-guide/api-changelog.md
index 75e5094d466..f51f92c1e7d 100644
--- a/docs/developers-guide/api-changelog.md
+++ b/docs/developers-guide/api-changelog.md
@@ -19,6 +19,11 @@ title: API changelog
- `POST /api/ee/serialization/export` accepts a `dirname` of 1-255 characters from `A-Z a-z 0-9 _ - . %`
that does not start with `.` or `-`. Other values get `400 Bad Request`.
+- `PUT /api/field/:id` no longer accepts `nfc_path`, which only sync sets, and answers `null` for `has_field_values`,
+ `display_name`, `visibility_type` or `json_unfolding` with `400 Bad Request`. Leave a key out to keep its value.
+ `PUT /api/table/:id`, `PUT /api/table` and the Data Studio bulk table edit answer `data_authority: null` the same
+ way.
+
## Metabase 0.65.0
- `POST /api/transform` and `PUT /api/transform/:id` answer a target table that already exists with
diff --git a/enterprise/backend/src/metabase_enterprise/action_v2/db.clj b/enterprise/backend/src/metabase_enterprise/action_v2/db.clj
index 0e0ad9ab67d..c3c9154af30 100644
--- a/enterprise/backend/src/metabase_enterprise/action_v2/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/action_v2/db.clj
@@ -7,18 +7,17 @@
[metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn active-table
"The active Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one :model/Table :id table-id :active true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id :active true))
(mu/defn database
"The Database with `database-id`, or nil."
@@ -28,29 +27,28 @@
(mu/defn fields
"The Fields with `field-ids`."
[field-ids :- [:sequential ::lib.schema.id/field]]
- (t2/select :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :id [:in field-ids]))
(mu/defn pk-fields-for-table
"The active primary key Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select :model/Field :table_id table-id :semantic_type :type/PK :active true {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :table_id table-id :semantic_type :type/PK :active true))
(mu/defn fields-by-name
"The Fields of the Table with `table-id` named one of `field-names`."
[table-id :- ::lib.schema.id/table
field-names :- [:sequential :string]]
- (t2/select :model/Field :table_id table-id :name [:in field-names] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :table_id table-id :name [:in field-names]))
(mu/defn active-fields-in-position-order
"The active Fields of the Table with `table-id`, in position order."
[table-id :- ::lib.schema.id/table]
- (t2/select :model/Field :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query)]
- :order-by [[:position]]}))
+ (t2/select :model/Field :table_id table-id :active true {:order-by [[:position]]}))
(mu/defn field-requirements-by-name
"A map of name to the name, required flag, and base type of the Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-fn->fn :name identity [:model/Field :name :database_required :base_type] :table_id table-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-fn->fn :name identity [:model/Field :name :database_required :base_type] :table_id table-id))
(mu/defn category-list-field-ids-by-name
"The `:id` and `:lower_name` rows of the category list Fields of the Table with `table-id` whose name matches one
@@ -58,7 +56,7 @@
[table-id :- ::lib.schema.id/table
names :- [:sequential :string]]
(t2/query {:select [:id [[:lower :name] :lower_name]]
- :from [(warehouse-schema-overlay/field-query)]
+ :from [(t2/table-name :model/Field)]
:where [:and
[:= :table_id table-id]
[:in [:lower :name] (map u/lower-case-en names)]
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/db.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/db.clj
index 410e640a766..8059b918c8e 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/db.clj
@@ -6,13 +6,12 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn group-manager?
"Whether the User with `user-id` manages the PermissionsGroup with `group-id`."
@@ -43,7 +42,7 @@
database-ids :- [:sequential ::lib.schema.id/database]]
(t2/query {:select [[:t.db_id :db_id]]
:from [[(t2/table-name :model/Sandbox) :s]]
- :join [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})
+ :join [[(t2/table-name :model/Table) :t]
[:= :s.table_id :t.id]]
:where [:and
[:= :s.group_id group-id]
@@ -85,7 +84,7 @@
group-ids :- [:sequential ms/PositiveInt]]
(t2/query {:select [[:s.group_id :group_id]]
:from [[(t2/table-name :model/Sandbox) :s]]
- :join [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})
+ :join [[(t2/table-name :model/Table) :t]
[:= :t.id :s.table_id]]
:where [:and
[:in :s.group_id group-ids]
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/db.clj b/enterprise/backend/src/metabase_enterprise/audit_app/db.clj
index ce59d6c9584..465959be02a 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/db.clj
@@ -6,7 +6,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.queries.schema :as queries.schema]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.schema :as warehouse-schema.schema]
[metabase.warehouses.schema :as warehouses.schema]
[toucan2.core :as t2]))
@@ -57,18 +56,17 @@
"The Tables of the Database with `database-id` named one of `table-names`."
[database-id :- ::lib.schema.id/database
table-names :- [:or [:set :string] [:sequential :string]]]
- (t2/select :model/Table :db_id database-id :name [:in table-names] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :db_id database-id :name [:in table-names]))
(mu/defn tables-of-database-in-id-order
"The `:id`, `:name`, `:schema`, and `:active` of the Tables of the Database with `database-id`, in ID order."
[database-id :- ::lib.schema.id/database]
- (t2/select [:model/Table :id :name :schema :active] :db_id database-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]
- :order-by [[:id :asc]]}))
+ (t2/select [:model/Table :id :name :schema :active] :db_id database-id {:order-by [[:id :asc]]}))
(mu/defn active-public-table-exists?
"Whether the Database with `database-id` has an active Table in the `public` schema."
[database-id :- ::lib.schema.id/database]
- (t2/exists? :model/Table :db_id database-id :schema "public" :active true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/exists? :model/Table :db_id database-id :schema "public" :active true))
(mu/defn table-ids-to-downcase
"The IDs of the Metabase-managed Tables of the Database with `database-id` that have no lower-cased counterpart
@@ -76,7 +74,7 @@
[database-id :- ::lib.schema.id/database]
(mapv :id
(t2/query {:select [:table.id]
- :from [(warehouse-schema-overlay/table-query {:alias :table, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :table]]
:where [:and [:= :table.db_id database-id]
;; Exclude DATABASECHANGELOG, DATABASECHANGELOGLOCK, and QRTZ_* tables, they are not metabase managed
[:not= :table.name "DATABASECHANGELOG"]
@@ -85,7 +83,7 @@
[:not= :table.name "DATABASECHANGELOG_FIRST_RUN"]
[:not [:like :table.name "QRTZ_%"]]
[:not [:exists ^:allow-subquery {:select [1]
- :from [(warehouse-schema-overlay/table-query {:alias :self_table, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :self_table]]
:where [:and
[:= :self_table.db_id :table.db_id]
[:or
@@ -106,8 +104,8 @@
[database-id :- ::lib.schema.id/database]
(mapv :id
(t2/query {:select [:field.id]
- :from [(warehouse-schema-overlay/field-query {:alias :field})]
- :inner-join [(warehouse-schema-overlay/table-query {:alias :table})
+ :from [[(t2/table-name :model/Field) :field]]
+ :inner-join [[(t2/table-name :model/Table) :table]
[:= :table.id :field.table_id]]
:where [:and [:= :table.db_id database-id]
[:not= :table.name "DATABASECHANGELOG"]
@@ -115,8 +113,8 @@
[:not= :table.name "DATABASECHANGELOG_FIRST_RUN"]
[:not [:like :table.name "QRTZ_%"]]
[:not [:exists ^:allow-subquery {:select [1]
- :from [(warehouse-schema-overlay/field-query {:alias :self_field})]
- :inner-join [(warehouse-schema-overlay/table-query {:alias :self_table})
+ :from [[(t2/table-name :model/Field) :self_field]]
+ :inner-join [[(t2/table-name :model/Table) :self_table]
[:= :self_table.id :self_field.table_id]]
:where [:and
[:= :self_table.db_id :table.db_id]
@@ -167,7 +165,7 @@
(mu/defn field-names-of-table
"The `:id` and `:name` of the Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select [:model/Field :id :name] :table_id table-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select [:model/Field :id :name] :table_id table-id))
(mu/defn card-result-metadata-reducible
"Reducible raw `:id` and `:result_metadata` rows of the Cards of the Database with `database-id`."
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj b/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj
index eec624b9c9b..637bccbd69a 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj
@@ -6,7 +6,7 @@
[metabase-enterprise.audit-app.pages.common.cards :as cards]
[metabase.app-db.core :as mdb]
[metabase.audit-app.core :as audit]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]))
+ [toucan2.core :as t2]))
;; List of all failing questions
(defmethod audit.i/internal-query ::bad-table
@@ -76,7 +76,7 @@
:from [[:report_card :card]]
:left-join [[:collection :coll] [:= :card.collection_id :coll.id]
[:metabase_database :db] [:= :card.database_id :db.id]
- (warehouse-schema-overlay/table-query {:alias :t}) [:= :card.table_id :t.id]
+ [(t2/table-name :model/Table) :t] [:= :card.table_id :t.id]
[:core_user :u] [:= :card.creator_id :u.id]
:latest_qe [:= :card.id :latest_qe.card_id]
:query_runs [:= :card.id :query_runs.card_id]
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
index 9a363d5fae5..db3af343b28 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
@@ -9,7 +9,6 @@
[metabase.queries.core :as queries]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(def non-blob-columns
@@ -154,15 +153,14 @@
[table-ids :- [:set ::lib.schema.id/table]]
(if (seq table-ids)
(t2/select :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and [:= :active true] [:in :id table-ids]]
+ {:where [:and [:= :active true] [:in :id table-ids]]
:order-by [[:name :asc] [:id :asc]]})
[]))
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one :model/Table {:from [(warehouse-schema-overlay/table-query)] :where [:= :id table-id]}))
+ (t2/select-one :model/Table {:where [:= :id table-id]}))
(mu/defn resource-cards
"The unarchived Cards in the collection with `collection-id`, in name then id order."
@@ -214,7 +212,7 @@
(mu/defn field-ids-and-table-ids
"The id and Table id of the Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select [:model/Field :id :table_id] :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select [:model/Field :id :table_id] :id [:in field-ids]))
(mu/defn card-dimensions
"The query-relevant columns (`:dimensions` and `:dimension_mappings` among them) of the Card with `card-id`,
@@ -225,20 +223,19 @@
(mu/defn table-names
"The id, database id, name, and display name of the Tables with `table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/select [:model/Table :id :db_id :name :display_name] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select [:model/Table :id :db_id :name :display_name] :id [:in table-ids]))
(mu/defn field-table-id
"The Table id of the Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :table_id :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :table_id :model/Field :id field-id))
(mu/defn published-tables-in-collections
"The active, published Tables in `collection-ids`, in name then id order."
[collection-ids :- [:set ::lib.schema.id/collection]]
(if (seq collection-ids)
(t2/select :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ {:where [:and
[:= :active true]
[:= :is_published true]
[:in :collection_id collection-ids]]
diff --git a/enterprise/backend/src/metabase_enterprise/data_complexity_score/db.clj b/enterprise/backend/src/metabase_enterprise/data_complexity_score/db.clj
index c5eae3c0024..34448af9e7a 100644
--- a/enterprise/backend/src/metabase_enterprise/data_complexity_score/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_complexity_score/db.clj
@@ -10,7 +10,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn active-field-counts-by-table
@@ -65,7 +64,7 @@
[audit-database-id :- ::lib.schema.id/database]
(t2/select [:model/Table :id :name :collection_id :is_published :visibility_type :db_id :data_layer :data_authority]
:active true
- :db_id [:not= audit-database-id] {:from [(warehouse-schema-overlay/table-query)]}))
+ :db_id [:not= audit-database-id]))
(mu/defn metabot-by-entity-id
"The Metabot with `entity-id`, or nil."
diff --git a/enterprise/backend/src/metabase_enterprise/data_studio/db.clj b/enterprise/backend/src/metabase_enterprise/data_studio/db.clj
index 0175e7c6d78..6a8287d57df 100644
--- a/enterprise/backend/src/metabase_enterprise/data_studio/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_studio/db.clj
@@ -7,7 +7,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.models.table-user-settings :as schema.table-user-settings]
[toucan2.core :as t2]))
@@ -52,9 +51,9 @@
[:not [:in output-table-id tables]])]
(t2/reducible-query {:select [[output-table-id :table_id]]
:from [[(t2/table-name :model/Dimension) :dim]]
- :join [(warehouse-schema-overlay/field-query {:alias :source_field, :user-settings? false})
+ :join [[(t2/table-name :model/Field) :source_field]
[:= :dim.field_id :source_field.id]
- (warehouse-schema-overlay/field-query {:alias :target_field, :user-settings? false})
+ [(t2/table-name :model/Field) :target_field]
[:= :dim.human_readable_field_id :target_field.id]]
:where [:and
[:= :dim.type "external"]
@@ -68,8 +67,7 @@
extra-table-ids :- [:maybe [:set ::lib.schema.id/table]]
extra-mode :- [:enum :unpublished :any]]
(t2/select-pks-set :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :where (let [selector-expr (table-selectors-expr selectors)]
+ {:where (let [selector-expr (table-selectors-expr selectors)]
(if (seq extra-table-ids)
[:or selector-expr (case extra-mode
:unpublished [:and [:in :id extra-table-ids] [:= :is_published false]]
@@ -79,12 +77,12 @@
(mu/defn published-table-ids
"The IDs of the published Tables among `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select-pks-set :model/Table :id [:in table-ids] :is_published true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-pks-set :model/Table :id [:in table-ids] :is_published true))
(mu/defn tables
"The Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
(mu/defn collection
"The Collection with `collection-id`, or nil."
@@ -109,13 +107,13 @@
"Publish the Tables with `table-ids` into the Collection with `collection-id`, returning the number updated."
[table-ids :- [:set ::lib.schema.id/table]
collection-id :- ::lib.schema.id/collection]
- (schema.table-user-settings/upsert-user-settings-for-tables!
+ (schema.table-user-settings/upsert-table-user-settings-for-tables!
table-ids {:collection_id collection-id, :is_published true}))
(mu/defn unpublish-tables!
"Unpublish the Tables with `table-ids` and detach them from their Collection, returning the number updated."
[table-ids :- [:set ::lib.schema.id/table]]
- (schema.table-user-settings/upsert-user-settings-for-tables!
+ (schema.table-user-settings/upsert-table-user-settings-for-tables!
table-ids {:collection_id nil, :is_published false}))
(mu/defn published-table-visible-to-user?
@@ -124,8 +122,7 @@
user-id :- ::lib.schema.id/user
superuser? :- :boolean]
(t2/exists? :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ {:where [:and
[:= :id table-id]
[:= :is_published true]
(collection/visible-collection-filter-clause
@@ -136,8 +133,7 @@
"Whether the current user can read the Collection of any published Table."
[]
(t2/exists? :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ {:where [:and
[:= :is_published true]
(collection/visible-collection-filter-clause :collection_id)]}))
@@ -145,8 +141,7 @@
"Whether the current user can read the Collection of any published Table in the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
(t2/exists? :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ {:where [:and
[:= :db_id database-id]
[:= :is_published true]
(collection/visible-collection-filter-clause :collection_id)]}))
diff --git a/enterprise/backend/src/metabase_enterprise/data_studio/permissions/published_tables.clj b/enterprise/backend/src/metabase_enterprise/data_studio/permissions/published_tables.clj
index f416ea99b01..6a3a4fee0b3 100644
--- a/enterprise/backend/src/metabase_enterprise/data_studio/permissions/published_tables.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_studio/permissions/published_tables.clj
@@ -8,7 +8,7 @@
[metabase.permissions.core :as perms]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.util.honey-sql-2 :as h2x]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]))
+ [toucan2.core :as t2]))
(defenterprise user-published-table-permission
"Returns :query-builder permission if table is published and user has collection access.
@@ -52,7 +52,7 @@
{:select [[:mt.id :id]
[(h2x/literal :perms/create-queries) :perm_type]
[(h2x/literal :query-builder) :perm_value]]
- :from [(warehouse-schema-overlay/table-query {:alias :mt})]
+ :from [[(t2/table-name :model/Table) :mt]]
:where (cond-> [:and
[:= :mt.is_published true]
(collection/visible-collection-filter-clause
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/db.clj b/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
index fa0637c04b1..d88966d0d6c 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
@@ -19,7 +19,6 @@
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(def ^:private Column
@@ -342,7 +341,7 @@
nil))]
{:table-name (case entity-type
:card :report_card
- :table (first (warehouse-schema-overlay/table-query))
+ :table (t2/table-name :model/Table)
:transform :transform
:snippet :native_query_snippet
:dashboard :report_dashboard
@@ -515,7 +514,7 @@
(:collection joins) (conj :collection [:= :entity.collection_id :collection.id])
(:dashboard joins) (conj [:report_dashboard :dashboard] [:= :entity.dashboard_id :dashboard.id])
(:document joins) (conj :document [:= :entity.document_id :document.id])
- (:table joins) (conj (warehouse-schema-overlay/table-query {:alias :table}) [:= :entity.table_id :table.id])))
+ (:table joins) (conj [(t2/table-name :model/Table) :table] [:= :entity.table_id :table.id])))
(mu/defn- dependency-item-select :- ::h2x/honeysql-clause-opts
"The per-entity-type SELECT that [[dependency-item-ids]] and [[dependency-item-count]] union together, matching
@@ -639,21 +638,13 @@
id :- ::deps.dependency-types/entity-id]
(first (entities entity-type #{id})))
-(mu/defn- entity-source :- [:map {:closed true}
- [:from {:optional true} [:sequential HoneySQLForm]]]
- "What a read of `entity-type` selects from: the overlay for a Table, else the model's own table."
- [entity-type :- ::deps.dependency-types/dependency-types]
- (if (= entity-type :table)
- {:from [(warehouse-schema-overlay/table-query)]}
- {}))
-
(mu/defn entities-with-columns
"The `columns` of the entities of the entity type `entity-type` with `ids`."
[entity-type :- ::deps.dependency-types/dependency-types
columns :- [:sequential :keyword]
ids :- [:sequential ::deps.dependency-types/entity-id]]
(t2/select (into [(deps.dependency-types/dependency-type->model entity-type)] columns)
- :id [:in ids] (entity-source entity-type)))
+ :id [:in ids]))
(mu/defn entity-with-columns
"The `columns` of the entity of the entity type `entity-type` with `id`, or nil."
@@ -661,7 +652,7 @@
columns :- [:sequential :keyword]
id :- ::deps.dependency-types/entity-id]
(t2/select-one (into [(deps.dependency-types/dependency-type->model entity-type)] columns)
- :id id (entity-source entity-type)))
+ :id id))
(mu/defn implicit-action-table-ids
"The `:table_id` of the ImplicitActions of the Actions with `action-ids`, keyed by Action id."
@@ -692,7 +683,7 @@
(mu/defn tables
"The Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
(mu/defn dashcard-dashboard-id :- [:maybe ::lib.schema.id/dashboard]
"The `:dashboard_id` of the DashboardCard with `dashcard-id`, or nil."
@@ -702,7 +693,7 @@
(mu/defn field-table-id :- [:maybe ::lib.schema.id/table]
"The `:table_id` of the Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :table_id :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :table_id :model/Field :id field-id))
(mu/defn source-database-transform-ids :- [:set ::lib.schema.id/transform]
"The IDs of the Transforms reading from the Database with `db-id`."
@@ -1079,7 +1070,7 @@
"The IDs of the Tables of the Fields remapped to one of the Fields with `field-ids` as their human-readable field."
[field-ids :- [:set ::lib.schema.id/field]]
(or (t2/select-fn-set :table_id [:model/Field :metabase_field.table_id]
- {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]
+ {:from [(t2/table-name :model/Field)]
:join [:dimension [:= :dimension.field_id :metabase_field.id]]
:where [:in :dimension.human_readable_field_id field-ids]})
#{}))
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/hooks.clj b/enterprise/backend/src/metabase_enterprise/dependencies/hooks.clj
index db82d3d3229..3b53e711d6d 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/hooks.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/hooks.clj
@@ -133,7 +133,7 @@
:model/ImplicitAction #{:table_id}
:model/Field field-columns
:model/TableUserSettings #{:display_name :visibility_type :data_layer :field_order}
- :model/FieldUserSettings (conj field-columns :custom_position :nfc_path :json_unfolding)})
+ :model/FieldUserSettings (conj field-columns :custom_position :json_unfolding)})
(mu/defn- mark-dependents!
"Mark the result metadata and analysis statuses of the entities directly depending on the entity `entity-type`
diff --git a/enterprise/backend/src/metabase_enterprise/erd/db.clj b/enterprise/backend/src/metabase_enterprise/erd/db.clj
index ec3bffc4bf0..b3ec7668523 100644
--- a/enterprise/backend/src/metabase_enterprise/erd/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/erd/db.clj
@@ -4,7 +4,6 @@
(:require
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.models.table :as schema.table]
[toucan2.core :as t2]))
@@ -23,8 +22,7 @@
opts :- [:maybe ActiveTablesOpts]]
(let [{:keys [table-ids schema]} opts]
(t2/select :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :select columns
+ {:select columns
:where (cond-> [:and [:= :db_id database-id] [:= :active true]]
(contains? opts :table-ids) (conj [:in :id table-ids])
(contains? opts :schema) (conj (if (= schema "")
@@ -34,8 +32,7 @@
(mu/defn active-fields-for-tables
"The active Fields of the Tables with `table-ids`, in field order."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Field {:from [(warehouse-schema-overlay/field-query)]
- :where [:and
+ (t2/select :model/Field {:where [:and
[:in :table_id table-ids]
[:= :active true]]
:order-by schema.table/field-order-rule}))
@@ -43,7 +40,6 @@
(mu/defn active-fields
"The active Fields with `field-ids`."
[field-ids :- [:or [:set ::lib.schema.id/field] [:sequential ::lib.schema.id/field]]]
- (t2/select :model/Field {:from [(warehouse-schema-overlay/field-query)]
- :where [:and
+ (t2/select :model/Field {:where [:and
[:in :id field-ids]
[:= :active true]]}))
diff --git a/enterprise/backend/src/metabase_enterprise/library/db.clj b/enterprise/backend/src/metabase_enterprise/library/db.clj
index 9697de137e4..7b0b4b3d5ef 100644
--- a/enterprise/backend/src/metabase_enterprise/library/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/library/db.clj
@@ -5,7 +5,6 @@
[metabase.collections.models.collection :as collection]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn card-types-in-collections
@@ -16,7 +15,7 @@
(mu/defn published-table-in-collections?
"Whether a published Table exists in the Collections with `collection-ids`."
[collection-ids :- [:sequential ::lib.schema.id/collection]]
- (t2/exists? :model/Table :is_published true :collection_id [:in collection-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/exists? :model/Table :is_published true :collection_id [:in collection-ids]))
(mu/defn dashboard-in-collections?
"Whether a Dashboard exists in the Collections with `collection-ids`."
diff --git a/enterprise/backend/src/metabase_enterprise/permission_debug/db.clj b/enterprise/backend/src/metabase_enterprise/permission_debug/db.clj
index dc793fbb982..3f384f139e1 100644
--- a/enterprise/backend/src/metabase_enterprise/permission_debug/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/permission_debug/db.clj
@@ -5,7 +5,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.permissions.core :as perms]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn user-superuser?
@@ -21,7 +20,7 @@
(defn- blocked-tables-select
[user-id tables-expr permissions-blocking permissions-granting]
{:select [[:db.name :db_name] :blocked.schema [:blocked.name :table_name] [:pg.name :group_name]]
- :from [(warehouse-schema-overlay/table-query {:alias :blocked, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :blocked]]
:join [[(perms/select-tables-and-groups-granting-perm
{:user-id user-id :is-superuser? false}
permissions-blocking) :perm_grant] [:= :blocked.id :perm_grant.id]
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj
index 49688a9c85c..3037b01f44e 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj
@@ -8,7 +8,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(def ^:private ConditionKey
@@ -170,23 +169,18 @@
:limit limit}))
(mu/defn instance
- "The instance of `model` with `id`, or nil; a Table is read through the overlay."
+ "The instance of `model` with `id`, or nil."
[model :- :keyword
id :- ms/PositiveInt]
- (t2/select-one model :id id (if (= model :model/Table)
- {:from [(warehouse-schema-overlay/table-query)]}
- {})))
+ (t2/select-one model :id id))
(mu/defn instance-with-columns
- "The `columns` of the instance of `model` with `id`, or nil; a Table is read through the overlay."
+ "The `columns` of the instance of `model` with `id`, or nil."
[model :- :keyword
columns :- [:sequential :keyword]
id :- ms/PositiveInt]
(t2/select-one (into [model] columns)
- :id id
- (if (= model :model/Table)
- {:from [(warehouse-schema-overlay/table-query)]}
- {})))
+ :id id))
(mu/defn instance-names
"The `:id` and `:name` of the instances of `model` with `ids`."
@@ -232,20 +226,20 @@
(case model-key
:model/TableUserSettings {:alias "t"
:select [:t.name [:t.id :table_id] :t.collection_id [:t.name :table_name]]
- :from [(warehouse-schema-overlay/table-query {:alias :t})]}
+ :from [[(t2/table-name :model/Table) :t]]}
(:model/FieldUserSettings :model/Dimension)
{:alias "f"
:select [:f.name :f.table_id [:t.collection_id :collection_id] [:t.name :table_name]]
:from [[:metabase_field :f]]
- :join [(warehouse-schema-overlay/table-query {:alias :t}) [:= :f.table_id :t.id]]}
+ :join [[(t2/table-name :model/Table) :t] [:= :f.table_id :t.id]]}
:model/Segment {:alias "s"
:select [:s.name :s.table_id [:t.collection_id :collection_id] [:t.name :table_name]]
:from [[:segment :s]]
- :join [(warehouse-schema-overlay/table-query {:alias :t}) [:= :s.table_id :t.id]]}
+ :join [[(t2/table-name :model/Table) :t] [:= :s.table_id :t.id]]}
:model/Measure {:alias "s"
:select [:s.name :s.table_id [:t.collection_id :collection_id] [:t.name :table_name]]
:from [[:measure :s]]
- :join [(warehouse-schema-overlay/table-query {:alias :t}) [:= :s.table_id :t.id]]}))
+ :join [[(t2/table-name :model/Table) :t] [:= :s.table_id :t.id]]}))
(mu/defn tracking-details-by-id
"The name, table id, collection id, and table name of the `model-key` (TableUserSettings, FieldUserSettings,
@@ -305,7 +299,7 @@
(mu/defn published-table-ids :- [:set ::lib.schema.id/table]
"The ids of the Tables published in the Collections with `collection-ids`."
[collection-ids :- [:sequential ::lib.schema.id/collection]]
- (set (t2/select-pks-set :model/Table {:from [(warehouse-schema-overlay/table-query {:alias :t})]
+ (set (t2/select-pks-set :model/Table {:from [[(t2/table-name :model/Table) :t]]
:where [:and [:= :t.is_published true] [:in :t.collection_id collection-ids]]})))
(mu/defn table-ids-with-user-settings
@@ -319,7 +313,7 @@
(t2/select-fn-vec :field_id :model/FieldUserSettings
{:select [:u.field_id]
:from [[(t2/table-name :model/FieldUserSettings) :u]]
- :join [(warehouse-schema-overlay/field-query {:alias :f :user-settings? false}) [:= :f.id :u.field_id]]
+ :join [[(t2/table-name :model/Field) :f] [:= :f.id :u.field_id]]
:where [:in :f.table_id table-ids]}))
(mu/defn field-ids-with-dimensions
@@ -328,28 +322,18 @@
(t2/select-fn-vec :field_id :model/Dimension
{:select [:d.field_id]
:from [[(t2/table-name :model/Dimension) :d]]
- :join [(warehouse-schema-overlay/field-query {:alias :f :user-settings? false}) [:= :f.id :d.field_id]]
+ :join [[(t2/table-name :model/Field) :f] [:= :f.id :d.field_id]]
:where [:in :f.table_id table-ids]}))
-(mu/defn delete-table-user-settings!
- "Delete the TableUserSettings of the Tables with `table-ids`, returning the number deleted."
- [table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/delete! :model/TableUserSettings :table_id [:in table-ids]))
-
-(mu/defn delete-field-user-settings!
- "Delete the FieldUserSettings of the Fields with `field-ids`, returning the number deleted."
- [field-ids :- [:sequential ::lib.schema.id/field]]
- (t2/delete! :model/FieldUserSettings :field_id [:in field-ids]))
-
(mu/defn delete-dimensions!
"Delete the Dimensions of the Fields with `field-ids`, returning the number deleted."
[field-ids :- [:sequential ::lib.schema.id/field]]
(t2/delete! :model/Dimension :field_id [:in field-ids]))
(mu/defn tables-tracking-details
- "The `:id`, `:name`, and `:collection_id` of the Tables with `table-ids`, read through the overlay."
+ "The `:id`, `:name`, and `:collection_id` of the Tables with `table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/select [:model/Table :id :name :collection_id] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select [:model/Table :id :name :collection_id] :id [:in table-ids]))
(mu/defn fields-tracking-details
"The `:id`, name, table id, collection id, and table name of the Fields with `field-ids`."
@@ -500,8 +484,7 @@
(t2/exists? :model/Document :collection_id [:in collection-ids])
(t2/exists? :model/Timeline :collection_id [:in collection-ids])
(t2/exists? :model/Action :collection_id [:in collection-ids])
- (t2/exists? :model/Table {:from [(warehouse-schema-overlay/table-query)]
- :where [:and [:= :is_published true] [:in :collection_id collection-ids]]}))))
+ (t2/exists? :model/Table {:where [:and [:= :is_published true] [:in :collection_id collection-ids]]}))))
(mu/defn remote-synced-subtree-collection-ids
"The IDs of the remote-synced Collections among `collections` and their descendants."
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
index e557c74a344..4824378eb78 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
@@ -28,6 +28,7 @@
[metabase.util.jvm :as u.jvm]
[metabase.util.log :as log]
[metabase.util.yaml :as yaml]
+ [metabase.warehouse-schema.core :as warehouse-schema]
[toucan2.core :as t2])
(:import (metabase_enterprise.remote_sync.source.protocol SourceSnapshot)))
@@ -326,11 +327,11 @@
(doseq [batch (->> (in-batches remote-sync.db/table-ids-with-user-settings table-ids)
(remove kept-tables)
(partition-all app-db-batch-size))]
- (remote-sync.db/delete-table-user-settings! (vec batch)))
+ (warehouse-schema/delete-table-user-settings! (vec batch)))
(doseq [batch (->> (in-batches remote-sync.db/field-ids-with-user-settings (remove legacy-tables table-ids))
(remove kept-fields)
(partition-all app-db-batch-size))]
- (remote-sync.db/delete-field-user-settings! (vec batch)))
+ (warehouse-schema/delete-field-user-settings! (vec batch)))
(doseq [batch (->> (in-batches remote-sync.db/field-ids-with-dimensions table-ids)
(remove kept-dims)
(partition-all app-db-batch-size))]
diff --git a/enterprise/backend/src/metabase_enterprise/replacement/db.clj b/enterprise/backend/src/metabase_enterprise/replacement/db.clj
index cb862373104..344bd5253e5 100644
--- a/enterprise/backend/src/metabase_enterprise/replacement/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/replacement/db.clj
@@ -13,7 +13,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn run
@@ -75,7 +74,7 @@
(mu/defn tables-with-ids
"The Tables with `ids`."
[ids :- [:sequential ms/PositiveInt]]
- (t2/select :model/Table :id [:in ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in ids]))
(mu/defn dashboards-with-ids
"The Dashboards with `ids`."
@@ -182,23 +181,23 @@
(mu/defn table-database-id
"The Database ID of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table :id table-id))
(mu/defn active-fields-of-table
"The active Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select :model/Field :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :table_id table-id :active true))
(mu/defn active-field-ids-of-table
"The IDs of the active Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-pks-set :model/Field :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-pks-set :model/Field :table_id table-id :active true))
(mu/defn active-fk-to-fields-exists?
"Whether an active Field on an active Table points at one of the Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/exists? :model/Field {:from [(warehouse-schema-overlay/field-query)]
- :join [(warehouse-schema-overlay/table-query {:user-settings? false})
+ (t2/exists? :model/Field {:from [(t2/table-name :model/Field)]
+ :join [(t2/table-name :model/Table)
[:= :metabase_field.table_id :metabase_table.id]]
:where [:and
[:in :metabase_field.fk_target_field_id field-ids]
diff --git a/enterprise/backend/src/metabase_enterprise/replacement/runner.clj b/enterprise/backend/src/metabase_enterprise/replacement/runner.clj
index 6fae87f12bf..044dfd0f1a7 100644
--- a/enterprise/backend/src/metabase_enterprise/replacement/runner.clj
+++ b/enterprise/backend/src/metabase_enterprise/replacement/runner.clj
@@ -180,7 +180,7 @@
:let [field (field-by-name (source-swap.util/column-match-key col-meta))
overrides (u/select-keys-when col-meta :non-nil metadata-override-keys)]
:when (and field (seq overrides))]
- (field-user-settings/upsert-user-settings field overrides))))
+ (field-user-settings/upsert-field-user-settings! field overrides))))
(defn run-swap-model-with-transform!
"Execute a transform, find the output table, then swap all dependents of the card
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/db.clj b/enterprise/backend/src/metabase_enterprise/sandbox/db.clj
index 042213cbed0..54f4ccc0b68 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/db.clj
@@ -12,7 +12,6 @@
[metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn sandbox
@@ -63,7 +62,7 @@
(t2/select :model/Sandbox
{:select [:s.group_id :s.table_id :t.db_id :t.schema]
:from [[:sandboxes :s]]
- :join [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})
+ :join [[(t2/table-name :model/Table) :t]
[:= :s.table_id :t.id]]
:where [:and
(when group-id [:= :s.group_id group-id])
@@ -83,7 +82,7 @@
[:table.db_id :db_id]
[:table.schema :schema]]
:from [[:sandboxes]]
- :left-join [(warehouse-schema-overlay/table-query {:alias :table, :user-settings? false})
+ :left-join [[(t2/table-name :model/Table) :table]
[:= :sandboxes.table_id :table.id]]
:where [:and
[:in :sandboxes.group_id group-ids]
@@ -153,7 +152,7 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn tables-of-database
"The `:id`, `:db_id`, and `:schema` of the Tables of the Database with `db-id`, restricted to `schema` when
@@ -162,8 +161,7 @@
schema-only? :- :boolean
schema :- [:maybe :string]]
(t2/select [:model/Table :id :db_id :schema]
- {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]
- :where [:and
+ {:where [:and
[:= :db_id db-id]
(when schema-only?
[:= :schema schema])]}))
@@ -173,14 +171,14 @@
[table-id :- ::lib.schema.id/table]
(t2/select-one :model/Database
:id ^:allow-subquery {:select [:t.db_id]
- :from [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :t]]
:where [:= :t.id table-id]}))
(mu/defn fields-of-table-named
"The `:id` and `:name` of the Fields of the Table with `table-id` named one of `field-names`."
[table-id :- ::lib.schema.id/table
field-names :- [:set :string]]
- (t2/select [:model/Field :id :name] :table_id table-id :name [:in field-names] {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select [:model/Field :id :name] :table_id table-id :name [:in field-names]))
(mu/defn cards-by-id
"A map of Card ID to the query-related columns of the Cards with `card-ids`."
diff --git a/enterprise/backend/src/metabase_enterprise/search/entity_retrieval/db.clj b/enterprise/backend/src/metabase_enterprise/search/entity_retrieval/db.clj
index 229092cf05c..fa6df88d179 100644
--- a/enterprise/backend/src/metabase_enterprise/search/entity_retrieval/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/search/entity_retrieval/db.clj
@@ -5,7 +5,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn library-cards-in-collections
@@ -26,8 +25,7 @@
[collection-ids :- [:sequential ::lib.schema.id/collection]
id :- [:maybe ::lib.schema.id/table]]
(t2/select [:model/Table :id :name :display_name :description]
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ {:where [:and
[:in :collection_id collection-ids]
[:= :is_published true]
[:= :active true]
diff --git a/enterprise/backend/src/metabase_enterprise/search/semantic/db.clj b/enterprise/backend/src/metabase_enterprise/search/semantic/db.clj
index ab00bc28ed4..58870cd4cc5 100644
--- a/enterprise/backend/src/metabase_enterprise/search/semantic/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/search/semantic/db.clj
@@ -12,7 +12,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn library-root-collections
@@ -30,8 +29,7 @@
authoritative."
[]
(t2/reducible-select [:model/Table :id :is_published :data_layer :data_authority]
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ {:where [:and
[:= :active true]
[:or [:= :is_published true]
[:= :data_authority "authoritative"]]]}))
diff --git a/enterprise/backend/src/metabase_enterprise/transforms_inspector/db.clj b/enterprise/backend/src/metabase_enterprise/transforms_inspector/db.clj
index 1c0c94f98c5..53a61b69740 100644
--- a/enterprise/backend/src/metabase_enterprise/transforms_inspector/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/transforms_inspector/db.clj
@@ -4,13 +4,12 @@
(:require
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn table-source-rows
"The ID, name, schema, and Database ID of the Tables with `table-ids`, as source info."
[table-ids :- [:or [:set ::lib.schema.id/table] [:sequential ::lib.schema.id/table]]]
- (t2/select [:model/Table [:id :table-id] [:name :table-name] :schema [:db_id :db-id]] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select [:model/Table [:id :table-id] [:name :table-name] :schema [:db_id :db-id]] :id [:in table-ids]))
(mu/defn database-engine
"The engine of the Database with `database-id`."
@@ -20,4 +19,4 @@
(mu/defn active-fields-for-table
"The active Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select :model/Field :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :table_id table-id :active true))
diff --git a/enterprise/backend/src/metabase_enterprise/transforms_python/db.clj b/enterprise/backend/src/metabase_enterprise/transforms_python/db.clj
index 0e8ae858e6c..fd0b8b7d221 100644
--- a/enterprise/backend/src/metabase_enterprise/transforms_python/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/transforms_python/db.clj
@@ -6,18 +6,17 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn table-database-ids
"The set of Database IDs of the Tables with `table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/select-fn-set :db_id [:model/Table :db_id] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-fn-set :db_id [:model/Table :db_id] :id [:in table-ids]))
(mu/defn table-database-id
- "The Database ID of the raw table row with `table-id`."
+ "The Database ID of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id (t2/table-name :model/Table) :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id (t2/table-name :model/Table) :id table-id))
(mu/defn database-engine
"The engine of the Database with `database-id`."
@@ -68,5 +67,4 @@
;; we are only interested in top-level objects, so filter out nested fields (parent or path)
:parent_id nil
:nfc_path nil
- {:from [(warehouse-schema-overlay/field-query)]
- :order-by [[:database_position :asc]]}))
+ {:order-by [[:database_position :asc]]}))
diff --git a/enterprise/backend/src/metabase_enterprise/upload_management/db.clj b/enterprise/backend/src/metabase_enterprise/upload_management/db.clj
index 6666afe3347..dc6ddd38152 100644
--- a/enterprise/backend/src/metabase_enterprise/upload_management/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/upload_management/db.clj
@@ -4,7 +4,6 @@
(:require
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn attached-dwh-database-id
@@ -15,15 +14,14 @@
(mu/defn non-upload-tables-for-database
"The active Tables of the Database with `database-id` that were not uploaded."
[database-id :- ::lib.schema.id/database]
- (t2/select :model/Table :db_id database-id :active true :is_upload false {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :db_id database-id :active true :is_upload false))
(mu/defn upload-tables
"The active uploaded Tables, ordered by name."
[]
- (t2/select :model/Table :active true :is_upload true {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:name :asc]]}))
+ (t2/select :model/Table :active true :is_upload true {:order-by [[:name :asc]]}))
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
diff --git a/enterprise/backend/test/metabase_enterprise/action_v2/db_test.clj b/enterprise/backend/test/metabase_enterprise/action_v2/db_test.clj
index cdd1c6b1efb..14406e3b35b 100644
--- a/enterprise/backend/test/metabase_enterprise/action_v2/db_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/action_v2/db_test.clj
@@ -2,7 +2,8 @@
(:require
[clojure.test :refer :all]
[metabase-enterprise.action-v2.db :as action-v2.db]
- [metabase.test :as mt]))
+ [metabase.test :as mt]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]))
(deftest pk-fields-for-table-honors-user-set-semantic-type-test
(testing "pk-fields-for-table honors a user-set semantic_type that differs from the sync value"
@@ -10,10 +11,8 @@
:model/Field {field-id :id} {:table_id table-id, :semantic_type nil}]
(testing "sanity check: not a PK by sync"
(is (= [] (map :id (action-v2.db/pk-fields-for-table table-id)))))
- (mt/with-temp [:model/FieldUserSettings _ {:field_id field-id
- :semantic_type :type/PK
- :semantic_type_set true}]
- (is (= [field-id] (map :id (action-v2.db/pk-fields-for-table table-id))))))))
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:semantic_type :type/PK})
+ (is (= [field-id] (map :id (action-v2.db/pk-fields-for-table table-id)))))))
(deftest category-list-field-ids-by-name-honors-user-settings-test
(testing "category-list-field-ids-by-name honors user-set has_field_values/semantic_type"
@@ -22,9 +21,7 @@
:has_field_values nil, :semantic_type nil}]
(testing "sanity check: not a category-list field by sync"
(is (= [] (action-v2.db/category-list-field-ids-by-name table-id ["status"]))))
- (mt/with-temp [:model/FieldUserSettings _ {:field_id field-id
- :has_field_values :list
- :semantic_type :type/Category
- :semantic_type_set true}]
- (is (= [{:id field-id, :lower_name "status"}]
- (action-v2.db/category-list-field-ids-by-name table-id ["status"])))))))
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:has_field_values :list
+ :semantic_type :type/Category})
+ (is (= [{:id field-id, :lower_name "status"}]
+ (action-v2.db/category-list-field-ids-by-name table-id ["status"]))))))
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
index bdd9d5c1e9d..af5df630d84 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
@@ -579,7 +579,7 @@
(update :base_type u/qualified-name)
(update :visibility_type u/qualified-name))
update-target (fn []
- (mt/user-http-request :rasta :put 200 (format "field/%d" (:id field)) (assoc field :fk_target_field_id (:id fk-field-2))))]
+ (mt/user-http-request :rasta :put 200 (format "field/%d" (:id field)) {:fk_target_field_id (:id fk-field-2)}))]
(testing "target should be hydrated if a non-admin has data model perms for the DB"
(mt/with-all-users-data-perms-graph! {db-id {:data-model {:schemas :all}}}
(is (= expected-target
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj
index d30fb94af2f..a5daaf31e18 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj
@@ -9,6 +9,7 @@
[metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.core :as perms]
[metabase.test :as mt]
+ [metabase.warehouse-schema.models.table-user-settings :as table-user-settings]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -275,8 +276,8 @@
:model/Table owls {:db_id (:id aviary) :name "owls" :display_name "Owls" :schema "PUBLIC"}
:model/Table albatrosses {:db_id (:id zoo) :name "albatrosses"
:display_name "Albatrosses" :schema nil}
- :model/TableUserSettings _ {:table_id (:id finches) :display_name "Finches"}
:model/PermissionsGroup group {}]
+ (table-user-settings/upsert-table-user-settings! finches {:display_name "Finches"})
(with-app-reading! [app [(:id albatrosses) (:id owls) (:id finches)]]
(doseq [group-id [(:id (perms/all-users-group)) (:id group)]
database-id [(:id aviary) (:id zoo)]]
diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/api/permissions_groups_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/api/permissions_groups_test.clj
index 58c0de7b827..02e7f5fe720 100644
--- a/enterprise/backend/test/metabase_enterprise/data_studio/api/permissions_groups_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_studio/api/permissions_groups_test.clj
@@ -4,14 +4,12 @@
[metabase.permissions.core :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(defn- user-table-fn
- "The value of `column` on the Table with `table-id` as users see it. A bare `t2/select :model/Table` shows sync's."
+ "The value of `column` on the Table with `table-id`."
[column table-id]
- (t2/select-one-fn column :model/Table :id table-id
- {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one-fn column :model/Table :id table-id))
(deftest fetch-groups-test
(testing "GET /api/permissions/group - Data Analysts group is visible with the feature"
diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
index f4f1253548f..44204ac7c08 100644
--- a/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
@@ -11,7 +11,6 @@
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -19,18 +18,17 @@
(use-fixtures :once (fixtures/initialize :db))
(defn- user-table
- "The Table with `table-id` as users see it. A bare `t2/select :model/Table` shows what sync wrote."
+ "The Table with `table-id`."
[table-id]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(defn- user-tables
- "The Tables with `table-ids` as users see it, ordered by `order-by`."
+ "The Tables with `table-ids`, ordered by `order-by`."
[table-ids order-by]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]
- :order-by [order-by]}))
+ (t2/select :model/Table :id [:in table-ids] {:order-by [order-by]}))
(defn- user-table-fn
- "The value of `column` on the Table with `table-id` as users see it."
+ "The value of `column` on the Table with `table-id`."
[column table-id]
(get (user-table table-id) column))
diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/permissions/query_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/permissions/query_test.clj
index 514d038855c..d00c155309c 100644
--- a/enterprise/backend/test/metabase_enterprise/data_studio/permissions/query_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_studio/permissions/query_test.clj
@@ -39,22 +39,24 @@
(mt/with-premium-features #{:library}
(mt/with-temp [:model/User {user-id :id} {}
:model/Collection {collection-id :id} {:type "library-data"}]
- (try
- (schema.table-user-settings/upsert-user-settings (t2/select-one :model/Table :id (mt/id :venues))
- {:is_published true
- :collection_id collection-id})
- (let [all-users (perms/all-users-group)]
- (perms/set-database-permission! all-users (mt/id) :perms/view-data db-view-data)
- (perms/set-database-permission! all-users (mt/id) :perms/create-queries :no)
- (if grant?
- (perms/grant-collection-read-permissions! all-users collection-id)
- (perms/revoke-collection-permissions! all-users collection-id))
- (perms/set-table-permission! all-users (mt/id :venues) :perms/view-data :unrestricted)
- (perms/set-table-permission! all-users (mt/id :venues) :perms/create-queries :no))
- (f user-id collection-id)
- (finally
- (schema.table-user-settings/unset-user-settings! (t2/select-one :model/Table :id (mt/id :venues))
- [:is_published :collection_id])))))))
+ (mt/with-temp-vals-in-db :model/Table (mt/id :venues) (select-keys (t2/select-one :model/Table :id (mt/id :venues))
+ [:is_published :collection_id])
+ (try
+ (schema.table-user-settings/upsert-table-user-settings! (t2/select-one :model/Table :id (mt/id :venues))
+ {:is_published true
+ :collection_id collection-id})
+ (let [all-users (perms/all-users-group)]
+ (perms/set-database-permission! all-users (mt/id) :perms/view-data db-view-data)
+ (perms/set-database-permission! all-users (mt/id) :perms/create-queries :no)
+ (if grant?
+ (perms/grant-collection-read-permissions! all-users collection-id)
+ (perms/revoke-collection-permissions! all-users collection-id))
+ (perms/set-table-permission! all-users (mt/id :venues) :perms/view-data :unrestricted)
+ (perms/set-table-permission! all-users (mt/id :venues) :perms/create-queries :no))
+ (f user-id collection-id)
+ (finally
+ (schema.table-user-settings/unset-table-user-settings! (t2/select-one :model/Table :id (mt/id :venues))
+ [:is_published :collection_id]))))))))
(deftest published-table-test
(testing "Published tables grant query access via collection permissions only with library enabled\n"
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/result_metadata_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/result_metadata_test.clj
index 17f19a4a62c..f8a0704b7f9 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/result_metadata_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/result_metadata_test.clj
@@ -17,6 +17,7 @@
[metabase.test :as mt]
[metabase.util :as u]
[metabase.util.json :as json]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -83,6 +84,21 @@
(mt/with-premium-features #{:dependencies}
(deps.result-metadata/update-result-metadata! 1000)))
+(defn- do-with-field-display-name!
+ "Run `thunk` with `display-name` set by a user as the display name of the Field with `field-id`."
+ [field-id display-name thunk]
+ (mt/with-temp-vals-in-db :model/Field field-id {:display_name (t2/select-one-fn :display_name :model/Field :id field-id)}
+ (try
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:display_name display-name})
+ (thunk)
+ (finally
+ (field-user-settings/delete-field-user-settings! [field-id])))))
+
+(defmacro ^:private with-field-display-name!
+ "Run `body` with `display-name` set by a user as the display name of the Field with `field-id`."
+ [field-id display-name & body]
+ `(do-with-field-display-name! ~field-id ~display-name (fn [] ~@body)))
+
(defn- add-grand-total [query]
(lib/expression query "Grand total" (lib/* (lib.metadata/field (mt/metadata-provider) (mt/id :orders :total)) 2)))
@@ -123,7 +139,7 @@
(let [run-count (column card-id "count")]
(is (some? (:fingerprint run-count)))
(run-jobs!)
- (mt/with-temp [:model/FieldUserSettings _ {:field_id (mt/id :orders :product_id) :display_name "Item"}]
+ (with-field-display-name! (mt/id :orders :product_id) "Item"
(run-jobs!)
(is (= "Item" (:display_name (column card-id "PRODUCT_ID"))))
(is (= (select-keys run-count [:base_type :fingerprint])
@@ -197,7 +213,7 @@
(t2/update! :model/Card :id model-id {:result_metadata (rename-column (t2/select-one-fn :result_metadata :model/Card
:id model-id)
"TOTAL" "Grand total")})
- (mt/with-temp [:model/FieldUserSettings _ {:field_id (mt/id :orders :total) :display_name "Order total"}]
+ (with-field-display-name! (mt/id :orders :total) "Order total"
(is (pending? model-id))
(run-jobs!)
(is (not (pending? model-id)))
@@ -208,7 +224,7 @@
(with-dependencies-feature
(mt/with-temp [:model/Card {card-id :id} {:dataset_query (table-query :orders)}]
(run-jobs!)
- (mt/with-temp [:model/FieldUserSettings _ {:field_id (mt/id :orders :total) :display_name "Grand total"}]
+ (with-field-display-name! (mt/id :orders :total) "Grand total"
(is (pending? card-id))
(run-jobs!)
(is (= "Grand total" (:display_name (column card-id "TOTAL"))))
@@ -280,7 +296,7 @@
(with-dependencies-feature
(mt/with-temp [:model/Card {card-id :id} {:dataset_query (table-query :orders)}]
(run-jobs!)
- (mt/with-temp [:model/FieldUserSettings _ {:field_id (mt/id :orders :total) :display_name "Order total"}]
+ (with-field-display-name! (mt/id :orders :total) "Order total"
(is (pos? (rederive-during-inference!
card-id
(once! #(t2/update! :model/Card :id card-id {:dataset_query (add-grand-total (table-query :orders))})))))
@@ -299,7 +315,7 @@
(let [run-metadata (-> (t2/select-one-fn :result_metadata :model/Card :id card-id)
(rename-column "TOTAL" "Run total")
(->> (mapv #(cond-> % (= "ID" (:name %)) (assoc :fingerprint {:global {:distinct-count 7}})))))]
- (mt/with-temp [:model/FieldUserSettings _ {:field_id (mt/id :orders :total) :display_name "Order total"}]
+ (with-field-display-name! (mt/id :orders :total) "Order total"
(is (pos? (rederive-during-inference! card-id (once! #(store-run-metadata! card-id run-metadata)))))
(run-jobs!)
(is (= "Order total" (:display_name (column card-id "TOTAL"))))
@@ -376,7 +392,7 @@
(is (not (pending? card-id)))
(run-jobs!)
(is (nil? (column card-id "Grand total")))
- (mt/with-temp [:model/FieldUserSettings _ {:field_id (mt/id :orders :total) :display_name "Order total"}]
+ (with-field-display-name! (mt/id :orders :total) "Order total"
(run-jobs!)
(is (some? (column card-id "Grand total"))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj
index 5364075ffb1..2fba86e4626 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj
@@ -17,6 +17,8 @@
[metabase.test.fixtures :as fixtures]
[metabase.test.util.thread-local :as tu.thread-local]
[metabase.util :as u]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
+ [metabase.warehouse-schema.models.table-user-settings :as table-user-settings]
[toucan2.core :as t2]))
(use-fixtures :each remote-sync.tu/allow-remote-synced-collections)
@@ -1205,8 +1207,8 @@
:model/Field remapped {:name "remapped" :table_id (:id table) :base_type :type/Integer}
:model/Field _plain {:name "plain" :table_id (:id table) :base_type :type/Text}
:model/Dimension _ {:field_id (:id remapped) :name "Remapped" :type :internal}]
- (t2/insert! :model/FieldUserSettings {:field_id (:id curated) :description "curated"})
- (t2/insert! :model/TableUserSettings {:table_id (:id table) :is_published true :collection_id (:id coll)})
+ (field-user-settings/upsert-field-user-settings! curated {:description "curated"})
+ (table-user-settings/upsert-table-user-settings! table {:is_published true :collection_id (:id coll)})
(t2/delete! :model/RemoteSyncObject)
(let [tracked (fn [] (into #{} (map (juxt :model_type :model_id :status)) (t2/select :model/RemoteSyncObject)))]
(events/publish-event! :event/table-publish
@@ -1216,7 +1218,7 @@
["FieldUserSettings" (:id curated) "update"]
["Dimension" (:id remapped) "update"]}
(tracked)))
- (t2/update! :model/TableUserSettings :table_id (:id table) {:is_published false :collection_id nil})
+ (table-user-settings/upsert-table-user-settings! table {:is_published false :collection_id nil})
(events/publish-event! :event/table-unpublish
{:object (assoc table :is_published false :collection_id nil)
:user-id (mt/user->id :rasta)})
@@ -1442,8 +1444,8 @@
(mt/with-temp [:model/Collection coll {:is_remote_synced true :name "Remote-Sync" :type "library-data"}
:model/Table table {:name "T"}
:model/Field field {:name "f" :table_id (:id table) :base_type :type/Text}]
- (t2/insert! :model/TableUserSettings {:table_id (:id table) :is_published true :collection_id (:id coll)})
- (t2/insert! :model/FieldUserSettings {:field_id (:id field) :description "curated"})
+ (table-user-settings/upsert-table-user-settings! table {:is_published true :collection_id (:id coll)})
+ (field-user-settings/upsert-field-user-settings! field {:description "curated"})
(t2/delete! :model/RemoteSyncObject)
(events/publish-event! :event/field-update {:object field :user-id (mt/user->id :rasta)})
(is (=? [{:model_type "FieldUserSettings" :model_id (:id field) :status "update"}]
diff --git a/enterprise/backend/test/metabase_enterprise/replacement/runner_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/runner_test.clj
index 960cdc4ef0a..d87eee12e5b 100644
--- a/enterprise/backend/test/metabase_enterprise/replacement/runner_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/replacement/runner_test.clj
@@ -263,7 +263,7 @@
[:stages 0 :source-card]))))))))))))))
(deftest copy-model-metadata-overrides!-test
- (testing "copies user-edited metadata from model result_metadata to FieldUserSettings only"
+ (testing "copies user-edited metadata from model result_metadata as user settings"
(mt/with-temp [:model/Table {table-id :id} {:name "transform_output"
:db_id (mt/id)
:active true}
@@ -296,14 +296,14 @@
:base_type "type/DateTimeWithLocalTZ"}])}
:where [:= :id card-id]})
(#'replacement.runner/copy-model-metadata-overrides! card-id table-id)
- (testing "Field records are untouched; they stay sync-owned"
+ (testing "Field rows show copied metadata"
(let [field-1 (t2/select-one :model/Field :id field-1-id)
field-2 (t2/select-one :model/Field :id field-2-id)]
- (is (= "Total" (:display_name field-1)))
- (is (nil? (:description field-1)))
- (is (nil? (:semantic_type field-1)))
- (is (= "Created At" (:display_name field-2)))
- (is (nil? (:semantic_type field-2)))))
+ (is (= "Order Total" (:display_name field-1)))
+ (is (= "The total amount" (:description field-1)))
+ (is (= :type/Currency (:semantic_type field-1)))
+ (is (= "Order Date" (:display_name field-2)))
+ (is (= :type/CreationTimestamp (:semantic_type field-2)))))
(testing "FieldUserSettings are created so overrides survive sync"
(let [fus-1 (t2/select-one :model/FieldUserSettings :field_id field-1-id)
fus-2 (t2/select-one :model/FieldUserSettings :field_id field-2-id)]
@@ -341,8 +341,8 @@
:where [:= :id card-id]})
(#'replacement.runner/copy-model-metadata-overrides! card-id table-id)
(let [field (t2/select-one :model/Field :id field-id)]
- (is (= "Products ID" (:display_name field)))
- (is (nil? (:description field))))
+ (is (= "Product ID" (:display_name field)))
+ (is (= "The product identifier" (:description field))))
(let [fus (t2/select-one :model/FieldUserSettings :field_id field-id)]
(is (some? fus))
(is (= "Product ID" (:display_name fus)))
diff --git a/frontend/src/metabase-types/api/field.ts b/frontend/src/metabase-types/api/field.ts
index a1c01dbaa79..ae6c9dfa891 100644
--- a/frontend/src/metabase-types/api/field.ts
+++ b/frontend/src/metabase-types/api/field.ts
@@ -144,7 +144,6 @@ export interface UpdateFieldRequest {
visibility_type?: FieldVisibilityType;
has_field_values?: FieldValuesType;
settings?: FieldFormattingSettings;
- nfc_path?: string[] | null;
json_unfolding?: boolean | null;
}
diff --git a/mage/src/mage/modules.clj b/mage/src/mage/modules.clj
index c18cb63776c..a0f48be7090 100644
--- a/mage/src/mage/modules.clj
+++ b/mage/src/mage/modules.clj
@@ -209,7 +209,6 @@
core.version
audit-app.view-log
warehouses.schema
- warehouse-schema-overlay
xrays})
(defn- affected-modules
diff --git a/mage/src/mage/project_tests.clj b/mage/src/mage/project_tests.clj
index 812e6bbf850..d4859c62b5e 100644
--- a/mage/src/mage/project_tests.clj
+++ b/mage/src/mage/project_tests.clj
@@ -15,9 +15,6 @@
metabase.core.module-history-test
metabase.core.modules-test])
-(def ^:private honey-sql-check-namespaces
- '[metabase.core.table-or-field-raw-usage-test])
-
(def ^:private ratchet-test-namespaces
;; These test the tooling itself. `./bin/mage kondo-ratchets` checks the source tree.
'[metabase.core.kondo-ratchet-test
@@ -29,8 +26,7 @@
(def ^:private backend-check-namespaces
(vec (concat module-check-namespaces
ratchet-test-namespaces
- source-layout-check-namespaces
- honey-sql-check-namespaces)))
+ source-layout-check-namespaces)))
(def ^:private default-suites
"Suites the bare `project-tests` command runs, in order."
diff --git a/mage/test/mage/project_tests_test.clj b/mage/test/mage/project_tests_test.clj
index b64b19b54d8..809faaa4b77 100644
--- a/mage/test/mage/project_tests_test.clj
+++ b/mage/test/mage/project_tests_test.clj
@@ -51,8 +51,7 @@
metabase.core.modules-test
metabase.core.kondo-ratchet-test
metabase.core.kondo-ratchet-check-test
- metabase.core.namespace-uniqueness-test
- metabase.core.table-or-field-raw-usage-test])]]
+ metabase.core.namespace-uniqueness-test])]]
calls)))
(testing "the failed suite is reported"
(is (= ["migrations"] failed))
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/db.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/db.clj
index 7ea17faa8c1..a7625ab30b4 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/db.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/db.clj
@@ -4,7 +4,6 @@
(:require
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouses.schema :as warehouses.schema]
;; the driver persists dataset-filter and project-id migrations of its Database details back to the app DB
^{:clj-kondo/ignore [:discouraged-namespace]}
@@ -33,5 +32,4 @@
"Whether the Table with `table-id` has an active database-partitioned Field named `field-name`."
[table-id :- ::lib.schema.id/table
field-name :- :string]
- (t2/exists? :model/Field :table_id table-id :name field-name :database_partitioned true :active true
- {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/exists? :model/Field :table_id table-id :name field-name :database_partitioned true :active true))
diff --git a/modules/drivers/druid-jdbc/test/metabase/driver/druid_jdbc/nested_field_columns_test.clj b/modules/drivers/druid-jdbc/test/metabase/driver/druid_jdbc/nested_field_columns_test.clj
index c3831de3568..c77ad1f10e2 100644
--- a/modules/drivers/druid-jdbc/test/metabase/driver/druid_jdbc/nested_field_columns_test.clj
+++ b/modules/drivers/druid-jdbc/test/metabase/driver/druid_jdbc/nested_field_columns_test.clj
@@ -143,7 +143,7 @@
get-database (fn [] (t2/select-one :model/Database :id (mt/id)))
set-json-unfolding-for-field! (fn [v]
(mt/user-http-request :crowberto :put 200 (format "field/%d" (mt/id :json :json_bit))
- (assoc (get-field) :json_unfolding v)))
+ {:json_unfolding v}))
set-json-unfolding-for-db! (fn [v]
(let [updated-db (into {} (assoc-in database [:details :json-unfolding] v))]
(mt/user-http-request :crowberto :put 200 (format "database/%d" (:id database))
@@ -190,7 +190,7 @@
get-database (fn [] (t2/select-one :model/Database :id (mt/id)))
set-json-unfolding-for-field! (fn [v]
(mt/user-http-request :crowberto :put 200 (format "field/%d" (mt/id :json :json_bit))
- (assoc field :json_unfolding v)))
+ {:json_unfolding v}))
set-json-unfolding-for-db! (fn [v]
(let [updated-db (into {} (assoc-in database [:details :json-unfolding] v))]
(mt/user-http-request :crowberto :put 200 (format "database/%d" (:id database))
diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
index ef8cf4464c5..ba434e36937 100644
--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
@@ -1191,9 +1191,9 @@
(sync/sync-database! (mt/id))
(let [v4-table (t2/select-one :model/Table :db_id (mt/id) :name table-name)
v4-field (t2/select-one :model/Field :table_id (:id v4-table) :name "text_column")]
- (field-user-settings/upsert-user-settings v4-field
- {:coercion_strategy :Coercion/String->Integer
- :effective_type :type/Integer})
+ (field-user-settings/upsert-field-user-settings! v4-field
+ {:coercion_strategy :Coercion/String->Integer
+ :effective_type :type/Integer})
(t2/update! :model/Field :id (:id v4-field)
{:coercion_strategy :Coercion/String->Integer
:effective_type :type/Integer})
diff --git a/resources/migrations/2026/20261102_move_user_settings_into_metabase_table_and_field.yaml b/resources/migrations/2026/20261102_move_user_settings_into_metabase_table_and_field.yaml
new file mode 100644
index 00000000000..3a692f644e6
--- /dev/null
+++ b/resources/migrations/2026/20261102_move_user_settings_into_metabase_table_and_field.yaml
@@ -0,0 +1,157 @@
+## metabase_table and metabase_field hold what users see again, with metabase_table_user_settings and
+## metabase_field_user_settings recording which of those values users set. Since v64 user edits went only to the
+## settings tables and were applied on read; these changesets copy every user-set value into its Table or Field
+## row, and clear the caveats and points_of_interest no user set. The settings columns that no longer carry
+## information (caveats_set, points_of_interest_set and nfc_path) stay for releases that still read them, unused.
+
+databaseChangeLog:
+ - changeSet:
+ id: ceygl0
+ author: ranquild
+ comment: Copy the user-set Table values in metabase_table_user_settings into metabase_table
+ changes:
+ - sql:
+ dbms: postgresql
+ sql: >-
+ UPDATE metabase_table AS t
+ SET display_name = CASE WHEN s.display_name IS NOT NULL THEN s.display_name ELSE t.display_name END,
+ description = CASE WHEN s.description_set = TRUE THEN s.description ELSE t.description END,
+ entity_type = CASE WHEN s.entity_type IS NOT NULL THEN s.entity_type ELSE t.entity_type END,
+ visibility_type = CASE WHEN s.visibility_type_set = TRUE THEN s.visibility_type ELSE t.visibility_type END,
+ caveats = CASE WHEN s.caveats_set = TRUE THEN s.caveats ELSE t.caveats END,
+ points_of_interest = CASE WHEN s.points_of_interest_set = TRUE THEN s.points_of_interest ELSE t.points_of_interest END,
+ data_layer = CASE WHEN s.data_layer_set = TRUE THEN s.data_layer ELSE t.data_layer END,
+ data_source = CASE WHEN s.data_source_set = TRUE THEN s.data_source ELSE t.data_source END,
+ owner_email = CASE WHEN s.owner_email IS NOT NULL THEN s.owner_email ELSE t.owner_email END,
+ owner_user_id = CASE WHEN s.owner_user_id IS NOT NULL THEN s.owner_user_id ELSE t.owner_user_id END,
+ field_order = CASE WHEN s.field_order IS NOT NULL THEN s.field_order ELSE t.field_order END,
+ show_in_getting_started = CASE WHEN s.show_in_getting_started IS NOT NULL THEN s.show_in_getting_started ELSE t.show_in_getting_started END,
+ data_authority = CASE WHEN s.data_authority IS NOT NULL THEN s.data_authority ELSE t.data_authority END,
+ is_published = CASE WHEN s.is_published IS NOT NULL THEN s.is_published ELSE t.is_published END,
+ collection_id = CASE WHEN s.is_published IS NOT NULL THEN s.collection_id ELSE t.collection_id END
+ FROM metabase_table_user_settings AS s
+ WHERE s.table_id = t.id
+ - sql:
+ dbms: mysql,mariadb
+ sql: >-
+ UPDATE metabase_table t
+ JOIN metabase_table_user_settings s ON s.table_id = t.id
+ SET t.display_name = CASE WHEN s.display_name IS NOT NULL THEN s.display_name ELSE t.display_name END,
+ t.description = CASE WHEN s.description_set = TRUE THEN s.description ELSE t.description END,
+ t.entity_type = CASE WHEN s.entity_type IS NOT NULL THEN s.entity_type ELSE t.entity_type END,
+ t.visibility_type = CASE WHEN s.visibility_type_set = TRUE THEN s.visibility_type ELSE t.visibility_type END,
+ t.caveats = CASE WHEN s.caveats_set = TRUE THEN s.caveats ELSE t.caveats END,
+ t.points_of_interest = CASE WHEN s.points_of_interest_set = TRUE THEN s.points_of_interest ELSE t.points_of_interest END,
+ t.data_layer = CASE WHEN s.data_layer_set = TRUE THEN s.data_layer ELSE t.data_layer END,
+ t.data_source = CASE WHEN s.data_source_set = TRUE THEN s.data_source ELSE t.data_source END,
+ t.owner_email = CASE WHEN s.owner_email IS NOT NULL THEN s.owner_email ELSE t.owner_email END,
+ t.owner_user_id = CASE WHEN s.owner_user_id IS NOT NULL THEN s.owner_user_id ELSE t.owner_user_id END,
+ t.field_order = CASE WHEN s.field_order IS NOT NULL THEN s.field_order ELSE t.field_order END,
+ t.show_in_getting_started = CASE WHEN s.show_in_getting_started IS NOT NULL THEN s.show_in_getting_started ELSE t.show_in_getting_started END,
+ t.data_authority = CASE WHEN s.data_authority IS NOT NULL THEN s.data_authority ELSE t.data_authority END,
+ t.is_published = CASE WHEN s.is_published IS NOT NULL THEN s.is_published ELSE t.is_published END,
+ t.collection_id = CASE WHEN s.is_published IS NOT NULL THEN s.collection_id ELSE t.collection_id END
+ - sql:
+ dbms: h2
+ sql: >-
+ MERGE INTO metabase_table t
+ USING metabase_table_user_settings s ON s.table_id = t.id
+ WHEN MATCHED THEN UPDATE
+ SET display_name = CASE WHEN s.display_name IS NOT NULL THEN s.display_name ELSE t.display_name END,
+ description = CASE WHEN s.description_set = TRUE THEN s.description ELSE t.description END,
+ entity_type = CASE WHEN s.entity_type IS NOT NULL THEN s.entity_type ELSE t.entity_type END,
+ visibility_type = CASE WHEN s.visibility_type_set = TRUE THEN s.visibility_type ELSE t.visibility_type END,
+ caveats = CASE WHEN s.caveats_set = TRUE THEN s.caveats ELSE t.caveats END,
+ points_of_interest = CASE WHEN s.points_of_interest_set = TRUE THEN s.points_of_interest ELSE t.points_of_interest END,
+ data_layer = CASE WHEN s.data_layer_set = TRUE THEN s.data_layer ELSE t.data_layer END,
+ data_source = CASE WHEN s.data_source_set = TRUE THEN s.data_source ELSE t.data_source END,
+ owner_email = CASE WHEN s.owner_email IS NOT NULL THEN s.owner_email ELSE t.owner_email END,
+ owner_user_id = CASE WHEN s.owner_user_id IS NOT NULL THEN s.owner_user_id ELSE t.owner_user_id END,
+ field_order = CASE WHEN s.field_order IS NOT NULL THEN s.field_order ELSE t.field_order END,
+ show_in_getting_started = CASE WHEN s.show_in_getting_started IS NOT NULL THEN s.show_in_getting_started ELSE t.show_in_getting_started END,
+ data_authority = CASE WHEN s.data_authority IS NOT NULL THEN s.data_authority ELSE t.data_authority END,
+ is_published = CASE WHEN s.is_published IS NOT NULL THEN s.is_published ELSE t.is_published END,
+ collection_id = CASE WHEN s.is_published IS NOT NULL THEN s.collection_id ELSE t.collection_id END
+ rollback: # not necessary; the user values stay in metabase_table_user_settings
+
+ - changeSet:
+ id: ziqcir
+ author: ranquild
+ comment: Copy the user-set Field values in metabase_field_user_settings into metabase_field
+ changes:
+ - sql:
+ dbms: postgresql
+ sql: >-
+ UPDATE metabase_field AS t
+ SET semantic_type = CASE WHEN s.semantic_type_set = TRUE THEN s.semantic_type ELSE t.semantic_type END,
+ description = CASE WHEN s.description_set = TRUE THEN s.description ELSE t.description END,
+ display_name = CASE WHEN s.display_name IS NOT NULL THEN s.display_name ELSE t.display_name END,
+ visibility_type = CASE WHEN s.visibility_type IS NOT NULL THEN s.visibility_type ELSE t.visibility_type END,
+ has_field_values = CASE WHEN s.has_field_values IS NOT NULL THEN s.has_field_values ELSE t.has_field_values END,
+ effective_type = CASE WHEN s.effective_type IS NOT NULL THEN s.effective_type ELSE t.effective_type END,
+ coercion_strategy = CASE WHEN s.effective_type IS NOT NULL THEN s.coercion_strategy ELSE t.coercion_strategy END,
+ fk_target_field_id = CASE WHEN s.fk_target_field_id_set = TRUE THEN s.fk_target_field_id ELSE t.fk_target_field_id END,
+ caveats = CASE WHEN s.caveats IS NOT NULL THEN s.caveats ELSE t.caveats END,
+ points_of_interest = CASE WHEN s.points_of_interest IS NOT NULL THEN s.points_of_interest ELSE t.points_of_interest END,
+ nfc_path = CASE WHEN s.nfc_path IS NOT NULL THEN s.nfc_path ELSE t.nfc_path END,
+ json_unfolding = CASE WHEN s.json_unfolding IS NOT NULL THEN s.json_unfolding ELSE t.json_unfolding END,
+ settings = CASE WHEN s.settings IS NOT NULL THEN s.settings ELSE t.settings END,
+ data_sensitivity = CASE WHEN s.data_sensitivity IS NOT NULL THEN s.data_sensitivity ELSE t.data_sensitivity END,
+ custom_position = CASE WHEN s.custom_position IS NOT NULL THEN s.custom_position ELSE t.custom_position END
+ FROM metabase_field_user_settings AS s
+ WHERE s.field_id = t.id
+ - sql:
+ dbms: mysql,mariadb
+ sql: >-
+ UPDATE metabase_field t
+ JOIN metabase_field_user_settings s ON s.field_id = t.id
+ SET t.semantic_type = CASE WHEN s.semantic_type_set = TRUE THEN s.semantic_type ELSE t.semantic_type END,
+ t.description = CASE WHEN s.description_set = TRUE THEN s.description ELSE t.description END,
+ t.display_name = CASE WHEN s.display_name IS NOT NULL THEN s.display_name ELSE t.display_name END,
+ t.visibility_type = CASE WHEN s.visibility_type IS NOT NULL THEN s.visibility_type ELSE t.visibility_type END,
+ t.has_field_values = CASE WHEN s.has_field_values IS NOT NULL THEN s.has_field_values ELSE t.has_field_values END,
+ t.effective_type = CASE WHEN s.effective_type IS NOT NULL THEN s.effective_type ELSE t.effective_type END,
+ t.coercion_strategy = CASE WHEN s.effective_type IS NOT NULL THEN s.coercion_strategy ELSE t.coercion_strategy END,
+ t.fk_target_field_id = CASE WHEN s.fk_target_field_id_set = TRUE THEN s.fk_target_field_id ELSE t.fk_target_field_id END,
+ t.caveats = CASE WHEN s.caveats IS NOT NULL THEN s.caveats ELSE t.caveats END,
+ t.points_of_interest = CASE WHEN s.points_of_interest IS NOT NULL THEN s.points_of_interest ELSE t.points_of_interest END,
+ t.nfc_path = CASE WHEN s.nfc_path IS NOT NULL THEN s.nfc_path ELSE t.nfc_path END,
+ t.json_unfolding = CASE WHEN s.json_unfolding IS NOT NULL THEN s.json_unfolding ELSE t.json_unfolding END,
+ t.settings = CASE WHEN s.settings IS NOT NULL THEN s.settings ELSE t.settings END,
+ t.data_sensitivity = CASE WHEN s.data_sensitivity IS NOT NULL THEN s.data_sensitivity ELSE t.data_sensitivity END,
+ t.custom_position = CASE WHEN s.custom_position IS NOT NULL THEN s.custom_position ELSE t.custom_position END
+ - sql:
+ dbms: h2
+ sql: >-
+ MERGE INTO metabase_field t
+ USING metabase_field_user_settings s ON s.field_id = t.id
+ WHEN MATCHED THEN UPDATE
+ SET semantic_type = CASE WHEN s.semantic_type_set = TRUE THEN s.semantic_type ELSE t.semantic_type END,
+ description = CASE WHEN s.description_set = TRUE THEN s.description ELSE t.description END,
+ display_name = CASE WHEN s.display_name IS NOT NULL THEN s.display_name ELSE t.display_name END,
+ visibility_type = CASE WHEN s.visibility_type IS NOT NULL THEN s.visibility_type ELSE t.visibility_type END,
+ has_field_values = CASE WHEN s.has_field_values IS NOT NULL THEN s.has_field_values ELSE t.has_field_values END,
+ effective_type = CASE WHEN s.effective_type IS NOT NULL THEN s.effective_type ELSE t.effective_type END,
+ coercion_strategy = CASE WHEN s.effective_type IS NOT NULL THEN s.coercion_strategy ELSE t.coercion_strategy END,
+ fk_target_field_id = CASE WHEN s.fk_target_field_id_set = TRUE THEN s.fk_target_field_id ELSE t.fk_target_field_id END,
+ caveats = CASE WHEN s.caveats IS NOT NULL THEN s.caveats ELSE t.caveats END,
+ points_of_interest = CASE WHEN s.points_of_interest IS NOT NULL THEN s.points_of_interest ELSE t.points_of_interest END,
+ nfc_path = CASE WHEN s.nfc_path IS NOT NULL THEN s.nfc_path ELSE t.nfc_path END,
+ json_unfolding = CASE WHEN s.json_unfolding IS NOT NULL THEN s.json_unfolding ELSE t.json_unfolding END,
+ settings = CASE WHEN s.settings IS NOT NULL THEN s.settings ELSE t.settings END,
+ data_sensitivity = CASE WHEN s.data_sensitivity IS NOT NULL THEN s.data_sensitivity ELSE t.data_sensitivity END,
+ custom_position = CASE WHEN s.custom_position IS NOT NULL THEN s.custom_position ELSE t.custom_position END
+ rollback: # not necessary; the user values stay in metabase_field_user_settings
+
+ - changeSet:
+ id: 1h91ve
+ author: ranquild
+ comment: Clear the caveats and points_of_interest that their unset flags said no user set, since a value now means a user set it
+ changes:
+ - sql:
+ sql: >-
+ UPDATE metabase_table_user_settings
+ SET caveats = CASE WHEN caveats_set = TRUE THEN caveats ELSE NULL END,
+ points_of_interest = CASE WHEN points_of_interest_set = TRUE THEN points_of_interest ELSE NULL END
+ WHERE caveats_set = FALSE OR points_of_interest_set = FALSE
+ rollback: # not necessary; the cleared values were not shown to users
diff --git a/src/metabase/actions/db.clj b/src/metabase/actions/db.clj
index cb137f9c45c..bc6968ddcb5 100644
--- a/src/metabase/actions/db.clj
+++ b/src/metabase/actions/db.clj
@@ -7,7 +7,6 @@
[metabase.queries.card-schema :as queries.card-schema]
[metabase.queries.schema :as queries.schema]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(defn- action-database-joins
@@ -31,7 +30,7 @@
(mu/defn table-database-id
"The Database id of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id [:model/Table :db_id] :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id [:model/Table :db_id] :id table-id))
(mu/defn card-query
"The query of the Card with `card-id`, or nil."
@@ -96,12 +95,12 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn tables
"The Tables with `table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
(mu/defn database
"The Database with `database-id`, or nil."
@@ -225,7 +224,7 @@
(mu/defn fields-for-parameters
"The id, base type, display name, and description of the Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select [:model/Field :id :base_type :display_name :description] :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select [:model/Field :id :base_type :display_name :description] :id [:in field-ids]))
(mu/defn action-database-settings
"The id and Database settings of the Actions with `action-ids`."
@@ -256,9 +255,9 @@
(mu/defn writable-table-exists?
"Whether the Database with `database-id` has a writable Table."
[database-id :- ::lib.schema.id/database]
- (t2/exists? :model/Table :db_id database-id :is_writable true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/exists? :model/Table :db_id database-id :is_writable true))
(mu/defn table-with-unknown-writability-exists?
"Whether the Database with `database-id` has a Table whose writability is unknown."
[database-id :- ::lib.schema.id/database]
- (t2/exists? :model/Table :db_id database-id :is_writable nil {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/exists? :model/Table :db_id database-id :is_writable nil))
diff --git a/src/metabase/activity_feed/db.clj b/src/metabase/activity_feed/db.clj
index f8b6429e11c..664b902bba9 100644
--- a/src/metabase/activity_feed/db.clj
+++ b/src/metabase/activity_feed/db.clj
@@ -8,7 +8,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn recent-cards
@@ -43,8 +42,7 @@
:display_name [:metabase_database.initial_sync_status :initial-sync-status]
[:visibility_type :visibility_type]
[:metabase_database.name :database-name]]
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:in :metabase_table.id ids]
+ {:where [:in :metabase_table.id ids]
:left-join [:metabase_database [:= :metabase_database.id :metabase_table.db_id]]}))
(mu/defn recent-dashboard-and-table-views
@@ -69,7 +67,7 @@
[:and
[:= :model "dashboard"]
[:= :d.id :model_id]]
- (warehouse-schema-overlay/table-query {:alias :t})
+ [(t2/table-name :model/Table) :t]
[:and
[:= :model "table"]
[:= :t.id :model_id]]]}))
@@ -101,7 +99,7 @@
(mu/defn table-exists?
"Whether a Table with `id` exists."
[id :- ::lib.schema.id/table]
- (t2/exists? :model/Table :id id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/exists? :model/Table :id id))
(mu/defn collection-exists?
"Whether a Collection with `id` exists."
@@ -121,7 +119,7 @@
(mu/defn table
"The Table with `id`, or nil."
[id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id id))
(mu/defn collection
"The Collection with `id`, or nil."
@@ -286,7 +284,7 @@
[:db.name :database-name]
[:db.id :db_id]
[:db.initial_sync_status :initial-sync-status]]
- :from [(warehouse-schema-overlay/table-query {:alias :t})]
+ :from [[(t2/table-name :model/Table) :t]]
:where [:and
[:or
[:= :visibility_type nil]
diff --git a/src/metabase/analytics/db.clj b/src/metabase/analytics/db.clj
index f679e4c617e..a270919eb66 100644
--- a/src/metabase/analytics/db.clj
+++ b/src/metabase/analytics/db.clj
@@ -9,7 +9,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn first-user-date-joined
@@ -50,8 +49,7 @@
(mu/defn published-table-count-in-collections
"The number of published Tables in the Collections with `collection-ids`."
[collection-ids :- [:set ::lib.schema.id/collection]]
- (t2/count :model/Table {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ (t2/count :model/Table {:where [:and
[:= :is_published true]
[:in :collection_id collection-ids]]}))
@@ -163,7 +161,7 @@
"The Database id and schema of the Tables of the non-internal Databases."
[]
(t2/query {:select [:t.db_id :t.schema]
- :from [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :t]]
:join [[(t2/table-name :model/Database) :d] [:= :d.id :t.db_id]]
:where (mi/exclude-internal-content-hsql :model/Database :table-alias :d)}))
@@ -171,8 +169,8 @@
"The Table id of the Fields of the non-internal Databases."
[]
(t2/query {:select [:f.table_id]
- :from [(warehouse-schema-overlay/field-query {:alias :f})]
- :join [(warehouse-schema-overlay/table-query {:alias :t}) [:= :t.id :f.table_id]
+ :from [[(t2/table-name :model/Field) :f]]
+ :join [[(t2/table-name :model/Table) :t] [:= :t.id :f.table_id]
[(t2/table-name :model/Database) :d] [:= :d.id :t.db_id]]
:where (mi/exclude-internal-content-hsql :model/Database :table-alias :d)}))
@@ -370,7 +368,7 @@
(mu/defn upload-table-exists?
"Whether an uploaded Table exists."
[]
- (t2/exists? :model/Table :is_upload true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/exists? :model/Table :is_upload true))
(mu/defn snippet-collection-exists?
"Whether a snippet Collection exists."
diff --git a/src/metabase/audit_app/db.clj b/src/metabase/audit_app/db.clj
index 40ed57777ac..ffe369e6746 100644
--- a/src/metabase/audit_app/db.clj
+++ b/src/metabase/audit_app/db.clj
@@ -6,7 +6,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn cards
@@ -37,7 +36,7 @@
(mu/defn table-database-id
"The Database id of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table, :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table, :id table-id))
(mu/defn insert-audit-log!
"Insert an AuditLog entry."
diff --git a/src/metabase/collections/children.clj b/src/metabase/collections/children.clj
index 5ae1596600c..a52ffa397e2 100644
--- a/src/metabase/collections/children.clj
+++ b/src/metabase/collections/children.clj
@@ -37,7 +37,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -790,7 +789,7 @@
[:t.db_id :database_id]
[[:!= :t.archived_at nil] :archived]
[(h2x/literal "table") :model]]
- :from [(warehouse-schema-overlay/table-query {:alias :t})]
+ :from [[(t2/table-name :model/Table) :t]]
:where [:and
[:= :t.is_published true]
(poison-when-pinned-clause pinned-state)
diff --git a/src/metabase/collections/db.clj b/src/metabase/collections/db.clj
index 8abb81cc070..736036d23ed 100644
--- a/src/metabase/collections/db.clj
+++ b/src/metabase/collections/db.clj
@@ -11,7 +11,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(def ^:private PermissionsRow
@@ -540,8 +539,7 @@
`skip-archived?`."
[collection-id :- [:maybe ::lib.schema.id/collection]
skip-archived? :- [:maybe :boolean]]
- (t2/select-pks-set :model/Table {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ (t2/select-pks-set :model/Table {:where [:and
[:= :collection_id collection-id]
[:= :is_published true]
(when skip-archived? [:= :archived_at nil])]}))
@@ -554,7 +552,7 @@
(mu/defn published-table-ids-in-collections
"The IDs of the published Tables in the Collections with `collection-ids`."
[collection-ids :- [:or [:set ::lib.schema.id/collection] [:sequential ::lib.schema.id/collection]]]
- (t2/select-pks-set :model/Table :collection_id [:in collection-ids] :is_published true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-pks-set :model/Table :collection_id [:in collection-ids] :is_published true))
(mu/defn unpublish-tables-in-collections!
"Unpublish the Tables in the Collections with `collection-ids`, in `metabase_table` and in their user settings,
@@ -597,7 +595,7 @@
"The distinct `:collection_id`s of the published, unarchived Tables in the Collections with `collection-ids`."
[collection-ids]
(t2/query {:select-distinct [:collection_id]
- :from [(warehouse-schema-overlay/table-query)]
+ :from [(t2/table-name :model/Table)]
:where [:and
[:= :is_published true]
[:= :archived_at nil]
diff --git a/src/metabase/collections_rest/db.clj b/src/metabase/collections_rest/db.clj
index c8bca4c826d..2f45d771d89 100644
--- a/src/metabase/collections_rest/db.clj
+++ b/src/metabase/collections_rest/db.clj
@@ -4,7 +4,6 @@
(:require
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn collection
@@ -42,7 +41,7 @@
"The distinct `:collection_id`s of the published, unarchived Tables."
[]
(t2/query {:select-distinct [:collection_id]
- :from [(warehouse-schema-overlay/table-query)]
+ :from [(t2/table-name :model/Table)]
:where [:and
[:= :is_published true]
[:= :archived_at nil]]}))
diff --git a/src/metabase/data_studio/api/table.clj b/src/metabase/data_studio/api/table.clj
index 396ee4a3c8d..c3763eb9a75 100644
--- a/src/metabase/data_studio/api/table.clj
+++ b/src/metabase/data_studio/api/table.clj
@@ -160,7 +160,7 @@
:- [:merge
::table-selectors
[:map {:closed true}
- [:data_authority {:optional true} [:maybe ::data-authorities]]
+ [:data_authority {:optional true} ::data-authorities]
[:data_source {:optional true} [:maybe ::data-sources]]
[:data_layer {:optional true} [:maybe ::data-layers]]
[:entity_type {:optional true} [:maybe :string]]
@@ -178,7 +178,7 @@
table-ids (set (map :id existing-tables))
set-map (select-keys body set-ks)]
(when (seq set-map)
- (schema.table-user-settings/upsert-user-settings-for-tables! table-ids set-map)
+ (schema.table-user-settings/upsert-table-user-settings-for-tables! table-ids set-map)
(maybe-sync-unhidden-tables! existing-tables set-map)
;; Publish update events for remote sync tracking
(let [updated-tables (data-studio.db/tables table-ids)]
diff --git a/src/metabase/data_studio/db.clj b/src/metabase/data_studio/db.clj
index 1c3847a88bf..aac4f7b3ea9 100644
--- a/src/metabase/data_studio/db.clj
+++ b/src/metabase/data_studio/db.clj
@@ -6,7 +6,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(def ^:private TableSelectors
@@ -38,9 +37,9 @@
exclude-table-ids :- [:set ::lib.schema.id/table]]
(t2/reducible-query {:select [[output-table-id :table_id]]
:from [[(t2/table-name :model/Dimension) :dim]]
- :join [(warehouse-schema-overlay/field-query {:alias :source_field, :user-settings? false})
+ :join [[(t2/table-name :model/Field) :source_field]
[:= :dim.field_id :source_field.id]
- (warehouse-schema-overlay/field-query {:alias :target_field, :user-settings? false})
+ [(t2/table-name :model/Field) :target_field]
[:= :dim.human_readable_field_id :target_field.id]]
:where [:and
[:= :dim.type "external"]
@@ -57,17 +56,17 @@
(let [where (table-selectors-where selectors)]
(t2/reducible-query {:select [[output-table-id :table_id]]
:from [[(t2/table-name :model/Dimension) :dim]]
- :join [(warehouse-schema-overlay/field-query {:alias :source_field})
+ :join [[(t2/table-name :model/Field) :source_field]
[:= :dim.field_id :source_field.id]
- (warehouse-schema-overlay/field-query {:alias :target_field})
+ [(t2/table-name :model/Field) :target_field]
[:= :dim.human_readable_field_id :target_field.id]]
:where [:and
[:= :dim.type "external"]
[:in input-table-id ^:allow-subquery
- {:select [:id] :from [(warehouse-schema-overlay/table-query)] :where where}]
+ {:select [:id] :from [(t2/table-name :model/Table)] :where where}]
[:not [:exists ^:allow-subquery
{:select [1]
- :from [(warehouse-schema-overlay/table-query)]
+ :from [(t2/table-name :model/Table)]
:where [:and where [:= :id output-table-id]]}]]]})))
(mu/defn database
@@ -83,32 +82,29 @@
(mu/defn tables-matching-selectors
"The Tables picked out by `selectors`, a map of `:database_ids`, `:schema_ids`, and/or `:table_ids`."
[selectors :- TableSelectors]
- (t2/select :model/Table {:from [(warehouse-schema-overlay/table-query)]
- :where (table-selectors-where selectors)}))
+ (t2/select :model/Table {:where (table-selectors-where selectors)}))
(mu/defn tables-matching-selectors-in-id-order
"The Tables picked out by `selectors`, in id order."
[selectors :- TableSelectors]
- (t2/select :model/Table {:from [(warehouse-schema-overlay/table-query)]
- :where (table-selectors-where selectors), :order-by [[:id]]}))
+ (t2/select :model/Table {:where (table-selectors-where selectors), :order-by [[:id]]}))
(mu/defn tables
"The Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
(mu/defn selection-columns-for-selectors
"Up to `limit` id, database, name, schema, and published flag of the Tables picked out by `selectors`."
[selectors :- TableSelectors
limit :- ms/PositiveInt]
(t2/select [:model/Table :id :db_id :name :display_name :schema :is_published]
- {:from [(warehouse-schema-overlay/table-query)]
- :where (table-selectors-where selectors), :limit limit}))
+ {:where (table-selectors-where selectors), :limit limit}))
(mu/defn selection-columns-for-tables
"The id, database, name, schema, and published flag of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select [:model/Table :id :db_id :name :display_name :schema :is_published] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select [:model/Table :id :db_id :name :display_name :schema :is_published] :id [:in table-ids]))
(mu/defn delete-field-values-for-tables!
"Delete the FieldValues of the Fields of the Tables with `table-ids`, returning the number deleted."
diff --git a/src/metabase/documents/db.clj b/src/metabase/documents/db.clj
index d62ac9048f3..b779fc41df5 100644
--- a/src/metabase/documents/db.clj
+++ b/src/metabase/documents/db.clj
@@ -10,7 +10,6 @@
[metabase.queries.schema :as queries.schema]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn document
@@ -142,7 +141,7 @@
(mu/defn table
"The Table with `id`, or nil."
[id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id id))
(mu/defn dashboard
"The Dashboard with `id`, or nil."
diff --git a/src/metabase/driver/db.clj b/src/metabase/driver/db.clj
index 7d090be0959..ad77d97f60c 100644
--- a/src/metabase/driver/db.clj
+++ b/src/metabase/driver/db.clj
@@ -4,7 +4,6 @@
(:require
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
^{:clj-kondo/ignore [:discouraged-namespace]}
[toucan2.core :as t2]))
@@ -21,7 +20,7 @@
(mu/defn table-field-names
"The names of the Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-fn-vec :name [:model/Field :name] :table_id table-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-fn-vec :name [:model/Field :name] :table_id table-id))
(mu/defn json-field-names-with-unfolding-disabled
"The names of the JSON Fields of the Table with `table-id` that have JSON unfolding disabled."
@@ -29,5 +28,4 @@
(t2/select-fn-set :name [:model/Field :name]
:table_id table-id
:base_type :type/JSON
- :json_unfolding false
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :json_unfolding false))
diff --git a/src/metabase/embedding_hub/db.clj b/src/metabase/embedding_hub/db.clj
index fe0b4ab0665..eea089e8b9c 100644
--- a/src/metabase/embedding_hub/db.clj
+++ b/src/metabase/embedding_hub/db.clj
@@ -2,7 +2,6 @@
"Application database queries for the embedding hub module. Every function here is a direct Toucan 2 call with no
additional logic, so the rest of the module never talks to `toucan2.core` itself."
(:require
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(defn user-added-database?
@@ -20,8 +19,7 @@
(defn uploaded-table?
"Whether the database with `database-id` has an active uploaded table."
[database-id]
- (t2/exists? :model/Table {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ (t2/exists? :model/Table {:where [:and
[:= :active true]
[:= :is_upload true]
[:= :db_id database-id]]}))
diff --git a/src/metabase/lib_be/metadata/jvm.clj b/src/metabase/lib_be/metadata/jvm.clj
index 693414bcec5..5770a3c0d35 100644
--- a/src/metabase/lib_be/metadata/jvm.clj
+++ b/src/metabase/lib_be/metadata/jvm.clj
@@ -24,7 +24,6 @@
[metabase.util.memoize :as u.memo]
[metabase.util.performance :as perf :refer [get-in]]
[metabase.util.snake-hating-map :as u.snake-hating-map]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[methodical.core :as methodical]
[potemkin :as p]
[pretty.core :as pretty]
@@ -223,7 +222,7 @@
[query-type model parsed-args honeysql]
(merge (next-method query-type model parsed-args honeysql)
{:select [:id :db_id :name :display_name :schema :active :visibility_type :database_require_filter]
- :from [(warehouse-schema-overlay/table-query)]}))
+ :from [(t2/table-name :model/Table)]}))
(t2/define-after-select :metadata/table
[table]
@@ -292,7 +291,7 @@
:dimension/type
:values/human_readable_values
:values/values]
- :from [(warehouse-schema-overlay/field-query {:alias :field})]
+ :from [[(t2/table-name :model/Field) :field]]
:left-join [[(t2/table-name :model/Table) :table]
[:= :field/table_id :table/id]
[(t2/table-name :model/Dimension) :dimension]
diff --git a/src/metabase/lib_metric/db.clj b/src/metabase/lib_metric/db.clj
index adbdea8eaf7..056eba12229 100644
--- a/src/metabase/lib_metric/db.clj
+++ b/src/metabase/lib_metric/db.clj
@@ -7,13 +7,12 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn fields
"The Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :id [:in field-ids]))
(mu/defn card-table-and-database-id
"The `:table_id` and `:database_id` of the Card with `card-id`, or nil."
@@ -23,7 +22,7 @@
(mu/defn table-database-id
"The Database id of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table :id table-id))
(defn- metric-where
"The `:where` map for the metric Cards picked out by `metadata-spec` (its `:id`, `:name`,
diff --git a/src/metabase/llm/db.clj b/src/metabase/llm/db.clj
index 46205b4e840..3a2b81c8832 100644
--- a/src/metabase/llm/db.clj
+++ b/src/metabase/llm/db.clj
@@ -6,7 +6,6 @@
[metabase.models.interface :as mi]
[metabase.util :as u]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn database-engine
@@ -32,8 +31,7 @@
[:table :string]
[:schema {:optional true} [:maybe :string]]]]]
(t2/select :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
+ {:where [:and
[:= :db_id database-id]
[:= :active true]
(into [:or] (map table-match-clause) tables)]}))
@@ -56,8 +54,7 @@
:db_id database-id
:active true
:visibility_type nil
- (cond-> {:from [(warehouse-schema-overlay/table-query)]
- :where clause}
+ (cond-> {:where clause}
with (assoc :with with)))))
(mu/defn unarchived-cards
@@ -68,4 +65,4 @@
(mu/defn field-names-and-tables
"The id, name, and Table id of the Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select [:model/Field :id :name :table_id] :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select [:model/Field :id :name :table_id] :id [:in field-ids]))
diff --git a/src/metabase/mcp/db.clj b/src/metabase/mcp/db.clj
index 04b3c6d46d1..a8f83f373ee 100644
--- a/src/metabase/mcp/db.clj
+++ b/src/metabase/mcp/db.clj
@@ -13,7 +13,6 @@
[metabase.session.core :as session]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(defn select-one-by-id
@@ -41,12 +40,12 @@
"The Table with `table-id`, or nil. Unlike [[select-one-by-id]], a nil `table-id` is answered with nil
rather than refused — callers reach here with a column value, not with an id an agent supplied."
[table-id]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(defn field-types
"The `base_type` and `effective_type` of the Field with `field-id`, or nil."
[field-id]
- (t2/select-one [:model/Field :base_type :effective_type] :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one [:model/Field :base_type :effective_type] :id field-id))
(defn nullable-fields-with-fingerprints
"The Fields among `field-ids` the warehouse declares nullable, carrying their `:fingerprint`.
@@ -55,8 +54,7 @@
[field-ids]
(t2/select [:model/Field :id :database_is_nullable :fingerprint]
:id [:in field-ids]
- :database_is_nullable true
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :database_is_nullable true))
(defn active-user-exists?
"Whether an active User with `user-id` exists."
@@ -100,7 +98,7 @@
"The active Tables with `table-ids`. `table-ids` is expected non-empty — an empty `:in` is a SQL error
rather than an empty result, so callers guard it."
[table-ids]
- (t2/select :model/Table :id [:in table-ids] :active true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids] :active true))
(defn active-visible-fields-for-tables
"The id, name, table id, and position of the active, non-hidden Fields of the Tables with `table-ids`, in
@@ -111,8 +109,7 @@
:table_id [:in table-ids]
:active true
:visibility_type [:not-in ["hidden" "sensitive" "retired"]]
- {:from [(warehouse-schema-overlay/field-query)]
- :order-by [[:position :asc] [:id :asc]]}))
+ {:order-by [[:position :asc] [:id :asc]]}))
(defn collections-for-read-check
"The Collections with `ids`, carrying the columns [[metabase.models.interface/can-read?]] consults.
diff --git a/src/metabase/measures/db.clj b/src/metabase/measures/db.clj
index f10d0caf8ca..a73eca91e2e 100644
--- a/src/metabase/measures/db.clj
+++ b/src/metabase/measures/db.clj
@@ -6,7 +6,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.measures.schema :as measures.schema]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn insert-measure!
@@ -34,7 +33,7 @@
(mu/defn table-database-ids
"The set of Database ids of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select-fn-set :db_id :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-fn-set :db_id :model/Table :id [:in table-ids]))
(mu/defn update-measure!
"Apply `changes` to the Measure with `id`."
@@ -45,12 +44,12 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn table-perms-columns
"The Database id, schema, and id of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one [:model/Table :db_id :schema :id] :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one [:model/Table :db_id :schema :id] :id table-id))
(mu/defn collections
"The Collections with `collection-ids`."
diff --git a/src/metabase/measures/models/measure.clj b/src/metabase/measures/models/measure.clj
index 4d2dee50c1f..3055d6673dc 100644
--- a/src/metabase/measures/models/measure.clj
+++ b/src/metabase/measures/models/measure.clj
@@ -260,24 +260,17 @@
:table_display_name :table.display_name
:table_schema :table.schema
;; The parent table's collection, for [[collection/table-root-collection-type]].
- ;; Publishing is a user setting, so the published state and collection come from the table's
- ;; user settings when they are set, as in `metabase.warehouse-schema-overlay.core/table-query`.
;; Listing these terms also reindexes a measure when its table moves into or out of the Library.
- :table-collection-id [:case
- [:not= :settings.is_published nil] :settings.collection_id
- :else :table.collection_id]
- :table-is-published [:coalesce :settings.is_published :table.is_published]
+ :table-collection-id :table.collection_id
+ :table-is-published :table.is_published
:table-collection-location :collection.location
:table-collection-type :collection.type}
;; Only published tables count as library content, matching the table spec's own collection join.
:joins {:table [:model/Table [:= :table.id :this.table_id]]
- :settings [:model/TableUserSettings [:= :settings.table_id :this.table_id]]
:collection [:model/Collection
[:and
- [:= [:coalesce :settings.is_published :table.is_published] true]
- [:= :collection.id [:case
- [:not= :settings.is_published nil] :settings.collection_id
- :else :table.collection_id]]]]}})
+ [:= :table.is_published true]
+ [:= :collection.id :table.collection_id]]]}})
;;; ------------------------------------------------- Dimension Persistence --------------------------------------------------
diff --git a/src/metabase/metabot/db.clj b/src/metabase/metabot/db.clj
index 7945de10154..5f1aac9c1ca 100644
--- a/src/metabase/metabot/db.clj
+++ b/src/metabase/metabot/db.clj
@@ -18,7 +18,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(declare collection metabot-metrics-and-models-query root-collections-of-types)
@@ -464,7 +463,7 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn active-table-with-columns
"The `columns` of the active Table with `table-id`, or nil."
@@ -475,7 +474,7 @@
(mu/defn table-database-id
"The Database ID of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table :id table-id))
(defn readable-active-table-database-id
"The Database ID of the active Table with `table-id`, or nil when there is no such table *or*
@@ -491,37 +490,34 @@
and `:collection_id`. Handing it a trimmed row makes it answer a narrower question and deny a
user whose access comes through that path."
[table-id]
- (when-let [table (t2/select-one :model/Table :id table-id :active true {:from [(warehouse-schema-overlay/table-query)]})]
+ (when-let [table (t2/select-one :model/Table :id table-id :active true)]
(when (mi/can-read? table)
(:db_id table))))
(mu/defn tables-by-id
"A map of ID to Table for `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select-fn->fn :id identity :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-fn->fn :id identity :model/Table :id [:in table-ids]))
(mu/defn table-summaries
"The ID, names, schema, Database ID, and description of the Tables with `table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/select [:model/Table :id :name :display_name :schema :db_id :description] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select [:model/Table :id :name :display_name :schema :db_id :description] :id [:in table-ids]))
(mu/defn table-schema-rows
"Return the ID, name, schema, and database ID of each table in `table-ids`.
Rows also include `:is_published` and `:collection_id`, which `can-read?` and `can-query?` need to check access
- through a collection. Both keys must be present, even when nil.
- Both are user values, so the rows read through the user-settings overlay."
+ through a collection, present even when nil."
[table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/select [:model/Table :id :name :schema :db_id :is_published :collection_id] :id [:in table-ids]
- {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select [:model/Table :id :name :schema :db_id :is_published :collection_id] :id [:in table-ids]))
(mu/defn table-curation-rows
"The ID, Database ID, active flag, visibility type, published flag, data layer, and data authority of the Tables with
`table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
(t2/select [:model/Table :id :db_id :active :visibility_type :is_published :data_layer :data_authority]
- :id [:in table-ids]
- {:from [(warehouse-schema-overlay/table-query)]}))
+ :id [:in table-ids]))
(mu/defn visible-table-summaries
"The ID, name, schema, and description of the active, unhidden Tables among `table-ids` in the Database with
@@ -532,7 +528,7 @@
:db_id database-id
:id [:in table-ids]
:active true
- :visibility_type nil {:from [(warehouse-schema-overlay/table-query)]}))
+ :visibility_type nil))
(defn- current-user-visible-table-clause
"Honey SQL `{:where …}` (plus `:with` when the filter needs a CTE) restricting Tables to those visible to the
@@ -558,7 +554,7 @@
:id [:in table-ids]
:active true
:visibility_type nil
- (assoc (current-user-visible-table-clause) :from [(warehouse-schema-overlay/table-query)])))
+ (current-user-visible-table-clause)))
(def ^:private max-visible-tables-to-consider
"Cap on the number of visible Tables fetched for fuzzy table-name matching."
@@ -573,9 +569,7 @@
:db_id database-id
:active true
:visibility_type nil
- (cond-> (assoc (current-user-visible-table-clause)
- :from [(warehouse-schema-overlay/table-query)]
- :limit max-visible-tables-to-consider)
+ (cond-> (assoc (current-user-visible-table-clause) :limit max-visible-tables-to-consider)
(seq excluded-table-ids)
(update :where (fn [where-clause]
(if where-clause
@@ -593,7 +587,6 @@
:active true
:visibility_type nil
(assoc (current-user-visible-table-clause)
- :from [(warehouse-schema-overlay/table-query)]
:order-by [[:view_count :desc]]
:limit limit)))
@@ -605,8 +598,7 @@
:db_id database-id
:active true
:visibility_type nil
- {:from [(warehouse-schema-overlay/table-query)]
- :limit limit}))
+ {:limit limit}))
(mu/defn active-tables-for-database
"The presentable columns of the active Tables in the Database with `database-id`, ordered by schema and name."
@@ -614,8 +606,7 @@
(t2/select [:model/Table :id :name :display_name :schema :db_id :description]
:db_id database-id
:active true
- {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:%lower.schema :asc] [:%lower.name :asc]]}))
+ {:order-by [[:%lower.schema :asc] [:%lower.name :asc]]}))
(mu/defn active-tables-in-schema
"The presentable columns of the active Tables in `schema` of the Database with `database-id`, ordered by name."
@@ -625,14 +616,13 @@
:db_id database-id
:schema schema
:active true
- {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:%lower.name :asc]]}))
+ {:order-by [[:%lower.name :asc]]}))
(mu/defn active-schemas-for-database
"The distinct `:schema` rows of the active Tables in the Database with `database-id`, ordered by schema."
[database-id :- ::lib.schema.id/database]
(t2/query {:select-distinct [:schema]
- :from [(warehouse-schema-overlay/table-query {:user-settings? false})]
+ :from [(t2/table-name :model/Table)]
:where [:and [:= :db_id database-id] [:= :active true]]
:order-by [[:schema :asc]]}))
@@ -676,7 +666,7 @@
table :- :string]
(t2/select-one :model/QueryTable
{:select [[:t.id :table-id] [:t.name :table] [:t.schema :schema]]
- :from [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :t]]
:where [:and
[:= :t.db_id db-id]
(table-match-clause {:schema schema :table table})]}))
@@ -691,7 +681,7 @@
[:table :string]]]]
(t2/select :model/QueryTable
{:select [[:t.id :table-id] [:t.name :table] [:t.schema :schema]]
- :from [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :t]]
:where [:and
[:= :t.db_id db-id]
(into [:or] (map table-match-clause) tables)]}))
@@ -701,22 +691,22 @@
(mu/defn field
"The Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one :model/Field :id field-id))
(mu/defn fields
"The Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :id [:in field-ids]))
(mu/defn field-fingerprint
"The fingerprint of the Field with `field-id`."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :fingerprint :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :fingerprint :model/Field :id field-id))
(mu/defn field-table-ids
"A map of Field ID to Table ID for `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select-fn->fn :id :table_id [:model/Field :id :table_id] :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-fn->fn :id :table_id [:model/Field :id :table_id] :id [:in field-ids]))
;;; -------------------------------------------------- Cards --------------------------------------------------
diff --git a/src/metabase/metrics/db.clj b/src/metabase/metrics/db.clj
index f6b05d6a2fb..56a5a2f2bab 100644
--- a/src/metabase/metrics/db.clj
+++ b/src/metabase/metrics/db.clj
@@ -6,7 +6,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(defn- visible-metric-cards-where
@@ -60,9 +59,9 @@
"The id and `columns` of the Fields with `field-ids`."
[columns :- [:sequential :keyword]
field-ids :- [:set ::lib.schema.id/field]]
- (t2/select (into [:model/Field :id] columns) :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select (into [:model/Field :id] columns) :id [:in field-ids]))
(mu/defn field
"The Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one :model/Field :id field-id))
diff --git a/src/metabase/models/db.clj b/src/metabase/models/db.clj
index 2f72527f049..40f2b95d706 100644
--- a/src/metabase/models/db.clj
+++ b/src/metabase/models/db.clj
@@ -8,7 +8,6 @@
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]
[toucan2.tools.identity-query :as t2.identity-query]))
@@ -242,10 +241,10 @@
[field-id :- ::lib.schema.id/field]
(t2/select :model/Field
{:with-recursive [[[:parents ^:allow-subquery {:columns [:id :name :parent_id :table_id]}]
- ^:allow-subquery {:union-all [^:allow-subquery {:from [(warehouse-schema-overlay/field-query {:alias :mf, :user-settings? false})]
+ ^:allow-subquery {:union-all [^:allow-subquery {:from [[(t2/table-name :model/Field) :mf]]
:select [:mf.id :mf.name :mf.parent_id :mf.table_id]
:where [:= :id field-id]}
- ^:allow-subquery {:from [(warehouse-schema-overlay/field-query {:alias :pf, :user-settings? false})]
+ ^:allow-subquery {:from [[(t2/table-name :model/Field) :pf]]
:select [:pf.id :pf.name :pf.parent_id :pf.table_id]
:join [[:parents :p] [:= :p.parent_id :pf.id]]}]}]]
:from [:parents]
@@ -254,7 +253,7 @@
(mu/defn table-ref-columns
"The id, Database id, name, and schema of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one [:model/Table :id :db_id :name :schema] :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one [:model/Table :id :db_id :name :schema] :id table-id))
(mu/defn database-name
"The name of the Database with `database-id`, or nil."
@@ -271,7 +270,7 @@
[table-name :- :string
schema :- [:maybe :string]
database-id :- ::lib.schema.id/database]
- (t2/select-one-fn :id :model/Table :name table-name :schema schema :db_id database-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :id :model/Table :name table-name :schema schema :db_id database-id))
(mu/defn insert-stub-database!
"Insert a stub Postgres Database named `database-name` with empty connection details and return its id."
@@ -297,7 +296,7 @@
[table-id :- ::lib.schema.id/table
field-name :- :string
parent-id :- [:maybe ms/PositiveInt]]
- (t2/select-one-pk :model/Field :table_id table-id :name field-name :parent_id parent-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-pk :model/Field :table_id table-id :name field-name :parent_id parent-id))
(defn- field-in-path-query
[table-id [field & rest]]
@@ -315,7 +314,7 @@
[table-id :- ::lib.schema.id/table
field-names :- [:sequential :string]]
(when (seq field-names)
- (t2/select-one-pk :model/Field (assoc (field-in-path-query table-id field-names) :from [(warehouse-schema-overlay/field-query {:user-settings? false})]))))
+ (t2/select-one-pk :model/Field :id [:in (field-in-path-query table-id field-names)])))
(mu/defn field-in-path
"The Field named by the last of `field-names` (each nested inside the previous, bottom-most first) under
@@ -323,7 +322,7 @@
[table-id :- [:maybe ::lib.schema.id/table]
field-names :- [:sequential :string]]
(when (seq field-names)
- (t2/select-one :model/Field (assoc (field-in-path-query table-id field-names) :from [(warehouse-schema-overlay/field-query)]))))
+ (t2/select-one :model/Field :id [:in (field-in-path-query table-id field-names)])))
(mu/defn insert-inactive-field!
"Insert an inactive, untyped Field named `field-name` under `parent-id` in the Table with `table-id` and return its
diff --git a/src/metabase/parameters/db.clj b/src/metabase/parameters/db.clj
index d77b8351c52..696fe618a8f 100644
--- a/src/metabase/parameters/db.clj
+++ b/src/metabase/parameters/db.clj
@@ -6,7 +6,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(defn- nested
@@ -30,9 +29,9 @@
[field-id mapping-type]
^:allow-subquery
{:select [[:dest.id :id] [(mapping-type-name->code mapping-type) :mapping_type]]
- :from [(warehouse-schema-overlay/field-query {:alias :source})]
+ :from [[(t2/table-name :model/Field) :source]]
:left-join [[:metabase_table :table] [:= :source.table_id :table.id]
- (warehouse-schema-overlay/field-query {:alias :dest}) [:= :dest.table_id :table.id]]
+ [(t2/table-name :model/Field) :dest] [:= :dest.table_id :table.id]]
:where [:and
[:= :source.id field-id]
(mdb/isa :source.semantic_type :type/PK)
@@ -63,7 +62,7 @@
(implicit-pk->name-mapping-query
^:allow-subquery
{:select [:fk_target_field_id]
- :from [(warehouse-schema-overlay/field-query)]
+ :from [(t2/table-name :model/Field)]
:where [:and
[:= :id field-id]
(mdb/isa :semantic_type :type/FK)]
@@ -78,32 +77,32 @@
(mu/defn field
"The Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one :model/Field :id field-id))
(mu/defn fields
"The Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :id [:in field-ids]))
(mu/defn fields-fk-info
"The id, FK target, and semantic type of the Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select [:model/Field :id :fk_target_field_id :semantic_type] :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select [:model/Field :id :fk_target_field_id :semantic_type] :id [:in field-ids]))
(mu/defn field-fk-target-field-id
"The FK target Field id of the Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :fk_target_field_id :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one-fn :fk_target_field_id :model/Field :id field-id))
(mu/defn field-base-type
"The base type of the Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :base_type :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :base_type :model/Field :id field-id))
(mu/defn field-name
"The name of the Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :name :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :name :model/Field :id field-id))
(mu/defn full-field-values-exist?
"Whether complete, non-remapped FieldValues of type `full` exist for the Field with `field-id`."
@@ -151,14 +150,13 @@
(t2/select (into [:model/Field] columns)
:table_id [:in table-ids]
:semantic_type (mdb/isa :type/Name)
- :active true
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :active true))
(mu/defn fields-with-columns
"The `columns` of the Fields with `field-ids`."
[columns :- [:sequential :keyword]
field-ids :- [:set ::lib.schema.id/field]]
- (t2/select (into [:model/Field] columns) :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select (into [:model/Field] columns) :id [:in field-ids]))
(mu/defn fk-relationships-for-database
"Rows describing FK -> PK Field relationships (`:f1`/`:t1` FK Field/Table ids, `:f2`/`:t2` PK Field/Table ids)
@@ -168,12 +166,12 @@
[:fk-table.id :t1]
[:pk-field.id :f2]
[:pk-field.table_id :t2]]
- :from [(warehouse-schema-overlay/field-query {:alias :fk-field})]
+ :from [[(t2/table-name :model/Field) :fk-field]]
:left-join [[:metabase_table :fk-table] [:and [:= :fk-field.table_id :fk-table.id]
:fk-table.active]
[:metabase_database :database] [:= :fk-table.db_id :database.id]
- (warehouse-schema-overlay/field-query {:alias :pk-field}) [:and [:= :fk-field.fk_target_field_id :pk-field.id]
- :pk-field.active]]
+ [(t2/table-name :model/Field) :pk-field] [:and [:= :fk-field.fk_target_field_id :pk-field.id]
+ :pk-field.active]]
:where [:and
[:= :database.id database-id]
[:not= :fk-field.fk_target_field_id nil]
diff --git a/src/metabase/permissions/db.clj b/src/metabase/permissions/db.clj
index bf99951b41a..5b4ba82c0f3 100644
--- a/src/metabase/permissions/db.clj
+++ b/src/metabase/permissions/db.clj
@@ -14,7 +14,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
;;; --------------------------------------------- DataPermissions ---------------------------------------------
@@ -743,37 +742,36 @@
`:data_source` is included because new-table permission defaults branch on whether the table came from an upload;
see [[metabase.permissions.models.data-permissions/set-default-table-permissions!]]."
[table-id :- ::lib.schema.id/table]
- (t2/select-one [:model/Table :id :db_id :schema :data_source] :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one [:model/Table :id :db_id :schema :data_source] :id table-id))
(mu/defn table-database-id
"The Database ID of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table :id table-id))
(mu/defn table-database-ids
"The ID and Database ID of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select [:model/Table :id :db_id] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select [:model/Table :id :db_id] :id [:in table-ids]))
(mu/defn active-table-locations-for-database
"The ID, Database ID, and schema of the active Tables of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/select [:model/Table :id :db_id :schema] :db_id database-id :active true {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select [:model/Table :id :db_id :schema] :db_id database-id :active true))
(mu/defn table-ids-and-schemas-excluding
"The ID and schema of the Tables of the Database with `database-id` other than `excluded-table-ids`."
[database-id :- ::lib.schema.id/database
excluded-table-ids :- [:sequential ::lib.schema.id/table]]
(t2/select [:model/Table :id :schema]
- {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]
- :where [:and
+ {:where [:and
[:= :db_id database-id]
[:not [:in :id excluded-table-ids]]]}))
(mu/defn field-visibility-info
"The ID, visibility type, and Table ID of the Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select [:model/Field :id :visibility_type :table_id] :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select [:model/Field :id :visibility_type :table_id] :id [:in field-ids]))
(mu/defn instance-by-id
"The instance of `model` with `id`, or nil."
diff --git a/src/metabase/permissions/models/data_permissions/sql.clj b/src/metabase/permissions/models/data_permissions/sql.clj
index 09b53b731b2..3dfe331e3d3 100644
--- a/src/metabase/permissions/models/data_permissions/sql.clj
+++ b/src/metabase/permissions/models/data_permissions/sql.clj
@@ -5,7 +5,7 @@
[metabase.permissions.schema :as permissions.schema]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay])
+ [toucan2.core :as t2])
(:import
(clojure.lang PersistentVector)))
@@ -139,7 +139,7 @@
"The plain `metabase_table`, aliased `mt`: permission queries read only sync-owned columns, and the overlay's
derived table is costly here and breaks on MariaDB."
[]
- (warehouse-schema-overlay/table-query {:alias :mt, :user-settings? false}))
+ [(t2/table-name :model/Table) :mt])
(mu/defn visible-table-filter-select
"Selects a column from tables that are visible to the provided user given a mapping of permission types to the required value or the required
diff --git a/src/metabase/permissions_rest/db.clj b/src/metabase/permissions_rest/db.clj
index c5faa7a6460..c35eba6c492 100644
--- a/src/metabase/permissions_rest/db.clj
+++ b/src/metabase/permissions_rest/db.clj
@@ -6,7 +6,6 @@
[metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(defn- managed-groups-clause
@@ -165,5 +164,4 @@
(mu/defn tables-for-databases
"The id, Database id, and schema of the Tables of the Databases with `database-ids`."
[database-ids :- [:set ::lib.schema.id/database]]
- (t2/select [:model/Table :id :db_id :schema] :db_id [:in database-ids]
- {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select [:model/Table :id :db_id :schema] :db_id [:in database-ids]))
diff --git a/src/metabase/queries/db.clj b/src/metabase/queries/db.clj
index e7372454142..93d9c976c28 100644
--- a/src/metabase/queries/db.clj
+++ b/src/metabase/queries/db.clj
@@ -13,7 +13,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
;;; ------------------------------------------------ Cards ------------------------------------------------
@@ -259,14 +258,14 @@
[:table.name :table-name]
[:table.db_id :field-db-id]]
:from [[:metabase_field :field]]
- :left-join [(warehouse-schema-overlay/table-query {:alias :table, :user-settings? false})
+ :left-join [[(t2/table-name :model/Table) :table]
[:= :field.table_id :table.id]]
:where [:in :field.id field-ids]}))
(mu/defn field-table-ids
"The set of Table IDs of the Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select-fn-set :table_id :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-fn-set :table_id :model/Field :id [:in field-ids]))
(mu/defn snippets
"The NativeQuerySnippets with `snippet-ids`."
diff --git a/src/metabase/queries_rest/db.clj b/src/metabase/queries_rest/db.clj
index d669861f632..64f05214ad2 100644
--- a/src/metabase/queries_rest/db.clj
+++ b/src/metabase/queries_rest/db.clj
@@ -8,7 +8,6 @@
[metabase.queries.schema :as queries.schema]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(def ^:private order-by-name {:order-by [[:%lower.name :asc]]})
@@ -76,14 +75,14 @@
"The Database id of the Table of the Segment with `segment-id`, or nil."
[segment-id :- ::lib.schema.id/segment]
(t2/select-one-fn :db_id :model/Table {:select [:t.db_id]
- :from [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :t]]
:join [[:segment :m] [:= :t.id :m.table_id]]
:where [:= :m.id segment-id]}))
(mu/defn table-database-id
"The Database id of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table, :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table, :id table-id))
(mu/defn card
"The Card with `card-id`, or nil."
diff --git a/src/metabase/query_permissions/db.clj b/src/metabase/query_permissions/db.clj
index 49066ffd23d..8d16d0e1b2b 100644
--- a/src/metabase/query_permissions/db.clj
+++ b/src/metabase/query_permissions/db.clj
@@ -4,7 +4,6 @@
(:require
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn card-collection-id
@@ -21,10 +20,9 @@
(mu/defn field-table-ids
"The set of Table ids of the Fields with `field-ids`."
[field-ids :- [:or [:set ::lib.schema.id/field] [:sequential ::lib.schema.id/field]]]
- (t2/select-fn-set :table_id :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-fn-set :table_id :model/Field :id [:in field-ids]))
(mu/defn table-id->database-id
"A map of Table id to Database id for the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select-pk->fn :db_id :model/Table :id [:in table-ids]
- {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-pk->fn :db_id :model/Table :id [:in table-ids]))
diff --git a/src/metabase/query_processor/db.clj b/src/metabase/query_processor/db.clj
index 35f4cf1a8f7..14ea0fffdca 100644
--- a/src/metabase/query_processor/db.clj
+++ b/src/metabase/query_processor/db.clj
@@ -8,7 +8,6 @@
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.queries.schema :as queries.schema]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
^{:clj-kondo/ignore [:discouraged-namespace]}
[toucan2.core :as t2]))
@@ -19,7 +18,7 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn dashcard-series-exists?
"Whether the Card with `card-id` is a series of the DashboardCard with `dashcard-id`."
diff --git a/src/metabase/search/in_place/legacy.clj b/src/metabase/search/in_place/legacy.clj
index 16f20a30ebc..2cb4db4d2f6 100644
--- a/src/metabase/search/in_place/legacy.clj
+++ b/src/metabase/search/in_place/legacy.clj
@@ -20,7 +20,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(def ^:private honeysql-registry
@@ -472,12 +471,10 @@
cols-or-nils (canonical-columns model column-alias->honeysql-clause)]
cols-or-nils))
-(mu/defn- from-clause-for-model :- [:tuple [:tuple :any :keyword]]
+(mu/defn- from-clause-for-model :- [:tuple [:tuple :keyword :keyword]]
[model :- SearchableModel]
(let [{:keys [db-model alias]} (get search.config/model-to-db-model model)]
- (if (= model "table")
- [(warehouse-schema-overlay/table-query {:alias alias})]
- [[(t2/table-name db-model) alias]])))
+ [[(t2/table-name db-model) alias]]))
(mu/defn- base-query-for-model :- [:map {:closed true}
[:select :any]
@@ -621,12 +618,12 @@
(defmethod search-query-for-model "measure"
[model search-ctx]
(-> (base-query-for-model model search-ctx)
- (sql.helpers/left-join (warehouse-schema-overlay/table-query {:alias :table}) [:= :measure.table_id :table.id])))
+ (sql.helpers/left-join [(t2/table-name :model/Table) :table] [:= :measure.table_id :table.id])))
(defmethod search-query-for-model "segment"
[model search-ctx]
(-> (base-query-for-model model search-ctx)
- (sql.helpers/left-join (warehouse-schema-overlay/table-query {:alias :table}) [:= :segment.table_id :table.id])))
+ (sql.helpers/left-join [(t2/table-name :model/Table) :table] [:= :segment.table_id :table.id])))
(defmethod search-query-for-model "table"
[model {:keys [current-user-perms table-db-id], :as search-ctx}]
diff --git a/src/metabase/search/ingestion/query.clj b/src/metabase/search/ingestion/query.clj
index dfa593a381c..0cecddf214a 100644
--- a/src/metabase/search/ingestion/query.clj
+++ b/src/metabase/search/ingestion/query.clj
@@ -54,9 +54,7 @@
(remove (comp search-terms key)))))
[:attrs :render-terms])
fn-selects))
- :from [(if-let [source (:source spec)]
- (source)
- [(t2/table-name (:model spec)) :this])]
+ :from [[(t2/table-name (:model spec)) :this]]
:where (:where spec [:= [:inline 1] [:inline 1]])
:left-join (when (:joins spec)
(into []
diff --git a/src/metabase/search/spec.clj b/src/metabase/search/spec.clj
index cb4449580de..06707ce7a3a 100644
--- a/src/metabase/search/spec.clj
+++ b/src/metabase/search/spec.clj
@@ -213,7 +213,6 @@
[:map-of :keyword [:or fn? true?]]]]
[:embedding-exclude {:optional true} [:set :keyword]]
[:render-terms [:map-of NonAttrKey AttrValue]]
- [:source {:optional true} fn?]
[:where {:optional true} vector?]
[:bookmark {:optional true} vector?]
[:joins {:optional true} JoinMap]])
@@ -414,10 +413,6 @@
Spec keys:
- `:model` - Toucan model keyword (required)
- `:attrs` - Map of search index attributes (required)
- - `:source` - A thunk returning what `:this` reads from, as a `[source :this]` pair, when the indexable rows are
- not simply the model's own table -- a Table, whose user-set values live in a side-car. A thunk because the spec
- map is evaluated as the namespace loads, and resolving another model there would be a load cycle. Defaults to
- the model's own table.
- `:search-terms` - Searchable text fields: a vector of column keywords, or a map of
column keyword to either `true` (use the raw value) or a transform fn applied for
full-text search (required)
diff --git a/src/metabase/segments/db.clj b/src/metabase/segments/db.clj
index b6519c333ea..6b7b8a4a5ab 100644
--- a/src/metabase/segments/db.clj
+++ b/src/metabase/segments/db.clj
@@ -6,7 +6,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.segments.schema :as segments.schema]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn insert-segment!
@@ -36,7 +35,7 @@
(mu/defn table-database-ids
"The set of Database ids of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select-fn-set :db_id :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-fn-set :db_id :model/Table :id [:in table-ids]))
(mu/defn update-segment!
"Apply `changes` to the Segment with `id`."
@@ -47,17 +46,17 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn table-database-id
"The Database id of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table :id table-id))
(mu/defn table-perms-columns
"The Database id, schema, and id of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one [:model/Table :db_id :schema :id] :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one [:model/Table :db_id :schema :id] :id table-id))
(mu/defn collections
"The Collections with `collection-ids`."
diff --git a/src/metabase/segments/models/segment.clj b/src/metabase/segments/models/segment.clj
index 57b0eaada03..4a37309b3fe 100644
--- a/src/metabase/segments/models/segment.clj
+++ b/src/metabase/segments/models/segment.clj
@@ -277,21 +277,14 @@
:table_display_name :table.display_name
:table_schema :table.schema
;; The parent table's collection, for [[collection/table-root-collection-type]].
- ;; Publishing is a user setting, so the published state and collection come from the table's
- ;; user settings when they are set, as in `metabase.warehouse-schema-overlay.core/table-query`.
;; Listing these terms also reindexes a segment when its table moves into or out of the Library.
- :table-collection-id [:case
- [:not= :settings.is_published nil] :settings.collection_id
- :else :table.collection_id]
- :table-is-published [:coalesce :settings.is_published :table.is_published]
+ :table-collection-id :table.collection_id
+ :table-is-published :table.is_published
:table-collection-location :collection.location
:table-collection-type :collection.type}
;; Only published tables count as library content, matching the table spec's own collection join.
:joins {:table [:model/Table [:= :table.id :this.table_id]]
- :settings [:model/TableUserSettings [:= :settings.table_id :this.table_id]]
:collection [:model/Collection
[:and
- [:= [:coalesce :settings.is_published :table.is_published] true]
- [:= :collection.id [:case
- [:not= :settings.is_published nil] :settings.collection_id
- :else :table.collection_id]]]]}})
+ [:= :table.is_published true]
+ [:= :collection.id :table.collection_id]]]}})
diff --git a/src/metabase/sql_tools/db.clj b/src/metabase/sql_tools/db.clj
index 742aa6e8fa0..acd29b1828c 100644
--- a/src/metabase/sql_tools/db.clj
+++ b/src/metabase/sql_tools/db.clj
@@ -5,7 +5,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util :as u]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn active-visible-table-ids-by-name
@@ -14,7 +13,7 @@
[database-id :- ::lib.schema.id/database
table-names :- [:sequential :string]]
(t2/select-pks-set :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
+ {:from [(t2/table-name :model/Table)]
:where [:and
[:= :db_id database-id]
;; `lower()` cannot use an index on the name column, but it still beats fetching every
diff --git a/src/metabase/sync/analyze/classify.clj b/src/metabase/sync/analyze/classify.clj
index 8c422f266b5..2713aff4125 100644
--- a/src/metabase/sync/analyze/classify.clj
+++ b/src/metabase/sync/analyze/classify.clj
@@ -68,8 +68,8 @@
;; cool, now we should be ok to update the model
(when values-to-set
(case (mi/model original-model)
- :model/Field (sync.db/update-field! (u/the-id original-model) values-to-set)
- :model/Table (sync.db/update-table! (u/the-id original-model) values-to-set))
+ :model/Field (sync-util/update-field-unless-user-set! (u/the-id original-model) values-to-set)
+ :model/Table (sync-util/update-table-unless-user-set! (u/the-id original-model) values-to-set))
true)))
(def ^:private ClassifyOpts
diff --git a/src/metabase/sync/analyze/data_sensitivity.clj b/src/metabase/sync/analyze/data_sensitivity.clj
index 67151105c52..a6913ddac88 100644
--- a/src/metabase/sync/analyze/data_sensitivity.clj
+++ b/src/metabase/sync/analyze/data_sensitivity.clj
@@ -3,9 +3,9 @@
[[metabase.analyze.core/infer-data-sensitivity]], writing `:PUBLIC` when no rule matches so each field is scanned
once. Selects fields whose `data_sensitivity` is `NULL` (or `PUBLIC` too under `force?`) across every active table
in the database, hidden and cruft tables included. Inert unless [[metabase.sync.settings/data-sensitivity-scan-enabled]]
- is true, except through [[scan-data-sensitivity!]]. User-set labels live in `FieldUserSettings`, which readers apply
- over the classifier's label, so the classifier never overwrites them. [[reset-data-sensitivity!]] clears the classifier's own labels so a rule
- change can reach fields that already carry a category."
+ is true, except through [[scan-data-sensitivity!]]. Fields with a label a user set in `FieldUserSettings` are never
+ scanned. [[reset-data-sensitivity!]] clears the classifier's own labels so a rule change can reach fields that
+ already carry a category."
(:require
[metabase.analyze.core :as analyze]
[metabase.driver.util :as driver.u]
@@ -51,7 +51,7 @@
table :- i/TableInstance]
(sync-util/with-error-handling (format "Error classifying data sensitivity for %s" (sync-util/name-for-logging field))
(let [category (analyze/infer-data-sensitivity field {:name (:name table) :entity_type (:entity_type table)})]
- (sync.db/update-field-data-sensitivity! (u/the-id field) (or category :PUBLIC))
+ (sync.db/update-field! (u/the-id field) {:data_sensitivity (or category :PUBLIC)})
category)))
(mu/defn scan-table! :- Stats
diff --git a/src/metabase/sync/db.clj b/src/metabase/sync/db.clj
index 6c57dc5ebcd..ba5ca7e6342 100644
--- a/src/metabase/sync/db.clj
+++ b/src/metabase/sync/db.clj
@@ -1,10 +1,6 @@
(ns metabase.sync.db
- "Application database queries for the sync module. Reads of what sync reconciles against the warehouse ask for the
- rows sync itself wrote (`{:user-settings? false}`), whatever a reader would be shown instead; choices of which
- Tables or Fields to act on read what users see.
-
- Every function here is a direct Toucan 2 call with no additional logic, so the rest of the module never talks to
- `toucan2.core` itself."
+ "Application database queries for the sync module, which reads the user settings of Tables and Fields to leave the
+ values users set alone."
(:require
[clojure.set :as set]
[metabase.app-db.core :as app-db]
@@ -14,19 +10,19 @@
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.models.table :as table]
[metabase.warehouse-schema.schema :as warehouse-schema.schema]
+ [metabase.warehouse-schema.user-settings :as warehouse-schema.user-settings]
[metabase.warehouses.schema :as warehouses.schema]
[toucan2.core :as t2]))
(mu/defn- sync-tables-clause
"Honey SQL clause matching the Tables aliased `table-alias` that take part in sync: active, with no
- `visibility_type` as users see it."
+ `visibility_type`."
[table-alias :- :keyword]
[:and
[:= (u/qualified-key table-alias :active) true]
- [:= (warehouse-schema-overlay/table-user-visibility-type table-alias) nil]])
+ [:= (u/qualified-key table-alias :visibility_type) nil]])
;;; ------------------------------------------------ Database ------------------------------------------------
@@ -95,39 +91,39 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn table-in-database
"The Table with `table-id` in the Database with `database-id`, or nil."
[database-id :- ::lib.schema.id/database
table-id :- ::lib.schema.id/table]
- (t2/select-one :model/Table :db_id database-id :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one :model/Table :db_id database-id :id table-id))
(mu/defn table-by-name
"The Table named `table-name` in the Database with `database-id`, or nil."
[database-id :- ::lib.schema.id/database
table-name :- :string]
- (t2/select-one :model/Table :db_id database-id :name table-name {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one :model/Table :db_id database-id :name table-name))
(mu/defn table-by-schema-and-name
"The Table named `table-name` in `schema` of the Database with `database-id`, or nil."
[database-id :- ::lib.schema.id/database
schema :- [:maybe :string]
table-name :- :string]
- (t2/select-one :model/Table :db_id database-id :name table-name :schema schema {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one :model/Table :db_id database-id :name table-name :schema schema))
(mu/defn inactive-table-by-schema-and-name
"The inactive Table named `table-name` in `schema` of the Database with `database-id`, or nil."
[database-id :- ::lib.schema.id/database
schema :- [:maybe :string]
table-name :- :string]
- (t2/select-one :model/Table :db_id database-id :schema schema :name table-name :active false {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one :model/Table :db_id database-id :schema schema :name table-name :active false))
(mu/defn active-table-id-by-name
"The ID of the active Table named `table-name` in the Database with `database-id`, or nil."
[database-id :- ::lib.schema.id/database
table-name :- :string]
- (t2/select-one-pk :model/Table :db_id database-id :name table-name :active true {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-pk :model/Table :db_id database-id :name table-name :active true))
(mu/defn active-tables-by-lower-name-and-schema
"The active Tables of the Database with `database-id` whose lower-cased name and schema match, hidden ones included."
@@ -138,8 +134,7 @@
:db_id database-id
:%lower.name lower-name
:%lower.schema lower-schema
- :active true
- {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ :active true))
(mu/defn tables-by-name
"The `columns` of the Tables of the Database with `database-id` named one of `table-names`."
@@ -159,46 +154,44 @@
:active false
:archived_at nil
:transform_target false
- :deactivated_at [:< (h2x/add-interval-honeysql-form (app-db/db-type) :%now amount unit)] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ :deactivated_at [:< (h2x/add-interval-honeysql-form (app-db/db-type) :%now amount unit)]))
(mu/defn table-database-ids
"A map of Table ID to Database ID for `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select-pk->fn :db_id :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-pk->fn :db_id :model/Table :id [:in table-ids]))
(mu/defn table-schemas-reducible
"Reducible `:schema` rows of the Tables of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/reducible-select [:model/Table :schema] :db_id database-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/reducible-select [:model/Table :schema] :db_id database-id))
(mu/defn active-table-ids-reducible
"Reducible `:id` rows of the active Tables of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/reducible-select [:model/Table :id] :db_id database-id :active true {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/reducible-select [:model/Table :id] :db_id database-id :active true))
(mu/defn active-table-count
"The number of active Tables in the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/count :model/Table :db_id database-id :active true {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/count :model/Table :db_id database-id :active true))
(mu/defn sync-table-ids
"The IDs of the synced Tables of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/select-fn-vec :id :model/Table :db_id database-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]
- :where (sync-tables-clause :metabase_table)}))
+ (t2/select-fn-vec :id :model/Table :db_id database-id {:where (sync-tables-clause (t2/table-name :model/Table))}))
(mu/defn sync-table-schemas
"The distinct `:schema` rows of the synced Tables of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
(t2/query {:select-distinct [:schema]
- :from [:metabase_table]
- :where [:and (sync-tables-clause :metabase_table) [:= :db_id database-id]]}))
+ :from [[(t2/table-name :model/Table) :t]]
+ :where [:and (sync-tables-clause :t) [:= :t.db_id database-id]]}))
(mu/defn sync-tables-count
"The number of synced Tables in the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/count :model/Table :db_id database-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]
- :where (sync-tables-clause :metabase_table)}))
+ (t2/count :model/Table :db_id database-id {:where (sync-tables-clause (t2/table-name :model/Table))}))
(mu/defn sync-tables-reducible
"Reducible synced Tables of the Database with `database-id` ordered by schema and name, optionally narrowed to
@@ -208,8 +201,7 @@
table-names :- [:maybe [:or [:set :string] [:sequential :string]]]]
(t2/reducible-select :model/Table
:db_id database-id
- {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]
- :where [:and (sync-tables-clause :metabase_table)
+ {:where [:and (sync-tables-clause (t2/table-name :model/Table))
(when (seq schema-names) [:in :schema schema-names])
(when (seq table-names) [:in :name table-names])]
:order-by [[:schema :asc] [:name :asc]]}))
@@ -219,10 +211,10 @@
[database-id :- ::lib.schema.id/database]
(t2/reducible-select :model/Table
{:select [:t.*]
- :from [(warehouse-schema-overlay/table-query {:user-settings? false :alias :t})]
+ :from [[(t2/table-name :model/Table) :t]]
:left-join [[^:allow-subquery {:select [:table_id
[[:min :last_analyzed] :earliest_last_analyzed]]
- :from [(warehouse-schema-overlay/field-query)]
+ :from [(t2/table-name :model/Field)]
:group-by [:table_id]} :sub]
[:= :t.id :sub.table_id]]
:where [:and (sync-tables-clause :t) [:= :t.db_id database-id]]
@@ -233,6 +225,11 @@
[table :- ::warehouse-schema.schema/table.update]
(t2/insert-returning-instance! :model/Table table))
+(mu/defn table-user-settings :- [:maybe ::warehouse-schema.schema/table-user-settings]
+ "The TableUserSettings of the Table with `table-id`, or nil."
+ [table-id :- ::lib.schema.id/table]
+ (t2/select-one :model/TableUserSettings :table_id table-id))
+
(mu/defn update-table!
"Apply `changes` to the Table with `table-id`, returning the number updated."
[table-id :- ::lib.schema.id/table
@@ -266,19 +263,16 @@
;;; ------------------------------------------------- Field -------------------------------------------------
(mu/defn fields
- "The Fields with `field-ids` as sync wrote them. Feeds `our-metadata`, which is diffed against the warehouse."
+ "The Fields with `field-ids`, which `our-metadata` diffs against the warehouse."
[field-ids :- [:sequential ::lib.schema.id/field]]
- (t2/select :model/Field :id [:in field-ids]
- {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select :model/Field :id [:in field-ids]))
(mu/defn fields-for-field-values
- "The columns needed to scan FieldValues of the Fields with `field-ids`, as users see them: the eligibility decision
- (`visibility_type`/`has_field_values`) has to see what the user set."
+ "The columns needed to scan FieldValues of the Fields with `field-ids`."
[field-ids :- [:sequential ::lib.schema.id/field]]
(t2/select [:model/Field :name :id :base_type :effective_type :coercion_strategy :semantic_type :visibility_type
:table_id :has_field_values]
- :id [:in field-ids]
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :id [:in field-ids]))
(defn- base-types->descendants
"Given a set of `base-types`, an expanded set including those types and all their descendants in the type
@@ -333,8 +327,7 @@
version->base-types :- [:map-of :int [:set [:or :keyword :string]]]
limit :- ms/PositiveInt]
(t2/select :model/Field
- {:from [(warehouse-schema-overlay/field-query)]
- :where [:and
+ {:where [:and
[:= :table_id table-id]
(needs-fingerprint-update-clause refingerprint? version->base-types)]
:order-by [[:id :asc]]
@@ -343,10 +336,10 @@
(mu/defn field-fingerprint
"The fingerprint of the Field with `field-id`."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :fingerprint :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :fingerprint :model/Field :id field-id))
(mu/defn active-fields-metadata-for-table
- "The sync metadata columns of the active Fields of the Table with `table-id`, as sync wrote them, in field order."
+ "The sync metadata columns of the active Fields of the Table with `table-id`, in field order."
[table-id :- ::lib.schema.id/table]
(t2/select [:model/Field :name :database_type :base_type :effective_type :coercion_strategy :semantic_type
:parent_id :id :description :database_position :nfc_path
@@ -355,8 +348,7 @@
:database_partitioned :json_unfolding :position :preview_display]
:table_id table-id
:active true
- {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]
- :order-by table/field-order-rule}))
+ {:order-by table/field-order-rule}))
(mu/defn normal-fields-for-table
"Up to `limit` active, normal-visibility Fields of the Table with `table-id`, ordered by ID."
@@ -366,8 +358,7 @@
:table_id table-id
:active true
:visibility_type "normal"
- {:from [(warehouse-schema-overlay/field-query)]
- :order-by [[:id :asc]], :limit limit}))
+ {:order-by [[:id :asc]], :limit limit}))
(mu/defn inactive-fields-by-lower-name
"The inactive Fields of the Table with `table-id` under `parent-id` whose lower-cased name is one of `lower-names`."
@@ -378,7 +369,7 @@
:table_id table-id
:%lower.name [:in lower-names]
:parent_id parent-id
- :active false {:from [(warehouse-schema-overlay/field-query)]}))
+ :active false))
(mu/defn incomplete-analysis-fields-for-table
"The active, visible Fields of the Table with `table-id` fingerprinted at `fingerprint-version` but not yet analyzed."
@@ -389,8 +380,7 @@
:active true
:visibility_type [:not-in ["sensitive" "retired"]]
:fingerprint_version fingerprint-version
- :last_analyzed nil
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :last_analyzed nil))
(mu/defn name-field-count-for-table
"The number of active, visible Fields of the Table with `table-id` whose semantic type is `:type/Name`."
@@ -399,8 +389,7 @@
:table_id table-id
:active true
:visibility_type [:not-in ["sensitive" "retired"]]
- :semantic_type :type/Name
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :semantic_type :type/Name))
(mr/def ::cursor
"A keyset-pagination cursor over Field ids: zero starts before the first Field, since ids are positive."
@@ -408,15 +397,14 @@
(mu/defn- unscored-fields-clause
"Honey SQL conditions matching the active, visible Fields of the Database with `database-id` that have no
- dimension interestingness score. Applied to [[warehouse-schema-overlay/field-query]], so `visibility_type` is the
- value as users see it."
+ dimension interestingness score."
[database-id :- ::lib.schema.id/database]
[:and
[:= :active true]
[:= :dimension_interestingness nil]
[:not-in :visibility_type ["sensitive" "retired"]]
[:in :table_id ^:allow-subquery {:select [:id]
- :from [(warehouse-schema-overlay/table-query {:user-settings? false})]
+ :from [(t2/table-name :model/Table)]
:where [:= :db_id database-id]}]])
(mu/defn unscored-field-ids-for-database :- [:sequential ::lib.schema.id/field]
@@ -433,7 +421,7 @@
;; backfill has caught up
(or (t2/select-fn-vec :id :model/Field
{:select [:id]
- :from [(warehouse-schema-overlay/field-query)]
+ :from [(t2/table-name :model/Field)]
:where [:and (unscored-fields-clause database-id) [:> :id after-id]]
:order-by [[:id :asc]]
:limit limit})
@@ -442,19 +430,18 @@
(mu/defn unscored-field-count-for-database :- :int
"How many active, visible Fields of the Database with `database-id` have no dimension interestingness score."
[database-id :- ::lib.schema.id/database]
- (t2/count :model/Field {:from [(warehouse-schema-overlay/field-query)]
- :where (unscored-fields-clause database-id)}))
+ (t2/count :model/Field {:where (unscored-fields-clause database-id)}))
(mu/defn fields-for-interestingness-scoring :- [:sequential (ms/InstanceOf :model/Field)]
- "The Fields with `field-ids`, as users see them, for interestingness scoring."
+ "The Fields with `field-ids`, for interestingness scoring."
[field-ids :- [:sequential {:min 1} ::lib.schema.id/field]]
- (t2/select :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :id [:in field-ids]))
(mu/defn top-level-field-ids-by-name
"The IDs of the top-level Fields of the Table with `table-id` named one of `field-names`."
[table-id :- ::lib.schema.id/table
field-names :- [:sequential :string]]
- (t2/select-pks-vec :model/Field :name [:in field-names] :table_id table-id :parent_id nil {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-pks-vec :model/Field :name [:in field-names] :table_id table-id :parent_id nil))
(mu/defn top-level-field-ids-by-schema-table-and-name-reducible
"Reducible `:id` rows of the top-level Fields of the Database with `database-id` matching one of the
@@ -462,8 +449,8 @@
[database-id :- ::lib.schema.id/database
schema+table+names :- [:sequential [:tuple :string :string :string]]]
(t2/reducible-query {:select [[:f.id]]
- :from [(warehouse-schema-overlay/field-query {:alias :f})]
- :inner-join [(warehouse-schema-overlay/table-query {:alias :t}) [:= :f.table_id :t.id]]
+ :from [[(t2/table-name :model/Field) :f]]
+ :inner-join [[(t2/table-name :model/Table) :t] [:= :f.table_id :t.id]]
:where [:and
[:in [:composite [:coalesce :t.schema "__null__"] :t.name :f.name] schema+table+names]
[:= :t.db_id database-id]
@@ -472,15 +459,15 @@
(mu/defn indexed-field-ids-for-table
"The IDs of the Fields of the Table with `table-id` marked as indexed."
[table-id :- ::lib.schema.id/table]
- (t2/select-pks-set :model/Field :table_id table-id :database_indexed true {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-pks-set :model/Field :table_id table-id :database_indexed true))
(mu/defn indexed-top-level-field-ids-for-database
"The IDs of the top-level Fields of the Database with `database-id` marked as indexed."
[database-id :- ::lib.schema.id/database]
(t2/select-pks-set :model/Field
- {:from [(warehouse-schema-overlay/field-query)]}
+ {}
:table_id [:in ^:allow-subquery {:select [[:t.id]]
- :from [(warehouse-schema-overlay/table-query {:alias :t})]
+ :from [[(t2/table-name :model/Table) :t]]
:where [:= :t.db_id database-id]}]
:parent_id nil
:database_indexed true))
@@ -490,18 +477,22 @@
[rows :- [:sequential ::warehouse-schema.schema/field.update]]
(t2/insert-returning-pks! :model/Field rows))
+(mu/defn field-user-settings :- [:maybe ::warehouse-schema.schema/field-user-settings]
+ "The FieldUserSettings of the Field with `field-id`, or nil."
+ [field-id :- ::lib.schema.id/field]
+ (t2/select-one :model/FieldUserSettings :field_id field-id))
+
(mu/defn update-field!
"Apply `changes` to the Field with `field-id`, returning the number updated."
[field-id :- ::lib.schema.id/field
changes :- ::warehouse-schema.schema/field.update]
(t2/update! :model/Field :id field-id changes))
-(mu/defn update-field-by-name!
- "Apply `changes` to the Field named `field-name` of the Table with `table-id`, returning the number updated."
+(mu/defn field-id-by-name :- [:maybe ::lib.schema.id/field]
+ "The ID of the Field named `field-name` of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table
- field-name :- :string
- changes :- ::warehouse-schema.schema/field.update]
- (t2/update! :model/Field {:name field-name, :table_id table-id} changes))
+ field-name :- :string]
+ (t2/select-one-pk :model/Field :name field-name :table_id table-id))
(mu/defn reactivate-fields!
"Mark the Fields with `field-ids` active, returning the number updated."
@@ -555,18 +546,20 @@
(defn- fk-field-id-subquery
"Subquery selecting the (lowest) id of the Field named `column-name` on the Table `[table-schema table-name]` in
- the Database with `db-id`, excluding Fields with a user-set foreign key target or semantic type. `min` limits the
- subquery to one result (MySQL disallows `LIMIT` in subqueries), needed because schema/table/column names can be
- non-unique when lower-cased for some DBs."
- [db-id table-schema table-name column-name]
+ the Database with `db-id`, excluding, when `unless-user-set?`, Fields with a user-set foreign key target or semantic
+ type. `min` limits the subquery to one result (MySQL disallows `LIMIT` in subqueries), needed because
+ schema/table/column names can be non-unique when lower-cased for some DBs."
+ [db-id table-schema table-name column-name unless-user-set?]
^:allow-subquery
{:select [[[:min :f.id] :id]]
- :from [(warehouse-schema-overlay/field-query {:alias :f})]
- :join [(warehouse-schema-overlay/table-query {:alias :t}) [:= :f.table_id :t.id]]
- :left-join [[:metabase_field_user_settings :u] [:= :f.id :u.field_id]]
+ :from [[(t2/table-name :model/Field) :f]]
+ :join [[(t2/table-name :model/Table) :t] [:= :f.table_id :t.id]]
+ :left-join [[(t2/table-name :model/FieldUserSettings) :u] [:= :f.id :u.field_id]]
:where [:and
- [:= :u.fk_target_field_id nil]
- [:= :u.semantic_type nil]
+ (when unless-user-set?
+ [:and
+ [:= [:coalesce :u.fk_target_field_id_set false] false]
+ [:= [:coalesce :u.semantic_type_set false] false]])
[:= :t.db_id db-id]
[:= [:lower :f.name] (u/lower-case-en column-name)]
[:= [:lower :t.name] (u/lower-case-en table-name)]
@@ -587,14 +580,18 @@
pk-table-schema :- [:maybe :string]
pk-table-name :- :string
pk-column-name :- :string]
- (let [fk-id (:id (t2/query-one (fk-field-id-subquery db-id fk-table-schema fk-table-name fk-column-name)))
- pk-id (:id (t2/query-one (fk-field-id-subquery db-id pk-table-schema pk-table-name pk-column-name)))
- field (when (and fk-id pk-id)
- (t2/select-one [:model/Field :id :fk_target_field_id :has_field_values] :id fk-id
- {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))]
- (if (and field (not= pk-id (:fk_target_field_id field)))
- (t2/update! :model/Field :id fk-id (cond-> {:fk_target_field_id pk-id :semantic_type :type/FK}
- (= :auto-list (:has_field_values field)) (assoc :has_field_values nil)))
+ (let [fk-id (:id (t2/query-one (fk-field-id-subquery db-id fk-table-schema fk-table-name fk-column-name true)))
+ pk-id (:id (t2/query-one (fk-field-id-subquery db-id pk-table-schema pk-table-name pk-column-name false)))]
+ (if (and fk-id pk-id)
+ (let [field (t2/select-one [:model/Field :id :fk_target_field_id :has_field_values] :id fk-id)
+ user-set (warehouse-schema.user-settings/user-set-field-columns (field-user-settings fk-id))]
+ (if (and (not (user-set :fk_target_field_id)) (not= pk-id (:fk_target_field_id field)))
+ (update-field!
+ fk-id (apply dissoc
+ (cond-> {:fk_target_field_id pk-id :semantic_type :type/FK}
+ (= :auto-list (:has_field_values field)) (assoc :has_field_values nil))
+ user-set))
+ 0))
0)))
(defn- data-sensitivity-to-scan-clause
@@ -605,18 +602,27 @@
[:or [:= :data_sensitivity nil] [:= :data_sensitivity "PUBLIC"]]
[:= :data_sensitivity nil]))
+(defn- user-set-data-sensitivity-clause
+ "Honey SQL clause matching Fields whose `data_sensitivity` a user set."
+ []
+ [:exists ^:allow-subquery {:select [1]
+ :from [[(t2/table-name :model/FieldUserSettings) :s]]
+ :where [:and
+ [:= :s.field_id :metabase_field.id]
+ [:not= :s.data_sensitivity nil]]}])
+
(mu/defn fields-to-scan-for-data-sensitivity
- "The active, non-retired Fields of the Table with `table-id` the data-sensitivity classifier still has to scan, by
- sync's own labels, ordered by ID."
+ "The active, non-retired Fields of the Table with `table-id` the data-sensitivity classifier still has to scan,
+ without a label a user set, ordered by ID."
[table-id :- ::lib.schema.id/table
rescan-public? :- [:maybe :boolean]]
(t2/select :model/Field
- {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]
- :where [:and
+ {:where [:and
[:= :table_id table-id]
[:= :active true]
[:not= :visibility_type "retired"]
- (data-sensitivity-to-scan-clause rescan-public?)]
+ (data-sensitivity-to-scan-clause rescan-public?)
+ [:not (user-set-data-sensitivity-clause)]]
:order-by [[:id :asc]]}))
(mu/defn table-ids-with-fields-to-scan-for-data-sensitivity
@@ -626,8 +632,8 @@
rescan-public? :- [:maybe :boolean]]
(t2/select-fn-set :table_id :model/Field
{:select [[:metabase_field.table_id :table_id]]
- :from [(warehouse-schema-overlay/field-query {:user-settings? false})]
- :join [(warehouse-schema-overlay/table-query {:user-settings? false}) [:= :metabase_field.table_id :metabase_table.id]]
+ :from [(t2/table-name :model/Field)]
+ :join [(t2/table-name :model/Table) [:= :metabase_field.table_id :metabase_table.id]]
:where [:and
[:= :metabase_table.db_id database-id]
[:= :metabase_table.active true]
@@ -639,45 +645,34 @@
(mu/defn tables-by-schema-and-name-reducible
"Reducible Tables with `table-ids`, ordered by schema and name."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/reducible-select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]
- :order-by [[:schema :asc] [:name :asc]]}))
-
-(mu/defn update-field-data-sensitivity!
- "Set the `data_sensitivity` of the Field with `field-id` to `data-sensitivity`, returning the number updated."
- [field-id :- ::lib.schema.id/field
- data-sensitivity :- [:or :keyword :string]]
- (t2/update! :model/Field :id field-id {:data_sensitivity data-sensitivity}))
-
-(def ^:private classifier-data-sensitivity-clause
- "Honey SQL clause matching Fields whose non-null `data_sensitivity` has no value in the `FieldUserSettings` mirror,
- i.e. was written by the data-sensitivity classifier rather than a user."
+ (t2/reducible-select :model/Table :id [:in table-ids] {:order-by [[:schema :asc] [:name :asc]]}))
+
+(defn- classifier-data-sensitivity-clause
+ "Honey SQL clause matching Fields whose non-null `data_sensitivity` the data-sensitivity classifier wrote."
+ []
[:and
[:not= :data_sensitivity nil]
- [:not [:exists ^:allow-subquery {:select [1]
- :from [[:metabase_field_user_settings :s]]
- :where [:and
- [:= :s.field_id :metabase_field.id]
- [:not= :s.data_sensitivity nil]]}]]])
+ [:not (user-set-data-sensitivity-clause)]])
(mu/defn reset-classifier-data-sensitivity-for-table!
"Clear the classifier-written `data_sensitivity` (see [[classifier-data-sensitivity-clause]]) of the Fields of the
Table with `table-id`. Returns the number of Fields cleared."
[table-id :- ::lib.schema.id/table]
- (t2/query-one {:update :metabase_field
+ (t2/query-one {:update (t2/table-name :model/Field)
:set {:data_sensitivity nil}
- :where [:and [:= :table_id table-id] classifier-data-sensitivity-clause]}))
+ :where [:and [:= :table_id table-id] (classifier-data-sensitivity-clause)]}))
(mu/defn reset-classifier-data-sensitivity-for-database!
"Clear the classifier-written `data_sensitivity` (see [[classifier-data-sensitivity-clause]]) of the Fields of every
Table of the Database with `database-id`. Returns the number of Fields cleared."
[database-id :- ::lib.schema.id/database]
- (t2/query-one {:update :metabase_field
+ (t2/query-one {:update (t2/table-name :model/Field)
:set {:data_sensitivity nil}
:where [:and
[:in :table_id ^:allow-subquery {:select [:id]
- :from [:metabase_table]
+ :from [(t2/table-name :model/Table)]
:where [:= :db_id database-id]}]
- classifier-data-sensitivity-clause]}))
+ (classifier-data-sensitivity-clause)]}))
;;; ---------------------------------------------- FieldValues ----------------------------------------------
diff --git a/src/metabase/sync/sync_metadata/fields/sync_metadata.clj b/src/metabase/sync/sync_metadata/fields/sync_metadata.clj
index 3d4180d6b37..eb6dc85fb51 100644
--- a/src/metabase/sync/sync_metadata/fields/sync_metadata.clj
+++ b/src/metabase/sync/sync_metadata/fields/sync_metadata.clj
@@ -4,7 +4,6 @@
Fields that were not newly created; newly created Fields are given appropriate metadata when first synced."
(:require
[clojure.string :as str]
- [metabase.sync.db :as sync.db]
[metabase.sync.interface :as i]
[metabase.sync.sync-metadata.crufty :as crufty]
[metabase.sync.sync-metadata.fields.common :as common]
@@ -128,7 +127,7 @@
old-base-type
new-base-type)
(when-not (= old-base-type :type/*)
- (field-user-settings/unset-user-settings!
+ (field-user-settings/unset-field-user-settings!
(select-keys metabase-field [:id]) [:effective_type :coercion_strategy :semantic_type]))
{:base_type new-base-type
:effective_type new-base-type
@@ -146,7 +145,7 @@
(common/field-metadata-name-for-logging table metabase-field)
old-effective-type
new-base-type)
- (field-user-settings/unset-user-settings! (select-keys metabase-field [:id]) [:effective_type :coercion_strategy])
+ (field-user-settings/unset-field-user-settings! (select-keys metabase-field [:id]) [:effective_type :coercion_strategy])
{:effective_type new-base-type})
(when new-semantic-type?
(log/infof "Semantic type of %s has changed from '%s' to '%s'."
@@ -236,7 +235,7 @@
{:preview_display false}))]
;; if any updates need to be done, do them and return 1 (because 1 Field was updated), otherwise return 0
(if (and (seq updates)
- (pos? (sync.db/update-field! (u/the-id metabase-field) updates)))
+ (pos? (sync-util/update-field-unless-user-set! (u/the-id metabase-field) updates)))
1
0)))
diff --git a/src/metabase/sync/sync_metadata/metabase_metadata.clj b/src/metabase/sync/sync_metadata/metabase_metadata.clj
index ce2723a138c..c54ad51c901 100644
--- a/src/metabase/sync/sync_metadata/metabase_metadata.clj
+++ b/src/metabase/sync/sync_metadata/metabase_metadata.clj
@@ -56,8 +56,9 @@
;; TODO: this needs to support schemas
(when-let [table-id (sync.db/active-table-id-by-name (u/the-id database) table-name)]
(if field-name
- (sync.db/update-field-by-name! table-id field-name {k value})
- (sync.db/update-table! table-id {k value})))
+ (when-let [field-id (sync.db/field-id-by-name table-id field-name)]
+ (sync-util/update-field-unless-user-set! field-id {k value}))
+ (sync-util/update-table-unless-user-set! table-id {k value})))
(sync.db/update-database! (u/the-id database) {k value})))))
(mu/defn- sync-metabase-metadata-table!
diff --git a/src/metabase/sync/sync_metadata/tables.clj b/src/metabase/sync/sync_metadata/tables.clj
index 4506a353809..3195b2d1837 100644
--- a/src/metabase/sync/sync_metadata/tables.clj
+++ b/src/metabase/sync/sync_metadata/tables.clj
@@ -192,13 +192,13 @@
[database :- i/DatabaseInstance
existing :- (ms/InstanceOf :model/Table)
{:keys [sample?]} :- [:map {:closed true} [:sample? :boolean]]]
- (sync.db/update-table! (:id existing)
- (cond-> (cruft-dependent-cols existing database ::reactivate)
- ;; do not unhide tables w/ cruft settings
- (some? (:visibility_type existing)) (dissoc :visibility_type)
- true (assoc :active true)
- sample? (assoc :data_authority :ingested
- :data_source :ingested))))
+ (sync-util/update-table-unless-user-set! (:id existing)
+ (cond-> (cruft-dependent-cols existing database ::reactivate)
+ ;; do not unhide tables w/ cruft settings
+ (some? (:visibility_type existing)) (dissoc :visibility_type)
+ true (assoc :active true)
+ sample? (assoc :data_authority :ingested
+ :data_source :ingested))))
(mu/defn create-or-reactivate-table!
"Create a single new table in the database, or mark it as active if a matching inactive one exists."
@@ -310,7 +310,7 @@
v))
(boolean
(when (seq changes)
- (sync.db/update-table! (:id metabase-table) changes)
+ (sync-util/update-table-unless-user-set! (:id metabase-table) changes)
true))))
(mu/defn- update-tables-metadata-if-needed! :- :int
diff --git a/src/metabase/sync/util.clj b/src/metabase/sync/util.clj
index a61144aeab1..8bbbec91ced 100644
--- a/src/metabase/sync/util.clj
+++ b/src/metabase/sync/util.clj
@@ -11,6 +11,7 @@
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
[metabase.events.core :as events]
+ [metabase.lib.schema.id :as lib.schema.id]
[metabase.models.interface :as mi]
[metabase.premium-features.core :as premium-features]
[metabase.query-processor.interface :as qp.i]
@@ -25,6 +26,8 @@
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
[metabase.util.memory :as u.mem]
+ [metabase.warehouse-schema.schema :as warehouse-schema.schema]
+ [metabase.warehouse-schema.user-settings :as warehouse-schema.user-settings]
[metabase.warehouses.models.database :as database]
[toucan2.realize :as t2.realize])
(:import
@@ -804,3 +807,23 @@
(and (or (isa? base-type :type/Text)
(isa? base-type :type/TextLike))
(can-be-list? base-type semantic-type)))
+
+(mu/defn update-field-unless-user-set! :- ms/IntGreaterThanOrEqualToZero
+ "Apply to the Field with `field-id` the `changes` to columns no user set, returning the number updated."
+ [field-id :- ::lib.schema.id/field
+ changes :- ::warehouse-schema.schema/field.update]
+ (let [changes (apply dissoc changes (warehouse-schema.user-settings/user-set-field-columns
+ (sync.db/field-user-settings field-id)))]
+ (if (seq changes)
+ (sync.db/update-field! field-id changes)
+ 0)))
+
+(mu/defn update-table-unless-user-set! :- ms/IntGreaterThanOrEqualToZero
+ "Apply to the Table with `table-id` the `changes` to columns no user set, returning the number updated."
+ [table-id :- ::lib.schema.id/table
+ changes :- ::warehouse-schema.schema/table.update]
+ (let [changes (apply dissoc changes (warehouse-schema.user-settings/user-set-table-columns
+ (sync.db/table-user-settings table-id)))]
+ (if (seq changes)
+ (sync.db/update-table! table-id changes)
+ 0)))
diff --git a/src/metabase/transforms/db.clj b/src/metabase/transforms/db.clj
index faa312b97c6..d3b34631435 100644
--- a/src/metabase/transforms/db.clj
+++ b/src/metabase/transforms/db.clj
@@ -11,7 +11,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(def ^:private no-active-run-clause
@@ -830,7 +829,7 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn databases
"The Databases with `database-ids`."
@@ -845,7 +844,7 @@
(mu/defn tables
"The Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
(mu/defn table-indexes-for-transforms
"The TableIndexes of the Transforms with `transform-ids`, ordered by index name."
@@ -855,17 +854,17 @@
(mu/defn field
"The Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one :model/Field :id field-id))
(mu/defn field-exists?
"Whether a Field with `field-id` exists."
[field-id :- ::lib.schema.id/field]
- (t2/exists? :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/exists? :model/Field :id field-id))
(mu/defn active-field-ids-by-name
"A map of name to ID for the active Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-fn->fn :name :id [:model/Field :name :id] :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-fn->fn :name :id [:model/Field :name :id] :table_id table-id :active true))
(mu/defn active-users
"The active Users with `user-ids`."
diff --git a/src/metabase/transforms_base/db.clj b/src/metabase/transforms_base/db.clj
index 0c0e75db93e..81f379f4794 100644
--- a/src/metabase/transforms_base/db.clj
+++ b/src/metabase/transforms_base/db.clj
@@ -5,7 +5,6 @@
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn transform-source
@@ -50,7 +49,7 @@
(mu/defn field-table-id
"The Table id of the Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :table_id :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :table_id :model/Field :id field-id))
(mu/defn target-table
"The Table named `table-name` in `schema` of the Database with `database-id` also matching the key-value
@@ -64,14 +63,14 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn table-for-transform
"The Table owned by the Transform with `transform-id`, or nil. `transform-id` is nil for a transform
that hasn't been saved yet, in which case there is no owned Table either."
[transform-id :- [:maybe ::lib.schema.id/transform]]
(when transform-id
- (t2/select-one :model/Table :transform_id transform-id {:from [(warehouse-schema-overlay/table-query)]})))
+ (t2/select-one :model/Table :transform_id transform-id)))
(mu/defn update-table!
"Apply `changes` to the Table with `table-id`."
@@ -109,16 +108,14 @@
[table-id :- ::lib.schema.id/table]
(t2/select-fn-vec :name [:model/Field :name :position]
:table_id table-id :active true
- {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]
- :order-by [[:position :asc]]}))
+ {:order-by [[:position :asc]]}))
(mu/defn table-refs-matching
"The id, Database id, schema, and name of the Tables matching any of `refs` (each a `[db-id schema table-name]`
triple; `schema` may be nil)."
[refs :- [:sequential [:tuple ms/PositiveInt [:maybe :string] :string]]]
(t2/select [:model/Table :id :db_id :schema :name]
- {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]
- :where (into [:or]
+ {:where (into [:or]
(map (fn [[db-id schema table-name]]
[:and
[:= :db_id db-id]
@@ -131,4 +128,4 @@
(mu/defn table-refs
"The id, Database id, schema, and name of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select [:model/Table :id :db_id :schema :name] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select [:model/Table :id :db_id :schema :name] :id [:in table-ids]))
diff --git a/src/metabase/upload/db.clj b/src/metabase/upload/db.clj
index 7e4542603ef..cc2d9c5bf9b 100644
--- a/src/metabase/upload/db.clj
+++ b/src/metabase/upload/db.clj
@@ -7,7 +7,6 @@
[metabase.queries.schema :as queries.schema]
[metabase.util :as u]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.core :as warehouse-schema]
[metabase.warehouse-schema.humanization :as humanization]
[toucan2.core :as t2]))
@@ -46,7 +45,7 @@
(mu/defn active-fields-for-table
"The active Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select :model/Field :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :table_id table-id :active true))
(mu/defn user-edited-field-names
"The lower-cased `names`, among the Fields of the Table with `table-id`, that have a user-set display name."
@@ -65,9 +64,7 @@
name->display-name (apply dissoc name->display-name user-edited)]
(doseq [{:keys [id name display_name]} (when (seq name->display-name)
(t2/select [:model/Field :id :name :display_name]
- {:from [(warehouse-schema-overlay/field-query
- {:user-settings? false})]
- :where [:and
+ {:where [:and
[:= :table_id table-id]
[:in [:lower :name] (keys name->display-name)]]}))
:let [lower-name (u/lower-case-en name)]
@@ -123,9 +120,9 @@
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn tables
"The Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
diff --git a/src/metabase/usage_metadata/db.clj b/src/metabase/usage_metadata/db.clj
index 1b344e8bf56..9d94b2e90a0 100644
--- a/src/metabase/usage_metadata/db.clj
+++ b/src/metabase/usage_metadata/db.clj
@@ -7,7 +7,6 @@
[metabase.usage-metadata.schema :as usage-metadata.schema]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn query-execution-hash-counts
@@ -115,17 +114,17 @@
(mu/defn field-names
"The id, name, and display name of the Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select [:model/Field :id :name :display_name] :id [:in (mapv long field-ids)] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select [:model/Field :id :name :display_name] :id [:in (mapv long field-ids)]))
(mu/defn table-names
"The id, name, display name, Database id, and schema of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select [:model/Table :id :name :display_name :db_id :schema] :id [:in (mapv long table-ids)] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select [:model/Table :id :name :display_name :db_id :schema] :id [:in (mapv long table-ids)]))
(mu/defn table-database-ids
"The id and Database id of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select [:model/Table :id :db_id] :id [:in (mapv long table-ids)] {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select [:model/Table :id :db_id] :id [:in (mapv long table-ids)]))
(mu/defn card-names
"The id and name of the Cards with `card-ids`."
diff --git a/src/metabase/warehouse_schema/core.clj b/src/metabase/warehouse_schema/core.clj
index f1a592f30a4..cde42283c06 100644
--- a/src/metabase/warehouse_schema/core.clj
+++ b/src/metabase/warehouse_schema/core.clj
@@ -2,11 +2,19 @@
"API namespace for the warehouse-schema module."
(:require
[metabase.warehouse-schema.db]
+ [metabase.warehouse-schema.models.field-user-settings]
+ [metabase.warehouse-schema.models.table-user-settings]
[potemkin :as p]))
(comment
- metabase.warehouse-schema.db/keep-me)
+ metabase.warehouse-schema.db/keep-me
+ metabase.warehouse-schema.models.field-user-settings/keep-me
+ metabase.warehouse-schema.models.table-user-settings/keep-me)
(p/import-vars
[metabase.warehouse-schema.db
- user-renamed-field-names])
+ user-renamed-field-names]
+ [metabase.warehouse-schema.models.field-user-settings
+ delete-field-user-settings!]
+ [metabase.warehouse-schema.models.table-user-settings
+ delete-table-user-settings!])
diff --git a/src/metabase/warehouse_schema/db.clj b/src/metabase/warehouse_schema/db.clj
index f3519ab2619..a9baf4fb5bd 100644
--- a/src/metabase/warehouse_schema/db.clj
+++ b/src/metabase/warehouse_schema/db.clj
@@ -10,7 +10,6 @@
[metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.schema :as warehouse-schema.schema]
[toucan2.core :as t2]))
@@ -21,7 +20,7 @@
(mu/defn field
"The ::warehouse-schema.schema/field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one :model/Field :id field-id))
(mu/defn field-in-path
"The ::warehouse-schema.schema/field named by the last of `field-names` (each nested inside the previous, bottom-most first) under
@@ -33,46 +32,44 @@
(mu/defn fields
"The Fields with `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :id [:in field-ids]))
(mu/defn fields-by-id
"A map of ID to ::warehouse-schema.schema/field for `field-ids`."
[field-ids :- [:sequential ::lib.schema.id/field]]
- (t2/select-fn->fn :id identity :model/Field :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-fn->fn :id identity :model/Field :id [:in field-ids]))
(mu/defn field-table-id-rows
"The ID and ::warehouse-schema.schema/table ID of the Fields with `field-ids`."
[field-ids :- [:sequential ::lib.schema.id/field]]
- (t2/select [:model/Field :id :table_id] :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select [:model/Field :id :table_id] :id [:in field-ids]))
(mu/defn field-table-id
"The ::warehouse-schema.schema/table ID of the ::warehouse-schema.schema/field with `field-id`."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :table_id :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :table_id :model/Field :id field-id))
(mu/defn field-id-by-name
"The ID of the ::warehouse-schema.schema/field named `field-name` under `parent-id` in the ::warehouse-schema.schema/table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table
parent-id :- [:maybe ms/PositiveInt]
field-name :- :string]
- (t2/select-one-pk :model/Field :name field-name :parent_id parent-id :table_id table-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-pk :model/Field :name field-name :parent_id parent-id :table_id table-id))
(mu/defn field-values-eligibility
"The columns deciding whether the ::warehouse-schema.schema/field with `field-id` should have FieldValues, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one [:model/Field :base_type :visibility_type :has_field_values :preview_display] :id field-id
- {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one [:model/Field :base_type :visibility_type :has_field_values :preview_display] :id field-id))
(mu/defn field-ids-for-table
"The IDs of the Fields of the ::warehouse-schema.schema/table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-pks-set :model/Field {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]
- :where [:= :table_id table-id]}))
+ (t2/select-pks-set :model/Field {:where [:= :table_id table-id]}))
(mu/defn active-field-ids-for-table
"The IDs of the active Fields of the ::warehouse-schema.schema/table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-pks-set :model/Field :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-pks-set :model/Field :table_id table-id :active true))
(defn- field-order-order-by
[field-order]
@@ -93,8 +90,7 @@
or `:alphabetical`)."
[table-id :- ::lib.schema.id/table
field-order :- [:enum :custom :smart :database :alphabetical]]
- (t2/select [:model/Field :id] :table_id table-id {:from [(warehouse-schema-overlay/field-query)]
- :order-by (field-order-order-by field-order)}))
+ (t2/select [:model/Field :id] :table_id table-id {:order-by (field-order-order-by field-order)}))
(mu/defn active-fields-for-tables
"The active, unretired Fields of the Tables with `table-ids`, in field order."
@@ -103,8 +99,7 @@
:active true
:table_id [:in table-ids]
:visibility_type [:not= "retired"]
- {:from [(warehouse-schema-overlay/field-query)]
- :order-by field-order-rule}))
+ {:order-by field-order-rule}))
(mu/defn pk-field-ids-by-table
"A map of ::warehouse-schema.schema/table ID to the ID of its visible primary key ::warehouse-schema.schema/field for `table-ids`."
@@ -113,19 +108,27 @@
:table_id [:in table-ids]
:semantic_type (app-db/isa :type/PK)
:visibility_type [:not-in ["sensitive" "retired"]]
- {:from [(warehouse-schema-overlay/field-query {:alias :f})]}))
+ {:from [[(t2/table-name :model/Field) :f]]}))
(mu/defn update-field!
"Apply `changes` to the ::warehouse-schema.schema/field with `field-id`, returning the number updated."
[field-id :- ::lib.schema.id/field
- changes :- (mut/select-keys ::warehouse-schema.schema/field.update [:position])]
+ changes :- ::warehouse-schema.schema/field.update]
(t2/update! :model/Field :id field-id changes))
-(mu/defn clear-fk-targets-to-field!
- "Clear the FK semantic type and target of every ::warehouse-schema.schema/field targeting the ::warehouse-schema.schema/field with `field-id`, returning the number
+(mu/defn update-field-custom-positions!
+ "Set the `custom_position` of each ::warehouse-schema.schema/field in `field-id->position`, returning the number
updated."
+ [field-id->position :- [:map-of ::lib.schema.id/field :int]]
+ (t2/update! :model/Field :id [:in (keys field-id->position)]
+ {:custom_position (into [:case] (mapcat (fn [[id position]] [[:= :id id] position])) field-id->position)}))
+
+(mu/defn clear-fk-targets-to-field!
+ "Clear the target, and an FK semantic type, of every ::warehouse-schema.schema/field targeting the
+ ::warehouse-schema.schema/field with `field-id`, returning the number updated."
[field-id :- ::lib.schema.id/field]
- (t2/update! :model/Field {:fk_target_field_id field-id} {:semantic_type nil, :fk_target_field_id nil}))
+ (t2/update! :model/Field {:fk_target_field_id field-id :semantic_type :type/FK} {:semantic_type nil})
+ (t2/update! :model/Field {:fk_target_field_id field-id} {:fk_target_field_id nil}))
(mu/defn delete-child-fields!
"Delete the Fields nested under the ::warehouse-schema.schema/field with `parent-id`, returning the number deleted."
@@ -153,7 +156,7 @@
(t2/select-fn-set (comp u/lower-case-en :name)
:model/Field
{:select [:f.name]
- :from [(warehouse-schema-overlay/field-query {:alias :f, :user-settings? false})]
+ :from [[(t2/table-name :model/Field) :f]]
:left-join [[(t2/table-name :model/FieldUserSettings) :u] [:= :u.field_id :f.id]]
:where [:and
[:= :f.table_id table-id]
@@ -166,7 +169,7 @@
[]
(t2/reducible-query
{:select [:f.id :f.name :f.display_name [:u.display_name :user_display_name]]
- :from [(warehouse-schema-overlay/field-query {:alias :f, :user-settings? false})]
+ :from [[(t2/table-name :model/Field) :f]]
:left-join [[(t2/table-name :model/FieldUserSettings) :u] [:= :u.field_id :f.id]]}))
(mu/defn set-field-display-name!
@@ -183,21 +186,16 @@
[field-id :- [:maybe ::lib.schema.id/field]]
(t2/select-one :model/FieldUserSettings :field_id field-id))
-(mu/defn field-user-settings-exist?
- "Whether the ::warehouse-schema.schema/field with `field-id` has a FieldUserSettings row."
- [field-id :- ::lib.schema.id/field]
- (t2/exists? :model/FieldUserSettings :field_id field-id))
-
(def ^:private field-user-settings-update-keys
"The columns an insert or update of a FieldUserSettings accepts."
- [:field_id :created_at :updated_at :semantic_type :description :display_name :visibility_type :fk_target_field_id :has_field_values :effective_type :coercion_strategy :caveats :points_of_interest :nfc_path :json_unfolding :settings :data_sensitivity :custom_position :description_set :semantic_type_set :fk_target_field_id_set])
+ [:field_id :created_at :updated_at :semantic_type :description :display_name :visibility_type :fk_target_field_id :has_field_values :effective_type :coercion_strategy :caveats :points_of_interest :json_unfolding :settings :data_sensitivity :custom_position :description_set :semantic_type_set :fk_target_field_id_set])
(mu/defn field-ids-with-user-settings-for-table :- [:set ::lib.schema.id/field]
"The ids of the Fields of the ::warehouse-schema.schema/table with `table-id` that have a FieldUserSettings row."
[table-id :- ::lib.schema.id/table]
(set (t2/select-fn-set :field_id :model/FieldUserSettings
{:from [[(t2/table-name :model/FieldUserSettings) :u]]
- :join [(warehouse-schema-overlay/field-query {:alias :f, :user-settings? false}) [:= :f.id :u.field_id]]
+ :join [[(t2/table-name :model/Field) :f] [:= :f.id :u.field_id]]
:where [:= :f.table_id table-id]})))
(mu/defn dimension-ids-for-table :- [:set ::lib.schema.id/dimension]
@@ -205,7 +203,7 @@
[table-id :- ::lib.schema.id/table]
(set (t2/select-pks-set :model/Dimension
{:from [[(t2/table-name :model/Dimension) :d]]
- :join [(warehouse-schema-overlay/field-query {:alias :f, :user-settings? false}) [:= :f.id :d.field_id]]
+ :join [[(t2/table-name :model/Field) :f] [:= :f.id :d.field_id]]
:where [:= :f.table_id table-id]})))
(mu/defn field-user-settings-for-tables
@@ -214,7 +212,7 @@
(t2/select :model/FieldUserSettings
{:select [:u.* [:f.table_id :table_id] [:f.name :field_name]]
:from [[(t2/table-name :model/FieldUserSettings) :u]]
- :join [(warehouse-schema-overlay/field-query {:alias :f, :user-settings? false}) [:= :f.id :u.field_id]]
+ :join [[(t2/table-name :model/Field) :f] [:= :f.id :u.field_id]]
:where [:in :f.table_id table-ids]
:order-by [[:f.name :asc]]}))
@@ -229,6 +227,14 @@
changes :- (mut/select-keys ::warehouse-schema.schema/field-user-settings.update field-user-settings-update-keys)]
(t2/update! :model/FieldUserSettings :field_id field-id changes))
+(mu/defn update-or-insert-field-user-settings!
+ "Update the FieldUserSettings of the ::warehouse-schema.schema/field with `field-id` with what `update-fn` returns
+ for it, or insert what `update-fn` returns for nil when it has none."
+ [field-id :- ::lib.schema.id/field
+ update-fn :- [:=> [:cat [:maybe ::warehouse-schema.schema/field-user-settings]]
+ (mut/select-keys ::warehouse-schema.schema/field-user-settings.update field-user-settings-update-keys)]]
+ (app-db/update-or-insert! :model/FieldUserSettings {:field_id field-id} update-fn))
+
(mu/defn field-ids-with-user-settings :- [:set ::lib.schema.id/field]
"The ids, among `field-ids`, of the Fields that have a FieldUserSettings row."
[field-ids :- [:sequential ::lib.schema.id/field]]
@@ -242,16 +248,24 @@
{:custom_position (into [:case] (mapcat (fn [[id position]] [[:= :field_id id] position])) field-id->position)}))
(mu/defn delete-field-user-settings!
- "Delete the FieldUserSettings of the ::warehouse-schema.schema/field with `field-id`, returning the number deleted."
- [field-id :- ::lib.schema.id/field]
- (t2/delete! :model/FieldUserSettings :field_id field-id))
+ "Delete the FieldUserSettings of the Fields with `field-ids`, returning the number deleted."
+ [field-ids :- [:sequential ::lib.schema.id/field]]
+ (if (seq field-ids)
+ (t2/delete! :model/FieldUserSettings :field_id [:in field-ids])
+ 0))
(mu/defn clear-user-settings-fk-targets-to-field!
"Unset the user-set FK semantic type and target of every ::warehouse-schema.schema/field targeting the
- ::warehouse-schema.schema/field with `field-id`, so the sync values show again. Returns the number updated."
+ ::warehouse-schema.schema/field with `field-id`, by its settings or by its own row, returning the number updated."
[field-id :- ::lib.schema.id/field]
- (t2/update! :model/FieldUserSettings {:fk_target_field_id field-id}
- {:semantic_type nil, :semantic_type_set false, :fk_target_field_id nil, :fk_target_field_id_set false}))
+ (let [changes {:semantic_type nil, :semantic_type_set false, :fk_target_field_id nil, :fk_target_field_id_set false}]
+ (+ (t2/update! :model/FieldUserSettings {:fk_target_field_id field-id} changes)
+ (t2/update! :model/FieldUserSettings
+ :semantic_type :type/FK
+ :field_id [:in ^:allow-subquery {:select [:id]
+ :from [(t2/table-name :model/Field)]
+ :where [:= :fk_target_field_id field-id]}]
+ changes))))
;;; -------------------------------------------- TableUserSettings --------------------------------------------
@@ -260,7 +274,7 @@
[:table_id :created_at :updated_at :display_name :description :entity_type :visibility_type :caveats
:points_of_interest :data_layer :data_source :owner_email :owner_user_id :field_order :show_in_getting_started
:data_authority :is_published :collection_id :description_set
- :visibility_type_set :caveats_set :points_of_interest_set :data_layer_set :data_source_set])
+ :visibility_type_set :data_layer_set :data_source_set])
(mu/defn table-user-settings
"The TableUserSettings of the ::warehouse-schema.schema/table with `table-id`, or nil (also for a nil `table-id`,
@@ -289,6 +303,14 @@
[rows :- [:or (mut/select-keys ::warehouse-schema.schema/table-user-settings.update table-user-settings-update-keys) [:sequential (mut/select-keys ::warehouse-schema.schema/table-user-settings.update table-user-settings-update-keys)]]]
(t2/insert! :model/TableUserSettings rows))
+(mu/defn update-or-insert-table-user-settings!
+ "Update the TableUserSettings of the ::warehouse-schema.schema/table with `table-id` with what `update-fn` returns
+ for it, or insert what `update-fn` returns for nil when it has none."
+ [table-id :- ::lib.schema.id/table
+ update-fn :- [:=> [:cat [:maybe ::warehouse-schema.schema/table-user-settings]]
+ (mut/select-keys ::warehouse-schema.schema/table-user-settings.update table-user-settings-update-keys)]]
+ (app-db/update-or-insert! :model/TableUserSettings {:table_id table-id} update-fn))
+
(mu/defn update-table-user-settings!
"Apply `changes` to the TableUserSettings of the ::warehouse-schema.schema/table with `table-id`, returning the
number updated."
@@ -297,19 +319,11 @@
(t2/update! :model/TableUserSettings :table_id table-id changes))
(mu/defn delete-table-user-settings!
- "Delete the TableUserSettings of the ::warehouse-schema.schema/table with `table-id`, returning the number deleted."
- [table-id :- ::lib.schema.id/table]
- (t2/delete! :model/TableUserSettings :table_id table-id))
-
-(mu/defn delete-field-user-settings-for-table!
- "Delete the FieldUserSettings of the Fields of the ::warehouse-schema.schema/table with `table-id`, returning the
- number deleted."
- [table-id :- ::lib.schema.id/table]
- (t2/delete! :model/FieldUserSettings
- {:where [:exists ^:allow-subquery
- {:select 1
- :from [[(t2/table-name :model/Field) :f]]
- :where [:and [:= :f.id :field_id] [:= :f.table_id table-id]]}]}))
+ "Delete the TableUserSettings of the Tables with `table-ids`, returning the number deleted."
+ [table-ids :- [:sequential ::lib.schema.id/table]]
+ (if (seq table-ids)
+ (t2/delete! :model/TableUserSettings :table_id [:in table-ids])
+ 0))
(mu/defn table-user-settings-for-tables
"The TableUserSettings of the Tables with `table-ids`, or every TableUserSettings when nil."
@@ -341,7 +355,7 @@
"The ::warehouse-schema.schema/field ID, values, and ::warehouse-schema.schema/table ID of the full FieldValues of the normal Fields of the Tables with `table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
(t2/select [:model/FieldValues :field_id :values :field.table_id]
- {:join [(warehouse-schema-overlay/field-query {:alias :field})
+ {:join [[(t2/table-name :model/Field) :field]
[:= :metabase_fieldvalues.field_id :field.id]]
:where [:and
[:in :field.table_id table-ids]
@@ -424,34 +438,46 @@
(mu/defn table
"The ::warehouse-schema.schema/table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
+
+(mu/defn update-table!
+ "Apply `changes` to the ::warehouse-schema.schema/table with `table-id`, returning the number updated."
+ [table-id :- ::lib.schema.id/table
+ changes :- ::warehouse-schema.schema/table.update]
+ (t2/update! :model/Table :id table-id changes))
+
+(mu/defn update-tables!
+ "Apply `changes` to the Tables with `table-ids`, returning the number updated."
+ [table-ids :- [:set ::lib.schema.id/table]
+ changes :- ::warehouse-schema.schema/table.update]
+ (t2/update! :model/Table :id [:in table-ids] changes))
(mu/defn tables
"The Tables with `table-ids`."
[table-ids :- [:or [:set ::lib.schema.id/table] [:sequential ::lib.schema.id/table]]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
(mu/defn table-by-name
"The ::warehouse-schema.schema/table named `table-name` in `schema` of the ::warehouses.schema/database with `database-id`, or nil."
[database-id :- ::lib.schema.id/database
schema :- [:maybe :string]
table-name :- :string]
- (t2/select-one :model/Table :name table-name :db_id database-id :schema schema {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :name table-name :db_id database-id :schema schema))
(mu/defn table-name-and-schema
"The name and schema of the ::warehouse-schema.schema/table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-one [:model/Table :name :schema] :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one [:model/Table :name :schema] :id table-id))
(mu/defn table-database-id
"The ::warehouses.schema/database ID of the ::warehouse-schema.schema/table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table :id table-id))
(defn active-tables-for-database
"The active Tables of the Database with `database-id`."
[database-id]
- (t2/select :model/Table :db_id database-id :active true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :db_id database-id :active true))
(defn active-table-schemas
"The distinct schemas of the active Tables of the Database with `database-id`, in schema order. When
@@ -461,8 +487,7 @@
(let [clauses (cond-> []
(not include-hidden?) (conj [:= :visibility_type nil]))]
(t2/select-fn-set :schema :model/Table :db_id database-id :active true
- (merge {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:%lower.schema :asc]]}
+ (merge {:order-by [[:%lower.schema :asc]]}
(when clauses
{:where (into [:and] clauses)})))))
@@ -473,8 +498,7 @@
:db_id database-id
:schema schema
:active true
- {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:display_name :asc]]}))
+ {:order-by [[:display_name :asc]]}))
(defn active-visible-tables-in-schema
"The active, visible Tables in `schema` of the Database with `database-id`, in display name order."
@@ -484,8 +508,7 @@
:schema schema
:active true
:visibility_type nil
- {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:display_name :asc]]}))
+ {:order-by [[:display_name :asc]]}))
(mu/defn unarchived-segments-for-tables
"The unarchived Segments of the Tables with `table-ids`, ordered by name."
@@ -526,7 +549,7 @@
(t2/reducible-query
{:select [:t.id :t.name :t.display_name
[:u.display_name :user_display_name]]
- :from [(warehouse-schema-overlay/table-query {:alias :t, :user-settings? false})]
+ :from [[(t2/table-name :model/Table) :t]]
:left-join [[(t2/table-name :model/TableUserSettings) :u] [:= :u.table_id :t.id]]}))
(mu/defn set-table-display-name!
diff --git a/src/metabase/warehouse_schema/models/field.clj b/src/metabase/warehouse_schema/models/field.clj
index fe88f7c2fdd..e6daaa878ec 100644
--- a/src/metabase/warehouse_schema/models/field.clj
+++ b/src/metabase/warehouse_schema/models/field.clj
@@ -15,7 +15,6 @@
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.db :as warehouse-schema.db]
[metabase.warehouse-schema.humanization :as humanization]
[metabase.warehouse-schema.models.field-values :as field-values]
@@ -166,15 +165,11 @@
(-> (merge defaults field)
enforce-effective-type-invariant)))
-(def field-user-settings
- "Set of user-settable values for a Field; see [[warehouse-schema-overlay/user-settable-field-columns]]."
- warehouse-schema-overlay/user-settable-field-columns)
-
(t2/define-before-update :model/Field
[field]
(when (false? (:active (t2/changes field)))
- (warehouse-schema.db/clear-fk-targets-to-field! (:id field))
- (warehouse-schema.db/clear-user-settings-fk-targets-to-field! (:id field)))
+ (warehouse-schema.db/clear-user-settings-fk-targets-to-field! (:id field))
+ (warehouse-schema.db/clear-fk-targets-to-field! (:id field)))
(enforce-effective-type-invariant field))
(t2/define-before-delete :model/Field
diff --git a/src/metabase/warehouse_schema/models/field_user_settings.clj b/src/metabase/warehouse_schema/models/field_user_settings.clj
index c287536671d..f0f507ca8d7 100644
--- a/src/metabase/warehouse_schema/models/field_user_settings.clj
+++ b/src/metabase/warehouse_schema/models/field_user_settings.clj
@@ -5,10 +5,10 @@
[metabase.models.serialization :as serdes]
[metabase.util :as u]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.db :as warehouse-schema.db]
[metabase.warehouse-schema.models.field :as field]
[metabase.warehouse-schema.schema :as warehouse-schema.schema]
+ [metabase.warehouse-schema.user-settings :as user-settings]
[methodical.core :as methodical]
[toucan2.core :as t2]))
@@ -23,8 +23,7 @@
:visibility_type mi/transform-keyword
:has_field_values mi/transform-keyword
:data_sensitivity mi/transform-keyword
- :settings mi/transform-json
- :nfc_path mi/transform-json})
+ :settings mi/transform-json})
(doto :model/FieldUserSettings
(derive :metabase/model)
@@ -39,16 +38,16 @@
(and (contains? effective column) (not (contains? effective flag)))
(assoc flag true)))
settings
- warehouse-schema-overlay/field-user-settings-flags))
+ user-settings/field-set-flags))
(methodical/defmethod t2/primary-keys :model/FieldUserSettings [_model] [:field_id])
(defn- delete-when-empty!
"Delete `settings` when it holds no user value and no true flag, returning it either way."
[settings]
- (when (and (every? #(nil? (get settings %)) warehouse-schema-overlay/user-settable-field-columns)
- (not-any? #(get settings %) (vals warehouse-schema-overlay/field-user-settings-flags)))
- (warehouse-schema.db/delete-field-user-settings! (:field_id settings)))
+ (when (and (every? #(nil? (get settings %)) user-settings/user-settable-field-columns)
+ (not-any? #(get settings %) (vals user-settings/field-set-flags)))
+ (warehouse-schema.db/delete-field-user-settings! [(:field_id settings)]))
settings)
(t2/define-before-insert :model/FieldUserSettings
@@ -67,39 +66,55 @@
[settings]
(delete-when-empty! settings))
-(mu/defn upsert-user-settings
- "Record the user-settable Field columns present in `settings` as the user values of `field`."
+(mu/defn upsert-field-user-settings!
+ "Record the user-settable Field columns present in `settings` as the user values of `field`, in its settings and its
+ row; a nil for a column without a set flag unsets it."
[{:keys [id]} :- ::warehouse-schema.schema/field
settings :- ::warehouse-schema.schema/field.update]
- (let [settings (u/select-keys-when settings :present field/field-user-settings)]
+ (let [settings (u/select-keys-when settings :present user-settings/user-settable-field-columns)]
(when (seq settings)
- (if (warehouse-schema.db/field-user-settings-exist? id)
- (warehouse-schema.db/update-field-user-settings! id (with-set-flags settings settings))
- (warehouse-schema.db/insert-field-user-settings! (assoc settings :field_id id))))))
+ (t2/with-transaction [_]
+ (warehouse-schema.db/update-or-insert-field-user-settings!
+ id (fn [existing] (cond->> settings existing (with-set-flags settings))))
+ (warehouse-schema.db/update-field! id settings)))))
(mu/defn set-custom-positions!
- "Record `field-id->position` as the Fields' user `custom_position`s."
+ "Record `field-id->position` as the Fields' user `custom_position`s, in their settings and their rows."
[field-id->position :- [:map-of ::lib.schema.id/field :int]]
- (let [existing (warehouse-schema.db/field-ids-with-user-settings (keys field-id->position))
- missing (remove existing (keys field-id->position))]
- (when (seq missing)
- (warehouse-schema.db/insert-field-user-settings!
- (mapv (fn [id] {:field_id id :custom_position (field-id->position id)}) missing)))
- (when (seq existing)
- (warehouse-schema.db/update-field-user-settings-custom-positions! (select-keys field-id->position existing)))))
-
-(mu/defn unset-user-settings!
- "Drop the user values of the Field columns `ks` for `field`."
+ (t2/with-transaction [_]
+ (let [existing (warehouse-schema.db/field-ids-with-user-settings (keys field-id->position))
+ missing (remove existing (keys field-id->position))]
+ (when (seq missing)
+ (warehouse-schema.db/insert-field-user-settings!
+ (mapv (fn [id] {:field_id id :custom_position (field-id->position id)}) missing)))
+ (when (seq existing)
+ (warehouse-schema.db/update-field-user-settings-custom-positions! (select-keys field-id->position existing)))
+ (warehouse-schema.db/update-field-custom-positions! field-id->position))))
+
+(mu/defn unset-field-user-settings!
+ "Drop the user values of the Field columns `ks` for `field` from its settings, leaving its row as it is for sync to
+ take over."
[{:keys [id]} :- ::warehouse-schema.schema/field
- ks :- [:sequential (into [:enum] warehouse-schema-overlay/user-settable-field-columns)]]
- (when (warehouse-schema.db/field-user-settings-exist? id)
+ ks :- [:sequential (into [:enum] user-settings/user-settable-field-columns)]]
+ (when (warehouse-schema.db/field-user-settings id)
(warehouse-schema.db/update-field-user-settings!
id
(into {} (mapcat (fn [k]
(cond-> [[k nil]]
- (warehouse-schema-overlay/field-user-settings-flags k) (conj [(warehouse-schema-overlay/field-user-settings-flags k) false]))))
+ (user-settings/field-set-flags k) (conj [(user-settings/field-set-flags k) false]))))
ks))))
+(mu/defn delete-field-user-settings!
+ "Delete the settings of the Fields with `field-ids`, leaving their rows as they are for sync to take over."
+ [field-ids :- [:sequential ::lib.schema.id/field]]
+ (doseq [batch (partition-all 1000 field-ids)]
+ (warehouse-schema.db/delete-field-user-settings! batch)))
+
+(mu/defn delete-field-user-settings-for-table!
+ "[[delete-field-user-settings!]] for every Field of the Table with `table-id`."
+ [table-id :- ::lib.schema.id/table]
+ (delete-field-user-settings! (vec (warehouse-schema.db/field-ids-with-user-settings-for-table table-id))))
+
(defmethod serdes/extract-query "FieldUserSettings" [_model-name {:keys [filter-column filter-ids] :as opts}]
(if (= filter-column :table_id)
(warehouse-schema.db/field-user-settings-for-tables filter-ids)
@@ -118,14 +133,22 @@
(defmethod serdes/load-update! "FieldUserSettings" [model-name ingested local]
((get-method serdes/load-update! :default)
model-name
- (merge (zipmap warehouse-schema-overlay/user-settable-field-columns (repeat nil)) ingested)
+ (merge (zipmap user-settings/user-settable-field-columns (repeat nil)) ingested)
local))
+(defmethod serdes/load-one! "FieldUserSettings" [ingested maybe-local]
+ (t2/with-transaction [_]
+ (u/prog1 (serdes/default-load-one! ingested maybe-local)
+ (when-let [field-id (or (:field_id maybe-local) (:field_id <>))]
+ (let [settings (warehouse-schema.db/field-user-settings field-id)]
+ (warehouse-schema.db/update-field! field-id (select-keys settings (user-settings/user-set-field-columns settings))))))))
+
(defmethod serdes/make-spec "FieldUserSettings" [_model-name _opts]
{:copy [:semantic_type :description :display_name :visibility_type
:has_field_values :effective_type :coercion_strategy :caveats
- :points_of_interest :nfc_path :json_unfolding :settings :data_sensitivity :custom_position
+ :points_of_interest :json_unfolding :settings :data_sensitivity :custom_position
:description_set :semantic_type_set :fk_target_field_id_set]
+ :skip [:nfc_path]
:defaults {:description_set false
:semantic_type_set false
:fk_target_field_id_set false}
diff --git a/src/metabase/warehouse_schema/models/table.clj b/src/metabase/warehouse_schema/models/table.clj
index b1052c008c9..77d352bf233 100644
--- a/src/metabase/warehouse_schema/models/table.clj
+++ b/src/metabase/warehouse_schema/models/table.clj
@@ -15,7 +15,6 @@
[metabase.util :as u]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.db :as warehouse-schema.db]
[metabase.warehouse-schema.humanization :as humanization]
[metabase.warehouse-schema.schema]
@@ -148,11 +147,7 @@
(mi/define-batched-hydration-method with-table
:table
- "Hydrate the Table of each of `instances` from its `table_id`.
-
- Batched rather than automagic because automagic hydration selects the model's own row, which shows what sync wrote:
- `mi/can-read?`/`can-query?` read `is_published` and `collection_id` off the hydrated Table to decide access through
- a published collection, and those are user values."
+ "Hydrate the Table of each of `instances` from its `table_id`, with one query for all of them."
[instances]
(let [table-ids (into #{} (keep :table_id) instances)
id->table (when (seq table-ids)
@@ -642,7 +637,6 @@
(search.spec/define-spec "table"
{:model :model/Table
- :source #(warehouse-schema-overlay/table-query {:alias :this})
:attrs {;; legacy search uses :active for this, but then has a rule to only ever show active tables
;; so we moved that to the where clause
:archived false
@@ -652,8 +646,7 @@
:database-id :db_id
:view-count true
:created-at true
- :updated-at [:case [:> :settings.updated_at :this.updated_at] :settings.updated_at
- :else :this.updated_at]
+ :updated-at true
:is-published :is_published
:collection-type :collection.type
:collection-location :collection.location
@@ -682,5 +675,4 @@
[:= :db.router_database_id nil]
[:not= :this.db_id [:inline audit/audit-db-id]]]
:joins {:db [:model/Database [:= :db.id :this.db_id]]
- :settings [:model/TableUserSettings [:= :settings.table_id :this.id]]
:collection [:model/Collection [:and [:= :this.is_published true] [:= :collection.id :this.collection_id]]]}})
diff --git a/src/metabase/warehouse_schema/models/table_user_settings.clj b/src/metabase/warehouse_schema/models/table_user_settings.clj
index a9fc7d5e360..8fed0b127e0 100644
--- a/src/metabase/warehouse_schema/models/table_user_settings.clj
+++ b/src/metabase/warehouse_schema/models/table_user_settings.clj
@@ -5,11 +5,11 @@
[metabase.models.serialization :as serdes]
[metabase.util :as u]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.db :as warehouse-schema.db]
[metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[metabase.warehouse-schema.models.table :as table]
[metabase.warehouse-schema.schema :as warehouse-schema.schema]
+ [metabase.warehouse-schema.user-settings :as user-settings]
[methodical.core :as methodical]
[toucan2.core :as t2]))
@@ -34,24 +34,26 @@
(doto :model/TableUserSettings
(derive :metabase/model)
(derive :hook/timestamped?)
- (derive :hook/search-index)
(derive :hook/dependencies))
(methodical/defmethod t2/primary-keys :model/TableUserSettings [_model] [:table_id])
(defn- complete-pairs
- "Fill in the other half of the two-column choices in `settings`: `visibility_type`/`data_layer`, and
- `is_published`/`collection_id`."
+ "Fill in the other half of the two-column choices in `settings`: `visibility_type`/`data_layer`, derived from a
+ changing half or else kept from `table`, and `is_published`/`collection_id`."
[settings table]
- (let [changing? (fn [k] (and (contains? settings k)
- (not= (some-> (get settings k) keyword)
- (some-> (get table k) keyword))))]
+ (let [changing? (fn [k] (not= (some-> (get settings k) keyword)
+ (some-> (get table k) keyword)))]
(cond-> settings
- (and (changing? :visibility_type) (not (contains? settings :data_layer)))
- (assoc :data_layer (table/visibility-type->data-layer (some-> (:visibility_type settings) keyword)))
+ (and (contains? settings :visibility_type) (not (contains? settings :data_layer)))
+ (assoc :data_layer (if (changing? :visibility_type)
+ (table/visibility-type->data-layer (some-> (:visibility_type settings) keyword))
+ (:data_layer table)))
- (and (changing? :data_layer) (not (contains? settings :visibility_type)))
- (assoc :visibility_type (table/data-layer->visibility-type (some-> (:data_layer settings) keyword)))
+ (and (contains? settings :data_layer) (not (contains? settings :visibility_type)))
+ (assoc :visibility_type (if (changing? :data_layer)
+ (table/data-layer->visibility-type (some-> (:data_layer settings) keyword))
+ (:visibility_type table)))
(and (contains? settings :collection_id) (not (contains? settings :is_published)))
(assoc :is_published (boolean (:is_published table))))))
@@ -64,7 +66,7 @@
(and (contains? effective column) (not (contains? explicit flag)))
(assoc flag true)))
settings
- warehouse-schema-overlay/table-user-settings-flags))
+ user-settings/table-set-flags))
(defn- enforce-invariants
"Validate `explicit`, complete its two-column pairs and set its flags, merged into `settings`."
@@ -78,9 +80,9 @@
(defn- delete-when-empty!
"Delete `settings` when it holds no user value and no true flag, returning it either way."
[settings]
- (when (and (every? #(nil? (get settings %)) warehouse-schema-overlay/user-settable-table-columns)
- (not-any? #(get settings %) (vals warehouse-schema-overlay/table-user-settings-flags)))
- (warehouse-schema.db/delete-table-user-settings! (:table_id settings)))
+ (when (and (every? #(nil? (get settings %)) user-settings/user-settable-table-columns)
+ (not-any? #(get settings %) (vals user-settings/table-set-flags)))
+ (warehouse-schema.db/delete-table-user-settings! [(:table_id settings)]))
settings)
(t2/define-before-insert :model/TableUserSettings
@@ -99,28 +101,42 @@
[settings]
(delete-when-empty! settings))
-(mu/defn upsert-user-settings
- "Record the user-settable Table columns present in `settings` as the user values of `table`."
+(defn- without-derived-half
+ "Drop from `changes` the half of a `visibility_type`/`data_layer` pair the Table's own update derives from the other."
+ [{:keys [visibility_type data_layer] :as changes}]
+ (if (and (contains? changes :visibility_type) (contains? changes :data_layer))
+ (if (= (some-> data_layer keyword) (table/visibility-type->data-layer (some-> visibility_type keyword)))
+ (dissoc changes :data_layer)
+ (dissoc changes :visibility_type))
+ changes))
+
+(mu/defn upsert-table-user-settings!
+ "Record the user-settable Table columns present in `settings` as the user values of `table`, in its settings and its
+ row; a nil for a column without a set flag unsets it."
[{:keys [id]} :- ::warehouse-schema.schema/table
settings :- ::warehouse-schema.schema/table.update]
- (let [settings (u/select-keys-when settings :present warehouse-schema-overlay/user-settable-table-columns)]
+ (let [settings (u/select-keys-when settings :present user-settings/user-settable-table-columns)]
(when (seq settings)
- (if (warehouse-schema.db/table-user-settings-exist? id)
- (warehouse-schema.db/update-table-user-settings! id (with-set-flags settings settings settings))
- (warehouse-schema.db/insert-table-user-settings! (assoc settings :table_id id))))))
-
-(mu/defn upsert-user-settings-for-tables!
- "[[upsert-user-settings]] for every Table in `table-ids`, in one statement per kind."
+ (t2/with-transaction [_]
+ (warehouse-schema.db/update-or-insert-table-user-settings!
+ id (fn [existing] (cond-> settings existing (with-set-flags settings settings))))
+ (warehouse-schema.db/update-table! id settings)))))
+
+(mu/defn upsert-table-user-settings-for-tables!
+ "[[upsert-table-user-settings!]] for every Table in `table-ids`, with one statement per kind for the settings and one for
+ the rows."
[table-ids :- [:set ::lib.schema.id/table]
settings :- ::warehouse-schema.schema/table.update]
- (let [settings (u/select-keys-when settings :present warehouse-schema-overlay/user-settable-table-columns)]
+ (let [settings (u/select-keys-when settings :present user-settings/user-settable-table-columns)]
(when (and (seq settings) (seq table-ids))
- (let [existing (warehouse-schema.db/table-ids-with-user-settings table-ids)]
- (when-let [missing (not-empty (remove existing table-ids))]
- (warehouse-schema.db/insert-table-user-settings! (mapv #(assoc settings :table_id %) missing)))
- (when (seq existing)
- (warehouse-schema.db/update-table-user-settings-for-tables!
- existing (with-set-flags settings settings settings)))))))
+ (t2/with-transaction [_]
+ (let [existing (warehouse-schema.db/table-ids-with-user-settings table-ids)]
+ (when-let [missing (not-empty (remove existing table-ids))]
+ (warehouse-schema.db/insert-table-user-settings! (mapv #(assoc settings :table_id %) missing)))
+ (when (seq existing)
+ (warehouse-schema.db/update-table-user-settings-for-tables!
+ existing (with-set-flags settings settings settings))))
+ (warehouse-schema.db/update-tables! table-ids settings)))))
(defn- valid-field-order?
"Field ordering is valid if all the fields from a given table are present and only from that table."
@@ -133,23 +149,29 @@
[table field-order]
{:pre [(valid-field-order? table field-order)]}
(t2/with-transaction [_]
- (upsert-user-settings table {:field_order :custom})
+ (upsert-table-user-settings! table {:field_order :custom})
(field-user-settings/set-custom-positions! (zipmap field-order (range)))
(table/update-field-positions! (warehouse-schema.db/table (u/the-id table)))))
-(mu/defn unset-user-settings!
- "Drop the user values of the Table columns `ks` for `table`."
+(mu/defn unset-table-user-settings!
+ "Drop the user values of the Table columns `ks` for `table` from its settings, leaving its row as it is for sync to
+ take over."
[{:keys [id]} :- ::warehouse-schema.schema/table
- ks :- [:sequential (into [:enum] warehouse-schema-overlay/user-settable-table-columns)]]
- (when (warehouse-schema.db/table-user-settings-exist? id)
+ ks :- [:sequential (into [:enum] user-settings/user-settable-table-columns)]]
+ (when (warehouse-schema.db/table-user-settings id)
(warehouse-schema.db/update-table-user-settings!
id
(into {} (mapcat (fn [k]
(cond-> [[k nil]]
- (warehouse-schema-overlay/table-user-settings-flags k)
- (conj [(warehouse-schema-overlay/table-user-settings-flags k) false]))))
+ (user-settings/table-set-flags k) (conj [(user-settings/table-set-flags k) false]))))
ks))))
+(mu/defn delete-table-user-settings!
+ "Delete the settings of the Tables with `table-ids`, leaving their rows as they are for sync to take over."
+ [table-ids :- [:sequential ::lib.schema.id/table]]
+ (doseq [batch (partition-all 1000 table-ids)]
+ (warehouse-schema.db/delete-table-user-settings! batch)))
+
;;; ------------------------------------------------- Serialization -------------------------------------------------
(defmethod serdes/extract-query "TableUserSettings" [_model-name {:keys [filter-ids]}]
@@ -172,27 +194,29 @@
(def ^:private legacy-fields
"The FieldUserSettings a settings file carried before they got files of their own."
(serdes/nested :model/FieldUserSettings :table_id
- {:delete-children! warehouse-schema.db/delete-field-user-settings-for-table!}))
+ {:delete-children! field-user-settings/delete-field-user-settings-for-table!}))
(defmethod serdes/load-one! "TableUserSettings" [ingested maybe-local]
- (let [settings (serdes/default-load-one! ingested maybe-local)
- table-id (serdes/*import-table-fk* (serdes/table-path->table-ref (pop (serdes/path ingested))))]
- (when (contains? ingested :fields)
- ((:import-with-context legacy-fields)
- (t2/instance :model/TableUserSettings {:table_id table-id}) :fields (:fields ingested)))
- (when (:field_order ingested)
- (table/update-field-positions! (warehouse-schema.db/table table-id)))
- settings))
+ (t2/with-transaction [_]
+ (let [settings (serdes/default-load-one! ingested maybe-local)
+ table-id (serdes/*import-table-fk* (serdes/table-path->table-ref (pop (serdes/path ingested))))]
+ (let [settings (warehouse-schema.db/table-user-settings table-id)]
+ (warehouse-schema.db/update-table! table-id (without-derived-half
+ (select-keys settings (user-settings/user-set-table-columns settings)))))
+ (when (contains? ingested :fields)
+ ((:import-with-context legacy-fields)
+ (t2/instance :model/TableUserSettings {:table_id table-id}) :fields (:fields ingested)))
+ (when (:field_order ingested)
+ (table/update-field-positions! (warehouse-schema.db/table table-id)))
+ settings)))
(defmethod serdes/make-spec "TableUserSettings" [_model-name _opts]
{:copy [:display_name :description :entity_type :visibility_type :field_order :caveats :points_of_interest
:show_in_getting_started :data_authority :data_source :owner_email :is_published
- :description_set :visibility_type_set :caveats_set
- :points_of_interest_set :data_layer_set :data_source_set]
+ :description_set :visibility_type_set :data_layer_set :data_source_set]
+ :skip [:caveats_set :points_of_interest_set]
:defaults {:description_set false
:visibility_type_set false
- :caveats_set false
- :points_of_interest_set false
:data_layer_set false
:data_source_set false}
:transform {:collection_id (serdes/fk :model/Collection)
diff --git a/src/metabase/warehouse_schema/schema.clj b/src/metabase/warehouse_schema/schema.clj
index 58c15abab28..fdafc8a3831 100644
--- a/src/metabase/warehouse_schema/schema.clj
+++ b/src/metabase/warehouse_schema/schema.clj
@@ -109,11 +109,11 @@
[:coercion_strategy {:optional true} [:maybe [:or :keyword :string]]]
[:caveats {:optional true} [:maybe :string]]
[:points_of_interest {:optional true} [:maybe :string]]
- [:nfc_path {:optional true} [:maybe [:sequential [:or :string :keyword]]]]
[:json_unfolding {:optional true} [:maybe :boolean]]
[:settings {:optional true} [:maybe ::field-user-settings.settings]]
[:data_sensitivity {:optional true} [:maybe [:or :keyword :string]]]
[:custom_position {:optional true} [:maybe :int]]
+ [:nfc_path {:optional true} [:maybe :string]]
[:description_set {:optional true} :boolean]
[:semantic_type_set {:optional true} :boolean]
[:fk_target_field_id_set {:optional true} :boolean]])
diff --git a/src/metabase/warehouse_schema/user_settings.clj b/src/metabase/warehouse_schema/user_settings.clj
new file mode 100644
index 00000000000..a9d2ba45535
--- /dev/null
+++ b/src/metabase/warehouse_schema/user_settings.clj
@@ -0,0 +1,54 @@
+(ns metabase.warehouse-schema.user-settings
+ "The user-settable columns of Fields and Tables, and how their settings rows say which ones a user set."
+ (:require
+ [metabase.util.malli :as mu]
+ [metabase.warehouse-schema.schema :as warehouse-schema.schema]))
+
+(def user-settable-field-columns
+ "The Field columns users can set, whose values `metabase_field` holds while `metabase_field_user_settings` records
+ which ones a user set."
+ #{:semantic_type :description :display_name :visibility_type :has_field_values :effective_type :coercion_strategy
+ :fk_target_field_id :caveats :points_of_interest :json_unfolding :settings :data_sensitivity :custom_position})
+
+(def field-set-flags
+ "The user-settable Field columns sync also writes, mapped to the flag that says the user set the column, NULL
+ included."
+ {:description :description_set
+ :semantic_type :semantic_type_set
+ :fk_target_field_id :fk_target_field_id_set})
+
+(mu/defn user-set-field-columns :- [:set (into [:enum] user-settable-field-columns)]
+ "The columns the FieldUserSettings row `settings` says a user set; `coercion_strategy` follows `effective_type`."
+ [settings :- [:maybe ::warehouse-schema.schema/field-user-settings]]
+ (into #{}
+ (filter (fn [column]
+ (cond
+ (field-set-flags column) (true? (get settings (field-set-flags column)))
+ (= column :coercion_strategy) (some? (:effective_type settings))
+ :else (some? (get settings column)))))
+ user-settable-field-columns))
+
+(def user-settable-table-columns
+ "The Table columns users can set, whose values `metabase_table` holds while `metabase_table_user_settings` records
+ which ones a user set."
+ #{:display_name :description :entity_type :visibility_type :caveats :points_of_interest :data_layer :data_source
+ :owner_email :owner_user_id :field_order :show_in_getting_started :data_authority :is_published :collection_id})
+
+(def table-set-flags
+ "The user-settable Table columns something besides users also writes, mapped to the flag that says the user set the
+ column, NULL included."
+ {:description :description_set
+ :visibility_type :visibility_type_set
+ :data_layer :data_layer_set
+ :data_source :data_source_set})
+
+(mu/defn user-set-table-columns :- [:set (into [:enum] user-settable-table-columns)]
+ "The columns the TableUserSettings row `settings` says a user set; `collection_id` follows `is_published`."
+ [settings :- [:maybe ::warehouse-schema.schema/table-user-settings]]
+ (into #{}
+ (filter (fn [column]
+ (cond
+ (table-set-flags column) (true? (get settings (table-set-flags column)))
+ (= column :collection_id) (some? (:is_published settings))
+ :else (some? (get settings column)))))
+ user-settable-table-columns))
diff --git a/src/metabase/warehouse_schema_overlay/core.clj b/src/metabase/warehouse_schema_overlay/core.clj
deleted file mode 100644
index 7eb052dc29b..00000000000
--- a/src/metabase/warehouse_schema_overlay/core.clj
+++ /dev/null
@@ -1,179 +0,0 @@
-(ns metabase.warehouse-schema-overlay.core
- "Sources for queries over Fields and Tables: [[field-query]] and [[table-query]] merge each row with the values
- in its user-settings table."
- (:require
- [metabase.util :as u]
- [metabase.util.malli :as mu]
- [toucan2.core :as t2]))
-
-(def user-settable-field-columns
- "The Field columns users can set. Their user values live in `metabase_field_user_settings`, never in `metabase_field`."
- #{:semantic_type :description :display_name :visibility_type :has_field_values :effective_type :coercion_strategy
- :fk_target_field_id :caveats :points_of_interest :nfc_path :json_unfolding :settings :data_sensitivity
- :custom_position})
-
-(def field-user-settings-flags
- "The user-settable Field columns sync also writes, mapped to the flag that says the user set the column, NULL
- included."
- {:description :description_set
- :semantic_type :semantic_type_set
- :fk_target_field_id :fk_target_field_id_set})
-
-(def field-columns
- "Every column of `metabase_field`. Spelled out rather than read from `:metabase.warehouse-schema.schema/field`,
- which lives in a module above this one; `metabase.warehouse-schema-overlay.core-test` fails if the two drift."
- #{:active :base_type :caveats :coercion_strategy :created_at :custom_position :data_sensitivity :database_default
- :database_indexed :database_is_auto_increment :database_is_generated :database_is_nullable :database_is_pk
- :database_partitioned :database_position :database_required :database_type :description
- :dimension_interestingness :display_name :effective_type :fingerprint :fingerprint_version :fk_target_field_id
- :has_field_values :id :is_defective_duplicate :json_unfolding :last_analyzed :name :nfc_path :parent_id
- :points_of_interest :position :preview_display :semantic_type :settings :table_id :unique_field_helper
- :updated_at :visibility_type})
-
-(def ^:private sync-owned-field-columns
- "The columns of `metabase_field` users cannot set."
- (sort (remove user-settable-field-columns field-columns)))
-
-(mu/defn- field-user-settings-join
- "The `:left-join` entries joining `metabase_field_user_settings` as `settings-alias` to the Field table aliased
- `field-alias`; see [[field-user-settings-column]]."
- [field-alias :- :keyword
- settings-alias :- :keyword]
- [[(t2/table-name :model/FieldUserSettings) settings-alias]
- [:= (u/qualified-key settings-alias :field_id) (u/qualified-key field-alias :id)]])
-
-(mu/defn- field-user-set-condition
- "Honey SQL test for whether the user set `column`, or nil when a non-NULL value says so itself."
- [column :- (into [:enum] user-settable-field-columns)
- settings-alias :- :keyword]
- (if-let [flag (field-user-settings-flags column)]
- [:= (u/qualified-key settings-alias flag) true]
- (when (= column :coercion_strategy)
- [:not= (u/qualified-key settings-alias :effective_type) nil])))
-
-(mu/defn- field-user-settings-column
- "Honey SQL expression for `column` as users see it: the user value when set, else the Field's."
- [column :- (into [:enum] user-settable-field-columns)
- field-alias :- :keyword
- settings-alias :- :keyword]
- (let [field-column (u/qualified-key field-alias column)
- settings-column (u/qualified-key settings-alias column)]
- (if-let [condition (field-user-set-condition column settings-alias)]
- [:case condition settings-column :else field-column]
- (if (= column :json_unfolding)
- [:case [:= [:coalesce settings-column field-column] true] true :else false]
- [:coalesce settings-column field-column]))))
-
-(mu/defn field-query :- [:tuple :any :keyword]
- "The source a query over Fields reads from: `metabase_field` merged with the user values. `:alias` names it for
- joins; `{:user-settings? false}` gives sync's own values."
- ([]
- (field-query nil))
-
- ([{:keys [alias user-settings?]
- :or {alias (t2/table-name :model/Field)
- user-settings? true}} :- [:maybe [:map {:closed true}
- [:alias {:optional true} :keyword]
- [:user-settings? {:optional true} :boolean]]]]
- [(if user-settings?
- ^:allow-subquery
- {:select (into (mapv #(u/qualified-key :f %) sync-owned-field-columns)
- (map (fn [column] [(field-user-settings-column column :f :u) column]))
- (sort user-settable-field-columns))
- :from [[(t2/table-name :model/Field) :f]]
- :left-join (field-user-settings-join :f :u)}
- (t2/table-name :model/Field))
- alias]))
-
-(def user-settable-table-columns
- "The Table columns users can set. Their user values live in `metabase_table_user_settings`, never in
- `metabase_table`."
- #{:display_name :description :entity_type :visibility_type :caveats :points_of_interest :data_layer :data_source
- :owner_email :owner_user_id :field_order :show_in_getting_started :data_authority :is_published :collection_id})
-
-(def table-user-settings-flags
- "The user-settable Table columns a user can clear, mapped to the flag that says the user set the column, NULL
- included."
- {:description :description_set
- :visibility_type :visibility_type_set
- :caveats :caveats_set
- :points_of_interest :points_of_interest_set
- :data_layer :data_layer_set
- :data_source :data_source_set})
-
-(def table-columns
- "Every column of `metabase_table`. Spelled out rather than read from `:metabase.warehouse-schema.schema/table`,
- which lives in a module above this one; `metabase.warehouse-schema-overlay.core-test` fails if the two drift."
- #{:active :archived_at :caveats :collection_id :created_at :data_authority :data_layer :data_source
- :database_require_filter :db_id :deactivated_at :description :display_name :entity_type :estimated_row_count
- :field_order :id :initial_sync_status :is_defective_duplicate :is_published :is_upload :is_writable :name
- :owner_email :owner_user_id :points_of_interest :schema :show_in_getting_started :transform_id :transform_target
- :unique_table_helper :updated_at :view_count :visibility_type})
-
-(def ^:private sync-owned-table-columns
- "The columns of `metabase_table` users cannot set."
- (sort (remove user-settable-table-columns table-columns)))
-
-(mu/defn- table-user-settings-join
- "The `:left-join` entries joining `metabase_table_user_settings` as `settings-alias` to the Table table aliased
- `table-alias`; see [[table-user-settings-column]]."
- [table-alias :- :keyword
- settings-alias :- :keyword]
- [[(t2/table-name :model/TableUserSettings) settings-alias]
- [:= (u/qualified-key settings-alias :table_id) (u/qualified-key table-alias :id)]])
-
-(mu/defn- table-user-set-condition
- "Honey SQL test for whether the user set `column`, or nil when a non-NULL value says so itself; `collection_id`
- follows `is_published`."
- [column :- (into [:enum] user-settable-table-columns)
- settings-alias :- :keyword]
- (if-let [flag (table-user-settings-flags column)]
- [:= (u/qualified-key settings-alias flag) true]
- (when (= column :collection_id)
- [:not= (u/qualified-key settings-alias :is_published) nil])))
-
-(mu/defn- table-user-settings-column
- "Honey SQL expression for `column` as users see it: the user value when set, else the Table's."
- [column :- (into [:enum] user-settable-table-columns)
- table-alias :- :keyword
- settings-alias :- :keyword]
- (let [table-column (u/qualified-key table-alias column)
- settings-column (u/qualified-key settings-alias column)]
- (if-let [condition (table-user-set-condition column settings-alias)]
- [:case condition settings-column :else table-column]
- (if (#{:show_in_getting_started :is_published} column)
- [:case [:= [:coalesce settings-column table-column] true] true :else false]
- [:coalesce settings-column table-column]))))
-
-(mu/defn table-user-visibility-type
- "Honey SQL expression for the `visibility_type` users see for the Table aliased `table-alias`: the user value
- when set, else the Table's. Reads only `table-alias`'s `id`, so it can filter a query over raw `metabase_table`."
- [table-alias :- :keyword]
- ;; A scalar subquery, not a join, so the caller's rows keep `metabase_table`'s shape. It re-reads the Table so the
- ;; LEFT JOIN always yields one row: a user-set NULL must not fall back to the Table's value.
- ^:allow-subquery
- {:select [[(table-user-settings-column :visibility_type :vt_t :vt_u)]]
- :from [[(t2/table-name :model/Table) :vt_t]]
- :left-join (table-user-settings-join :vt_t :vt_u)
- :where [:= :vt_t.id (u/qualified-key table-alias :id)]})
-
-(mu/defn table-query :- [:tuple :any :keyword]
- "The source a query over Tables reads from: `metabase_table` merged with the user values, with the options of
- [[field-query]]."
- ([]
- (table-query nil))
-
- ([{:keys [alias user-settings?]
- :or {alias (t2/table-name :model/Table)
- user-settings? true}} :- [:maybe [:map {:closed true}
- [:alias {:optional true} :keyword]
- [:user-settings? {:optional true} :boolean]]]]
- [(if user-settings?
- ^:allow-subquery
- {:select (into (mapv #(u/qualified-key :t %) sync-owned-table-columns)
- (map (fn [column] [(table-user-settings-column column :t :u) column]))
- (sort user-settable-table-columns))
- :from [[(t2/table-name :model/Table) :t]]
- :left-join (table-user-settings-join :t :u)}
- (t2/table-name :model/Table))
- alias]))
diff --git a/src/metabase/warehouse_schema_rest/api/field.clj b/src/metabase/warehouse_schema_rest/api/field.clj
index 33e5b8d4043..1f1248579cf 100644
--- a/src/metabase/warehouse_schema_rest/api/field.clj
+++ b/src/metabase/warehouse_schema_rest/api/field.clj
@@ -155,17 +155,16 @@
:as body} :- [:map {:closed true}
[:caveats {:optional true} [:maybe ms/NonBlankString]]
[:description {:optional true} [:maybe ms/NonBlankString]]
- [:display_name {:optional true} [:maybe ms/NonBlankString]]
+ [:display_name {:optional true} ms/NonBlankString]
[:fk_target_field_id {:optional true} [:maybe ms/PositiveInt]]
[:points_of_interest {:optional true} [:maybe ms/NonBlankString]]
[:semantic_type {:optional true} [:maybe ms/FieldSemanticOrRelationTypeKeywordOrString]]
[:coercion_strategy {:optional true} [:maybe ms/CoercionStrategyKeywordOrString]]
- [:visibility_type {:optional true} [:maybe FieldVisibilityType]]
+ [:visibility_type {:optional true} FieldVisibilityType]
[:data_sensitivity {:optional true} [:maybe FieldDataSensitivity]]
- [:has_field_values {:optional true} [:maybe ::lib.schema.metadata/column.has-field-values]]
+ [:has_field_values {:optional true} ::lib.schema.metadata/column.has-field-values]
[:settings {:optional true} [:maybe ms/VisualizationSettings]]
- [:nfc_path {:optional true} [:maybe [:sequential ms/NonBlankString]]]
- [:json_unfolding {:optional true} [:maybe :boolean]]]]
+ [:json_unfolding {:optional true} :boolean]]]
(let [field (-> (warehouse-schema-rest.db/field id)
api/write-check
(t2/hydrate :dimensions))
@@ -200,7 +199,7 @@
(when removed-fk?
(clear-dimension-on-fk-change! field))
(clear-dimension-on-type-change! field (:base_type field) new-semantic-type)
- (schema.field-user-settings/upsert-user-settings
+ (schema.field-user-settings/upsert-field-user-settings!
field
(cond-> body
(or removed-fk? (contains? body :fk_target_field_id))
diff --git a/src/metabase/warehouse_schema_rest/api/table.clj b/src/metabase/warehouse_schema_rest/api/table.clj
index ba53b513e33..0be3aad0a95 100644
--- a/src/metabase/warehouse_schema_rest/api/table.clj
+++ b/src/metabase/warehouse_schema_rest/api/table.clj
@@ -186,7 +186,7 @@
[:points_of_interest {:optional true} [:maybe :string]]
[:show_in_getting_started {:optional true} [:maybe :boolean]]
[:field_order {:optional true} [:maybe FieldOrder]]
- [:data_authority {:optional true} [:maybe ::data-authority-write]]
+ [:data_authority {:optional true} ::data-authority-write]
[:data_source {:optional true} [:maybe :string]]
[:data_layer {:optional true} [:maybe :string]]
[:owner_email {:optional true} [:maybe :string]]
@@ -216,7 +216,7 @@
(u/update-some :data_layer keyword)
(u/update-some :data_source keyword)
not-empty)]
- (schema.table-user-settings/upsert-user-settings existing-table changes))
+ (schema.table-user-settings/upsert-table-user-settings! existing-table changes))
(let [updated-table (warehouse-schema-rest.db/table id)
changed-field-order? (not= (:field_order updated-table) (:field_order existing-table))]
(if changed-field-order?
diff --git a/src/metabase/warehouse_schema_rest/db.clj b/src/metabase/warehouse_schema_rest/db.clj
index 67818121665..20cf1eac491 100644
--- a/src/metabase/warehouse_schema_rest/db.clj
+++ b/src/metabase/warehouse_schema_rest/db.clj
@@ -9,7 +9,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.schema :as warehouse-schema.schema]
[toucan2.core :as t2]))
@@ -19,16 +18,16 @@
target-field-id :- ::lib.schema.id/field]
(t2/query {:select [[:source_t.db_id :source_db_id]
[:target_t.db_id :target_db_id]]
- :from [(warehouse-schema-overlay/field-query {:alias :sf})]
- :join [(warehouse-schema-overlay/table-query {:alias :source_t}) [:= :sf.table_id :source_t.id]
- (warehouse-schema-overlay/field-query {:alias :tf}) [:= :tf.id target-field-id]
- (warehouse-schema-overlay/table-query {:alias :target_t}) [:= :tf.table_id :target_t.id]]
+ :from [[(t2/table-name :model/Field) :sf]]
+ :join [[(t2/table-name :model/Table) :source_t] [:= :sf.table_id :source_t.id]
+ [(t2/table-name :model/Field) :tf] [:= :tf.id target-field-id]
+ [(t2/table-name :model/Table) :target_t] [:= :tf.table_id :target_t.id]]
:where [:= :sf.id source-field-id]}))
(mu/defn field
"The Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one :model/Field :id field-id))
(mu/defn set-nested-fields-active!
"Set the active flag of the Fields of the Table with `table-id` whose NFC path matches the SQL LIKE
@@ -123,17 +122,17 @@
:where [:and
[:= :d.to_entity_id :metabase_table.id]
[:= :d.to_entity_type "table"]]}]))]
- (t2/select :model/Table {:from [(warehouse-schema-overlay/table-query)], :where where, :order-by [[:name :asc]]})))
+ (t2/select :model/Table {:where where, :order-by [[:name :asc]]})))
(mu/defn tables-by-ids
"The Tables with `table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
(mu/defn table
"The Table with `table-id`, or nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn database
"The Database with `database-id`, or nil."
@@ -153,18 +152,17 @@
(mu/defn active-unretired-field-ids-for-table
"The ids of the active, unretired Fields of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-pks-set :model/Field :table_id table-id :visibility_type [:not= "retired"] :active true
- {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-pks-set :model/Field :table_id table-id :visibility_type [:not= "retired"] :active true))
(mu/defn field-ids-for-table
"The ids of the Fields of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-pks-set :model/Field :table_id table-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-pks-set :model/Field :table_id table-id))
(mu/defn active-fields-targeting
- "The active Fields whose FK target, as users see it, is one of `field-ids`."
+ "The active Fields whose FK target is one of `field-ids`."
[field-ids :- [:sequential ::lib.schema.id/field]]
- (t2/select :model/Field :fk_target_field_id [:in field-ids] :active true {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :fk_target_field_id [:in field-ids] :active true))
(mu/defn delete-field-values-for-fields!
"Delete the FieldValues of the Fields with `field-ids`."
diff --git a/src/metabase/warehouses/db.clj b/src/metabase/warehouses/db.clj
index ded820b7076..c27aec74a2a 100644
--- a/src/metabase/warehouses/db.clj
+++ b/src/metabase/warehouses/db.clj
@@ -5,7 +5,6 @@
[metabase.app-db.core :as mdb]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouses.schema :as warehouses.schema]
[toucan2.core :as t2]))
@@ -65,7 +64,7 @@
(mu/defn fields-exist-for-database?
"Whether any Field belongs to a Table of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/exists? :model/Field :table_id [:in (table-ids-of-database-query database-id)] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/exists? :model/Field :table_id [:in (table-ids-of-database-query database-id)]))
(mu/defn delete-childless-fields-for-database!
"Delete the childless Fields of the Tables of the Database with `database-id`, returning the number deleted."
@@ -107,14 +106,13 @@
"Delete the query Actions whose query runs against the Database with `database-id` and the implicit Actions that write
to one of its Tables, returning the number deleted."
[database-id :- ::lib.schema.id/database]
- (let [[table-subquery _] (warehouse-schema-overlay/table-query)]
- (if-let [action-ids (not-empty (into (t2/select-fn-set :action_id :model/QueryAction :database_id database-id)
- (t2/select-fn-set :action_id :model/ImplicitAction
- {:join [[table-subquery :table]
- [:= :table.id :implicit_action.table_id]]
- :where [:= :table.db_id database-id]})))]
- (t2/delete! :model/Action :id [:in action-ids])
- 0)))
+ (if-let [action-ids (not-empty (into (t2/select-fn-set :action_id :model/QueryAction :database_id database-id)
+ (t2/select-fn-set :action_id :model/ImplicitAction
+ {:join [[(t2/table-name :model/Table) :table]
+ [:= :table.id :implicit_action.table_id]]
+ :where [:= :table.db_id database-id]})))]
+ (t2/delete! :model/Action :id [:in action-ids])
+ 0))
(mu/defn delete-cards-for-database!
"Delete the Cards of the Database with `database-id`."
@@ -130,8 +128,7 @@
(mu/defn active-tables-for-database
"The active Tables of the Database with `database-id`, in case-insensitive display name order."
[database-id :- ::lib.schema.id/database]
- (t2/select :model/Table :db_id database-id :active true {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:%lower.display_name :asc]]}))
+ (t2/select :model/Table :db_id database-id :active true {:order-by [[:%lower.display_name :asc]]}))
(mu/defn active-tables-for-databases
"The active Tables of the Databases with `database-ids`, in database then display name order."
@@ -139,19 +136,17 @@
(t2/select :model/Table
:db_id [:in database-ids]
:active true
- {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:db_id :asc] [:%lower.display_name :asc]]}))
+ {:order-by [[:db_id :asc] [:%lower.display_name :asc]]}))
(mu/defn active-table-ids-for-database
"The ids of the active Tables of the Database with `database-id`, or nil."
[database-id :- ::lib.schema.id/database]
- (t2/select-pks-set :model/Table, :db_id database-id, :active true {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-pks-set :model/Table, :db_id database-id, :active true))
(mu/defn pk-fields-for-tables
"The primary-key Fields of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Field :table_id [:in table-ids] :semantic_type (mdb/isa :type/PK)
- {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :table_id [:in table-ids] :semantic_type (mdb/isa :type/PK)))
(mu/defn databases-for-serdes-reducible
"A reducible of the Databases to export via serdes: routing destinations, stubs and the sample database are always
@@ -174,4 +169,4 @@
(mu/defn table-database-id
"The Database id of the Table with `table-id`, or nil."
[table-id :- ::lib.schema.id/table]
- (t2/select-one-fn :db_id :model/Table, :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one-fn :db_id :model/Table, :id table-id))
diff --git a/src/metabase/warehouses_rest/db.clj b/src/metabase/warehouses_rest/db.clj
index 720deae0343..4a703b611da 100644
--- a/src/metabase/warehouses_rest/db.clj
+++ b/src/metabase/warehouses_rest/db.clj
@@ -11,7 +11,6 @@
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouses.schema :as warehouses.schema]
[toucan2.core :as t2]))
@@ -22,8 +21,7 @@
:active true
:db_id [:in database-ids]
:visibility_type nil
- {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:%lower.schema :asc]
+ {:order-by [[:%lower.schema :asc]
[:%lower.display_name :asc]]}))
(mu/defn active-visible-schemas-for-databases
@@ -32,8 +30,7 @@
(t2/select [:model/Table :db_id :schema]
:db_id [:in database-ids]
:active true
- :visibility_type nil
- {:from [(warehouse-schema-overlay/table-query)]}))
+ :visibility_type nil))
(mu/defn database-engines
"The id and engine of every Database."
@@ -130,8 +127,7 @@
like-pattern :- [:or :string vector?]
limit :- ms/PositiveInt]
(t2/select [:model/Table :id :db_id :schema :name]
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and [:= :db_id database-id]
+ {:where [:and [:= :db_id database-id]
[:= :active true]
[:like :%lower.name like-pattern]
[:= :visibility_type nil]]
@@ -210,26 +206,24 @@
:%lower.metabase_field/name [:like like-pattern]
:metabase_field.visibility_type [:not-in ["sensitive" "retired"]]
:table.db_id database-id
- {:from [(warehouse-schema-overlay/field-query)]
- :order-by [[[:lower :metabase_field.name] :asc]
+ {:order-by [[[:lower :metabase_field.name] :asc]
[[:lower :table.name] :asc]]
;; checking for table.active in join makes query faster when there are a lot of inactive tables
- :inner-join [(warehouse-schema-overlay/table-query {:alias :table}) [:and :table.active
- [:= :table.id :metabase_field.table_id]]]
+ :inner-join [[(t2/table-name :model/Table) :table] [:and :table.active
+ [:= :table.id :metabase_field.table_id]]]
:limit limit}))
(mu/defn table-ids-for-database
"The ids of the Tables of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/select-fn-set :id :model/Table, :db_id database-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-fn-set :id :model/Table, :db_id database-id))
(mu/defn non-sensitive-fields-for-tables
"The id, name, display name, Table id, and types of the non-sensitive Fields of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
(t2/select [:model/Field :id :name :display_name :table_id :base_type :semantic_type]
:table_id [:in table-ids]
- :visibility_type [:not-in ["sensitive" "retired"]]
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :visibility_type [:not-in ["sensitive" "retired"]]))
(mu/defn insert-database!
"Insert the Database `row` and return the inserted instance."
@@ -280,7 +274,7 @@
(mu/defn active-tables-for-database
"The active Tables of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
- (t2/select :model/Table :db_id database-id :active true {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :db_id database-id :active true))
(mu/defn active-table-schemas
"The distinct schemas of the active Tables of the Database with `database-id`, in schema order. When
@@ -291,8 +285,7 @@
(let [clauses (cond-> []
(not include-hidden?) (conj [:= :visibility_type nil]))]
(t2/select-fn-set :schema :model/Table :db_id database-id :active true
- (merge {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:%lower.schema :asc]]}
+ (merge {:order-by [[:%lower.schema :asc]]}
(when clauses
{:where (into [:and] clauses)})))))
@@ -304,8 +297,7 @@
:db_id database-id
:schema schema
:active true
- {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:display_name :asc]]}))
+ {:order-by [[:display_name :asc]]}))
(mu/defn active-visible-tables-in-schema
"The active, visible Tables in `schema` of the Database with `database-id`, in display name order."
@@ -316,8 +308,7 @@
:schema schema
:active true
:visibility_type nil
- {:from [(warehouse-schema-overlay/table-query)]
- :order-by [[:display_name :asc]]}))
+ {:order-by [[:display_name :asc]]}))
(mu/defn collection-ids-named
"The ids of the Collections named `collection-name`, or nil."
diff --git a/src/metabase/xrays/db.clj b/src/metabase/xrays/db.clj
index a3fa39b8637..7eaa161e56f 100644
--- a/src/metabase/xrays/db.clj
+++ b/src/metabase/xrays/db.clj
@@ -7,7 +7,6 @@
[metabase.queries.schema :as queries.schema]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
(mu/defn database
@@ -19,19 +18,19 @@
"The Table with `table-id`, or nil. `table-id` may be nil (some callers pass a Card's or Metric's possibly-absent
table id), in which case this returns nil."
[table-id :- [:maybe ::lib.schema.id/table]]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
(mu/defn tables
"The Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Table :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :id [:in table-ids]))
(mu/defn tables-in-schema
"The Tables in `schema` of the Database with `database-id`. `schema` may be nil (some drivers have no
schema concept)."
[database-id :- ::lib.schema.id/database
schema :- [:maybe :string]]
- (t2/select :model/Table :db_id database-id :schema schema {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select :model/Table :db_id database-id :schema schema))
(mu/defn sibling-tables
"The active, visible Tables in `schema` of the Database with `database-id` other than `table-id`. `schema`
@@ -44,7 +43,7 @@
:schema schema
:id [:not= table-id]
:visibility_type nil
- :active true {:from [(warehouse-schema-overlay/table-query)]}))
+ :active true))
(mu/defn candidate-tables-with-field-stats
"The id, schema, name, entity type, Database, field count, and list-likeness of the active, visible Tables of the
@@ -56,8 +55,7 @@
[[:and
[:>= :ts.count 2]
[:= :ts.count_non_pks 1]] :list-like?]]
- {:from [(warehouse-schema-overlay/table-query)]
- :inner-join [[^:allow-subquery {:select [:f.table_id
+ {:inner-join [[^:allow-subquery {:select [:f.table_id
[:%count.* :count]
[[:count [:case [:or [:not= :semantic_type "type/PK"]
[:= :f.semantic_type nil]]
@@ -66,7 +64,7 @@
[[:count [:case [:in :f.semantic_type ["type/PK" "type/FK"]]
[:inline 1] :else [:inline nil]]]
:count_pks_and_fks]]
- :from [(warehouse-schema-overlay/field-query {:alias :f})]
+ :from [[(t2/table-name :model/Field) :f]]
:where [:= :f.active true]
:group-by [:f.table_id]} :ts]
[:and [:= :ts.table_id :id]
@@ -81,12 +79,12 @@
(mu/defn field
"The Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-one :model/Field :id field-id))
(mu/defn field-name
"The name of the Field with `field-id`, or nil."
[field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :name :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-one-fn :name :model/Field :id field-id))
(mu/defn metadata-column
"The `:metadata/column` with `field-id`, or nil."
@@ -96,17 +94,17 @@
(mu/defn fields-targeting
"The Fields whose FK target is the Field with `field-id`."
[field-id :- ::lib.schema.id/field]
- (t2/select :model/Field :fk_target_field_id field-id {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :fk_target_field_id field-id))
(mu/defn fk-fields-for-tables
"The FK Fields of the Tables with `table-ids`."
[table-ids :- [:set ::lib.schema.id/table]]
- (t2/select :model/Field :fk_target_field_id [:not= nil] :table_id [:in table-ids] {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :fk_target_field_id [:not= nil] :table_id [:in table-ids]))
(mu/defn active-fk-fields-for-table
"The active FK Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select :model/Field :table_id table-id :fk_target_field_id [:not= nil] :active true {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select :model/Field :table_id table-id :fk_target_field_id [:not= nil] :active true))
(mu/defn fk-target-field-ids-for-table
"The FK target Field ids of the active Fields of the Table with `table-id`."
@@ -114,18 +112,17 @@
(t2/select-fn-set :fk_target_field_id :model/Field
:table_id table-id
:fk_target_field_id [:not= nil]
- :active true
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :active true))
(mu/defn active-field-ids-for-table
"The ids of the active Fields of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
- (t2/select-fn-set :id :model/Field :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
+ (t2/select-fn-set :id :model/Field :table_id table-id :active true))
(mu/defn table-ids-of-fields-targeting
"The Table ids of the active Fields whose FK target is one of `field-ids`."
[field-ids :- [:set ::lib.schema.id/field]]
- (t2/select-fn-set :table_id :model/Field :fk_target_field_id [:in field-ids] :active true {:from [(warehouse-schema-overlay/field-query)]}))
+ (t2/select-fn-set :table_id :model/Field :fk_target_field_id [:in field-ids] :active true))
(mu/defn visible-fields-for-tables
"The active, normally visible, previewable Fields of the Tables with `table-ids`."
@@ -134,8 +131,7 @@
:table_id [:in table-ids]
:visibility_type "normal"
:preview_display true
- :active true
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :active true))
(mu/defn other-visible-fields-in-table
"The active, normally visible Fields of the Table with `table-id` other than `field-id`."
@@ -145,8 +141,7 @@
:table_id table-id
:id [:not= field-id]
:visibility_type "normal"
- :active true
- {:from [(warehouse-schema-overlay/field-query)]}))
+ :active true))
(mu/defn card
"The Card with `card-id`, or nil."
diff --git a/test/metabase/app_db/schema_migrations_test.clj b/test/metabase/app_db/schema_migrations_test.clj
index ddd5c7566c3..737d04cd6ab 100644
--- a/test/metabase/app_db/schema_migrations_test.clj
+++ b/test/metabase/app_db/schema_migrations_test.clj
@@ -45,7 +45,6 @@
[metabase.util.encryption :as encryption]
[metabase.util.encryption-test :as encryption-test]
[metabase.util.json :as json]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2])
(:import
(liquibase Scope)
@@ -3537,9 +3536,10 @@
visible (insert-table! "visible" true nil "internal")
unpublished (insert-table! "unpublished" false "hidden" "hidden")
user-visibility (fn [table-id]
- (t2/select-one-fn :v [:metabase_table
- [(warehouse-schema-overlay/table-user-visibility-type :metabase_table) :v]]
- {:where [:= :metabase_table.id table-id]}))]
+ (let [settings (t2/select-one :metabase_table_user_settings :table_id table-id)]
+ (if (:visibility_type_set settings)
+ (:visibility_type settings)
+ (t2/select-one-fn :visibility_type :metabase_table :id table-id))))]
(migrate!)
(testing "the backfill moves a published table's hidden or technical visibility into its settings row"
(doseq [[table-id visibility-type] [[hidden "hidden"] [technical "technical"]]]
@@ -3558,6 +3558,153 @@
(is (= {hidden "hidden" technical "technical" cruft "cruft" visible nil unpublished "hidden"}
(into {} (map (juxt identity user-visibility)) [hidden technical cruft visible unpublished]))))))))
+(deftest move-user-settings-into-metabase-table-and-field-test
+ (testing "ceygl0, ziqcir, 1h91ve: user-set values move into metabase_table and metabase_field"
+ (impl/test-migrations ["ceygl0" "1h91ve"] [migrate!]
+ (let [db-id (t2/insert-returning-pk! :metabase_database {:name "Move User Settings Test DB"
+ :engine "h2"
+ :created_at :%now
+ :updated_at :%now
+ :details "{}"})
+ insert-table! (fn [table-name]
+ (t2/insert-returning-pk! :metabase_table {:active true
+ :db_id db-id
+ :name table-name
+ :display_name "Synced Name"
+ :description "synced comment"
+ :visibility_type "cruft"
+ :data_layer "internal"
+ :field_order "database"
+ :created_at :%now
+ :updated_at :%now}))
+ edited (insert-table! "edited")
+ untouched (insert-table! "untouched")
+ insert-field! (fn [table-id field-name]
+ (t2/insert-returning-pk! :metabase_field {:table_id table-id
+ :name field-name
+ :display_name "Synced Field"
+ :description "synced comment"
+ :semantic_type "type/Category"
+ :active true
+ :base_type "type/Text"
+ :database_type "TEXT"
+ :created_at :%now
+ :updated_at :%now}))
+ edited-field (insert-field! edited "edited")
+ plain-field (insert-field! edited "plain")]
+ (t2/insert! :metabase_table_user_settings {:table_id edited
+ :display_name "User Name"
+ :description nil
+ :description_set true
+ :caveats "user caveats"
+ :caveats_set true
+ :data_layer "final"
+ :data_layer_set true
+ :field_order "custom"
+ :is_published true})
+ (t2/insert! :metabase_field_user_settings {:field_id edited-field
+ :display_name "User Field"
+ :description nil
+ :description_set true
+ :effective_type "type/Integer"
+ :coercion_strategy "Coercion/String->Integer"
+ :custom_position 3})
+ (migrate!)
+ (testing "a Table gets the values its user set, user-set NULLs included, and keeps the ones nobody set"
+ (is (=? {:display_name "User Name"
+ :description nil
+ :caveats "user caveats"
+ :data_layer "final"
+ :field_order "custom"
+ :is_published true
+ :visibility_type "cruft"}
+ (t2/select-one :metabase_table :id edited))))
+ (testing "a Table without settings is untouched"
+ (is (=? {:display_name "Synced Name" :description "synced comment" :field_order "database"}
+ (t2/select-one :metabase_table :id untouched))))
+ (testing "a Field gets the values its user set, and keeps the semantic type nobody set"
+ (is (=? {:display_name "User Field"
+ :description nil
+ :semantic_type "type/Category"
+ :effective_type "type/Integer"
+ :coercion_strategy "Coercion/String->Integer"
+ :custom_position 3}
+ (t2/select-one :metabase_field :id edited-field))))
+ (testing "a Field without settings is untouched"
+ (is (=? {:display_name "Synced Field" :description "synced comment"}
+ (t2/select-one :metabase_field :id plain-field))))))))
+
+(defn- app-db-boolean
+ "`x` read from a boolean column of the app DB, which MySQL returns as a number or a byte array."
+ [x]
+ (cond (number? x) (= 1 x)
+ (instance? (Class/forName "[B") x) (= 1 (first ^bytes x))
+ :else x))
+
+(deftest move-user-settings-follows-flags-and-pairs-test
+ (testing "ceygl0, ziqcir, 1h91ve: a false flag keeps the synced value and clears the settings value, a true flag
+ copies NULL, and a column that follows another moves with it"
+ (impl/test-migrations ["ceygl0" "1h91ve"] [migrate!]
+ (let [db-id (t2/insert-returning-pk! :metabase_database {:name "Flags Test DB" :engine "h2" :created_at :%now
+ :updated_at :%now :details "{}"})
+ coll-id (t2/insert-returning-pk! :collection {:name "c" :slug "c" :created_at :%now
+ :entity_id "flagsflagsflagsflags1"})
+ table-id (t2/insert-returning-pk! :metabase_table {:active true :db_id db-id :name "t" :display_name "Synced"
+ :description "synced" :visibility_type "cruft"
+ :data_layer "hidden" :data_source "ingested"
+ :field_order "database" :caveats "synced caveats"
+ :show_in_getting_started true :is_published true
+ :collection_id coll-id :created_at :%now
+ :updated_at :%now})
+ field! (fn [field-name extra]
+ (t2/insert-returning-pk! :metabase_field
+ (merge {:table_id table-id :name field-name :display_name "Synced"
+ :description "synced" :semantic_type "type/Category"
+ :active true :base_type "type/Text" :database_type "TEXT"
+ :json_unfolding true :has_field_values "auto-list"
+ :coercion_strategy "Coercion/Synced" :created_at :%now
+ :updated_at :%now}
+ extra)))
+ target-id (field! "target" {})
+ flagged (field! "flagged" {:fk_target_field_id target-id})
+ coerced (field! "coerced" {})]
+ (t2/insert! :metabase_table_user_settings {:table_id table-id
+ :description "stale" :description_set false
+ :visibility_type "hidden" :visibility_type_set false
+ :data_layer nil :data_layer_set true
+ :data_source "upload" :data_source_set true
+ :caveats "stale" :caveats_set false
+ :show_in_getting_started false
+ :is_published false :collection_id nil})
+ (t2/insert! :metabase_field_user_settings {:field_id flagged
+ :semantic_type nil :semantic_type_set true
+ :fk_target_field_id nil :fk_target_field_id_set true
+ :description "stale" :description_set false
+ :has_field_values "list"
+ :json_unfolding false
+ :settings "{\"currency\":\"EUR\"}"
+ :coercion_strategy "Coercion/Orphan"})
+ (t2/insert! :metabase_field_user_settings {:field_id coerced :effective_type "type/Integer" :coercion_strategy nil})
+ (migrate!)
+ (let [table (t2/select-one :metabase_table :id table-id)]
+ (testing "a Table"
+ (is (=? {:description "synced" :visibility_type "cruft" :data_layer nil :data_source "upload"
+ :caveats "synced caveats" :collection_id nil}
+ table))
+ (is (false? (app-db-boolean (:show_in_getting_started table))))
+ (is (false? (app-db-boolean (:is_published table))) "collection_id follows is_published"))
+ (testing "a settings value whose flag said no user set it is cleared"
+ (is (nil? (:caveats (t2/select-one :metabase_table_user_settings :table_id table-id))))))
+ (let [field (t2/select-one :metabase_field :id flagged)]
+ (testing "a Field"
+ (is (=? {:semantic_type nil :fk_target_field_id nil :description "synced" :has_field_values "list"
+ :settings "{\"currency\":\"EUR\"}" :coercion_strategy "Coercion/Synced"}
+ field))
+ (is (false? (app-db-boolean (:json_unfolding field))))))
+ (testing "a NULL coercion_strategy moves with the effective_type it follows"
+ (is (=? {:effective_type "type/Integer" :coercion_strategy nil}
+ (t2/select-one :metabase_field :id coerced))))))))
+
(deftest glossary-entity-id-backfill-test
(testing "v64.2026-09-11: glossary.entity_id is added, backfilled for existing rows, NOT NULL and unique"
(impl/test-migrations ["v64.2026-09-11T12:00:00" "v64.2026-09-11T12:00:03"] [migrate!]
diff --git a/test/metabase/data_studio/api/table_test.clj b/test/metabase/data_studio/api/table_test.clj
index 45c73782620..f894da7e9c4 100644
--- a/test/metabase/data_studio/api/table_test.clj
+++ b/test/metabase/data_studio/api/table_test.clj
@@ -9,7 +9,6 @@
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.util.quick-task :as quick-task]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2])
(:import (java.util.concurrent CountDownLatch Executors TimeUnit)))
@@ -18,10 +17,9 @@
(use-fixtures :once (fixtures/initialize :db))
(defn- user-table-fn
- "The value of `column` on the Table with `table-id` as users see it. A bare `t2/select :model/Table` shows sync's."
+ "The value of `column` on the Table with `table-id`."
[column table-id]
- (t2/select-one-fn column :model/Table :id table-id
- {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one-fn column :model/Table :id table-id))
(deftest bulk-edit-visibility-sync-test
(testing "POST /api/data-studio/table/edit visibility field synchronization"
@@ -318,9 +316,9 @@
:data_layer "hidden"
:data_authority "authoritative"
:data_source "ingested"})
- (is (= #{:hidden} (t2/select-fn-set :data_layer :model/Table :db_id [:in [clojure jvm]] {:from [(warehouse-schema-overlay/table-query)]})))
- (is (= #{:authoritative} (t2/select-fn-set :data_authority :model/Table :db_id [:in [clojure jvm]] {:from [(warehouse-schema-overlay/table-query)]})))
- (is (= #{:ingested} (t2/select-fn-set :data_source :model/Table :db_id [:in [clojure jvm]] {:from [(warehouse-schema-overlay/table-query)]}))))
+ (is (= #{:hidden} (t2/select-fn-set :data_layer :model/Table :db_id [:in [clojure jvm]])))
+ (is (= #{:authoritative} (t2/select-fn-set :data_authority :model/Table :db_id [:in [clojure jvm]])))
+ (is (= #{:ingested} (t2/select-fn-set :data_source :model/Table :db_id [:in [clojure jvm]]))))
(testing "updating with all selectors"
(mt/user-http-request :crowberto :post 200 "data-studio/table/edit"
{:database_ids [clojure]
@@ -333,9 +331,9 @@
classes :internal
gc :internal
jit :internal}
- (t2/select-pk->fn :data_layer :model/Table :db_id [:in [clojure jvm]] {:from [(warehouse-schema-overlay/table-query)]}))))
+ (t2/select-pk->fn :data_layer :model/Table :db_id [:in [clojure jvm]]))))
(testing "can update owner_email"
- (is (= #{nil} (t2/select-fn-set :owner_email :model/Table :db_id [:in [clojure jvm]] {:from [(warehouse-schema-overlay/table-query)]})))
+ (is (= #{nil} (t2/select-fn-set :owner_email :model/Table :db_id [:in [clojure jvm]])))
(mt/user-http-request :crowberto :post 200 "data-studio/table/edit"
{:database_ids [clojure]
:owner_email "[email protected]"})
@@ -345,9 +343,9 @@
classes nil
gc nil
jit nil}
- (t2/select-pk->fn :owner_email :model/Table :db_id [:in [clojure jvm]] {:from [(warehouse-schema-overlay/table-query)]}))))
+ (t2/select-pk->fn :owner_email :model/Table :db_id [:in [clojure jvm]]))))
(testing "can update owner_user_id"
- (is (= #{nil} (t2/select-fn-set :owner_user_id :model/Table :db_id [:in [clojure jvm]] {:from [(warehouse-schema-overlay/table-query)]})))
+ (is (= #{nil} (t2/select-fn-set :owner_user_id :model/Table :db_id [:in [clojure jvm]])))
(mt/user-http-request :crowberto :post 200 "data-studio/table/edit"
{:table_ids [beans classes]
:owner_user_id (mt/user->id :rasta)})
@@ -357,7 +355,7 @@
classes (mt/user->id :rasta)
gc nil
jit nil}
- (t2/select-pk->fn :owner_user_id :model/Table :db_id [:in [clojure jvm]] {:from [(warehouse-schema-overlay/table-query)]})))))))
+ (t2/select-pk->fn :owner_user_id :model/Table :db_id [:in [clojure jvm]])))))))
;;; ------------------------------------------------- Selection Tests -------------------------------------------------
diff --git a/test/metabase/driver/db_test.clj b/test/metabase/driver/db_test.clj
index 86cbc66a885..add28e00fcf 100644
--- a/test/metabase/driver/db_test.clj
+++ b/test/metabase/driver/db_test.clj
@@ -2,7 +2,8 @@
(:require
[clojure.test :refer :all]
[metabase.driver.db :as driver.db]
- [metabase.test :as mt]))
+ [metabase.test :as mt]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]))
(deftest json-field-names-with-unfolding-disabled-honors-user-setting-test
(testing "json-field-names-with-unfolding-disabled honors a user-set json_unfolding that differs from the sync value"
@@ -11,5 +12,5 @@
:base_type :type/JSON, :json_unfolding true}]
(testing "sanity check: unfolding is enabled by sync"
(is (empty? (driver.db/json-field-names-with-unfolding-disabled table-id))))
- (mt/with-temp [:model/FieldUserSettings _ {:field_id field-id, :json_unfolding false}]
- (is (= #{"PAYLOAD"} (driver.db/json-field-names-with-unfolding-disabled table-id)))))))
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:json_unfolding false})
+ (is (= #{"PAYLOAD"} (driver.db/json-field-names-with-unfolding-disabled table-id))))))
diff --git a/test/metabase/metabot/db_test.clj b/test/metabase/metabot/db_test.clj
index 3a48bfcb007..080e90026bd 100644
--- a/test/metabase/metabot/db_test.clj
+++ b/test/metabase/metabot/db_test.clj
@@ -12,6 +12,7 @@
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
+ [metabase.warehouse-schema.models.table-user-settings :as table-user-settings]
[toucan2.core :as t2]))
(use-fixtures :once (fixtures/initialize :db))
@@ -35,10 +36,8 @@
(deftest table-schema-rows-read-published-state-test
(testing "Rows carry a table's published state, which is stored with the user's table settings"
(collections.tu/with-library [{data :data}]
- (mt/with-temp [:model/Table {table-id :id} {:db_id (mt/id), :name "metabot published", :active true}
- :model/TableUserSettings _ {:table_id table-id
- :is_published true
- :collection_id (:id data)}]
+ (mt/with-temp [:model/Table {table-id :id :as table} {:db_id (mt/id), :name "metabot published", :active true}]
+ (table-user-settings/upsert-table-user-settings! table {:is_published true :collection_id (:id data)})
(is (=? [{:id table-id, :is_published true, :collection_id (:id data)}]
(metabot.db/table-schema-rows [table-id])))))))
diff --git a/test/metabase/metabot/tools/curated_content_test.clj b/test/metabase/metabot/tools/curated_content_test.clj
index ed9dbc5a401..2da877ad3b6 100644
--- a/test/metabase/metabot/tools/curated_content_test.clj
+++ b/test/metabase/metabot/tools/curated_content_test.clj
@@ -31,6 +31,7 @@
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.util.json :as json]
+ [metabase.warehouse-schema.models.table-user-settings :as table-user-settings]
[toucan2.core :as t2]))
(def ^:private curation-features
@@ -1027,15 +1028,13 @@
(testing "a table published through the Data Studio keeps its published flag in its user settings, and the read
check sees it: a user with no query permission reads it through its Library collection"
(mt/with-temp [:model/Collection {lib :id} {:type "library-data" :name "data library"}
- :model/Table {table-id :id} {:db_id (mt/id)
- :name "published_by_settings"
- :schema "LIBRARY_SCHEMA"
- :active true
- :data_layer :final}
- :model/TableUserSettings _ {:table_id table-id
- :is_published true
- :collection_id lib}
+ :model/Table {table-id :id :as table} {:db_id (mt/id)
+ :name "published_by_settings"
+ :schema "LIBRARY_SCHEMA"
+ :active true
+ :data_layer :final}
:model/Metabot metabot {:name "curated metabot" :use_verified_content true}]
+ (table-user-settings/upsert-table-user-settings! table {:is_published true :collection_id lib})
(mt/with-no-data-perms-for-all-users!
(perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)
(mt/with-current-user (mt/user->id :rasta)
diff --git a/test/metabase/parameters/chain_filter_test.clj b/test/metabase/parameters/chain_filter_test.clj
index 97d00da12d8..2d9373e1808 100644
--- a/test/metabase/parameters/chain_filter_test.clj
+++ b/test/metabase/parameters/chain_filter_test.clj
@@ -15,6 +15,7 @@
[metabase.test :as mt]
[metabase.util :as u]
[metabase.util.json :as json]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[metabase.warehouse-schema.models.field-values :as field-values]
[toucan2.core :as t2]))
@@ -572,13 +573,14 @@
(mt/with-temp-vals-in-db :model/Field (mt/id :orders :user_id) {:semantic_type nil, :fk_target_field_id nil}
(testing "sanity check: with the sync FK cleared, there's no remapping"
(is (nil? (#'chain-filter/remapped-field-id (mt/id :orders :user_id)))))
- (mt/with-temp [:model/FieldUserSettings _ {:field_id (mt/id :orders :user_id)
- :semantic_type :type/FK
- :semantic_type_set true
- :fk_target_field_id (mt/id :people :id)
- :fk_target_field_id_set true}]
+ (try
+ (field-user-settings/upsert-field-user-settings! {:id (mt/id :orders :user_id)}
+ {:semantic_type :type/FK
+ :fk_target_field_id (mt/id :people :id)})
(is (= (mt/id :people :name)
- (#'chain-filter/remapped-field-id (mt/id :orders :user_id))))))))
+ (#'chain-filter/remapped-field-id (mt/id :orders :user_id))))
+ (finally
+ (field-user-settings/delete-field-user-settings! [(mt/id :orders :user_id)]))))))
(deftest ^:parallel field-to-field-remapped-chain-filter-test
(testing "Field-to-field remapping: venues.category_id -> categories.name\n"
diff --git a/test/metabase/permissions/db_test.clj b/test/metabase/permissions/db_test.clj
index 0a31d0ceeed..ba2919dfdc8 100644
--- a/test/metabase/permissions/db_test.clj
+++ b/test/metabase/permissions/db_test.clj
@@ -2,7 +2,8 @@
(:require
[clojure.test :refer :all]
[metabase.permissions.db :as permissions.db]
- [metabase.test :as mt]))
+ [metabase.test :as mt]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]))
(deftest field-visibility-info-honors-user-set-visibility-type-test
(testing "field-visibility-info honors a user-set visibility_type (e.g. sensitive) over the sync value"
@@ -11,6 +12,6 @@
(testing "sanity check: the sync value shows by default"
(is (=? {:id field-id, :visibility_type :normal, :table_id table-id}
(first (permissions.db/field-visibility-info #{field-id})))))
- (mt/with-temp [:model/FieldUserSettings _ {:field_id field-id, :visibility_type :sensitive}]
- (is (=? {:id field-id, :visibility_type :sensitive, :table_id table-id}
- (first (permissions.db/field-visibility-info #{field-id}))))))))
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:visibility_type :sensitive})
+ (is (=? {:id field-id, :visibility_type :sensitive, :table_id table-id}
+ (first (permissions.db/field-visibility-info #{field-id})))))))
diff --git a/test/metabase/search/appdb/scoring_test.clj b/test/metabase/search/appdb/scoring_test.clj
index 2ac9adf7da5..c3e10c8eb4d 100644
--- a/test/metabase/search/appdb/scoring_test.clj
+++ b/test/metabase/search/appdb/scoring_test.clj
@@ -18,6 +18,7 @@
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.util :as u]
+ [metabase.warehouse-schema.models.table-user-settings :as table-user-settings]
[toucan2.core :as t2])
(:import
(java.time Instant)
@@ -410,16 +411,17 @@
(search.tu/with-temp-index-table
(testing "measures and segments follow a table published and unpublished through its user settings"
(collections.tu/with-library [{data :data}]
- (mt/with-temp [:model/Table {table-id :id} {:name "tbl"}
+ (mt/with-temp [:model/Table {table-id :id :as table} {:name "tbl"}
:model/Measure _ {:name "foo measure" :table_id table-id}
:model/Segment _ {:name "foo segment" :table_id table-id}]
(let [curated #(mapv :curated (search-results** "foo" {:context :metabot :models #{"measure" "segment"}}))]
(is (= [[false false] [true true] [false false]]
[(curated)
- (do (t2/insert! :model/TableUserSettings
- {:table_id table-id :is_published true :collection_id (:id data)})
+ (do (table-user-settings/upsert-table-user-settings!
+ table {:is_published true :collection_id (:id data)})
(curated))
- (do (t2/update! :model/TableUserSettings :table_id table-id {:is_published false :collection_id nil})
+ (do (table-user-settings/upsert-table-user-settings!
+ table {:is_published false :collection_id nil})
(curated))]))))))))
(deftest ^:parallel data-layer-test
diff --git a/test/metabase/search/spec_test.clj b/test/metabase/search/spec_test.clj
index 31b560e8eba..79e163e6704 100644
--- a/test/metabase/search/spec_test.clj
+++ b/test/metabase/search/spec_test.clj
@@ -121,24 +121,14 @@
:where [:and [:= :this.is_published true] [:= :updated.id :this.collection_id]]}
{:search-model "segment"
:fields #{:location :type}
- :where [:and
- [:= [:coalesce :settings.is_published :table.is_published] true]
- [:= :updated.id [:case
- [:not= :settings.is_published nil] :settings.collection_id
- :else :table.collection_id]]]}}
- :TableUserSettings #{{:search-model "table"
- :fields #{:updated_at}
- :where [:= :updated.table_id :this.id]}
- {:search-model "segment"
- :fields #{:collection_id :is_published}
- :where [:= :updated.table_id :this.table_id]}}}
+ :where [:and [:= :table.is_published true] [:= :updated.id :table.collection_id]]}}}
(#'search.spec/merge-hooks
[(#'search.spec/search-model-hooks (search.spec/spec "table"))
(#'search.spec/search-model-hooks (search.spec/spec "segment"))
(#'search.spec/search-model-hooks (search.spec/spec "collection"))]))))
(deftest ^:parallel measure-parent-table-hooks-test
- (testing "a measure is reindexed when its parent table, that table's user settings, or its collection changes"
+ (testing "a measure is reindexed when its parent table or its collection changes"
(is (= #:model{:Table #{{:search-model "measure"
:fields #{:collection_id
:db_id
@@ -150,14 +140,7 @@
:where [:= :updated.id :this.table_id]}}
:Collection #{{:search-model "measure"
:fields #{:location :type}
- :where [:and
- [:= [:coalesce :settings.is_published :table.is_published] true]
- [:= :updated.id [:case
- [:not= :settings.is_published nil] :settings.collection_id
- :else :table.collection_id]]]}}
- :TableUserSettings #{{:search-model "measure"
- :fields #{:collection_id :is_published}
- :where [:= :updated.table_id :this.table_id]}}}
+ :where [:and [:= :table.is_published true] [:= :updated.id :table.collection_id]]}}}
(select-keys (#'search.spec/merge-hooks [(#'search.spec/search-model-hooks (search.spec/spec "measure"))])
[:model/Table :model/Collection :model/TableUserSettings])))))
diff --git a/test/metabase/sync/analyze/data_sensitivity_test.clj b/test/metabase/sync/analyze/data_sensitivity_test.clj
index 5ebb9be6305..98d96b277e0 100644
--- a/test/metabase/sync/analyze/data_sensitivity_test.clj
+++ b/test/metabase/sync/analyze/data_sensitivity_test.clj
@@ -7,18 +7,11 @@
[metabase.sync.core :as sync]
[metabase.test :as mt]
[metabase.util :as u]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[toucan2.core :as t2]))
(defn- label
- "The `data_sensitivity` a user sees: their own label when they set one, else the classifier's."
- [field-or-id]
- (t2/select-one-fn :data_sensitivity :model/Field :id (u/the-id field-or-id)
- {:from [(warehouse-schema-overlay/field-query {:alias :f})]}))
-
-(defn- sync-label
- "The `data_sensitivity` the classifier itself wrote to `metabase_field`."
+ "The `data_sensitivity` of the Field."
[field-or-id]
(t2/select-one-fn :data_sensitivity :model/Field :id (u/the-id field-or-id)))
@@ -119,34 +112,28 @@
(is (= :PHI (label foo)))
(is (= :PII (label ssn))))))))
-(deftest user-label-in-mirror-wins-test
- (testing "a mirror-only user label survives the classifier writing its own inference to the raw Field"
+(deftest user-label-wins-test
+ (testing "user label survives classifier scans"
(mt/with-temp [:model/Database db {}
:model/Table table {:db_id (:id db) :name "app_users"}
:model/Field ssn {:table_id (:id table) :name "ssn" :base_type :type/Text}
:model/Field notes {:table_id (:id table) :name "notes" :base_type :type/Text}]
- (field-user-settings/upsert-user-settings ssn {:data_sensitivity :PUBLIC})
- (field-user-settings/upsert-user-settings notes {:data_sensitivity :PHI})
- (testing "before the scan only the user's label exists"
- (is (nil? (sync-label ssn)))
- (is (nil? (sync-label notes)))
+ (field-user-settings/upsert-field-user-settings! ssn {:data_sensitivity :PUBLIC})
+ (field-user-settings/upsert-field-user-settings! notes {:data_sensitivity :PHI})
+ (testing "before scan, Field shows user's label"
(is (= :PUBLIC (label ssn)))
(is (= :PHI (label notes))))
- (testing "the scan still classifies both fields on the raw Field"
- (is (= {:fields-scanned 2 :fields-labeled 1 :fields-failed 0}
- (sync.data-sensitivity/scan-data-sensitivity! (:id db)))))
- (testing "the classifier wrote its own label, and the user's still wins on read"
- (is (= :PII (sync-label ssn)))
- (is (= :PUBLIC (mirror-label ssn)))
+ (testing "scan skips user-labeled fields"
+ (is (= {:fields-scanned 0 :fields-labeled 0 :fields-failed 0}
+ (sync.data-sensitivity/scan-data-sensitivity! (:id db))))
(is (= :PUBLIC (label ssn)))
- (is (= :PUBLIC (sync-label notes)))
- (is (= :PHI (mirror-label notes)))
(is (= :PHI (label notes))))
- (testing "a forced rescan does not revisit a categorized Field, mirror or not"
+ (testing "forced rescan revisits user's PUBLIC field without overwriting it"
(sync.data-sensitivity/scan-data-sensitivity! (:id db) :force? true)
- (is (= :PII (sync-label ssn)))
+ (is (= :PUBLIC (label ssn)))
(is (= :PUBLIC (mirror-label ssn)))
- (is (= :PUBLIC (label ssn)))))))
+ (is (= :PHI (label notes)))
+ (is (= :PHI (mirror-label notes)))))))
(deftest reset-data-sensitivity-test
(mt/with-temp [:model/Database db {}
@@ -160,25 +147,24 @@
:model/Field email {:table_id (:id users) :name "email" :base_type :type/Text}
:model/Field total {:table_id (:id orders) :name "total" :base_type :type/Float}
:model/Field far {:table_id (:id foreign) :name "ssn" :base_type :type/Text}]
- (field-user-settings/upsert-user-settings notes {:data_sensitivity :PHI})
+ (field-user-settings/upsert-field-user-settings! notes {:data_sensitivity :PHI})
(t2/insert! :model/FieldUserSettings {:field_id (:id email)})
(sync.data-sensitivity/scan-data-sensitivity! (:id db))
(sync.data-sensitivity/scan-data-sensitivity! (:id other-db))
- (is (= [:PII :PUBLIC :PUBLIC :PII :PUBLIC :PII]
- (map sync-label [ssn foo notes email total far])))
+ (is (= [:PII :PUBLIC :PHI :PII :PUBLIC :PII]
+ (map label [ssn foo notes email total far])))
(testing "a table scope clears only that table's classifier labels"
(is (= 1 (sync.data-sensitivity/reset-table-data-sensitivity! orders)))
- (is (nil? (sync-label total)))
- (is (= :PII (sync-label ssn))))
+ (is (nil? (label total)))
+ (is (= :PII (label ssn))))
(testing "a database scope clears categories and PUBLIC alike, including fields with a label-less mirror row"
(is (= 3 (sync.data-sensitivity/reset-data-sensitivity! (:id db))))
- (is (= [nil nil nil] (map sync-label [ssn foo email]))))
- (testing "a label backed by the mirror is human-set; the reset clears only the classifier's own label"
- (is (= :PUBLIC (sync-label notes)))
+ (is (= [nil nil nil] (map label [ssn foo email]))))
+ (testing "user-set label is left alone by reset"
(is (= :PHI (mirror-label notes)))
(is (= :PHI (label notes))))
(testing "other databases are untouched"
- (is (= :PII (sync-label far))))
+ (is (= :PII (label far))))
(testing "a second reset finds nothing"
(is (zero? (sync.data-sensitivity/reset-data-sensitivity! (:id db)))))))
@@ -187,19 +173,18 @@
:model/Table table {:db_id (:id db) :name "app_users"}
:model/Field ssn {:table_id (:id table) :name "ssn" :base_type :type/Text}
:model/Field notes {:table_id (:id table) :name "notes" :base_type :type/Text}]
- (field-user-settings/upsert-user-settings notes {:data_sensitivity :PHI})
+ (field-user-settings/upsert-field-user-settings! notes {:data_sensitivity :PHI})
(sync.data-sensitivity/scan-data-sensitivity! (:id db))
- (is (= :PII (sync-label ssn)))
+ (is (= :PII (label ssn)))
(mt/with-dynamic-fn-redefs [analyze/infer-data-sensitivity (constantly :SEC_KEY)]
(testing "a forced scan leaves a categorized field on its old label"
(sync.data-sensitivity/scan-data-sensitivity! (:id db) :force? true)
- (is (= :PII (sync-label ssn))))
+ (is (= :PII (label ssn))))
(testing "a reset scan recomputes it under the current rules and reports the reset count"
(is (= {:fields-scanned 1 :fields-labeled 1 :fields-failed 0 :fields-reset 1}
(sync.data-sensitivity/scan-data-sensitivity! (:id db) :reset? true)))
- (is (= :SEC_KEY (sync-label ssn))))
- (testing "the user's label in the mirror is untouched, and is still what readers see"
- (is (= :SEC_KEY (sync-label notes)) "reclassified by the earlier forced scan, which also revisits PUBLIC fields")
+ (is (= :SEC_KEY (label ssn))))
+ (testing "user's label is untouched"
(is (= :PHI (mirror-label notes)))
(is (= :PHI (label notes)))))))
diff --git a/test/metabase/sync/analyze/interestingness_test.clj b/test/metabase/sync/analyze/interestingness_test.clj
index a8431ff4134..00ca1f81c3e 100644
--- a/test/metabase/sync/analyze/interestingness_test.clj
+++ b/test/metabase/sync/analyze/interestingness_test.clj
@@ -7,6 +7,7 @@
[metabase.sync.analyze.interestingness :as sync.interestingness]
[metabase.test :as mt]
[metabase.util :as u]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[toucan2.core :as t2]))
;;; Smoke tests for the canonical weight profiles. The sync step itself is verified
@@ -153,8 +154,7 @@
:model/Table table {:db_id (:id database)}
:model/Field visible {:table_id (:id table)}
:model/Field hidden {:table_id (:id table)}]
- (t2/insert! :model/FieldUserSettings {:field_id (:id hidden)
- :visibility_type :sensitive})
+ (field-user-settings/upsert-field-user-settings! hidden {:visibility_type :sensitive})
(is (= {:fields-scored 1 :fields-failed 0}
(#'sync.interestingness/score-missing-leftovers! (driver.u/lib-database (:id database)))))
(is (some? (score-of visible)))
diff --git a/test/metabase/sync/field_values_test.clj b/test/metabase/sync/field_values_test.clj
index 70580c4ed61..fdadb05ce66 100644
--- a/test/metabase/sync/field_values_test.clj
+++ b/test/metabase/sync/field_values_test.clj
@@ -14,6 +14,7 @@
[metabase.test.data :as data]
[metabase.test.data.one-off-dbs :as one-off-dbs]
[metabase.warehouse-schema.field-values.distinct-batch :as distinct-batch]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[metabase.warehouse-schema.models.field-values :as field-values]
[toucan2.core :as t2]))
@@ -342,9 +343,9 @@
(testing "sanity check: the sync values show by default"
(is (=? {:has_field_values nil, :visibility_type :normal}
(first (sync.db/fields-for-field-values [field-id])))))
- (mt/with-temp [:model/FieldUserSettings _ {:field_id field-id, :has_field_values :none, :visibility_type :sensitive}]
- (is (=? {:has_field_values :none, :visibility_type :sensitive}
- (first (sync.db/fields-for-field-values [field-id]))))))))
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:has_field_values :none, :visibility_type :sensitive})
+ (is (=? {:has_field_values :none, :visibility_type :sensitive}
+ (first (sync.db/fields-for-field-values [field-id])))))))
(deftest can-batch-distinct?-test
(testing "SQL driver with :nested-queries and no required filter → batch path"
diff --git a/test/metabase/sync/sync_metadata/comments_test.clj b/test/metabase/sync/sync_metadata/comments_test.clj
index a1ce14290b9..a893bbbd1ee 100644
--- a/test/metabase/sync/sync_metadata/comments_test.clj
+++ b/test/metabase/sync/sync_metadata/comments_test.clj
@@ -12,7 +12,6 @@
[metabase.test.data.interface :as tx]
[metabase.test.data.sql :as sql.tx]
[metabase.util :as u]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[toucan2.core :as t2]))
@@ -52,20 +51,19 @@
[["foo"]]]])
(deftest comment-should-not-overwrite-custom-description-test
- (testing "a user-set description lives in FieldUserSettings and is never overwritten by resyncing, while the raw
- Field still picks up the comment from the source db"
+ (testing "user-set description is never overwritten by resyncing comment from source db"
(mt/test-drivers (mt/normal-driver-select {:+features [::field-comments-sync]})
(mt/dataset update-desc
(mt/with-temp-copy-of-db
(let [field-id (mt/id "update_desc" "updated_desc")]
(t2/update! :model/Field :id field-id {:description nil})
- (field-user-settings/upsert-user-settings {:id field-id} {:description "updated description"})
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:description "updated description"})
(sync/sync-table! (t2/select-one :model/Table :id (mt/id "update_desc")))
(is (= #{{:name (mt/format-name "id"), :description nil}
- {:name (mt/format-name "updated_desc"), :description "original comment"}}
+ {:name (mt/format-name "updated_desc"), :description "updated description"}}
(db->fields (mt/db))))
- (is (= "updated description"
- (:description (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))))))))))
+ (is (=? {:description "updated description" :description_set true}
+ (t2/select-one :model/FieldUserSettings :field_id field-id)))))))))
(tx/defdataset ^:private comment-after-sync
[["comment_after_sync"
diff --git a/test/metabase/sync/sync_metadata/fields/sync_metadata_test.clj b/test/metabase/sync/sync_metadata/fields/sync_metadata_test.clj
index e9f5a494919..a2bbddaaefe 100644
--- a/test/metabase/sync/sync_metadata/fields/sync_metadata_test.clj
+++ b/test/metabase/sync/sync_metadata/fields/sync_metadata_test.clj
@@ -33,6 +33,17 @@
metadata-in-application-db)
@update-operations)))))
+(defn- perform-updates!
+ "Run sync's metadata update for one Field against the app DB, without stubbing its writes."
+ [new-metadata-from-sync metadata-in-application-db]
+ (mt/with-temp [:model/Database db {}
+ :model/Table table {:db_id (u/the-id db) :field_order :alphabetical}]
+ (#'sync-metadata/update-field-metadata-if-needed!
+ (driver.u/lib-database (:id db))
+ table
+ new-metadata-from-sync
+ metadata-in-application-db)))
+
(deftest database-type-changed-test
(testing "test that if database-type changes we will update it in the DB"
(is (= [["Field" 1 {:database_type "Integer"}]]
@@ -303,30 +314,24 @@
(deftest base-type-change-will-trigger-fingerprint-and-analyze-test
(testing "A base type of a field changes only when the field is dropped then a new field with the name is created (#37047).
In this case, we should make sure effective type is set to base type"
- (mt/with-temp [:model/Field {field-id :id} {}]
- (field-user-settings/upsert-user-settings {:id field-id} {:semantic_type :type/Category})
- (is (= [["FieldUserSettings"
- field-id
- {:effective_type nil
- :coercion_strategy nil
- :semantic_type nil
- :semantic_type_set false}]
- ["Field"
- field-id
- {:base_type :type/Text
- :effective_type :type/Text
- :coercion_strategy nil
- :fingerprint_version 0
- :fingerprint nil
- :semantic_type nil}]]
- (updates-that-will-be-performed!
- (merge default-metadata
- {:base-type :type/Text
- :effective-type :type/Text})
- (merge default-metadata
- {:id field-id
- :base-type :type/Integer
- :effective-type :type/Integer})))))
+ (mt/with-temp [:model/Field {field-id :id} {:base_type :type/Integer :effective_type :type/Integer}]
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:semantic_type :type/Category})
+ (perform-updates!
+ (merge default-metadata
+ {:base-type :type/Text
+ :effective-type :type/Text})
+ (merge default-metadata
+ {:id field-id
+ :base-type :type/Integer
+ :effective-type :type/Integer}))
+ (is (=? {:base_type :type/Text
+ :effective_type :type/Text
+ :coercion_strategy nil
+ :fingerprint_version 0
+ :fingerprint nil
+ :semantic_type nil}
+ (t2/select-one :model/Field :id field-id)))
+ (is (nil? (t2/select-one :model/FieldUserSettings :field_id field-id))))
(testing "and sync will re-fingerprint and analyze this field"
(mt/with-temp-test-data [["table"
[{:field-name "field"
@@ -366,23 +371,22 @@
(deftest untyped-field-getting-its-base-type-keeps-user-settings-test
(testing "A Field that never had a real base type, like one serdes synthesized for a stub database, keeps its user settings"
(mt/with-temp [:model/Field {field-id :id} {:base_type :type/* :active false}]
- (field-user-settings/upsert-user-settings {:id field-id} {:semantic_type :type/Category})
- (is (= [["Field"
- field-id
- {:base_type :type/Text
- :effective_type :type/Text
- :coercion_strategy nil
- :fingerprint_version 0
- :fingerprint nil
- :semantic_type nil}]]
- (updates-that-will-be-performed!
- (merge default-metadata
- {:base-type :type/Text
- :effective-type :type/Text})
- (merge default-metadata
- {:id field-id
- :base-type :type/*
- :effective-type :type/*})))))))
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:semantic_type :type/Category})
+ (perform-updates!
+ (merge default-metadata
+ {:base-type :type/Text
+ :effective-type :type/Text})
+ (merge default-metadata
+ {:id field-id
+ :base-type :type/*
+ :effective-type :type/*}))
+ (is (=? {:base_type :type/Text
+ :effective_type :type/Text
+ :fingerprint_version 0
+ :semantic_type :type/Category}
+ (t2/select-one :model/Field :id field-id)))
+ (is (=? {:semantic_type :type/Category :semantic_type_set true}
+ (t2/select-one :model/FieldUserSettings :field_id field-id))))))
(deftest create-or-replace-table-updates-effective-type-test
(testing "GHY-3388: when a column's database type changes in place (e.g. TEXT -> numeric via
@@ -469,18 +473,20 @@
heal effective_type to match base_type. This catches drifted state from older
Metabase versions that the customer's instance carries forward."
(testing "broken row gets healed even when base_type does not change"
- (mt/with-temp [:model/Field {field-id :id} {}]
- (field-user-settings/upsert-user-settings {:id field-id} {:effective_type :type/Text})
- (is (= [["FieldUserSettings" field-id {:effective_type nil :coercion_strategy nil}]
- ["Field" field-id {:effective_type :type/Number}]]
- (updates-that-will-be-performed!
- (merge default-metadata
- {:base-type :type/Number})
- (merge default-metadata
- {:id field-id
- :base-type :type/Number
- :effective-type :type/Text
- :coercion-strategy nil}))))))
+ (mt/with-temp [:model/Field {field-id :id} {:base_type :type/Number :effective_type :type/Number}
+ :model/FieldUserSettings _ {:field_id field-id :effective_type :type/Text}]
+ (t2/update! (t2/table-name :model/Field) :id field-id {:effective_type "type/Text"})
+ (perform-updates!
+ (merge default-metadata
+ {:base-type :type/Number})
+ (merge default-metadata
+ {:id field-id
+ :base-type :type/Number
+ :effective-type :type/Text
+ :coercion-strategy nil}))
+ (is (=? {:base_type :type/Number :effective_type :type/Number :coercion_strategy nil}
+ (t2/select-one :model/Field :id field-id)))
+ (is (nil? (t2/select-one :model/FieldUserSettings :field_id field-id)))))
(testing "row with a real coercion_strategy is left alone — effective_type ≠ base_type is legitimate"
(is (= []
(updates-that-will-be-performed!
@@ -518,7 +524,7 @@
(deftest base-type-change-leaves-data-sensitivity-test
(testing "the override write on a base type change does not include data_sensitivity"
(mt/with-temp [:model/Field {field-id :id} {:data_sensitivity :PII}]
- (field-user-settings/upsert-user-settings {:id field-id} {:semantic_type :type/Category})
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:semantic_type :type/Category})
(let [updates (updates-that-will-be-performed!
(merge default-metadata
{:base-type :type/Text
@@ -550,7 +556,7 @@
(sync/sync-table! (t2/select-one :model/Table :id (mt/id :table)))
(is (=? {:base_type :type/Integer
:effective_type :type/Integer
- :data_sensitivity nil}
+ :data_sensitivity :PII}
(t2/select-one :model/Field :id field-id)))
(is (=? {:effective_type nil
:data_sensitivity :PII}
diff --git a/test/metabase/sync/sync_metadata/fields_test.clj b/test/metabase/sync/sync_metadata/fields_test.clj
index 3d477d6d465..e1c36fc888a 100644
--- a/test/metabase/sync/sync_metadata/fields_test.clj
+++ b/test/metabase/sync/sync_metadata/fields_test.clj
@@ -10,6 +10,7 @@
[metabase.lib.metadata :as lib.metadata]
[metabase.query-processor.test :as qp]
[metabase.sync.core :as sync]
+ [metabase.sync.db :as sync.db]
[metabase.sync.fetch-metadata :as fetch-metadata]
[metabase.sync.sync-metadata :as sync-metadata]
[metabase.sync.sync-metadata.fields :as sync-fields]
@@ -21,7 +22,6 @@
[metabase.test.data.one-off-dbs :as one-off-dbs]
[metabase.test.mock.toucanery :as toucanery]
[metabase.util :as u]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[toucan2.connection :as t2.connection]
[toucan2.core :as t2]))
@@ -171,7 +171,7 @@
(mt/user-http-request :crowberto :put 200 (format "field/%d" (:id field)) {:coercion_strategy :Coercion/String->Integer})
(sync/sync-database! (:id db))
(is (=? {:effective_type :type/Integer :coercion_strategy :Coercion/String->Integer}
- (t2/select-one :model/Field :id (:id field) {:from [(warehouse-schema-overlay/field-query)]})))
+ (t2/select-one :model/Field :id (:id field))))
(jdbc/execute! db-spec ["ALTER TABLE \"base_type_change_test\" ALTER COLUMN \"string_tbc_int_col\" TYPE int USING \"string_tbc_int_col\"::integer;"])
(sync/sync-database! (:id db))
(testing "the base type change unsets the user's coercion, on the Field and for the user"
@@ -312,7 +312,7 @@
{:step-info (sync.util-test/only-step-keys step-info)
:task-details task_details
:semantic-type semantic_type
- :fk-target-exists? (t2/exists? :model/Field :id fk_target_field_id {:from [(warehouse-schema-overlay/field-query)]})}))]
+ :fk-target-exists? (t2/exists? :model/Field :id fk_target_field_id)}))]
(testing "before"
(is (= {:step-info {:total-fks 6, :updated-fks 0, :total-failed 0}
:task-details {:total-fks 6, :updated-fks 0, :total-failed 0}
@@ -328,13 +328,12 @@
(state))))))))
(deftest sync-table-fks-test2
- (testing "Check that sync-table! keeps writing FKs to the Field while the user keeps seeing their own values"
+ (testing "Check that sync-table! leaves user-set semantic type alone"
(mt/with-temp-copy-of-db
(letfn [(state []
(let [{:keys [step-info]
{:keys [task_details]} :task-history} (sync.util-test/sync-database! "sync-fks" (mt/db))
- {:keys [semantic_type fk_target_field_id]} (t2/select-one :model/Field :id (mt/id :checkins :user_id)
- {:from [(warehouse-schema-overlay/field-query)]})]
+ {:keys [semantic_type fk_target_field_id]} (t2/select-one :model/Field :id (mt/id :checkins :user_id))]
{:step-info (sync.util-test/only-step-keys step-info)
:task-details task_details
:semantic-type semantic_type
@@ -351,10 +350,7 @@
:task-details {:total-fks 6, :updated-fks 0, :total-failed 0}
:semantic-type :type/Name
:fk-target-exists? false}
- (state)))
- (testing "sync's own row still carries the FK it detected, under the user's :type/Name"
- (is (=? {:semantic_type :type/FK :fk_target_field_id int?}
- (t2/select-one :model/Field :id (mt/id :checkins :user_id))))))))))
+ (state))))))))
(deftest case-sensitive-conflict-test
(testing "Two columns with same lower-case name can be synced (#17387)"
@@ -538,14 +534,12 @@
(let [table-id (t2/select-one-pk :model/Table :db_id (u/the-id database) :name "test_table")
field-id (t2/select-one-pk :model/Field :table_id table-id :name "something")]
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id) {:visibility_type :normal})
- (is (= :normal (:visibility_type (t2/select-one :model/Field :id field-id
- {:from [(warehouse-schema-overlay/field-query)]})))
+ (is (= :normal (:visibility_type (t2/select-one :model/Field :id field-id)))
"Manual change should set visibility_type to :normal"))
(sync/sync-database! (:id database))
(let [table-id (t2/select-one-pk :model/Table :db_id (u/the-id database) :name "test_table")
field-id (t2/select-one-pk :model/Field :table_id table-id :name "something")]
- (is (= :normal (:visibility_type (t2/select-one :model/Field :id field-id
- {:from [(warehouse-schema-overlay/field-query)]})))
+ (is (= :normal (:visibility_type (t2/select-one :model/Field :id field-id)))
"Second sync should preserve manually set :normal visibility_type"))))))))
(deftest hiding-a-case-variant-table-leaves-the-other-tables-fields-alone-test
@@ -585,16 +579,29 @@
(testing "should not have FK relationship"
(is (nil? (:fk_target_field_id flocks-example-bird-name-field)))
(is (not= :type/FK (:semantic_type flocks-example-bird-name-field))))
- (field-user-settings/upsert-user-settings
+ (field-user-settings/upsert-field-user-settings!
{:id (u/the-id flocks-example-bird-name-field)}
{:semantic_type :type/FK
:fk_target_field_id (u/the-id birds-example-name-field)})
(testing "after sync, user-set FK is preserved"
(sync/sync-database! (mt/id))
- (let [field-after-sync (t2/select-one :model/Field :id (u/the-id flocks-example-bird-name-field) {:from [(warehouse-schema-overlay/field-query)]})]
+ (let [field-after-sync (t2/select-one :model/Field :id (u/the-id flocks-example-bird-name-field))]
(is (= :type/FK (:semantic_type field-after-sync)))
(is (= (u/the-id birds-example-name-field) (:fk_target_field_id field-after-sync)))))))))
+(deftest mark-fk-keeps-user-set-columns-test
+ (testing "sync marks a foreign key to a primary key whose semantic type a user set, and keeps the FK's own user values"
+ (mt/with-temp [:model/Database {db-id :id} {}
+ :model/Table {pk-table :id} {:db_id db-id :name "pk_table" :schema "s"}
+ :model/Table {fk-table :id} {:db_id db-id :name "fk_table" :schema "s"}
+ :model/Field {pk-id :id} {:table_id pk-table :name "id"}
+ :model/Field {fk-id :id :as fk-field} {:table_id fk-table :name "pk_id" :has_field_values :auto-list}]
+ (field-user-settings/upsert-field-user-settings! {:id pk-id} {:semantic_type :type/PK})
+ (field-user-settings/upsert-field-user-settings! fk-field {:has_field_values :auto-list})
+ (is (= 1 (sync.db/mark-fk! db-id "s" "fk_table" "pk_id" "s" "pk_table" "id")))
+ (is (=? {:fk_target_field_id pk-id :semantic_type :type/FK :has_field_values :auto-list}
+ (t2/select-one :model/Field :id fk-id))))))
+
(deftest database-default-test
(with-test-db
(let [ddl "CREATE TABLE t (a INTEGER DEFAULT 42, b INTEGER NULL)"]
@@ -626,7 +633,7 @@
(is (false? (:database_is_nullable b)))))))
(deftest data-sensitivity-survives-sync-test
- (testing "a user-set data_sensitivity lives in FieldUserSettings, untouched by a full sync or a bare Field update"
+ (testing "user-set data_sensitivity is written to Field and survives full sync"
(mt/with-temp-test-data [["sens_table"
[{:field-name "email", :base-type :type/Text}]
@@ -638,17 +645,13 @@
(is (nil? (field)))
(is (not (t2/exists? :model/FieldUserSettings :field_id field-id))))
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id) {:data_sensitivity "PII"})
- (testing "after a full sync, the classifier skips the user-overridden Field"
+ (testing "after full sync, Field and its settings keep user's label"
(sync/sync-database! (:id db))
- (is (nil? (field)))
- (is (= :PII (mirror))))
- (testing "a bare update to the Field only changes the classifier's own value"
- (t2/update! :model/Field :id field-id {:data_sensitivity :SEC_KEY})
- (is (= :SEC_KEY (field)))
+ (is (= :PII (field)))
(is (= :PII (mirror))))))))
(deftest data-sensitivity-classifier-respects-user-label-test
- (testing "a user-set data_sensitivity in FieldUserSettings survives the classifier reclassifying the raw Field"
+ (testing "user-set data_sensitivity survives classifier"
(mt/with-temp-test-data [["app_users"
[{:field-name "email", :base-type :type/Text}
{:field-name "ssn", :base-type :type/Text}
@@ -660,9 +663,8 @@
(is (nil? (field :ssn)))
(mt/with-temporary-setting-values [data-sensitivity-scan-enabled true]
(sync/sync-database! (mt/id)))
- (testing "the classifier still infers and writes its own category on the raw Field"
- (is (= :PII (field :email))))
- (testing "the user's PUBLIC override on email survives in FieldUserSettings"
+ (testing "user's PUBLIC label on email stays on Field and in its settings"
+ (is (= :PUBLIC (field :email)))
(is (= :PUBLIC (mirror :email))))
(testing "the classifier labels the unlabeled siblings without creating mirror rows"
(is (= :PII (field :ssn)))
@@ -684,13 +686,13 @@
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id) {:data_sensitivity "PHI"})
(jdbc/execute! db-spec ["ALTER TABLE \"READD_TABLE\" DROP COLUMN \"EMAIL\";"])
(sync/sync-database! (:id db))
- (testing "the field is inactive while the column is gone; the mirror keeps the label"
- (is (=? {:active false, :data_sensitivity nil} (field)))
+ (testing "field is inactive while column is gone, and keeps its label"
+ (is (=? {:active false, :data_sensitivity :PHI} (field)))
(is (= :PHI (mirror))))
(jdbc/execute! db-spec ["ALTER TABLE \"READD_TABLE\" ADD COLUMN \"EMAIL\" VARCHAR;"])
(sync/sync-database! (:id db))
- (testing "the same Field row is reactivated and the mirror still keeps its label"
- (is (=? {:active true, :data_sensitivity nil} (field)))
+ (testing "same Field row is reactivated and still keeps its label"
+ (is (=? {:active true, :data_sensitivity :PHI} (field)))
(is (= :PHI (mirror)))))
(finally
(t2/delete! :model/Database :id (mt/id)))))))
@@ -728,3 +730,15 @@
(let [msgs (map :message (messages))]
(is (seq msgs))
(is (not-any? #(re-find #"Table Table" %) msgs)))))))
+
+(deftest sync-keeps-custom-field-order-test
+ (testing "sync keeps positions of table whose field order users set to custom (#86100)"
+ (mt/test-driver :h2
+ (mt/with-temp-copy-of-db
+ (let [table-id (mt/id :venues)
+ custom-order (vec (reverse (t2/select-pks-vec :model/Field :table_id table-id {:order-by [:database_position]})))
+ field-order #(t2/select-pks-vec :model/Field :table_id table-id {:order-by [:position]})]
+ (mt/user-http-request :crowberto :put 200 (format "table/%d/fields/order" table-id) custom-order)
+ (dotimes [_ 2]
+ (is (= custom-order (field-order)))
+ (sync-fields/sync-fields-for-table! (t2/select-one :model/Table :id table-id))))))))
diff --git a/test/metabase/upload/impl_test.clj b/test/metabase/upload/impl_test.clj
index ef23b8c318c..af16fcb2ba6 100644
--- a/test/metabase/upload/impl_test.clj
+++ b/test/metabase/upload/impl_test.clj
@@ -2560,7 +2560,7 @@
(is (= (header-with-auto-pk ["A"])
(column-display-names-for-table table))))
(testing "But we can configure it"
- (field-user-settings/upsert-user-settings
+ (field-user-settings/upsert-field-user-settings!
{:id (t2/select-one-pk :model/Field :name "a" :table_id (:id table))}
{:display_name bespoke-name})
(is (= (header-with-auto-pk [bespoke-name])
diff --git a/test/metabase/warehouse_schema/models/field_test.clj b/test/metabase/warehouse_schema/models/field_test.clj
index 7daa7ccf6c3..a481c96676e 100644
--- a/test/metabase/warehouse_schema/models/field_test.clj
+++ b/test/metabase/warehouse_schema/models/field_test.clj
@@ -7,8 +7,6 @@
[metabase.permissions.models.data-permissions :as data-perms]
[metabase.test :as mt]
[metabase.util :as u]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
- [metabase.warehouse-schema.db :as warehouse-schema.db]
[metabase.warehouse-schema.models.field :as field]
[metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[toucan2.core :as t2]))
@@ -304,106 +302,110 @@
(is (not (t2/exists? :model/FieldUserSettings :field_id field-id))))))
(deftest data-sensitivity-user-setting-test
- (testing "a user-set label lives in the user settings and does not touch the Field"
+ (testing "user-set label is written to Field and recorded in its settings"
(mt/with-temp [:model/Field {field-id :id :as field} {:data_sensitivity :PII}]
- (field-user-settings/upsert-user-settings field {:data_sensitivity :PUBLIC})
- (is (= :PII (t2/select-one-fn :data_sensitivity :model/Field :id field-id)))
- (is (= :PUBLIC (t2/select-one-fn :data_sensitivity :model/FieldUserSettings :field_id field-id)))
- (is (= :PUBLIC (:data_sensitivity (warehouse-schema.db/field field-id))))))
- (testing "a bare update to the Field is written as given and stays hidden behind the user label"
+ (field-user-settings/upsert-field-user-settings! field {:data_sensitivity :PUBLIC})
+ (is (= :PUBLIC (t2/select-one-fn :data_sensitivity :model/Field :id field-id)))
+ (is (= :PUBLIC (t2/select-one-fn :data_sensitivity :model/FieldUserSettings :field_id field-id)))))
+ (testing "upserting nil unsets label, clearing it on Field and dropping emptied settings"
(mt/with-temp [:model/Field {field-id :id :as field} {:data_sensitivity :PII}]
- (field-user-settings/upsert-user-settings field {:data_sensitivity :PUBLIC})
- (t2/update! :model/Field :id field-id {:data_sensitivity :PHI})
- (is (= :PHI (t2/select-one-fn :data_sensitivity :model/Field :id field-id)))
- (is (= :PUBLIC (:data_sensitivity (warehouse-schema.db/field field-id))))))
- (testing "upsert-user-settings with a nil value clears a previously set label on the mirror"
- (mt/with-temp [:model/Field {field-id :id :as field} {:data_sensitivity :PII}]
- (field-user-settings/upsert-user-settings field {:data_sensitivity :PUBLIC})
- (field-user-settings/upsert-user-settings field {:data_sensitivity nil})
- (is (nil? (t2/select-one-fn :data_sensitivity :model/FieldUserSettings :field_id field-id)))
- (is (= :PII (:data_sensitivity (warehouse-schema.db/field field-id)))))))
+ (field-user-settings/upsert-field-user-settings! field {:data_sensitivity :PUBLIC})
+ (field-user-settings/upsert-field-user-settings! field {:data_sensitivity nil})
+ (is (nil? (t2/select-one :model/FieldUserSettings :field_id field-id)))
+ (is (nil? (t2/select-one-fn :data_sensitivity :model/Field :id field-id))))))
-(deftest field-user-settings-read-test
- (mt/with-temp [:model/Field {edited-id :id :as edited} {:display_name "Sync Name"
+(deftest field-user-settings-write-field-row-test
+ (mt/with-temp [:model/Field {edited-id :id :as edited} {:name "sync_name"
+ :display_name "Sync Name"
:description "sync description"
:semantic_type :type/Category
:base_type :type/Text
:effective_type :type/Text
:coercion_strategy nil}
:model/Field {plain-id :id} {:display_name "Plain"}]
- (field-user-settings/upsert-user-settings edited {:display_name "User Name" :description nil :semantic_type nil})
- (testing "the sync row is untouched"
- (is (=? {:display_name "Sync Name" :description "sync description" :semantic_type :type/Category}
- (t2/select-one :model/Field :id edited-id))))
- (testing "user values, a user NULL included, replace the sync ones; the rest of the row and its transforms are intact"
+ (field-user-settings/upsert-field-user-settings! edited {:display_name "User Name" :description nil :semantic_type nil})
+ (testing "user values, user NULL included, are written to Field; rest of row is intact"
(is (=? {:id edited-id :display_name "User Name" :description nil :semantic_type nil :base_type :type/Text
:effective_type :type/Text :coercion_strategy nil :visibility_type :normal}
- (warehouse-schema.db/field edited-id)))
- (is (=? {:id plain-id :display_name "Plain"} (warehouse-schema.db/field plain-id))))
- (testing "a flag left false means the sync value shows, even next to a user value"
- (t2/update! :model/FieldUserSettings :field_id edited-id {:description_set false})
- (is (= "sync description" (:description (warehouse-schema.db/field edited-id)))))
- (testing "coercion_strategy follows the user's effective_type, so a cleared coercion shows as cleared"
- (t2/update! :model/Field :id edited-id {:effective_type :type/Number :coercion_strategy :Coercion/String->Number})
- (field-user-settings/upsert-user-settings edited {:effective_type :type/Text :coercion_strategy nil})
- (is (=? {:effective_type :type/Text :coercion_strategy nil} (warehouse-schema.db/field edited-id)))
- (field-user-settings/unset-user-settings! edited [:effective_type :coercion_strategy])
- (is (=? {:effective_type :type/Number :coercion_strategy :Coercion/String->Number} (warehouse-schema.db/field edited-id))))))
+ (t2/select-one :model/Field :id edited-id)))
+ (is (=? {:id plain-id :display_name "Plain"} (t2/select-one :model/Field :id plain-id))))
+ (testing "settings record which columns user set"
+ (is (=? {:display_name "User Name" :description nil :description_set true :semantic_type nil
+ :semantic_type_set true :fk_target_field_id_set false}
+ (t2/select-one :model/FieldUserSettings :field_id edited-id))))
+ (testing "unsetting leaves Field as it is for sync to take over"
+ (field-user-settings/unset-field-user-settings! edited [:display_name :description])
+ (is (=? {:display_name "User Name" :description nil :semantic_type nil}
+ (t2/select-one :model/Field :id edited-id))))
+ (testing "coercion follows user's effective_type, and unsetting both leaves them for sync"
+ (field-user-settings/upsert-field-user-settings! edited {:effective_type :type/Integer
+ :coercion_strategy :Coercion/String->Integer})
+ (is (=? {:effective_type :type/Integer :coercion_strategy :Coercion/String->Integer}
+ (t2/select-one :model/Field :id edited-id)))
+ (field-user-settings/unset-field-user-settings! edited [:effective_type :coercion_strategy])
+ (is (=? {:effective_type :type/Integer :coercion_strategy :Coercion/String->Integer}
+ (t2/select-one :model/Field :id edited-id))))))
-(deftest upsert-user-settings-flags-test
+(deftest deleting-field-settings-keeps-field-row-test
+ (testing "deleting the settings leaves the Field as it is for sync to take over"
+ (mt/with-temp [:model/Field {field-id :id :as field} {:name "sync_name" :semantic_type :type/Category}]
+ (field-user-settings/upsert-field-user-settings! field {:semantic_type :type/Email
+ :visibility_type :sensitive
+ :display_name "User Name"})
+ (field-user-settings/delete-field-user-settings! [field-id])
+ (is (=? {:semantic_type :type/Email :visibility_type :sensitive :display_name "User Name"}
+ (t2/select-one :model/Field :id field-id)))
+ (is (nil? (t2/select-one :model/FieldUserSettings :field_id field-id))))))
+
+(deftest upsert-field-user-settings-flags-test
(mt/with-temp [:model/Field {field-id :id :as field} {}
:model/Field {target-id :id} {}]
(testing "only the flagged columns present in the settings are flagged"
- (field-user-settings/upsert-user-settings field {:display_name "X" :semantic_type :type/FK :fk_target_field_id target-id})
+ (field-user-settings/upsert-field-user-settings! field {:display_name "X" :semantic_type :type/FK :fk_target_field_id target-id})
(is (=? {:display_name "X" :semantic_type :type/FK :fk_target_field_id target-id
:description_set false :semantic_type_set true :fk_target_field_id_set true}
(t2/select-one :model/FieldUserSettings :field_id field-id))))
(testing "an explicit nil is recorded and flagged as set"
- (field-user-settings/upsert-user-settings field {:description nil})
+ (field-user-settings/upsert-field-user-settings! field {:description nil})
(is (=? {:description nil :description_set true :semantic_type_set true}
(t2/select-one :model/FieldUserSettings :field_id field-id))))
- (testing "unset-user-settings! drops the values and their flags"
- (field-user-settings/unset-user-settings! field [:semantic_type :fk_target_field_id :display_name])
+ (testing "unset-field-user-settings! drops the values and their flags"
+ (field-user-settings/unset-field-user-settings! field [:semantic_type :fk_target_field_id :display_name])
(is (=? {:display_name nil :semantic_type nil :fk_target_field_id nil
:description_set true :semantic_type_set false :fk_target_field_id_set false}
(t2/select-one :model/FieldUserSettings :field_id field-id))))
(testing "dropping the last value deletes the row"
- (field-user-settings/unset-user-settings! field [:description])
+ (field-user-settings/unset-field-user-settings! field [:description])
(is (nil? (t2/select-one :model/FieldUserSettings :field_id field-id))))
(testing "an upsert that leaves no value behind creates no row"
- (field-user-settings/upsert-user-settings field {:display_name nil})
+ (field-user-settings/upsert-field-user-settings! field {:display_name nil})
(is (nil? (t2/select-one :model/FieldUserSettings :field_id field-id))))
(testing "keys outside the user-settable set are ignored"
- (field-user-settings/upsert-user-settings field {:name "nope" :position 3})
+ (field-user-settings/upsert-field-user-settings! field {:name "nope" :position 3})
(is (nil? (:name (t2/select-one :model/FieldUserSettings :field_id field-id)))))))
-(deftest field-query-test
- (testing "a query sourced from field-query reads the user's values, a user NULL included"
- (mt/with-temp [:model/Field {field-id :id :as field} {:display_name "Sync" :description "sync" :semantic_type :type/Category}]
- (field-user-settings/upsert-user-settings field {:display_name "User" :description nil})
- (is (=? {:display_name "User" :description nil :semantic_type :type/Category}
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]})))
- (testing "and metabase_field itself still holds sync's values"
- (is (=? {:display_name "Sync" :description "sync" :semantic_type :type/Category}
- (t2/select-one :model/Field :id field-id))))))
- (testing "{:user-settings? false} asks for sync's values from the same helper"
- (mt/with-temp [:model/Field {field-id :id :as field} {:display_name "Sync"}]
- (field-user-settings/upsert-user-settings field {:display_name "User"})
- (is (= "Sync" (:display_name (t2/select-one :model/Field :id field-id
- {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))))))
- (testing "a coercion the user cleared reads as cleared, following their effective_type"
- (mt/with-temp [:model/Field {field-id :id :as field} {:base_type :type/Text
- :effective_type :type/Number
- :coercion_strategy :Coercion/String->Number}]
- (field-user-settings/upsert-user-settings field {:effective_type :type/Text :coercion_strategy nil})
- (is (=? {:effective_type :type/Text :coercion_strategy nil}
- (t2/select-one :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query)]}))))))
-
(deftest deactivating-fk-target-unsets-user-fk-test
- (testing "retiring a Field drops the user-set FKs pointing at it so the sync values show again"
+ (testing "retiring Field drops user-set FKs pointing at it and clears them on source Field"
(mt/with-temp [:model/Field {target-id :id} {}
:model/Field {source-id :id :as source} {:semantic_type :type/Category}]
- (field-user-settings/upsert-user-settings source {:semantic_type :type/FK :fk_target_field_id target-id})
+ (field-user-settings/upsert-field-user-settings! source {:semantic_type :type/FK :fk_target_field_id target-id})
(t2/update! :model/Field :id target-id {:active false})
(is (nil? (t2/select-one :model/FieldUserSettings :field_id source-id)))
- (is (= :type/Category (:semantic_type (warehouse-schema.db/field source-id)))))))
+ (is (=? {:semantic_type nil :fk_target_field_id nil} (t2/select-one :model/Field :id source-id)))))
+ (testing "retiring Field also drops a user-set FK semantic type whose target sync set"
+ (mt/with-temp [:model/Field {target-id :id} {}
+ :model/Field {source-id :id :as source} {}]
+ (field-user-settings/upsert-field-user-settings! source {:semantic_type :type/FK})
+ (t2/update! :model/Field :id source-id {:fk_target_field_id target-id})
+ (t2/update! :model/Field :id target-id {:active false})
+ (is (not (t2/select-one-fn :semantic_type_set :model/FieldUserSettings :field_id source-id)))
+ (is (=? {:semantic_type nil :fk_target_field_id nil} (t2/select-one :model/Field :id source-id)))))
+ (testing "retiring Field keeps a user-set semantic type other than FK on a Field sync pointed at it"
+ (mt/with-temp [:model/Field {target-id :id} {}
+ :model/Field {source-id :id :as source} {}]
+ (field-user-settings/upsert-field-user-settings! source {:semantic_type :type/Category})
+ (t2/update! :model/Field :id source-id {:fk_target_field_id target-id})
+ (t2/update! :model/Field :id target-id {:active false})
+ (is (=? {:semantic_type :type/Category :semantic_type_set true}
+ (t2/select-one :model/FieldUserSettings :field_id source-id)))
+ (is (=? {:semantic_type :type/Category :fk_target_field_id nil} (t2/select-one :model/Field :id source-id))))))
diff --git a/test/metabase/warehouse_schema/models/table_user_settings_test.clj b/test/metabase/warehouse_schema/models/table_user_settings_test.clj
index 0766d9fafda..358d3b43f4c 100644
--- a/test/metabase/warehouse_schema/models/table_user_settings_test.clj
+++ b/test/metabase/warehouse_schema/models/table_user_settings_test.clj
@@ -2,66 +2,66 @@
(:require
[clojure.test :refer :all]
[metabase.test :as mt]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.db :as warehouse-schema.db]
[metabase.warehouse-schema.models.table-user-settings :as table-user-settings]
[toucan2.core :as t2]))
(defn- user-table
- "The Table as users see it: what they set, else what sync wrote."
+ "The Table row."
[table-id]
- (t2/select-one :model/Table :id table-id {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one :model/Table :id table-id))
-(deftest table-query-test
- (testing "a query sourced from table-query reads the user's values over sync's"
- (mt/with-temp [:model/Table {table-id :id :as table} {:display_name "Sync Name"
- :description "sync description"
- :entity_type :entity/GenericTable}]
- (table-user-settings/upsert-user-settings table {:display_name "User Name" :description nil})
- (testing "the sync row is untouched"
- (is (=? {:display_name "Sync Name" :description "sync description"}
- (t2/select-one :model/Table :id table-id))))
- (testing "user values, a user NULL included, replace the sync ones"
- (is (=? {:id table-id :display_name "User Name" :description nil :entity_type :entity/GenericTable}
- (user-table table-id))))
- (testing "a flag left false means the sync value shows again"
- (t2/update! :model/TableUserSettings :table_id table-id {:description_set false})
- (is (= "sync description" (:description (user-table table-id))))))))
+(deftest table-user-settings-write-table-row-test
+ (mt/with-temp [:model/Table {table-id :id :as table} {:name "sync_name"
+ :display_name "Sync Name"
+ :description "sync description"
+ :entity_type :entity/GenericTable}]
+ (table-user-settings/upsert-table-user-settings! table {:display_name "User Name" :description nil})
+ (testing "user values, user NULL included, are written to Table; rest of row is intact"
+ (is (=? {:id table-id :display_name "User Name" :description nil :entity_type :entity/GenericTable}
+ (user-table table-id))))
+ (testing "settings record which columns user set"
+ (is (=? {:display_name "User Name" :description nil :description_set true}
+ (t2/select-one :model/TableUserSettings :table_id table-id))))
+ (testing "unsetting leaves Table as it is for sync to take over"
+ (table-user-settings/unset-table-user-settings! table [:display_name :description])
+ (is (=? {:display_name "User Name" :description nil} (user-table table-id)))
+ (is (nil? (t2/select-one :model/TableUserSettings :table_id table-id))))))
(deftest not-null-columns-need-no-flag-test
(testing "a NULL in a column that is NOT NULL on the Table can only mean the user set nothing"
(mt/with-temp [:model/Table {table-id :id :as table} {:field_order :database}]
(is (= :database (:field_order (user-table table-id))))
- (table-user-settings/upsert-user-settings table {:field_order :alphabetical})
+ (table-user-settings/upsert-table-user-settings! table {:field_order :alphabetical})
(is (= :alphabetical (:field_order (user-table table-id))))
- (testing "clearing it falls back to the Table's own value, and drops the row it emptied"
- (table-user-settings/unset-user-settings! table [:field_order])
- (is (= :database (:field_order (user-table table-id))))
+ (testing "clearing it keeps the Table's value, and drops the row it emptied"
+ (table-user-settings/unset-table-user-settings! table [:field_order])
+ (is (= :alphabetical (:field_order (user-table table-id))))
(is (nil? (t2/select-one :model/TableUserSettings :table_id table-id)))))))
(deftest visibility-pair-is-recorded-whole-test
(testing "visibility_type and data_layer are one choice, so setting either records both"
(mt/with-temp [:model/Table {table-id :id :as table} {}]
- (table-user-settings/upsert-user-settings table {:visibility_type :hidden})
+ (table-user-settings/upsert-table-user-settings! table {:visibility_type :hidden})
(is (=? {:visibility_type :hidden :data_layer :hidden} (user-table table-id)))
- (table-user-settings/upsert-user-settings table {:data_layer :internal})
+ (table-user-settings/upsert-table-user-settings! table {:data_layer :internal})
(is (=? {:visibility_type nil :data_layer :internal} (user-table table-id))))))
(deftest publishing-is-recorded-as-a-pair-test
(testing "collection_id is only meaningful alongside is_published, which is NOT NULL and answers for the pair"
(mt/with-temp [:model/Collection {collection-id :id} {}
:model/Table {table-id :id :as table} {}]
- (table-user-settings/upsert-user-settings table {:is_published true :collection_id collection-id})
+ (table-user-settings/upsert-table-user-settings! table {:is_published true :collection_id collection-id})
(is (=? {:is_published true :collection_id collection-id} (user-table table-id)))
(testing "a Table published into the root collection keeps its NULL collection_id"
- (table-user-settings/upsert-user-settings table {:is_published true :collection_id nil})
+ (table-user-settings/upsert-table-user-settings! table {:is_published true :collection_id nil})
(is (=? {:is_published true :collection_id nil} (user-table table-id)))))))
(deftest bulk-upsert-test
(testing "a bulk edit records the same values on every Table it names"
(mt/with-temp [:model/Table {t1 :id} {}
:model/Table {t2 :id} {}]
- (table-user-settings/upsert-user-settings-for-tables! #{t1 t2} {:owner_email "[email protected]"})
+ (table-user-settings/upsert-table-user-settings-for-tables! #{t1 t2} {:owner_email "[email protected]"})
(map (comp :owner_email user-table) [t1 t2])))
(is (= 2 (count (warehouse-schema.db/table-ids-with-user-settings #{t1 t2})))))))
@@ -70,7 +70,7 @@
(testing "the model's hooks, not just the upsert fns, enforce what has to hold of a settings row -- so a serdes
import or any other writer cannot store one that readers would misread"
(mt/with-temp [:model/Database {db-id :id} {}
- :model/Table {table-id :id} {:db_id db-id}
+ :model/Table {table-id :id :as table} {:db_id db-id}
:model/Field {field-id :id} {:table_id table-id}]
(testing "a written column carries its _set flag, on insert and on update"
(t2/insert! :model/FieldUserSettings {:field_id field-id :description "d"})
@@ -87,7 +87,7 @@
:visibility_type_set true :data_layer_set true}
(t2/select-one :model/TableUserSettings :table_id table-id))))
(testing "a change a user may not make is refused whoever writes it"
- (t2/update! :model/TableUserSettings :table_id table-id {:data_authority :authoritative})
+ (table-user-settings/upsert-table-user-settings! table {:data_authority :authoritative})
(is (thrown-with-msg? Exception #"Cannot set data_authority back to unconfigured"
(t2/update! :model/TableUserSettings :table_id table-id
{:data_authority :unconfigured})))))))
@@ -101,4 +101,22 @@
(is (= :custom (:field_order (user-table table-id))))
(is (= {f2 0 f1 1} (t2/select-fn->fn :field_id :custom_position :model/FieldUserSettings :field_id [:in [f1 f2]])))
(is (= [f2 f1] (t2/select-pks-vec :model/Field :table_id table-id {:order-by [[:position :asc]]})))
- (is (= #{0} (t2/select-fn-set :custom_position :model/Field :table_id table-id))))))
+ (is (= {f2 0 f1 1} (t2/select-pk->fn :custom_position :model/Field :table_id table-id))))))
+
+(deftest deleting-settings-keeps-table-row-test
+ (testing "deleting the settings leaves the Table as it is for sync to take over"
+ (mt/with-temp [:model/Table {table-id :id :as table} {:name "sync_name" :data_authority :unconfigured}]
+ (table-user-settings/upsert-table-user-settings! table {:data_authority :authoritative
+ :display_name "User Name"
+ :description "user description"})
+ (table-user-settings/delete-table-user-settings! [table-id])
+ (is (=? {:data_authority :authoritative :description "user description" :display_name "User Name"}
+ (user-table table-id)))
+ (is (nil? (t2/select-one :model/TableUserSettings :table_id table-id))))))
+
+(deftest unchanged-half-of-visibility-pair-is-recorded-test
+ (testing "setting data_layer to its current value records visibility_type as set too"
+ (mt/with-temp [:model/Table {table-id :id :as table} {:visibility_type :cruft :data_layer :hidden}]
+ (table-user-settings/upsert-table-user-settings! table {:data_layer :hidden})
+ (is (=? {:visibility_type :cruft :data_layer :hidden :visibility_type_set true :data_layer_set true}
+ (t2/select-one :model/TableUserSettings :table_id table-id))))))
diff --git a/test/metabase/warehouse_schema/settings_test.clj b/test/metabase/warehouse_schema/settings_test.clj
index 56f74481400..d40b0000b09 100644
--- a/test/metabase/warehouse_schema/settings_test.clj
+++ b/test/metabase/warehouse_schema/settings_test.clj
@@ -3,7 +3,6 @@
[clojure.test :refer :all]
[metabase.test :as mt]
[metabase.test.util :as tu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema.humanization :as humanization]
[metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[metabase.warehouse-schema.settings :as warehouse-schema.settings]
@@ -65,12 +64,10 @@
(mt/with-temp [:model/Field {user-set-id :id} {:name "toucansare_cool", :display_name "Toucansare Cool"}
:model/Field {custom-id :id} {:name "fussybird_sightings", :display_name "Some Other Name"}
:model/Field {synced-id :id} {:name "bird_watchers", :display_name "Bird Watchers"}]
- (field-user-settings/upsert-user-settings {:id user-set-id} {:display_name "User's Name"})
+ (field-user-settings/upsert-field-user-settings! {:id user-set-id} {:display_name "User's Name"})
(warehouse-schema.settings/humanization-strategy! "none")
- (testing "the user-set Field's raw display name is left alone, and their own name is what reads back"
- (is (= "Toucansare Cool" (t2/select-one-fn :display_name :model/Field, :id user-set-id)))
- (is (= "User's Name" (t2/select-one-fn :display_name :model/Field, :id user-set-id
- {:from [(warehouse-schema-overlay/field-query)]}))))
+ (testing "the user-set Field keeps the user's name"
+ (is (= "User's Name" (t2/select-one-fn :display_name :model/Field, :id user-set-id))))
(testing "a raw display name that differs from the old humanization is left alone"
(is (= "Some Other Name" (t2/select-one-fn :display_name :model/Field, :id custom-id))))
(testing "a humanized display name is rewritten to the new strategy"
diff --git a/test/metabase/warehouse_schema_overlay/core_test.clj b/test/metabase/warehouse_schema_overlay/core_test.clj
deleted file mode 100644
index d7b7c31f0f9..00000000000
--- a/test/metabase/warehouse_schema_overlay/core_test.clj
+++ /dev/null
@@ -1,52 +0,0 @@
-(ns metabase.warehouse-schema-overlay.core-test
- (:require
- [clojure.test :refer :all]
- [metabase.test :as mt]
- [metabase.util.malli :as mu]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
- [metabase.warehouse-schema.schema]
- [toucan2.core :as t2]))
-
-(comment metabase.warehouse-schema.schema/keep-me)
-
-(deftest ^:parallel table-columns-match-the-table-schema-test
- (testing "the spelled-out column list keeps up with the Table schema it cannot require"
- (is (= (into #{:id :unique_table_helper} (mu/map-schema-keys :metabase.warehouse-schema.schema/table.update))
- warehouse-schema-overlay/table-columns))))
-
-(deftest ^:parallel field-columns-match-the-field-schema-test
- (testing "the spelled-out column list keeps up with the Field schema it cannot require"
- (is (= (into #{:id :unique_field_helper} (mu/map-schema-keys :metabase.warehouse-schema.schema/field.update))
- warehouse-schema-overlay/field-columns))))
-
-(defn- visibility-types
- "`[table-user-visibility-type, visibility_type read through table-query]` for the Table `table-id`."
- [table-id]
- [(t2/select-one-fn :v [(t2/table-name :model/Table)
- [(warehouse-schema-overlay/table-user-visibility-type :metabase_table) :v]]
- {:where [:= :metabase_table.id table-id]})
- (some-> (t2/select-one-fn :visibility_type [:model/Table :visibility_type] :id table-id
- {:from [(warehouse-schema-overlay/table-query)]})
- name)])
-
-(deftest table-user-visibility-type-test
- (testing "table-user-visibility-type matches the visibility_type table-query reads"
- (mt/with-temp [:model/Table {plain :id} {}
- :model/Table {cruft :id} {:visibility_type :cruft}
- :model/Table {put-hidden :id} {}
- :model/Table {data-studio :id} {}
- :model/Table {cruft-cleared :id} {:visibility_type :cruft}
- :model/Table {description-only :id} {:visibility_type :cruft}]
- (mt/user-http-request :crowberto :put 200 (format "table/%d" put-hidden) {:visibility_type "hidden"})
- (mt/user-http-request :crowberto :post 200 "data-studio/table/edit"
- {:table_ids [data-studio] :data_layer "hidden"})
- (t2/insert! :model/TableUserSettings {:table_id cruft-cleared :visibility_type nil})
- (t2/insert! :model/TableUserSettings {:table_id description-only :description "user description"})
- (doseq [[desc table-id expected] [["no settings row" plain nil]
- ["sync's cruft with no settings row" cruft "cruft"]
- ["hidden through PUT /api/table/:id" put-hidden "hidden"]
- ["data_layer hidden through Data Studio" data-studio "hidden"]
- ["an explicit user NULL over sync's cruft" cruft-cleared nil]
- ["a settings row with visibility_type unset over cruft" description-only "cruft"]]]
- (testing desc
- (is (= [expected expected] (visibility-types table-id))))))))
diff --git a/test/metabase/warehouse_schema_rest/api/field_test.clj b/test/metabase/warehouse_schema_rest/api/field_test.clj
index f9c1484d6af..4c3971cf15d 100644
--- a/test/metabase/warehouse_schema_rest/api/field_test.clj
+++ b/test/metabase/warehouse_schema_rest/api/field_test.clj
@@ -109,8 +109,8 @@
["distincts" 75]]
(mt/user-http-request :crowberto :get 200 (format "field/%d/summary" (mt/id :categories :name)))))))
-(defn- sync-field
- "The Field row as sync wrote it: what `metabase_field` itself holds, with no user values applied."
+(defn- field-row
+ "The Field row in `metabase_field`."
[field-id]
(t2/select-one :model/Field :id field-id))
@@ -122,7 +122,6 @@
:semantic_type
:json_unfolding
:fk_target_field_id
- :nfc_path
:data_sensitivity]))
(mt/defdataset integer-coerceable
@@ -134,7 +133,7 @@
(testing "PUT /api/field/:id"
(testing "test that we can do basic field update work, including unsetting some fields such as semantic-type"
(mt/with-temp [:model/Field {field-id :id} {:name "Field Test"}]
- (let [original-field (simple-field-details (sync-field field-id))]
+ (let [original-field (simple-field-details (field-row field-id))]
(testing "orignal value"
(is (= {:name "Field Test"
:display_name "Field Test"
@@ -143,7 +142,6 @@
:visibility_type :normal
:json_unfolding false
:fk_target_field_id nil
- :nfc_path nil
:data_sensitivity nil}
original-field)))
(let [;; set it
@@ -155,7 +153,6 @@
:semantic_type :type/Name
:json_unfolding true
:visibility_type :sensitive
- :nfc_path ["bob" "dobbs"]
:data_sensitivity "PII"})]
(testing "response body shows the user's values"
(is (= {:name "Field Test"
@@ -165,11 +162,18 @@
:visibility_type "sensitive"
:json_unfolding true
:fk_target_field_id nil
- :nfc_path ["bob" "dobbs"]
:data_sensitivity "PII"}
(simple-field-details response))))
- (testing ":model/Field stays at the sync value; :model/FieldUserSettings holds the user's values"
- (is (= original-field (simple-field-details (sync-field field-id))))
+ (testing "Field row shows user's values and FieldUserSettings records them"
+ (is (= {:name "Field Test"
+ :display_name "yay"
+ :description "foobar"
+ :semantic_type :type/Name
+ :visibility_type :sensitive
+ :json_unfolding true
+ :fk_target_field_id nil
+ :data_sensitivity :PII}
+ (simple-field-details (field-row field-id))))
(is (=? {:display_name "yay"
:description "foobar"
:description_set true
@@ -177,7 +181,6 @@
:semantic_type_set true
:visibility_type :sensitive
:json_unfolding true
- :nfc_path ["bob" "dobbs"]
:data_sensitivity :PII}
(t2/select-one :model/FieldUserSettings :field_id field-id))))
(testing "GET /api/field/:id shows the user's values"
@@ -188,16 +191,22 @@
:visibility_type "sensitive"
:json_unfolding true
:fk_target_field_id nil
- :nfc_path ["bob" "dobbs"]
:data_sensitivity "PII"}
(simple-field-details (mt/user-http-request :crowberto :get 200 (format "field/%d" field-id))))))
;; unset it
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id) {:description nil
:semantic_type nil
- :nfc_path nil
:data_sensitivity nil})
- (testing ":model/Field is still at the sync value after clearing the user's values"
- (is (= original-field (simple-field-details (sync-field field-id)))))
+ (testing "Field row shows cleared values"
+ (is (= {:name "Field Test"
+ :display_name "yay"
+ :description nil
+ :semantic_type nil
+ :visibility_type :sensitive
+ :json_unfolding true
+ :fk_target_field_id nil
+ :data_sensitivity nil}
+ (simple-field-details (field-row field-id)))))
(testing "GET shows the cleared values"
(is (= {:name "Field Test"
:display_name "yay"
@@ -206,7 +215,6 @@
:visibility_type "sensitive"
:json_unfolding true
:fk_target_field_id nil
- :nfc_path nil
:data_sensitivity nil}
(simple-field-details (mt/user-http-request :crowberto :get 200 (format "field/%d" field-id))))))))))))
@@ -217,8 +225,8 @@
(is (= "user description"
(:description (mt/user-http-request :crowberto :get 200 (format "field/%d" field-id)))))
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id) {:description nil})
- (testing "the user's cleared description wins even though metabase_field.description is untouched"
- (is (= "synced description" (:description (sync-field field-id))))
+ (testing "user's cleared description is written to Field and recorded in its settings"
+ (is (nil? (:description (field-row field-id))))
(is (nil? (:description (mt/user-http-request :crowberto :get 200 (format "field/%d" field-id)))))
(is (=? {:description nil :description_set true}
(t2/select-one :model/FieldUserSettings :field_id field-id)))))))
@@ -226,21 +234,21 @@
(deftest update-field-data-sensitivity-test
(testing "PUT /api/field/:id"
(mt/with-temp [:model/Field {field-id :id} {:name "Field Test"}]
- (testing "setting data_sensitivity writes only to :model/FieldUserSettings"
+ (testing "setting data_sensitivity writes it to Field and its settings"
(is (= "SEC_KEY"
(:data_sensitivity (mt/user-http-request :crowberto :put 200 (format "field/%d" field-id)
{:data_sensitivity "SEC_KEY"}))))
- (is (nil? (:data_sensitivity (sync-field field-id))))
+ (is (= :SEC_KEY (:data_sensitivity (field-row field-id))))
(is (= :SEC_KEY (t2/select-one-fn :data_sensitivity :model/FieldUserSettings :field_id field-id))))
(testing "a null data_sensitivity clears the user setting"
(is (nil? (:data_sensitivity (mt/user-http-request :crowberto :put 200 (format "field/%d" field-id)
{:data_sensitivity nil}))))
- (is (nil? (:data_sensitivity (sync-field field-id))))
+ (is (nil? (:data_sensitivity (field-row field-id))))
(is (nil? (t2/select-one-fn :data_sensitivity :model/FieldUserSettings :field_id field-id))))
(testing "an update that omits data_sensitivity leaves it unchanged"
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id) {:data_sensitivity "PHI"})
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id) {:description "unrelated"})
- (is (nil? (:data_sensitivity (sync-field field-id))))
+ (is (= :PHI (:data_sensitivity (field-row field-id))))
(is (= :PHI (t2/select-one-fn :data_sensitivity :model/FieldUserSettings :field_id field-id)))
(is (= "PHI" (:data_sensitivity (mt/user-http-request :crowberto :get 200 (format "field/%d" field-id)))))))))
@@ -252,7 +260,7 @@
(is (=? {:errors {:data_sensitivity some?}}
(mt/user-http-request :crowberto :put 400 (format "field/%d" field-id)
{:data_sensitivity bad-value})))
- (is (nil? (:data_sensitivity (sync-field field-id)))))))))
+ (is (nil? (:data_sensitivity (field-row field-id)))))))))
(deftest update-field-test-2
(testing "PUT /api/field/:id"
@@ -300,18 +308,19 @@
(let [response (set-strategy! :Coercion/UNIXSeconds->DateTime)]
(is (= "type/Instant" (:effective_type response)))
(is (= "type/Instant" (:effective_type (mt/user-http-request :crowberto :get 200 (format "field/%d" field-id)))))
- (is (not= :type/Instant (:effective_type (sync-field field-id))))
+ (is (= :type/Instant (:effective_type (field-row field-id))))
(is (contains? (get-in (t2/select-one :model/Field :id field-id) [:fingerprint :type]) :type/DateTime))))))))))
(deftest update-field-coercion-set-then-cleared-test
(testing "PUT /api/field/:id"
(mt/with-temp [:model/Field {field-id :id} {:name "Field Test"}]
- (testing "setting a coercion strategy is user-set only"
+ (testing "setting coercion strategy writes it to Field and its settings"
(let [response (mt/user-http-request :crowberto :put 200 (format "field/%d" field-id)
{:coercion_strategy :Coercion/YYYYMMDDHHMMSSString->Temporal})]
(is (= "Coercion/YYYYMMDDHHMMSSString->Temporal" (:coercion_strategy response)))
(is (= "type/DateTime" (:effective_type response))))
- (is (nil? (:coercion_strategy (sync-field field-id))))
+ (is (=? {:coercion_strategy :Coercion/YYYYMMDDHHMMSSString->Temporal :effective_type :type/DateTime}
+ (field-row field-id)))
(is (= :Coercion/YYYYMMDDHHMMSSString->Temporal
(t2/select-one-fn :coercion_strategy :model/FieldUserSettings :field_id field-id)))
(is (= :type/DateTime
@@ -321,6 +330,7 @@
{:coercion_strategy nil})]
(is (nil? (:coercion_strategy response)))
(is (= "type/Text" (:effective_type response))))
+ (is (=? {:coercion_strategy nil :effective_type :type/Text} (field-row field-id)))
(is (=? {:coercion_strategy nil :effective_type :type/Text}
(t2/select-one :model/FieldUserSettings :field_id field-id)))))))
@@ -330,7 +340,7 @@
(mt/with-temp [:model/Field {field-id :id} {:name "Field Test"}]
(mt/user-http-request :rasta :put 403 (format "field/%d" field-id) {:name "Field Test 2"})
(mt/user-http-request :rasta :put 403 (format "field/%d" field-id) {:data_sensitivity "PII"})
- (is (nil? (:data_sensitivity (sync-field field-id))))))))
+ (is (nil? (:data_sensitivity (field-row field-id))))))))
(deftest ^:parallel update-field-hydrated-target-test
(testing "PUT /api/field/:id"
@@ -339,20 +349,21 @@
:model/Field fk-field-2 {}
:model/Field field {:semantic_type :type/FK :fk_target_field_id (:id fk-field-1)}]
(is (= (:id fk-field-2)
- (:id (:target (mt/user-http-request :crowberto :put 200 (format "field/%d" (:id field)) (assoc field :fk_target_field_id (:id fk-field-2)))))))))))
+ (:id (:target (mt/user-http-request :crowberto :put 200 (format "field/%d" (:id field)) (-> field
+ (dissoc :has_field_values)
+ (assoc :fk_target_field_id (:id fk-field-2))))))))))))
(deftest remove-fk-semantic-type-test
(testing "PUT /api/field/:id"
- (testing "when we set the semantic-type from `:type/FK` to something else, `:fk_target_field_id` reads as nil
- though :model/Field's is untouched"
+ (testing "when we set the semantic-type from `:type/FK` to something else, `:fk_target_field_id` is cleared"
(mt/with-temp [:model/Field {fk-field-id :id} {}
:model/Field {field-id :id} {:semantic_type :type/FK :fk_target_field_id fk-field-id}]
(testing "before API call"
- (is (= fk-field-id (:fk_target_field_id (sync-field field-id)))))
+ (is (= fk-field-id (:fk_target_field_id (field-row field-id)))))
;; unset the :type/FK semantic-type
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id) {:semantic_type :type/Name})
(testing "after API call"
- (is (= fk-field-id (:fk_target_field_id (sync-field field-id))))
+ (is (=? {:semantic_type :type/Name :fk_target_field_id nil} (field-row field-id)))
(is (=? {:semantic_type :type/Name
:semantic_type_set true
:fk_target_field_id nil}
@@ -360,7 +371,7 @@
(is (nil? (:fk_target_field_id (mt/user-http-request :crowberto :get 200 (format "field/%d" field-id))))))))))
(deftest remove-sync-detected-fk-test
- (testing "PUT /api/field/:id clearing semantic_type on a sync-detected FK hides the target without touching sync data"
+ (testing "PUT /api/field/:id clearing semantic_type on a sync-detected FK clears the target"
(mt/with-temp [:model/Field {target-id :id} {}
:model/Field {field-id :id} {}]
(t2/update! :model/Field :id field-id {:semantic_type :type/FK :fk_target_field_id target-id})
@@ -375,9 +386,10 @@
:fk_target_field_id nil
:target nil}
(mt/user-http-request :crowberto :get 200 (format "field/%d" field-id)))))
- (testing ":model/Field still has the sync-detected FK"
- (is (= :type/FK (:semantic_type (sync-field field-id))))
- (is (= target-id (:fk_target_field_id (sync-field field-id))))))))
+ (testing "Field row has no FK, and its settings record user cleared it"
+ (is (=? {:semantic_type nil :fk_target_field_id nil} (field-row field-id)))
+ (is (=? {:semantic_type_set true :fk_target_field_id_set true}
+ (t2/select-one :model/FieldUserSettings :field_id field-id)))))))
(deftest update-fk-target-field-id-test
(testing "PUT /api/field/:id"
@@ -385,7 +397,7 @@
(mt/with-temp [:model/Field {field-id :id} {:base_type :type/Integer}]
(mt/user-http-request :crowberto :put 200 (str "field/" field-id)
{:semantic_type :type/Quantity})
- (is (nil? (:semantic_type (sync-field field-id))))
+ (is (= :type/Quantity (:semantic_type (field-row field-id))))
(is (= :type/Quantity
(t2/select-one-fn :semantic_type :model/FieldUserSettings :field_id field-id)))
(is (= "type/Quantity"
@@ -552,7 +564,9 @@
(testing "before update"
(is (= "Widget ID"
(:name (dimension-for-field (:id field))))))
- (mt/user-http-request :crowberto :put 200 (format "field/%d" (:id field)) (assoc field :display_name "SKU"))
+ (mt/user-http-request :crowberto :put 200 (format "field/%d" (:id field)) (-> field
+ (dissoc :has_field_values)
+ (assoc :display_name "SKU")))
(testing "after update"
(is (= "SKU"
(:name (dimension-for-field (:id field)))))))))
@@ -745,19 +759,19 @@
(mt/boolean-ids-and-timestamps (dimension-for-field field-id-1))))))))))
(deftest remove-fk-semantic-type-test-2
- (testing "When removing the FK semantic type, the fk_target_field_id reads as cleared too, without touching :model/Field"
+ (testing "When removing the FK semantic type, the fk_target_field_id is cleared too"
(mt/with-temp [:model/Field {field-id-1 :id} {:name "Field Test 1"}
:model/Field {field-id-2 :id} {:name "Field Test 2"
:semantic_type :type/FK
:fk_target_field_id field-id-1}]
- (let [raw-field #(simple-field-details (sync-field field-id-2))
+ (let [raw-field #(simple-field-details (field-row field-id-2))
before (raw-field)]
(testing "before change"
(is (= {:semantic_type :type/FK, :fk_target_field_id field-id-1}
(select-keys before [:semantic_type :fk_target_field_id]))))
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id-2) {:semantic_type nil})
- (testing "after change, :model/Field is untouched"
- (is (= before (raw-field))))
+ (testing "after change, Field row has no FK"
+ (is (= (assoc before :semantic_type nil :fk_target_field_id nil) (raw-field))))
(testing "after change, the user sees no FK"
(is (=? {:semantic_type nil, :fk_target_field_id nil}
(mt/user-http-request :crowberto :get 200 (format "field/%d" field-id-2)))))))))
@@ -769,13 +783,13 @@
:model/Field {field-id-3 :id} {:name "Field Test 3"
:semantic_type :type/FK
:fk_target_field_id field-id-1}]
- (let [raw-field #(simple-field-details (sync-field field-id-3))
+ (let [raw-field #(simple-field-details (field-row field-id-3))
before (raw-field)]
(testing "before change"
(is (= field-id-1 (:fk_target_field_id before))))
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id-3) {:fk_target_field_id field-id-2})
- (testing "after change, :model/Field is untouched"
- (is (= before (raw-field))))
+ (testing "after change, Field row has new target"
+ (is (= (assoc before :fk_target_field_id field-id-2) (raw-field))))
(testing "after change, the user sees the new target"
(is (= field-id-2
(:fk_target_field_id (mt/user-http-request :crowberto :get 200 (format "field/%d" field-id-3))))))))))
@@ -784,15 +798,15 @@
(testing "Checking update of the fk_target_field_id along with an FK change"
(mt/with-temp [:model/Field {field-id-1 :id} {:name "Field Test 1"}
:model/Field {field-id-2 :id} {:name "Field Test 2"}]
- (let [raw-field #(simple-field-details (sync-field field-id-2))
+ (let [raw-field #(simple-field-details (field-row field-id-2))
before (raw-field)]
(testing "before change"
(is (= {:semantic_type nil, :fk_target_field_id nil}
(select-keys before [:semantic_type :fk_target_field_id]))))
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id-2) {:semantic_type :type/FK
:fk_target_field_id field-id-1})
- (testing "after change, :model/Field is untouched"
- (is (= before (raw-field))))
+ (testing "after change, Field row has new FK"
+ (is (= (assoc before :semantic_type :type/FK :fk_target_field_id field-id-1) (raw-field))))
(testing "after change, the user sees the new FK"
(is (=? {:semantic_type "type/FK", :fk_target_field_id field-id-1}
(mt/user-http-request :crowberto :get 200 (format "field/%d" field-id-2)))))))))
@@ -804,14 +818,14 @@
:model/Field {field-id-2 :id} {:name "Field Test 2"
:semantic_type :type/FK
:fk_target_field_id field-id-1}]
- (let [raw-field #(simple-field-details (sync-field field-id-2))
+ (let [raw-field #(simple-field-details (field-row field-id-2))
before (raw-field)]
(testing "before change"
(is (= {:semantic_type :type/FK, :fk_target_field_id field-id-1, :description nil}
(select-keys before [:semantic_type :fk_target_field_id :description]))))
(mt/user-http-request :crowberto :put 200 (format "field/%d" field-id-2) {:description "foo"})
- (testing "after change, :model/Field is untouched, including its FK columns"
- (is (= before (raw-field))))
+ (testing "after change, Field row has new description and same FK"
+ (is (= (assoc before :description "foo") (raw-field))))
(testing "after change, the user sees the new description and the same FK"
(is (=? {:semantic_type "type/FK", :fk_target_field_id field-id-1, :description "foo"}
(mt/user-http-request :crowberto :get 200 (format "field/%d" field-id-2))))))))))
@@ -934,7 +948,7 @@
get-database (fn [] (t2/select-one :model/Database :id (mt/id)))
set-json-unfolding-for-field! (fn [v]
(mt/user-http-request :crowberto :put 200 (format "field/%d" (mt/id :json :json_bit))
- (assoc field :json_unfolding v)))
+ {:json_unfolding v}))
set-json-unfolding-for-db! (fn [v]
(let [updated-db (into {} (assoc-in database [:details :json-unfolding] v))]
(mt/user-http-request :crowberto :put 200 (format "database/%d" (:id database))
@@ -976,7 +990,7 @@
get-database (fn [] (t2/select-one :model/Database :id (mt/id)))
set-json-unfolding-for-field! (fn [v]
(mt/user-http-request :crowberto :put 200 (format "field/%d" (mt/id :json :json_bit))
- (assoc (get-field) :json_unfolding v)))
+ {:json_unfolding v}))
set-json-unfolding-for-db! (fn [v]
(let [updated-db (into {} (assoc-in database [:details :json-unfolding] v))]
(mt/user-http-request :crowberto :put 200 (format "database/%d" (:id database))
@@ -1021,8 +1035,8 @@
(is (= "Coercion/UNIXSeconds->DateTime" (:coercion_strategy field)))
(is (isa? (keyword (:effective_type field)) :type/DateTime))
(is (= "minutes" (-> field :settings :time_enabled))))
- (testing ":model/Field itself never gets a coercion strategy"
- (is (nil? (:coercion_strategy (sync-field (mt/id :venues :price)))))))))
+ (testing "Field row keeps coercion strategy"
+ (is (= :Coercion/UNIXSeconds->DateTime (:coercion_strategy (field-row (mt/id :venues :price)))))))))
(deftest field-values-requires-query-permission-test
(testing "GET /api/field/:id/values requires query permission (view-data + create-queries)"
diff --git a/test/metabase/warehouse_schema_rest/api/table_test.clj b/test/metabase/warehouse_schema_rest/api/table_test.clj
index 4f9f6adc59e..4545be201e5 100644
--- a/test/metabase/warehouse_schema_rest/api/table_test.clj
+++ b/test/metabase/warehouse_schema_rest/api/table_test.clj
@@ -25,7 +25,6 @@
[metabase.upload.impl-test :as upload-test]
[metabase.util :as u]
[metabase.util.quick-task :as quick-task]
- [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[metabase.warehouse-schema-rest.api.table :as api.table]
[toucan2.core :as t2])
(:import
@@ -38,11 +37,9 @@
;; authentication test on every single individual endpoint
(defn- user-table-fn
- "The value of `column` on the Table with `table-id` as users see it: what they set, else what sync wrote. A bare
- `t2/select :model/Table` shows sync's values only."
+ "The value of `column` on the Table with `table-id`."
[column table-id]
- (t2/select-one-fn column :model/Table :id table-id
- {:from [(warehouse-schema-overlay/table-query)]}))
+ (t2/select-one-fn column :model/Table :id table-id))
(deftest ^:parallel unauthenticated-test
(is (= (get api.response/response-unauthentic :body)
diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.clj
index 65096b7df4b..d370e25eede 100644
--- a/test/metabase/warehouses_rest/api_test.clj
+++ b/test/metabase/warehouses_rest/api_test.clj
@@ -45,6 +45,7 @@
[metabase.util.malli.schema :as ms]
[metabase.util.quick-task :as quick-task]
[metabase.util.random :as u.random]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[metabase.warehouse-schema.table :as schema.table]
[metabase.warehouses-rest.api :as api.database]
[metabase.warehouses.core :as warehouses]
@@ -1121,14 +1122,20 @@
(mt/user-http-request :rasta :get 200
(format "database/%d/autocomplete_suggestions" (mt/id))
:prefix "price"))))]
- (testing "a user-set semantic_type replaces the sync value"
- (mt/with-temp [:model/FieldUserSettings _ {:field_id field-id
- :semantic_type :type/Currency
- :semantic_type_set true}]
- (is (= ["PRICE" (str "VENUES " base-type " :type/Currency")] (venues-row)))))
- (testing "a user-set sensitive visibility_type hides the Field"
- (mt/with-temp [:model/FieldUserSettings _ {:field_id field-id :visibility_type :sensitive}]
- (is (nil? (venues-row))))))))
+ (mt/with-temp-vals-in-db :model/Field field-id (select-keys (t2/select-one :model/Field :id field-id)
+ [:semantic_type :visibility_type])
+ (testing "a user-set semantic_type replaces the sync value"
+ (try
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:semantic_type :type/Currency})
+ (is (= ["PRICE" (str "VENUES " base-type " :type/Currency")] (venues-row)))
+ (finally
+ (field-user-settings/delete-field-user-settings! [field-id]))))
+ (testing "a user-set sensitive visibility_type hides the Field"
+ (try
+ (field-user-settings/upsert-field-user-settings! {:id field-id} {:visibility_type :sensitive})
+ (is (nil? (venues-row)))
+ (finally
+ (field-user-settings/delete-field-user-settings! [field-id]))))))))
(deftest ^:parallel autocomplete-suggestions-test-2
(testing "GET /api/database/:id/autocomplete_suggestions"
diff --git a/test/metabase/xrays/automagic_dashboards/core_test.clj b/test/metabase/xrays/automagic_dashboards/core_test.clj
index 6859cf02e34..0c339bd407f 100644
--- a/test/metabase/xrays/automagic_dashboards/core_test.clj
+++ b/test/metabase/xrays/automagic_dashboards/core_test.clj
@@ -23,6 +23,7 @@
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.match :as match]
+ [metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[metabase.xrays.api.automagic-dashboards :as api.automagic-dashboards]
[metabase.xrays.automagic-dashboards.comparison :as comparison]
[metabase.xrays.automagic-dashboards.core :as magic]
@@ -1140,12 +1141,10 @@
(is (partial= {:list-like? false, :num-fields 2}
(-> (#'magic/load-tables-with-enhanced-table-stats db-id nil)
first))))
- (mt/with-temp [:model/FieldUserSettings _ {:field_id pk-id
- :semantic_type :type/PK
- :semantic_type_set true}]
- (is (partial= {:list-like? true, :num-fields 2}
- (-> (#'magic/load-tables-with-enhanced-table-stats db-id nil)
- first)))))))))
+ (field-user-settings/upsert-field-user-settings! {:id pk-id} {:semantic_type :type/PK})
+ (is (partial= {:list-like? true, :num-fields 2}
+ (-> (#'magic/load-tables-with-enhanced-table-stats db-id nil)
+ first))))))))
(deftest enhance-table-stats-fk-test
(mt/with-temp [:model/Database {db-id :id} {}