Skip to content

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

Tidy up some Search comments (#82623)

Chris Truter63b189937266
diff --git a/src/metabase/app_db/sql_errors.clj b/src/metabase/app_db/sql_errors.clj
index 649694213ca..7ecb78025be 100644
--- a/src/metabase/app_db/sql_errors.clj
+++ b/src/metabase/app_db/sql_errors.clj
@@ -6,31 +6,32 @@
(set! *warn-on-reflection* true)
-;; The registries name every code we recognize so the classifier reads by intent rather than by bare string.
-;; Keep them cumulative: append newly recognized codes rather than removing entries that temporarily have no caller.
-;; They are the shared app-db SQL vocabulary, not an inventory of what the current change happens to use.
-;; Keep the missing-table states aligned with `impl-table-known-to-not-exist?` in the H2, Postgres, and MySQL
-;; drivers: this module cannot depend on the driver module, so the lists are maintained in both places.
+;; These registries are cumulative shared app-db SQL vocabulary.
+;; Keep recognized states and vendor codes even when no current caller uses them.
(def sql-states
"SQLSTATE codes returned by supported application databases."
- ;; `undefined_table` (42P01) is PostgreSQL-specific. 42S02 is the ODBC/X-Open state for a missing table; H2 adds
- ;; two of its own, mapping `TABLE_OR_VIEW_NOT_FOUND_1`, `..._WITH_CANDIDATES_2`, and `..._DATABASE_EMPTY_1` in
- ;; `org.h2.api.ErrorCode` to 42S02, 42S03, and 42S04. 23000 is the standard integrity-constraint state; 23505 is
- ;; the PostgreSQL and H2 unique-violation state.
- {:undefined-table "42P01"
+ {;; PostgreSQL, missing table.
+ :undefined-table "42P01"
+ ;; MySQL, MariaDB, and H2, missing table.
:table-or-view-not-found "42S02"
+ ;; H2, missing table when it can suggest a similar name.
:table-or-view-not-found-with-candidates "42S03"
+ ;; H2, missing table in an empty database.
:table-or-view-not-found-database-empty "42S04"
+ ;; PostgreSQL and H2, duplicate key.
:unique-violation "23505"
+ ;; MySQL and MariaDB, every kind of constraint failure.
:integrity-constraint-violation "23000"})
(def error-codes
"Vendor-specific error codes returned by supported application databases."
- ;; MySQL and MariaDB use one SQLSTATE for every integrity-constraint failure, so `ER_DUP_ENTRY` identifies
- ;; duplicate keys. Consult a vendor code only after its SQLSTATE matched: H2 and MySQL both use small integers,
- ;; so a bare code lookup would classify the other vendor's error.
+ ;; MySQL and MariaDB use SQLSTATE 23000 for every integrity-constraint failure, so `ER_DUP_ENTRY` (1062)
+ ;; identifies duplicate keys. Check a vendor code only after its SQLSTATE has matched: H2 and MySQL both use small
+ ;; integers, so a bare code lookup could classify an H2 error as a MySQL or MariaDB error.
{:mysql/duplicate-entry 1062})
+;; Keep this in step with `impl-table-known-to-not-exist?` in the H2, Postgres, and MySQL drivers.
+;; This module cannot depend on the driver module, so the states are listed in both places.
(def ^:private table-not-found-states
(into #{} (map sql-states) [:undefined-table
:table-or-view-not-found
@@ -46,6 +47,7 @@
(= (sql-states :unique-violation) state)
:duplicate-key
+ ;; Vendor codes are meaningful only after the SQLSTATE has matched.
(and (= (sql-states :integrity-constraint-violation) state)
(= (error-codes :mysql/duplicate-entry) (.getErrorCode e)))
:duplicate-key)))
diff --git a/src/metabase/search/appdb/index.clj b/src/metabase/search/appdb/index.clj
index 7c0732821dc..a831b1451e7 100644
--- a/src/metabase/search/appdb/index.clj
+++ b/src/metabase/search/appdb/index.clj
@@ -254,7 +254,8 @@
Returns the table name written, or nil if no table is tracked or the batch is skipped.
Throws when the tracked table is missing and the refresh names the same table, when the retry hits a missing
- table again, and on interruption. Any other failure is logged and the batch is skipped."
+ table again, and on interruption.
+ Any other failure is logged and the batch is skipped."
[table-type table-name-fn entries]
(when-let [table-name (table-name-fn)]
(let [upsert! (fn [t]
diff --git a/src/metabase/search/db.clj b/src/metabase/search/db.clj
index 627ccf697de..04fb30472f0 100644
--- a/src/metabase/search/db.clj
+++ b/src/metabase/search/db.clj
@@ -29,6 +29,7 @@
:native_search_terms}
(remove #{:id :created_at :updated_at} search.spec/attr-columns))]
[column {:optional true} ::h2x/expr])))
+
(mu/defn spec-index-reducible-rows
"A reducible of the indexable rows of `search-model` (see `metabase.search.ingestion.query/spec-index-query`)
matching `where-clause`, or every row when it is nil.
@@ -119,11 +120,12 @@
[table-name :- [:or :keyword :string]]
(t2/query (sql.helpers/drop-table :if-exists table-name)))
-;; `IF EXISTS` cannot reliably report whether it dropped a table: PostgreSQL emits only a JDBC warning, which Toucan
-;; does not expose, and H2 emits nothing. Let an absent table throw so callers can detect races.
(mu/defn drop-search-index-table!
"Drop the search index table named `table-name`, throwing if it is already gone."
[table-name :- [:or :keyword :string]]
+ ;; `IF EXISTS` cannot reliably report whether it dropped a table.
+ ;; PostgreSQL emits only a JDBC warning, which Toucan does not expose, and H2 emits nothing.
+ ;; Let an absent table throw so callers can detect races.
(t2/query (sql.helpers/drop-table table-name)))
(mu/defn create-search-index-table!
@@ -274,8 +276,7 @@
(t2/delete! :conn conn :model/SearchIndexMetadata :index_name index-name))
(mu/defn delete-non-active-index-metadata!
- "Delete the SearchIndexMetadata rows of `engine`, `version`, `lang-code`, and `index-name` in any
- non-active state (pending or retired)."
+ "Delete the pending or retired SearchIndexMetadata rows of `engine`, `version`, `lang-code`, and `index-name`."
[engine :- :keyword
version :- :string
lang-code :- :string
diff --git a/src/metabase/search/models/search_index_metadata.clj b/src/metabase/search/models/search_index_metadata.clj
index 68233aa5ac1..4c0ebce458d 100644
--- a/src/metabase/search/models/search_index_metadata.clj
+++ b/src/metabase/search/models/search_index_metadata.clj
@@ -62,8 +62,8 @@
"If there is 'pending' index, make it 'active'. Return the name of the active index, regardless."
[engine version]
(t2/with-transaction [_conn]
- ;; Lock the exact row before retiring the active index. A concurrent reset can otherwise delete the pending row
- ;; after the existence check, leaving the coordinate with no active index.
+ ;; Lock the pending row before retiring the active one.
+ ;; Otherwise a concurrent reset can delete the pending row after this check, leaving no active index.
(when (search.db/lock-pending-index-metadata! engine version (i18n/site-locale-string))
(search.db/delete-retired-index-metadata! engine version (i18n/site-locale-string))
(search.db/retire-active-index-metadata! engine version (i18n/site-locale-string))
diff --git a/test/metabase/search/api_test.clj b/test/metabase/search/api_test.clj
index ae20c62ef88..270fcde4f1e 100644
--- a/test/metabase/search/api_test.clj
+++ b/test/metabase/search/api_test.clj
@@ -2073,8 +2073,7 @@
(deftest ^:synchronized multiple-limits-test
(testing "Multiple `limit` query args should be handled correctly (#45345)"
- ;; Repeated query-parameter keywords reach Ring as `limit=1&limit=3`, so the mock client exercises the same
- ;; duplicate-parameter parsing as a real request.
+ ;; The mock client sends repeated `:limit` keys as `limit=1&limit=3`, the same shape a real request would.
(let [q (str "multiplelimits" (u/lower-case-en (mt/random-name)))]
(mt/with-temp [:model/Card _ {:name (str q " one")}
:model/Card _ {:name (str q " two")}]
diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.clj
index 0b36d7a1aa2..3640e024a83 100644
--- a/test/metabase/test/util.clj
+++ b/test/metabase/test/util.clj
@@ -1002,8 +1002,8 @@
reindex? (some (comp search-relevant? first) models)]
(doseq [[model pk] models]
(delete-new-rows! model pk (get model->old-max-id model)))
- ;; Search has no delete hook, so the body may already have deleted a row while leaving its indexed document
- ;; behind. Reindex whenever the declared cleanup scope can affect search, even when the final delete is empty.
+ ;; Search has no delete hook, so a row the body deleted may still have its document in the index.
+ ;; Reindex whenever the cleanup scope touches search, even when nothing is left to delete here.
(when reindex?
(reindex-search-index!)))))))