The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Better error messages for actions that violate custom constraints (#62271)
Chris Truter5d5d7f0e6743
diff --git a/src/metabase/actions/core.clj b/src/metabase/actions/core.cljindex 0a058b40c04..4712fbc87bc 100644--- a/src/metabase/actions/core.clj+++ b/src/metabase/actions/core.clj@@ -40,6 +40,7 @@validate-inputs!][metabase.actions.errorincorrect-value-type+ violate-check-constraintviolate-foreign-key-constraintviolate-not-null-constraintviolate-unique-constraint]diff --git a/src/metabase/actions/error.clj b/src/metabase/actions/error.cljindex e63ad717086..1f01e0317a9 100644--- a/src/metabase/actions/error.clj+++ b/src/metabase/actions/error.clj@@ -15,3 +15,7 @@(def incorrect-value-type"Error type for SQL incorrect value type."::incorrect-value-type)++(def violate-check-constraint+ "Error type for SQL check constraint violation."+ ::violate-check-constraint)diff --git a/src/metabase/driver/h2/actions.clj b/src/metabase/driver/h2/actions.cljindex da697817b74..eed90e48240 100644--- a/src/metabase/driver/h2/actions.clj+++ b/src/metabase/driver/h2/actions.clj@@ -109,3 +109,11 @@{:type error-type:message (tru "Some of your values aren’t of the correct type for the database."):errors {}}))++(defmethod sql-jdbc.actions/maybe-parse-sql-error [:h2 driver-api/violate-check-constraint]+ [_driver error-type _database _action-type error-message]+ (when-let [[_match constraint-name]+ (re-find #"Check constraint violation: \"([^\"]+)\"" error-message)]+ {:type error-type+ :message (tru "Some of your values violate the constraint: {0}" constraint-name)+ :errors {}}))diff --git a/src/metabase/driver/mysql/actions.clj b/src/metabase/driver/mysql/actions.cljindex 1f878f8e5c3..1927b81d45f 100644--- a/src/metabase/driver/mysql/actions.clj+++ b/src/metabase/driver/mysql/actions.clj@@ -111,6 +111,19 @@:message (tru "Some of your values aren’t of the correct type for the database."):errors {column (tru "This value should be of type {0}." (str/capitalize expected-type))}})))+(defmethod sql-jdbc.actions/maybe-parse-sql-error [:mysql driver-api/violate-check-constraint]+ [_driver error-type _database _action-type error-message]+ (or (when-let [[_match constraint-name]+ (re-find #"Check constraint '([^']+)' is violated" error-message)]+ {:type error-type+ :message (tru "Some of your values violate the constraint: {0}" constraint-name)+ :errors {}})+ (when-let [[_match constraint-name]+ (re-find #"CONSTRAINT `([^']+)` failed for" error-message)]+ {:type error-type+ :message (tru "Some of your values violate the constraint: {0}" constraint-name)+ :errors {}})))+;;; There is a huge discrepancy between the types used in DDL statements and;;; types that can be used in CAST:;;; cf https://dev.mysql.com/doc/refman/8.0/en/data-types.htmldiff --git a/src/metabase/driver/postgres/actions.clj b/src/metabase/driver/postgres/actions.cljindex 56f305790ab..279a719f17c 100644--- a/src/metabase/driver/postgres/actions.clj+++ b/src/metabase/driver/postgres/actions.clj@@ -74,6 +74,14 @@:message (tru "Some of your values aren’t of the correct type for the database."):errors {}}))+(defmethod sql-jdbc.actions/maybe-parse-sql-error [:postgres driver-api/violate-check-constraint]+ [_driver error-type _database _action-type error-message]+ (when-let [[_match constraint-name]+ (re-find #"violates check constraint \"([^\"]+)\"" error-message)]+ {:type error-type+ :message (tru "Some of your values violate the constraint: {0}" constraint-name)+ :errors {}}))+(defmethod sql-jdbc.actions/base-type->sql-type-map :postgres[_driver]{:type/BigInteger "BIGINT"diff --git a/src/metabase/driver_api/core.clj b/src/metabase/driver_api/core.cljindex 3158c731281..4404f0b5f10 100644--- a/src/metabase/driver_api/core.clj+++ b/src/metabase/driver_api/core.clj@@ -68,6 +68,7 @@actions/cached-valueactions/incorrect-value-typeactions/perform-action!*+ actions/violate-check-constraintactions/violate-foreign-key-constraintactions/violate-not-null-constraintactions/violate-unique-constraintdiff --git a/test/metabase/driver/h2_test.clj b/test/metabase/driver/h2_test.cljindex d09443f0bad..f4217d33b29 100644--- a/test/metabase/driver/h2_test.clj+++ b/test/metabase/driver/h2_test.clj@@ -416,3 +416,12 @@(sql-jdbc.actions/maybe-parse-sql-error:h2 actions.error/violate-foreign-key-constraint {:id 1} :model.row/update"Referential integrity constraint violation: \"USER_GROUP-ID_GROUP_-159406530: PUBLIC.\"\"USER\"\" FOREIGN KEY(\"\"GROUP-ID\"\") REFERENCES PUBLIC.\"\"GROUP\"\"(ID) (CAST(999 AS BIGINT))\"; SQL statement:\nINSERT INTO \"PUBLIC\".\"USER\" (\"NAME\", \"GROUP-ID\") VALUES (CAST(? AS VARCHAR), CAST(? AS INTEGER)) [23506-214]")))))++(deftest ^:parallel actions-maybe-parse-sql-violate-check-constraint-test+ (testing "violate check constraint"+ (is (= {:type :metabase.actions.error/violate-check-constraint,+ :message "Some of your values violate the constraint: users_email_check"+ :errors {}}+ (sql-jdbc.actions/maybe-parse-sql-error+ :h2 actions.error/violate-check-constraint nil :model.row/create+ "Check constraint violation: \"users_email_check\"")))))diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.cljindex 20b53790f57..9b12b186168 100644--- a/test/metabase/driver/mysql_test.clj+++ b/test/metabase/driver/mysql_test.clj@@ -645,6 +645,34 @@:mysql actions.error/violate-foreign-key-constraint nil :model.row/delete"(conn=21) Cannot delete or update a parent row: a foreign key constraint fails (`action-error-handling`.`user`, CONSTRAINT `user_group-id_group_-159406530` FOREIGN KEY (`group-id`) REFERENCES `group` (`id`))")))))+(deftest check-constraint-test+ (mt/test-driver :mysql+ (testing "violate check constraints"+ (tx/drop-if-exists-and-create-db! driver/*driver* "check_constraint_test")+ (let [details (mt/dbdef->connection-details driver/*driver* :db {:database-name "check_constraint_test"})]+ (doseq [stmt ["CREATE TABLE test_users (+ id INT AUTO_INCREMENT PRIMARY KEY,+ email VARCHAR(255) NOT NULL,+ age INT,+ CONSTRAINT email_format_check CHECK (email REGEXP '^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$')+ );"]]+ (jdbc/execute! (sql-jdbc.conn/connection-details->spec driver/*driver* details) [stmt]))+ (mt/with-temp [:model/Database database {:engine driver/*driver* :details details}]+ (mt/with-db database+ (sync/sync-database! database)+ (mt/with-actions-enabled+ (testing "when creating with invalid email"+ (is (= {:errors {}+ :message "Some of your values violate the constraint: email_format_check"+ :status-code 400+ :type actions.error/violate-check-constraint}+ (sql-jdbc.actions-test/perform-action-ex-data+ :model.row/create (mt/$ids {:create-row {"email" "invalid-email"+ "age" 25}+ :database (:id database)+ :query {:source-table $$test_users}+ :type :query}))))))))))))+;; this contains specical test cases for mysql;; for generic tests, check [[metabase.driver.sql-jdbc.actions-test/action-error-handling-test]](deftest action-error-handling-testdiff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.cljindex 5504fe4cc74..1f77148495c 100644--- a/test/metabase/driver/postgres_test.clj+++ b/test/metabase/driver/postgres_test.clj@@ -1119,7 +1119,37 @@:postgres actions.error/violate-foreign-key-constraint nil :model.row/delete"ERROR: update or delete on table \"group\" violates foreign key constraint \"user_group-id_group_-159406530\" on table \"user\"\n Detail: Key (id)=(1) is still referenced from table \"user\".")))))-;; this contains specical tests case for postgres+;; this contains special tests case for postgres+;; for generic tests, check [[metabase.driver.sql-jdbc.actions-test/action-error-handling-test]]+(deftest check-constraint-test+ (mt/test-driver :postgres+ (testing "violate check constraints"+ (tx/drop-if-exists-and-create-db! driver/*driver* "check-constraint-test")+ (let [details (mt/dbdef->connection-details :postgres :db {:database-name "check-constraint-test"})]+ (doseq [stmt ["CREATE TABLE test_users (+ id serial PRIMARY KEY,+ email VARCHAR(255) NOT NULL,+ age INTEGER,+ CONSTRAINT email_format_check CHECK (email ~ '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$')+ );"]]+ (jdbc/execute! (sql-jdbc.conn/connection-details->spec :postgres details) [stmt]))+ (mt/with-temp [:model/Database database {:engine driver/*driver* :details details}]+ (mt/with-db database+ (sync/sync-database! database)+ (mt/with-actions-enabled+ (testing "when creating with invalid email"+ (is (= {:errors {}+ :message "Some of your values violate the constraint: email_format_check"+ :status-code 400+ :type actions.error/violate-check-constraint}+ (sql-jdbc.actions-test/perform-action-ex-data+ :model.row/create (mt/$ids {:create-row {"email" "invalid-email"+ "age" 25}+ :database (:id database)+ :query {:source-table $$test_users}+ :type :query}))))))))))))++;; this contains special tests case for postgres;; for generic tests, check [[metabase.driver.sql-jdbc.actions-test/action-error-handling-test]](deftest action-error-handling-test(mt/test-driver :postgres