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
Stop copying sessions in load-from-h2 and dump-to-h2 (#83914)
appleby1619b42706bf
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 1655cfcb0d2..d6a632210f5 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -603,7 +603,6 @@:model-imports #{:model/AuditLog:model/Card:model/LoginHistory- :model/Session:model/Setting:model/User:model/UserParameterValuediff --git a/src/metabase/cloud_migration/models/cloud_migration.clj b/src/metabase/cloud_migration/models/cloud_migration.cljindex 4220b0c0fbe..acc270bbcdc 100644--- a/src/metabase/cloud_migration/models/cloud_migration.clj+++ b/src/metabase/cloud_migration/models/cloud_migration.clj@@ -44,7 +44,7 @@(update-keys {;; Migrations need to update their own state:model/CloudMigration :all :model/Setting :all;; Users need to login, make queries, and we need need to audit them.- :model/User :all :model/Session :all :model/LoginHistory :all+ :model/User :all :model/LoginHistory :all:model/UserParameterValue :all:model/AuditLog :all :model/ViewLog :all;; Cards need to able to update their last used at timestamp, but we don't want to creatediff --git a/src/metabase/cmd/copy.clj b/src/metabase/cmd/copy.cljindex 2510a8563db..365e3175bd7 100644--- a/src/metabase/cmd/copy.clj+++ b/src/metabase/cmd/copy.clj@@ -70,7 +70,6 @@:model/ModerationReview:model/Revision:model/ViewLog- :model/Session:model/Collection:model/CollectionPermissionGraphRevision:model/Dashboard@@ -259,6 +258,10 @@;; unique_perms_helper is a computed/generated column(map #(dissoc % :unique_perms_helper))+ :model/LoginHistory+ ;; Sessions are no longer copied, and a session deleted while dumping could leave a dangling reference (UXW-5258).+ (map #(dissoc % :session_id))+;; elseidentity))@@ -408,7 +411,6 @@(def ^:private entities-without-autoinc-ids"Entities that do NOT use an auto incrementing ID column."#{:model/Setting- :model/Session:model/ImplicitAction:model/FieldUserSettings:model/TableUserSettingsdiff --git a/test/metabase/cloud_migration/models/cloud_migration_test.clj b/test/metabase/cloud_migration/models/cloud_migration_test.cljindex bb2605b2a34..d39fa3256ae 100644--- a/test/metabase/cloud_migration/models/cloud_migration_test.clj+++ b/test/metabase/cloud_migration/models/cloud_migration_test.clj@@ -7,8 +7,11 @@[metabase.config.core :as config][metabase.task.core :as task][metabase.test :as mt]+ [metabase.test.fixtures :as fixtures][toucan2.core :as t2]))+(use-fixtures :once (fixtures/initialize :test-users))+(use-fixtures :each (fn [thunk](mt/discard-setting-changes [read-only-mode](thunk))))diff --git a/test/metabase/cmd/compare_h2_dbs_test_util.clj b/test/metabase/cmd/compare_h2_dbs_test_util.cljindex d8589644aba..68a5662bef5 100644--- a/test/metabase/cmd/compare_h2_dbs_test_util.clj+++ b/test/metabase/cmd/compare_h2_dbs_test_util.clj@@ -23,7 +23,8 @@(def ^:private ignored-table-names"Set of Table names to skip diffing (e.g. because they're not ones we migrate.)"- #{"DATABASECHANGELOG"+ #{"CORE_SESSION"+ "DATABASECHANGELOG""QRTZ_BLOB_TRIGGERS""QRTZ_CALENDARS""QRTZ_CRON_TRIGGERS"diff --git a/test/metabase/cmd/copy_test.clj b/test/metabase/cmd/copy_test.cljindex 3a25a7d0e67..7ad4374c588 100644--- a/test/metabase/cmd/copy_test.clj+++ b/test/metabase/cmd/copy_test.clj@@ -75,13 +75,70 @@(finally(run! shutdown! [source target]))))))+(deftest copy-login-history-with-orphaned-session-id-test+ (testing "copying a login history row whose session was deleted mid-dump clears the dangling session_id (UXW-5258)"+ (let [source (h2-data-source)+ target (h2-data-source)]+ (try+ (mdb.setup/setup-db! :h2 source {:manage-encryption-state? false})+ (jdbc/execute! {:datasource source} ["SET REFERENTIAL_INTEGRITY FALSE"])+ (jdbc/execute! {:datasource source}+ ["INSERT INTO core_user (+ id, email, first_name, last_name, password, password_salt, date_joined,+ is_superuser, is_active, entity_id+ )+ VALUES (+ 1, '[email protected]', 'No', 'Body', 'nopassword', 'nosalt', NOW(),+ FALSE, TRUE, 'loginhistoryuser00001'+ )"])+ (jdbc/execute! {:datasource source}+ ["INSERT INTO login_history (+ timestamp, user_id, session_id, device_id, device_description, ip_address+ )+ VALUES (+ NOW(), 1, 'deleted-session-id', 'device', 'browser', '127.0.0.1'+ )"])+ (jdbc/execute! {:datasource source} ["SET REFERENTIAL_INTEGRITY TRUE"])+ (copy/copy! :h2 source :h2 target)+ (is (= [{:user_id 1, :session_id nil}]+ (jdbc/query {:datasource target} ["SELECT user_id, session_id FROM login_history"])))+ (finally+ (run! shutdown! [source target]))))))++(deftest copy-does-not-copy-sessions-test+ ;; Check that `copy/copy!` excludes :model/Session. For the rest of the `models-to-exclude`, we just verify they are+ ;; not present in `copy/entities` (see `entities-and-models-to-exclude-do-not-overlap-test`).+ (testing "copy! leaves the target's core_session table empty"+ (let [source (h2-data-source)+ target (h2-data-source)]+ (try+ (mdb.setup/setup-db! :h2 source {:manage-encryption-state? false})+ (jdbc/execute! {:datasource source}+ ["INSERT INTO core_user (+ id, email, first_name, last_name, password, password_salt, date_joined,+ is_superuser, is_active, entity_id+ )+ VALUES (+ 1, '[email protected]', 'No', 'Body', 'nopassword', 'nosalt', NOW(),+ FALSE, TRUE, 'sessioncopyuser000001'+ )"])+ (jdbc/execute! {:datasource source}+ ["INSERT INTO core_session (id, user_id, created_at, key_hashed)+ VALUES ('session-id', 1, NOW(), 'hashed-key')"])+ (is (= [{:count 1}]+ (jdbc/query {:datasource source} ["SELECT COUNT(*) AS count FROM core_session"])))+ (copy/copy! :h2 source :h2 target)+ (is (= [{:count 0}]+ (jdbc/query {:datasource target} ["SELECT COUNT(*) AS count FROM core_session"])))+ (finally+ (run! shutdown! [source target]))))))+(def ^:private models-to-exclude"Models that should *not* be migrated in `load-from-h2`."#{:model/AgentApiCallLog:model/AiUsageLog:model/AnalysisFinding:model/AnalysisFindingError- :model/ApiKey:model/ApiKeyUsageLog:model/CacheConfig:model/CardFavorite@@ -99,10 +156,6 @@:model/McpQueryHandle:model/McpSessionLog:model/McpToolCallLog- :model/MetabotConversation- :model/MetabotGroupLimit- :model/MetabotInstanceLimit- :model/MetabotMessage:model/MetabotPermissions:model/PremiumFeaturesCache:model/PythonLibrary@@ -117,6 +170,7 @@:model/SearchIndexMetadata:model/SecurityAdvisory:model/SemanticSearchTokenTracking+ :model/Session:model/SourceDimensionDaily:model/SourceDimensionProfileDaily:model/SourceMetricDaily@@ -150,10 +204,17 @@(format "%s should be added to %s, or to %s" model `copy/entities `models-to-exclude)))(is (apply distinct? (map t2/table-name copy/entities))))+(deftest ^:parallel entities-and-models-to-exclude-do-not-overlap-test+ (is (= #{}+ (set (filter models-to-exclude copy/entities)))+ (format "%s and %s should not overlap" `copy/entities `models-to-exclude)))+(def ^:private foreign-key-coverage-exceptions"Known exceptions to foreign-key coverage.";; OSS cannot create tenants and does not copy them, so only EE-created dumps are affected.- #{{:child_table "core_user", :parent_table "tenant"}})+ #{{:child_table "core_user", :parent_table "tenant"}+ ;; Sessions aren't copied and `copy/model-results-xform` clears `login_history.session_id`.+ {:child_table "login_history", :parent_table "core_session"}})(def ^:private fk-graph-sql"Foreign keys from the test's H2 database."