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
GHY-4722: Remote sync: model actions aren't exported, and dashboard-button actions only appear on full exports (#83347)
Eric Normande868c207e3d2
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex b467977acaa..701e93639cc 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -140,6 +140,7 @@api-scopeapi.eid-translationcollections+ eventsliblib.schemapermissions@@ -992,7 +993,8 @@:api #{metabase.events.core metabase.events.impl metabase.events.init}:uses #{modelsutil}- :model-imports #{:model/Card+ :model-imports #{:model/Action+ :model/Card:model/Collection:model/Dashboard:model/Database@@ -2138,7 +2140,8 @@searchutilwarehouse-schema-overlay}- :model-imports #{:model/Card+ :model-imports #{:model/Action+ :model/Card:model/CardBookmark:model/Collection:model/Dashboard@@ -4117,7 +4120,8 @@enterprise/serializationenterprise/transforms.python}:model-exports #{:model/RemoteSyncTask}- :model-imports #{:model/Card+ :model-imports #{:model/Action+ :model/Card:model/Collection:model/Dashboard:model/Documentdiff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/core.cljindex e36518ea0a4..ba1e52453c4 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj@@ -3,6 +3,7 @@[java-time.api :as t][medley.core :as m][metabase-enterprise.remote-sync.db :as remote-sync.db]+ [metabase-enterprise.remote-sync.events :as rs-events][metabase-enterprise.remote-sync.guards :as guards][metabase-enterprise.remote-sync.settings :as settings][metabase-enterprise.remote-sync.source :as source]@@ -140,7 +141,9 @@:model_id (:id entity):status "create":status_changed_at (t/offset-date-time)}- (spec/build-sync-object-fields spec entity)))))+ (spec/build-sync-object-fields spec entity))))+ ;; Actions live in their model's collection, not a collection_id of their own.+ (rs-events/track-untracked-actions! collection-ids))(defn- collections-by-id"`{id collection}` for `ids`, carrying the fields the failure descriptions need. Nil and duplicate idsdiff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/db.cljindex d03a3a98ce7..26f72f41a61 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj@@ -20,10 +20,11 @@[:maybe [:map-of ConditionKey [:maybe [:or :string :int :boolean :keyword sequential?]]]])(def ^:private RemovalOpts- "The `:scope-key`, `:synced-collection-ids`, `:entity-ids`, and `:removal-conditions` describing which rows an- import removes (see [[removal-exprs]])."+ "The `:scope-key`, `:scope-table`, `:synced-collection-ids`, `:entity-ids`, and `:removal-conditions` describing+ which rows an import removes (see [[removal-exprs]])."[:map {:closed true}[:scope-key [:maybe :keyword]]+ [:scope-table {:optional true} [:maybe :keyword]][:synced-collection-ids [:maybe [:or [:set ::lib.schema.id/collection] [:sequential ::lib.schema.id/collection]]]][:entity-ids [:maybe [:or [:set :string] [:sequential :string]]]][:removal-conditions Conditions]])@@ -85,12 +86,17 @@(defn- removal-exprs"The `:where` fragments (see [[removal-condition-exprs]]) selecting the `model-key` rows an import removes:- scoped to `synced-collection-ids` (when `scope-key` is given), excluding `entity-ids`, and matching- `removal-conditions`. Returns nil for a scoped model with no synced collections (removes nothing)."- [{:keys [scope-key synced-collection-ids entity-ids removal-conditions]}]+ scoped to `synced-collection-ids` (when `scope-key` is given; through the `scope-table` rows in those collections+ when that is given too), excluding `entity-ids`, and matching `removal-conditions`. Returns nil for a scoped model+ with no synced collections (removes nothing)."+ [{:keys [scope-key scope-table synced-collection-ids entity-ids removal-conditions]}](when-not (and scope-key (empty? synced-collection-ids))(cond-> []- scope-key (conj [:in scope-key synced-collection-ids])+ scope-key (conj [:in scope-key (if scope-table+ ^:allow-subquery {:select [:id]+ :from [scope-table]+ :where [:in :collection_id synced-collection-ids]}+ synced-collection-ids)])(seq entity-ids) (conj [:not-in :entity_id entity-ids]):always (into (removal-condition-exprs removal-conditions)))))@@ -193,11 +199,16 @@(t2/delete! model :id [:in ids]))(defn- tracking-select-parts- "The SELECT/FROM/JOIN joining a `model-key` instance (Field, Segment, or Measure) to its Table for sync tracking,- plus the `alias` its own table is joined under (so callers can address its `:id` and `:entity_id` columns).- Segment and Measure share alias \"s\" — both are looked up the same way by [[tracking-details-by-entity-ids]]."+ "The SELECT/FROM/JOIN joining a `model-key` instance to the parent that carries its collection for sync tracking+ (Field, Segment, or Measure to its Table; Action to its model Card), plus the `alias` its own table is joined under+ (so callers can address its `:id` and `:entity_id` columns). Segment and Measure share alias \"s\" — both are+ looked up the same way by [[tracking-details-by-entity-ids]]."[model-key](case model-key+ :model/Action {:alias "a"+ :select [:a.name [:c.collection_id :collection_id]]+ :from [[:action :a]]+ :join [[:report_card :c] [:= :a.model_id :c.id]]}:model/Field {:alias "f":select [:f.name :f.table_id [:t.collection_id :collection_id] [:t.name :table_name]]:from [[:metabase_field :f]]@@ -212,16 +223,16 @@:join [(warehouse-schema-overlay/table-query {:alias :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` (Field, Segment, or Measure) instance with- `model-id`, or nil."+ "The name, table id, collection id, and table name of the `model-key` (Field, Segment, Measure, or Action) instance+ with `model-id`, or nil. An Action has no table, and its collection is its model's."[model-key :- :keywordmodel-id :- ms/PositiveInt](let [{:keys [alias select from join]} (tracking-select-parts model-key)](first (t2/query {:select select :from from :join join :where [:= (keyword alias "id") model-id]}))))(mu/defn tracking-details-by-entity-ids- "The `:id`, name, table id, collection id, and table name of the `model-key` (Segment or Measure) instances with- `entity-ids`."+ "The `:id`, name, table id, collection id, and table name of the `model-key` (Segment, Measure, or Action)+ instances with `entity-ids`."[model-key :- :keywordentity-ids :- [:or [:set :string] [:sequential :string]]](let [{:keys [alias select from join]} (tracking-select-parts model-key)@@ -526,14 +537,48 @@(t2/select :model/RemoteSyncObject :model_type model-type :model_id [:in model-ids]))(mu/defn active-child-rsos- "The RemoteSyncObjects of `model-type` under the Table with `table-id` that are not pending removal or deletion."- [model-type :- :string- table-id :- ::lib.schema.id/table]+ "The RemoteSyncObjects of `model-type` whose `parent-rso-key` column is `parent-id`, and that are not pending removal+ or deletion."+ [model-type :- :string+ parent-rso-key :- :keyword+ parent-id :- ms/PositiveInt](t2/select :model/RemoteSyncObject:model_type model-type- :model_table_id table-id+ parent-rso-key parent-id:status [:not-in ["removed" "delete"]]))+(mu/defn untracked-actions-in-collections+ "The `:id`, `:name`, and model `:collection_id` of the unarchived Actions whose unarchived model Card is in the+ Collections with `collection-ids`, and that have no Action RemoteSyncObject."+ [collection-ids :- [:sequential ::lib.schema.id/collection]]+ (t2/query {:select [:a.id :a.name [:c.collection_id :collection_id]]+ :from [[:action :a]]+ :join [[:report_card :c] [:= :a.model_id :c.id]]+ :where [:and+ [:in :c.collection_id collection-ids]+ [:= :a.archived false]+ [:= :c.archived false]+ [:not [:exists ^:allow-subquery {:select [1]+ :from [:remote_sync_object]+ :where [:and+ [:= :remote_sync_object.model_type "Action"]+ [:= :remote_sync_object.model_id :a.id]]}]]]}))++(mu/defn active-rsos-of-children+ "The RemoteSyncObjects of `child-model-type` whose `child-model-key` rows have `fk` equal to `parent-id`, and that+ are not pending removal or deletion."+ [child-model-key :- :keyword+ child-model-type :- :string+ fk :- :keyword+ parent-id :- ms/PositiveInt]+ (t2/select :model/RemoteSyncObject+ {:where [:and+ [:= :model_type child-model-type]+ [:not-in :status ["removed" "delete"]]+ [:in :model_id ^:allow-subquery {:select [:id]+ :from [(t2/table-name child-model-key)]+ :where [:= fk parent-id]}]]}))+(mu/defn content-rso-statuses"The `:id` and `:status` of the RemoteSyncObjects of the Collections with `collection-ids` and their contents."[collection-ids :- [:set ::lib.schema.id/collection]]diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/events.cljindex ac1f4cb5013..7905c9bad42 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj@@ -67,6 +67,29 @@(remote-sync.db/insert-rsos! rows))(count rows)))+(defn track-untracked-actions!+ "Insert a 'create' ledger row for every unarchived Action with none whose model is in the Collections with+ `collection-ids`. Returns the number of rows inserted."+ [collection-ids]+ (let [action-spec (spec/spec-for-model-key :model/Action)+ timestamp (t/offset-date-time)+ rows (when (seq collection-ids)+ (for [action (remote-sync.db/untracked-actions-in-collections (vec collection-ids))]+ (merge {:model_type "Action"+ :model_id (:id action)+ :status "create"+ :status_changed_at timestamp}+ (spec/build-sync-object-fields action-spec action))))]+ (when (seq rows)+ (remote-sync.db/insert-rsos! rows))+ (count rows)))++(defn backfill-action-tracking!+ "Insert a 'create' ledger row for every untracked Action of a model in a remote-synced collection, so an instance+ upgraded with synced models still pushes their actions. Returns the number of rows inserted."+ []+ (track-untracked-actions! (remote-sync.db/remote-synced-collection-ids)))+(defn disable-library-tracking!"Remove all snippet, snippets-namespace collection, and glossary tracking entries."[]@@ -207,7 +230,7 @@(defn- cascade-to-children!"When a parent model becomes eligible/ineligible, cascade to its children.For eligible: query child entities using :parent-fk and derived filter, check eligibility, create RSOs.- For ineligible: query existing RSOs by model_table_id and mark as removed."+ For ineligible: find the children's active RSOs and mark them as removed."[model-spec model-id status eligible?](doseq [child-spec (spec/children-specs (:model-key model-spec))](let [fk (:parent-fk child-spec)@@ -218,7 +241,10 @@(when (spec/check-eligibility child-spec child)(create-or-update-sync-object-from-spec! child-spec (:id child) status)));; Ineligible branch: mark existing child RSOs as removed- (doseq [child-rso (remote-sync.db/active-child-rsos (:model-type child-spec) model-id)]+ (doseq [child-rso (if-let [rso-key (:parent-rso-key child-spec)]+ (remote-sync.db/active-child-rsos (:model-type child-spec) rso-key model-id)+ (remote-sync.db/active-rsos-of-children (:model-key child-spec) (:model-type child-spec)+ fk model-id))](create-or-update-sync-object-from-spec! child-spec (:model_id child-rso) "removed"))))))(defn- handle-model-event-from-specdiff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.cljindex 17652dca4c4..282b95eeb3e 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj@@ -64,8 +64,9 @@Takes a sequence of remote-synced collection IDs and imported-data map from spec/extract-imported-entities.For each entity-id based model, deletes entities whose entity_id is not in the imported set.- Models with :scope-key in their spec are scoped to synced collections (using :id for Collection, :collection_id- for others). Models without :scope-key (like TransformTag) are deleted globally by entity_id.+ Models with :scope-key in their spec are scoped to synced collections (using :id for Collection, :model_id through+ the model Card for Action, :collection_id for others). Models without :scope-key (like TransformTag) are deleted+ globally by entity_id.Path-based models (Table, Field) are not removed here - they are controlled by published table settings.@@ -76,10 +77,7 @@:let [entity-ids (get by-entity-id (:model-type model-spec) [])]](remote-sync.db/delete-removed-instances!model-key- {:scope-key (get-in model-spec [:removal :scope-key])- :synced-collection-ids synced-collection-ids- :entity-ids entity-ids- :removal-conditions (spec/removal-conditions model-spec)})))+ (spec/removal-opts model-spec synced-collection-ids entity-ids))))(defn- quoted"Wraps `s` in backticks so that leading and trailing whitespace is visible to the reader."@@ -513,7 +511,8 @@;; re-indexed by the load itself — serdes' t2 insert!/update! fire the :hook/search-index;; after-insert/after-update hooks. Deletes have no such hook, so remove them explicitly.(doseq [[model-key ds] (group-by model-key-of deletes)]- (search/delete! model-key (mapv :model_id ds)))+ ;; the search index stores model_id as text+ (search/delete! model-key (mapv (comp str :model_id) ds)))(report 0.95 {:force? true})(log/info "Successfully reloaded entities from git repository"){:status :successdiff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/init.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/init.cljindex e49ce66895a..0599968caf2 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/init.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/init.clj@@ -62,6 +62,10 @@(let [n (events/backfill-glossary-tracking!)](when (pos? n)(log/infof "Tracking %d existing glossary entries for remote sync" n))))+ (when (= :read-write (settings/remote-sync-type))+ (let [n (events/backfill-action-tracking!)]+ (when (pos? n)+ (log/infof "Tracking %d existing actions for remote sync" n))))(when-not (collection/has-remote-synced-collection?)(if (nil? (settings/remote-sync-branch))(log/warn "Remote sync is enabled but no remote-sync branch is set. Cannot do initial import.")diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.cljindex b6ad81c2769..26814669d1d 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj@@ -34,9 +34,12 @@- :model-key - Toucan2 model keyword (e.g., :model/Card)- :identity - Identity strategy: :entity-id, :path, or :hybrid- :path-keys - For :path or :hybrid identity: vector of path components [:database :schema :table :field]- - :parent-model - For :parent-table eligibility: the parent model key to check eligibility against- (e.g., :model/Table for Field, Segment, Measure)+ - :parent-model - For :parent-table and :parent eligibility: the parent model key to check eligibility against+ (e.g., :model/Table for Field, Segment, Measure; :model/Card for Action)- :parent-fk - For child models: the FK column pointing to the parent (e.g., :table_id)+ - :parent-rso-key - Optional, for child models whose RemoteSyncObject rows record their parent's id: the+ RemoteSyncObject column that holds it (e.g., :model_table_id for Field). A cascade from the+ parent then finds the child rows by this column instead of through :parent-fk.- :cascade-filter - Optional map of additional filter conditions for cascade queries.Only needed when the filter differs from {archived-key false}.E.g., Field needs {:active true} since it has no :archived-key.@@ -47,7 +50,8 @@:prefix - Event keyword prefix (e.g., :event/card):types - Vector of event types to handle [:create :update :delete]- :eligibility - Eligibility configuration:- :type - :collection, :published-table, :parent-table, :setting, or :library-synced+ :type - :collection, :published-table, :parent-table, :parent, :setting, or+ :library-synced (:parent follows :parent-fk to a :parent-model instance):collection - For :collection type: :remote-synced, :transforms-namespace, :snippets-namespace, or :any:setting - For :setting type: setting keyword to check(Note: :library-synced type uses the library-is-remote-synced? setting to determine eligibility)@@ -56,7 +60,7 @@:select-fields - Fields to select for hydration:hydrate-query? - When true, hydrate via the model's`metabase-enterprise.remote-sync.db` tracking-join query instead of- :select-fields (Field, Segment, Measure)+ :select-fields (Field, Segment, Measure, Action):field-mappings - Map of RemoteSyncObject column -> source field or [field transform-fn]- :conditions - Optional map of conditions for filtering syncable entities.Only entities matching these conditions are eligible for sync@@ -68,6 +72,8 @@:statuses - Set of statuses to check for removal (e.g., #{\"removed\" \"delete\"}):scope-key - Optional key for scoping deletions (e.g., :collection_id, :id).If nil, deletions are global (by entity_id only).+ :scope-table - Optional table that :scope-key points into; the rows are+ then scoped to that table's rows in the synced collections (Action):all-on-setting-disable - Optional setting keyword; when this setting's sentinelRSO exists with 'delete' status, remove ALL entities of this type- :export-scope - Export scope for query-export-roots:@@ -95,6 +101,25 @@:scope-key :collection_id}:enabled? true}+ :model/Action+ {:model-type "Action"+ :model-key :model/Action+ :identity :entity-id+ :delete-after [:model/Card] ; has model_id FK+ :parent-model :model/Card+ :parent-fk :model_id+ :events {:prefix :event/action+ :types [:create :update :delete]}+ :eligibility {:type :parent}+ :archived-key :archived+ :tracking {:hydrate-query? true+ :field-mappings {:model_name :name+ :model_collection_id :collection_id}}+ :removal {:statuses #{"removed" "delete"}+ :scope-key :model_id+ :scope-table :report_card}+ :enabled? true}+:model/Dashboard{:model-type "Dashboard":model-key :model/Dashboard@@ -220,6 +245,7 @@:path-keys [:database :schema :table :field]:parent-model :model/Table:parent-fk :table_id+ :parent-rso-key :model_table_id:cascade-filter {:active true}:events {:prefix :event/field:types [:create :update :delete]}@@ -615,12 +641,13 @@:message (format "Import contains %s but local instance has unsynced %s namespace collections"category category)}))))-(defn- removal-opts- "The `metabase-enterprise.remote-sync.db` removal-opts (`:scope-key`, `:synced-collection-ids`, `:entity-ids`,- `:removal-conditions`) for removing the entity-id `spec`'s rows not in the import, scoped to+(defn removal-opts+ "The `metabase-enterprise.remote-sync.db` removal-opts (`:scope-key`, `:scope-table`, `:synced-collection-ids`,+ `:entity-ids`, `:removal-conditions`) for removing the entity-id `spec`'s rows not in the import, scoped to`synced-collection-ids` when the spec has a `:scope-key`, minus the imported `entity-ids`."[spec synced-collection-ids entity-ids]{:scope-key (get-in spec [:removal :scope-key])+ :scope-table (get-in spec [:removal :scope-table]):synced-collection-ids synced-collection-ids:entity-ids entity-ids:removal-conditions (removal-conditions spec)})@@ -803,6 +830,12 @@(when-let [table (remote-sync.db/instance parent-model table_id)](check-eligibility (spec-for-model-key parent-model) table))))+(defmethod check-eligibility-by-type :parent+ [{:keys [parent-model parent-fk]} object]+ (when-let [parent-id (get object parent-fk)]+ (when-let [parent (remote-sync.db/instance parent-model parent-id)]+ (check-eligibility (spec-for-model-key parent-model) parent))))+(defmethod check-eligibility-by-type :setting[{:keys [eligibility]} _object](setting/get-value-of-type :boolean (:setting eligibility)))@@ -1062,7 +1095,9 @@(when (seq entity-ids)(let [;; Get select fields from spec, with :id always includedselect-fields (into [:id] (or (:select-fields tracking) [:name :collection_id]))- entities (remote-sync.db/instances-with-columns-by-entity-ids model-key select-fields entity-ids)]+ entities (if (:hydrate-query? tracking)+ (remote-sync.db/tracking-details-by-entity-ids model-key entity-ids)+ (remote-sync.db/instances-with-columns-by-entity-ids model-key select-fields entity-ids))](map (fn [entity](let [;; Apply field mappingsfield-mappings (:field-mappings tracking)diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.cljindex 900af73a477..a25670f1b9b 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj@@ -6,6 +6,7 @@[diehard.core :as dh][java-time.api :as t][metabase-enterprise.remote-sync.core :as remote-sync.core]+ [metabase-enterprise.remote-sync.events :as remote-sync.events][metabase-enterprise.remote-sync.impl :as impl][metabase-enterprise.remote-sync.models.remote-sync-object :as remote-sync.object][metabase-enterprise.remote-sync.models.remote-sync-task :as remote-sync.task]@@ -809,6 +810,307 @@"collections/main/marketing_reports/question_2.yaml"}(repo-files))))))))))+(deftest export-includes-model-actions-test+ (testing "GHY-4722: pushing a synced model writes its actions to actions/, though the model's YAML doesn't reference them"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-actions-enabled+ (mt/with-model-cleanup [:model/Action :model/Card]+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Synced" :location "/"}]+ (let [source (test-helpers/versioned-source :current "v-remote" :trees {"v-remote" {}})+ repo-files #(set (source.p/list-files (source.p/snapshot source)))+ push! #(wait-for-task-completion+ (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})))]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly source)+ settings/check-and-update-remote-settings! (constantly nil)+ impl/finish-remote-config! (constantly nil)]+ (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})+ (is (remote-sync.task/successful? (push!)))+ (let [model (mt/user-http-request :crowberto :post 200 "card"+ {:name "Venues Model"+ :type "model"+ :collection_id coll-id+ :display "table"+ :visualization_settings {}+ :dataset_query (mt/mbql-query venues)})]+ (mt/user-http-request :crowberto :post 200 "action"+ {:name "Create Venue"+ :type "implicit"+ :kind "row/create"+ :model_id (:id model)})+ (mt/user-http-request :crowberto :post 200 "action"+ {:name "Rename Venue"+ :type "query"+ :model_id (:id model)+ :database_id (mt/id)+ :dataset_query {:type "native"+ :database (mt/id)+ :native {:query "UPDATE venues SET name = 'x' WHERE id = 1"}}+ :parameters []})+ (is (remote-sync.task/successful? (push!)))+ (is (= #{"actions/create_venue.yaml" "actions/rename_venue.yaml"}+ (into #{} (filter #(str/starts-with? % "actions/")) (repo-files))))))))))))))++(deftest push-tracks-action-changes-on-pushed-model-test+ (testing "GHY-4722: creating, editing, and deleting an action on an already-pushed model each reach the repo on the next push"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-actions-enabled+ (mt/with-model-cleanup [:model/Action :model/Card]+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Synced" :location "/"}]+ (let [source (test-helpers/versioned-source :current "v-remote" :trees {"v-remote" {}})+ repo-file #(source.p/read-file (source.p/snapshot source) %)+ push! #(wait-for-task-completion+ (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})))]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly source)+ settings/check-and-update-remote-settings! (constantly nil)+ impl/finish-remote-config! (constantly nil)]+ (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})+ (let [model (mt/user-http-request :crowberto :post 200 "card"+ {:name "Venues Model"+ :type "model"+ :collection_id coll-id+ :display "table"+ :visualization_settings {}+ :dataset_query (mt/mbql-query venues)})]+ (is (remote-sync.task/successful? (push!)))+ (is (nil? (repo-file "actions/create_venue.yaml")))+ (let [action (mt/user-http-request :crowberto :post 200 "action"+ {:name "Create Venue"+ :type "implicit"+ :kind "row/create"+ :model_id (:id model)})]+ (testing "a new action"+ (is (remote-sync.task/successful? (push!)))+ (is (some? (repo-file "actions/create_venue.yaml"))))+ (testing "an edited action"+ (mt/user-http-request :crowberto :put 200 (str "action/" (:id action))+ {:description "Adds a venue"})+ (is (remote-sync.task/successful? (push!)))+ (is (str/includes? (str (repo-file "actions/create_venue.yaml")) "Adds a venue")))+ (testing "a deleted action"+ (mt/user-http-request :crowberto :delete 204 (str "action/" (:id action)))+ (is (remote-sync.task/successful? (push!)))+ (is (nil? (repo-file "actions/create_venue.yaml")))))))))))))))++(defn- do-with-pushed-model!+ "Pushes a model \"Venues Model\" in a synced collection through the API, then calls `f` with a map of:+ `:coll-id`, `:model`, `:model-path` (its YAML path in the repo), `:push!` and `:pull!` (each returns the finished+ task), `:repo-file` (path -> content or nil), `:repo-files` (the set of paths), and `:commit-remote!`, which takes+ `{:upsert {path content} :delete [path]}` and commits it straight to the repo, as another instance's push would."+ [f]+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-actions-enabled+ (mt/with-model-cleanup [:model/Action :model/Card]+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Synced" :location "/"}]+ (let [source (test-helpers/versioned-source :current "v-remote" :trees {"v-remote" {}})+ repo-file #(source.p/read-file (source.p/snapshot source) %)+ repo-files #(set (source.p/list-files (source.p/snapshot source)))+ push! #(wait-for-task-completion+ (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})))+ pull! #(wait-for-task-completion+ (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:expected_branch "main"})))+ commit-remote! (fn [{:keys [upsert delete]}]+ (let [commit (source.p/open-commit (source.p/snapshot source))]+ (doseq [[path content] upsert]+ (source.p/stage-upsert! commit {:path path :content content}))+ (doseq [path delete]+ (source.p/stage-delete! commit path))+ (source.p/finish-commit! commit "Another instance's change")))]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly source)+ settings/check-and-update-remote-settings! (constantly nil)+ impl/finish-remote-config! (constantly nil)]+ (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})+ (let [model (mt/user-http-request :crowberto :post 200 "card"+ {:name "Venues Model"+ :type "model"+ :collection_id coll-id+ :display "table"+ :visualization_settings {}+ :dataset_query (mt/mbql-query venues)})]+ (is (remote-sync.task/successful? (push!)))+ (f {:coll-id coll-id+ :model model+ :model-path (some #(when (str/includes? % "venues_model") %) (repo-files))+ :push! push!+ :pull! pull!+ :repo-file repo-file+ :repo-files repo-files+ :commit-remote! commit-remote!}))))))))))++(defn- do-with-pushed-model-actions!+ "Pushes a synced model with an implicit action (`actions/create_venue.yaml`) and a query action+ (`actions/rename_venue.yaml`) through the API, then calls `f` with the map from [[do-with-pushed-model!]]."+ [f]+ (do-with-pushed-model!+ (fn [{:keys [model push! repo-file] :as ctx}]+ (mt/user-http-request :crowberto :post 200 "action"+ {:name "Create Venue" :type "implicit" :kind "row/create" :model_id (:id model)})+ (mt/user-http-request :crowberto :post 200 "action"+ {:name "Rename Venue"+ :type "query"+ :model_id (:id model)+ :database_id (mt/id)+ :dataset_query {:type "native"+ :database (mt/id)+ :native {:query "UPDATE venues SET name = 'x' WHERE id = 1"}}+ :parameters []})+ (is (remote-sync.task/successful? (push!)))+ (is (some? (repo-file "actions/create_venue.yaml")))+ (is (some? (repo-file "actions/rename_venue.yaml")))+ (f ctx))))++(deftest push-after-model-becomes-question-removes-actions-test+ (testing "GHY-4722: turning a pushed model into a question removes its actions from the repo on the next push"+ (do-with-pushed-model-actions!+ (fn [{:keys [model push! repo-file]}]+ (mt/user-http-request :crowberto :put 200 (str "card/" (:id model)) {:type "question"})+ (is (remote-sync.task/successful? (push!)))+ (is (nil? (repo-file "actions/create_venue.yaml")))+ (is (nil? (repo-file "actions/rename_venue.yaml")))))))++(deftest push-after-model-query-drops-implicit-actions-test+ (testing "GHY-4722: a model query that no longer supports implicit actions removes them from the repo on the next push"+ (do-with-pushed-model-actions!+ (fn [{:keys [model push! repo-file]}]+ (mt/user-http-request :crowberto :put 200 (str "card/" (:id model))+ {:dataset_query (mt/mbql-query venues {:filter [:> $price 1]})})+ (is (remote-sync.task/successful? (push!)))+ (is (nil? (repo-file "actions/create_venue.yaml")))+ (is (some? (repo-file "actions/rename_venue.yaml")) "the query action stays")))))++(deftest push-after-model-deleted-removes-actions-test+ (testing "GHY-4722: deleting a pushed model removes its actions from the repo on the next push"+ (do-with-pushed-model-actions!+ (fn [{:keys [model push! repo-file]}]+ (mt/user-http-request :crowberto :delete 204 (str "card/" (:id model)))+ (is (remote-sync.task/successful? (push!)))+ (is (nil? (repo-file "actions/create_venue.yaml")))+ (is (nil? (repo-file "actions/rename_venue.yaml")))))))++(defn- insert-untracked-action!+ "Inserts an implicit action \"Local Action\" (`actions/local_action.yaml`) on the model with `model-id` straight+ into the app DB, so no event records it in the remote sync ledger. Returns its id."+ [model-id]+ (let [action-id (t2/insert-returning-pk! :model/Action {:name "Local Action"+ :type :implicit+ :model_id model-id+ :created_at :%now+ :updated_at :%now})]+ (t2/insert! :model/ImplicitAction {:action_id action-id :kind "row/create"})+ action-id))++(defn- action-rso-status+ "The status of the ledger row of the Action with `action-id`, or nil if it has none."+ [action-id]+ (t2/select-one-fn :status :model/RemoteSyncObject :model_type "Action" :model_id action-id))++(deftest pull-that-deletes-a-model-keeps-the-ledger-clean-test+ (testing "GHY-4722: a pull that deletes a model with a local, untracked action leaves the instance clean"+ (do-with-pushed-model!+ (fn [{:keys [model model-path pull! commit-remote!]}]+ (insert-untracked-action! (:id model))+ (commit-remote! {:delete [model-path]})+ (let [task (pull!)]+ (is (remote-sync.task/successful? task))+ (is (= "pulled" (get-in task [:outcome :kind]))))+ (is (not (t2/exists? :model/Card (:id model))))+ (is (false? (remote-sync.object/dirty?)))+ (is (not (t2/exists? :model/RemoteSyncObject :model_type "Action")))))))++(deftest pull-that-turns-a-model-into-a-question-keeps-the-ledger-clean-test+ (testing "GHY-4722: a pull that turns a model with a local, untracked action into a question leaves the instance clean"+ (do-with-pushed-model!+ (fn [{:keys [model model-path pull! repo-file commit-remote!]}]+ (insert-untracked-action! (:id model))+ (commit-remote! {:upsert {model-path (str/replace (repo-file model-path) "type: model" "type: question")}})+ (let [task (pull!)]+ (is (remote-sync.task/successful? task))+ (is (= "pulled" (get-in task [:outcome :kind]))))+ (is (= :question (t2/select-one-fn :type :model/Card (:id model))))+ (is (false? (remote-sync.object/dirty?)))+ (is (not (t2/exists? :model/RemoteSyncObject :model_type "Action")))))))++(deftest push-tracks-archiving-an-action-test+ (testing "GHY-4722: archiving an action removes its file on the next push, and unarchiving it writes the file back"+ (do-with-pushed-model-actions!+ (fn [{:keys [model push! repo-file]}]+ (let [action-id (t2/select-one-pk :model/Action :model_id (:id model) :name "Rename Venue")]+ (mt/user-http-request :crowberto :put 200 (str "action/" action-id) {:archived true})+ (is (remote-sync.task/successful? (push!)))+ (is (nil? (repo-file "actions/rename_venue.yaml")))+ (mt/user-http-request :crowberto :put 200 (str "action/" action-id) {:archived false})+ (is (remote-sync.task/successful? (push!)))+ (is (some? (repo-file "actions/rename_venue.yaml"))))))))++(deftest push-tracks-moving-an-action-between-models-test+ (testing "GHY-4722: moving an action to a model outside the synced collections removes its file on the next push, and moving it back writes the file again"+ (do-with-pushed-model-actions!+ (fn [{:keys [model push! repo-file]}]+ (mt/with-temp [:model/Collection {unsynced-id :id} {:name "Unsynced" :location "/"}]+ (let [action-id (t2/select-one-pk :model/Action :model_id (:id model) :name "Rename Venue")+ other (mt/user-http-request :crowberto :post 200 "card"+ {:name "Other Model"+ :type "model"+ :collection_id unsynced-id+ :display "table"+ :visualization_settings {}+ :dataset_query (mt/mbql-query venues)})]+ (mt/user-http-request :crowberto :put 200 (str "action/" action-id) {:model_id (:id other)})+ (is (remote-sync.task/successful? (push!)))+ (is (nil? (repo-file "actions/rename_venue.yaml")))+ (mt/user-http-request :crowberto :put 200 (str "action/" action-id) {:model_id (:id model)})+ (is (remote-sync.task/successful? (push!)))+ (is (some? (repo-file "actions/rename_venue.yaml")))))))))++(deftest turning-sync-on-again-tracks-untracked-actions-test+ (testing "GHY-4722: turning sync on again for a collection tracks an action created while sync was off, and the next push writes it"+ (do-with-pushed-model!+ (fn [{:keys [coll-id model push! repo-file]}]+ (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id false}})+ (let [action-id (insert-untracked-action! (:id model))]+ (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})+ (is (= "create" (action-rso-status action-id)))+ (is (remote-sync.task/successful? (push!)))+ (is (some? (repo-file "actions/local_action.yaml"))))))))++(deftest archiving-a-collection-marks-its-actions-for-deletion-test+ (testing "GHY-4722: archiving the synced collection that holds a model marks its actions for deletion, and the next push removes their files"+ (do-with-pushed-model-actions!+ (fn [{:keys [coll-id model push! repo-file]}]+ (let [action-id (t2/select-one-pk :model/Action :model_id (:id model) :name "Rename Venue")]+ (mt/user-http-request :crowberto :put 200 (str "collection/" coll-id) {:archived true})+ (is (= "delete" (action-rso-status action-id)))+ (is (remote-sync.task/successful? (push!)))+ (is (nil? (repo-file "actions/rename_venue.yaml")))+ (is (nil? (repo-file "actions/create_venue.yaml"))))))))++(deftest upgrade-backfill-requires-a-push-before-a-pull-test+ (testing "GHY-4722: after the upgrade backfill tracks an existing action, a pull is refused until a push writes the action to the repo"+ (do-with-pushed-model!+ (fn [{:keys [model model-path push! pull! repo-file commit-remote!]}]+ (let [action-id (insert-untracked-action! (:id model))]+ (remote-sync.events/backfill-action-tracking!)+ (is (= "create" (action-rso-status action-id)))+ (testing "a normal pull is refused"+ (is (= "There are unsaved changes in the Remote Sync collection which will be overwritten by the import. Force the import to discard these changes."+ (:message (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"})))))+ (testing "a push writes the action to the repo"+ (is (remote-sync.task/successful? (push!)))+ (is (some? (repo-file "actions/local_action.yaml"))))+ (testing "a normal pull after another instance's change keeps the action"+ (commit-remote! {:upsert {model-path (str/replace (repo-file model-path) "name: Venues Model" "name: Renamed Model")}})+ (is (remote-sync.task/successful? (pull!)))+ (is (= "Renamed Model" (t2/select-one-fn :name :model/Card (:id model))))+ (is (t2/exists? :model/Action action-id))))))))+;;; ------------------------------------------------- Current Task Endpoint -------------------------------------------------(deftest current-task-requires-superuser-testdiff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.cljindex 469b6fc4e5b..9b9512936e1 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj@@ -137,6 +137,59 @@(let [entries (t2/select :model/RemoteSyncObject)](is (= 0 (count entries)))))))+(defn- action-rso [action-id]+ (t2/select-one :model/RemoteSyncObject :model_type "Action" :model_id action-id))++(deftest action-create-event-creates-entry-test+ (testing "GHY-4722: action-create on a synced model tracks the action under its model's collection"+ ;; remote-sync-type defaults to :read-only, where synced items are not tracked+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection {coll-id :id} {:is_remote_synced true :name "Remote-Sync"}+ :model/Card {model-id :id} {:type :model :collection_id coll-id}+ :model/Action action {:type :implicit :name "Create Venue" :model_id model-id}]+ (t2/delete! :model/RemoteSyncObject)+ (events/publish-event! :event/action-create {:object action :user-id (mt/user->id :rasta)})+ (is (=? {:status "create" :model_name "Create Venue" :model_collection_id coll-id}+ (action-rso (:id action))))))))++(deftest action-event-on-unsynced-model-no-entry-test+ (testing "GHY-4722: action events on a model outside synced collections are not tracked"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Normal"}+ :model/Card {model-id :id} {:type :model :collection_id coll-id}+ :model/Action action {:type :implicit :name "Create Venue" :model_id model-id}]+ (t2/delete! :model/RemoteSyncObject)+ (events/publish-event! :event/action-create {:object action :user-id (mt/user->id :rasta)})+ (is (nil? (action-rso (:id action))))))))++(deftest model-events-cascade-to-actions-test+ (testing "GHY-4722: a model's events carry to its actions"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection {synced-id :id} {:is_remote_synced true :name "Remote-Sync"}+ :model/Collection {plain-id :id} {:name "Normal"}+ :model/Card model {:type :model :collection_id synced-id}+ :model/Action {action-id :id} {:type :implicit :name "Create Venue" :model_id (:id model)}]+ (let [seed! (fn []+ (t2/delete! :model/RemoteSyncObject)+ (t2/insert! :model/RemoteSyncObject+ [{:model_type "Card" :model_id (:id model) :model_name "M" :status "synced"+ :status_changed_at (t/offset-date-time)}+ {:model_type "Action" :model_id action-id :model_name "Create Venue" :status "synced"+ :status_changed_at (t/offset-date-time)}]))]+ (testing "archiving the model marks its actions for deletion"+ (seed!)+ (events/publish-event! :event/card-update {:object (assoc model :archived true)+ :previous-object model+ :user-id (mt/user->id :rasta)})+ (is (=? {:status "delete"} (action-rso action-id))))+ (testing "moving the model out of synced collections marks its actions removed"+ (seed!)+ (t2/update! :model/Card (:id model) {:collection_id plain-id})+ (events/publish-event! :event/card-update {:object (assoc model :collection_id plain-id)+ :previous-object model+ :user-id (mt/user->id :rasta)})+ (is (=? {:status "removed"} (action-rso action-id)))))))))+(deftest dashboard-create-event-creates-entry-test(testing "dashboard-create event creates remote sync object entry with create status"(mt/with-temporary-setting-values [remote-sync-type :read-write]diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_export_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_export_test.cljindex 89fcee8d677..f9853309c02 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_export_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_export_test.clj@@ -575,6 +575,24 @@(is (entity-exported? mock y-eid)"the external dependency referenced by the new card is exported alongside it")))))))+(deftest create-dashboard-with-action-button-exports-action-test+ (testing "GHY-4722: a new dashboard with a button for an action exports the action incrementally, as a full export does"+ (with-cross-scope-setup!+ (fn [{:keys [mock c-id ext-id]}]+ (mt/with-temp [:model/Card {model-id :id} {:name "Outside Model" :type :model :collection_id ext-id+ :database_id (mt/id) :dataset_query (venues-query)}+ :model/Action {action-id :id} {:type :implicit :name "Button Action" :model_id model-id}+ :model/Dashboard {d-id :id} {:name "Buttons" :collection_id c-id}+ :model/DashboardCard _ {:dashboard_id d-id :action_id action-id}]+ (seed-create-row! "Dashboard" d-id)+ (let [task (new-task!)+ action-eid (t2/select-one-fn :entity_id :model/Action :id action-id)]+ (impl/export! (source.p/snapshot mock) task "add dashboard")+ (is (= "apply-changes-version" (written-version task))+ "the create stays incremental")+ (is (entity-exported? mock action-eid)+ "the button's action is exported alongside the dashboard")))))))+(deftest update-referencing-synced-card-stays-incremental-test(testing "an in-place edit of a card that references a card in another SYNCED collection stays incremental"(mt/with-temporary-setting-values [remote-sync-type :read-write@@ -636,6 +654,28 @@(is (= 1 (t2/count :model/Card :entity_id a-eid))"exactly one card for the entity — no duplicate from a stale file"))))))+(deftest pull-removes-action-missing-from-repo-test+ (testing "GHY-4722: a pull deletes a synced model's action that the repo no longer has"+ (mt/with-temporary-setting-values [remote-sync-type :read-write+ remote-sync-transforms false]+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Bench" :is_remote_synced true :location "/"}+ :model/Card {model-id :id} {:name "Model" :type :model :collection_id coll-id+ :database_id (mt/id) :dataset_query (venues-query)}+ :model/Action {action-id :id} {:type :implicit :name "Create Venue" :model_id model-id}]+ (t2/delete! :model/RemoteSyncObject)+ (doseq [[model-type model-id] [["Collection" coll-id] ["Card" model-id] ["Action" action-id]]]+ (seed-create-row! model-type model-id))+ (let [mock (rs.test/create-mock-source :initial-files {"main" {}})+ action-eid (t2/select-one-fn :entity_id :model/Action :id action-id)]+ (impl/export! (source.p/snapshot mock) (new-task!) "init")+ (is (entity-exported? mock action-eid) "the action is in the repo")+ ;; another instance deletes the action and pushes+ (swap! (:files-atom mock) update "main"+ (fn [tree] (into {} (remove (fn [[_ content]] (str/includes? content action-eid))) tree)))+ (is (= :success (:status (impl/import! (source.p/snapshot mock) (new-task!)))))+ (is (not (t2/exists? :model/Action :id action-id))+ "the pull deleted the local action"))))))+(deftest two-same-named-creates-use-incremental-path-test(with-exported-collection!(fn [{:keys [mock coll-id]}]diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_import_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_import_test.cljindex 1ef132c482e..5ea61458a9a 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_import_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_import_test.clj@@ -224,8 +224,9 @@(let [b-id (t2/select-one-pk :model/Card :entity_id b-eid)](mt/with-dynamic-fn-redefs [search/delete! (fn [model ids] (swap! deleted conj [model (vec ids)]))](import-at! src "v1")) ; incremental delete of card_b- (is (some (fn [[model ids]] (and (= :model/Card model) (some #{b-id} ids))) @deleted)- "the removed card is deleted from the search index by id"))))))))+ (is (some (fn [[model ids]] (and (= :model/Card model) (some #{(str b-id)} ids))) @deleted)+ "the removed card is deleted from the search index by id, as the string the index stores+ (an integer id fails on Postgres with `text = integer`)"))))))))(deftest rename-card-equivalence-test(testing "GHY-3779: renaming a card (same entity_id at a new path) imports equivalently — the olddiff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/init_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/init_test.cljindex b54f366192b..92c0d451e4c 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/init_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/init_test.clj@@ -229,6 +229,52 @@(#'init/remote-sync-init)(is (zero? (glossary-rso-count))))))))+;;; -------------------------------------------- Action ledger backfill --------------------------------------------++(defn- do-with-untracked-actions!+ "Runs `f` with `{:live :archived :unsynced}` action ids and no Action ledger rows, under `remote-sync-type`+ `sync-type`. `:live` and `:archived` belong to a model in a synced collection, `:unsynced` to a model outside one."+ [sync-type f]+ (mt/with-temporary-setting-values [:remote-sync-url "file://my/repo.git"+ :remote-sync-type sync-type+ :remote-sync-branch "main"]+ (mt/with-model-cleanup [:model/RemoteSyncObject]+ (mt/with-temp [:model/Collection {synced-id :id} {:name "Synced" :is_remote_synced true :location "/"}+ :model/Collection {plain-id :id} {:name "Plain" :location "/"}+ :model/Card {model-id :id} {:type :model :collection_id synced-id}+ :model/Card {other-id :id} {:type :model :collection_id plain-id}+ :model/Action {live :id} {:type :implicit :name "Live" :model_id model-id}+ :model/Action {archived :id} {:type :implicit :name "Old" :model_id model-id :archived true}+ :model/Action {unsynced :id} {:type :implicit :name "Elsewhere" :model_id other-id}]+ (t2/delete! :model/RemoteSyncObject :model_type "Action")+ (mt/with-dynamic-fn-redefs [impl/async-import! (constantly nil)+ remote-sync.object/dirty? (constantly false)]+ (f {:live live :archived archived :unsynced unsynced}))))))++(defn- action-rso-ids []+ (t2/select-fn-set :model_id :model/RemoteSyncObject :model_type "Action"))++(deftest remote-sync-init-backfills-action-tracking-test+ (testing "GHY-4722: read-write init tracks the unarchived actions of synced models as 'create', once, so the next push writes them"+ (do-with-untracked-actions!+ :read-write+ (fn [{:keys [live]}]+ (#'init/remote-sync-init)+ (is (= #{live} (action-rso-ids)))+ (is (=? {:status "create" :model_name "Live"}+ (t2/select-one :model/RemoteSyncObject :model_type "Action" :model_id live)))+ (testing "a second run inserts nothing"+ (#'init/remote-sync-init)+ (is (= 1 (t2/count :model/RemoteSyncObject :model_type "Action"))))))))++(deftest remote-sync-init-action-backfill-skips-read-only-test+ (testing "GHY-4722: a read-only instance is not backfilled"+ (do-with-untracked-actions!+ :read-only+ (fn [_]+ (#'init/remote-sync-init)+ (is (empty? (action-rso-ids)))))))+(deftest remote-sync-init-glossary-backfill-skips-unsynced-library-test(testing "Glossary entries are only tracked when the Library is synced"(collections.tu/with-library-not-synceddiff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.cljindex 3bdace2e1a3..249dc172836 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj@@ -44,7 +44,7 @@(deftest all-specs-have-valid-eligibility-test(testing "Every spec has a valid eligibility type"- (let [valid-eligibility-types #{:collection :published-table :parent-table :setting :library-synced}]+ (let [valid-eligibility-types #{:collection :published-table :parent-table :parent :setting :library-synced}](doseq [[model-key spec] spec/remote-sync-specs](testing (str "Spec for " model-key)(is (contains? valid-eligibility-types (get-in spec [:eligibility :type]))@@ -100,8 +100,10 @@(is (= 1 (count children)))(is (= #{:model/Field}(into #{} (map :model-key) children)))))+ (testing "GHY-4722: children-specs derives Action as a child of Card"+ (is (= [:model/Action] (map :model-key (spec/children-specs :model/Card)))))(testing "children-specs returns empty for models with no children"- (is (empty? (spec/children-specs :model/Card)))))+ (is (empty? (spec/children-specs :model/Dashboard)))));;; ------------------------------------------------ Helper Function Tests ---------------------------------------------@@ -134,7 +136,8 @@(is (contains? types "TransformTag"))(is (contains? types "TransformTest"))(is (contains? types "Glossary"))- (is (= 15 (count types))))))+ (is (contains? types "Action"))+ (is (= 16 (count types))))))(deftest specs-by-identity-type-test(testing "specs-by-identity-type filters correctly"@@ -325,6 +328,47 @@(is (nil? (spec/query-export-roots segment-spec)))(is (nil? (spec/query-export-roots measure-spec))))))+;;; ---------------------------------------------------- Actions ----------------------------------------------------++(defn- do-with-synced-and-plain-actions!+ "Runs `f` with `{:synced-coll :synced-action :plain-action}`: one action on a model in a remote-synced collection,+ one on a model outside any."+ [f]+ (mt/with-temp [:model/Collection {synced-coll :id} {:name "Synced" :is_remote_synced true :location "/"}+ :model/Collection {plain-coll :id} {:name "Plain" :location "/"}+ :model/Card {synced-model :id} {:type :model :collection_id synced-coll}+ :model/Card {plain-model :id} {:type :model :collection_id plain-coll}+ :model/Action {synced-action :id} {:type :implicit :name "In Sync" :model_id synced-model}+ :model/Action {plain-action :id} {:type :implicit :name "Outside" :model_id plain-model}]+ (f {:synced-coll synced-coll :synced-action synced-action :plain-action plain-action})))++(deftest action-eligibility-follows-model-test+ (testing "GHY-4722: an action is eligible for remote sync exactly when its model is"+ (do-with-synced-and-plain-actions!+ (fn [{:keys [synced-action plain-action]}]+ (let [action-spec (spec/spec-for-model-key :model/Action)]+ (is (true? (spec/check-eligibility action-spec (t2/select-one :model/Action :id synced-action))))+ (is (false? (spec/check-eligibility action-spec (t2/select-one :model/Action :id plain-action)))))))))++(deftest action-removal-scoped-to-synced-models-test+ (testing "GHY-4722: a pull removes absent actions only when their model is in a synced collection"+ (do-with-synced-and-plain-actions!+ (fn [{:keys [synced-coll synced-action plain-action]}]+ (remote-sync.db/delete-removed-instances!+ :model/Action+ (spec/removal-opts (spec/spec-for-model-key :model/Action) [synced-coll] #{}))+ (is (not (t2/exists? :model/Action :id synced-action)))+ (is (t2/exists? :model/Action :id plain-action))))))++(deftest action-sync-rows-carry-model-collection-test+ (testing "GHY-4722: the ledger rows rebuilt after a pull give an action its model's collection"+ (do-with-synced-and-plain-actions!+ (fn [{:keys [synced-coll synced-action]}]+ (let [eid (t2/select-one-fn :entity_id :model/Action :id synced-action)]+ (is (=? [{:model_type "Action" :model_id synced-action :model_name "In Sync"+ :model_collection_id synced-coll :status "synced"}]+ (spec/sync-all-entities! (t/offset-date-time) {:by-entity-id {"Action" #{eid}}}))))))))+;;; -------------------------------------------- Editability Checking Tests ----------------------------------------(deftest model-editable?-unknown-model-testdiff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.cljindex d8c6acdd4b2..263e10822c2 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj@@ -1039,6 +1039,31 @@(is (= #{[{:model "Card" :id card-eid-1}]}(set (serdes/deserialization-dependencies ser)))))))))))))+(deftest collection-export-includes-model-actions-test+ (testing "GHY-4722: exporting a collection exports the actions of its models, though a model doesn't reference them"+ (mt/with-empty-h2-app-db!+ (ts/with-temp-dpc [:model/Database {db-id :id} {:name "My Database"}+ :model/Collection {coll-id :id} {:name "Models"}+ :model/Card {model-id :id} {:name "Model"+ :type :model+ :database_id db-id+ :collection_id coll-id+ :dataset_query {:database db-id+ :type :native+ :native {:query "select 1"}}}+ :model/Action {action-id :id} {:name "My Action" :type :implicit :model_id model-id}+ :model/Action {archived-id :id} {:name "Old Action" :type :implicit :model_id model-id+ :archived true}]+ (let [eid (fn [id] (t2/select-one-fn :entity_id :model/Action :id id))]+ (is (= #{(eid action-id) (eid archived-id)}+ (ids-by-model "Action" (extract/extract {:targets [["Collection" coll-id]]+ :no-data-model true}))))+ (testing "with :skip-archived, the archived action is left out"+ (is (= #{(eid action-id)}+ (ids-by-model "Action" (extract/extract {:targets [["Collection" coll-id]]+ :no-data-model true+ :skip-archived true}))))))))))+(deftest query-action-test(mt/with-empty-h2-app-db!(ts/with-temp-dpc [:model/User {ann-id :id} {:first_name "Ann"diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsxindex 3f3bfec6bae..08604010f4c 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx@@ -992,4 +992,27 @@ describe("AllChangesView", () => {expect(screen.queryByText("Library")).not.toBeInTheDocument();});});++ describe("actions", () => {+ // The dirty entry for an action has no parent model id, so there is no+ // action URL to build. The item must show without a dead /404 link (GHY-4722).+ it("should display a model action in its model's collection with the bolt icon and no link", async () => {+ const actionEntity = createMockRemoteSyncEntity({+ id: 600,+ name: "Create order",+ model: "action",+ collection_id: 1,+ sync_status: "create",+ });++ setup({ entities: [actionEntity] });++ expect(await screen.findByText("Entity Collection")).toBeInTheDocument();+ expect(screen.getByText("Create order")).toBeInTheDocument();+ expect(screen.getByLabelText("bolt icon")).toBeInTheDocument();+ expect(+ screen.queryByRole("link", { name: /Create order/ }),+ ).not.toBeInTheDocument();+ });+ });});diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/EntityLink.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/EntityLink.tsxindex ffd3826647f..e4a0c3d41a9 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/EntityLink.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/EntityLink.tsx@@ -2,7 +2,7 @@ import { useMemo } from "react";import type { IconData, IconModel } from "metabase/common/utils/icon";import { useGetIcon } from "metabase/hooks/use-icon";-import { Anchor, Group, Icon } from "metabase/ui";+import { Anchor, Group, Icon, Text } from "metabase/ui";import { modelToUrl } from "metabase/urls";import type { RemoteSyncEntity } from "metabase-types/api";@@ -14,6 +14,14 @@ interface EntityLinkProps {entity: RemoteSyncEntity;}+function getEntityUrl(entity: RemoteSyncEntity): string | null {+ // An action URL needs the parent model id, which dirty entries do not carry.+ if (entity.model === "action") {+ return null;+ }+ return modelToUrl(entity);+}+export const EntityLink = ({ entity }: EntityLinkProps) => {const getIcon = useGetIcon();const entityIcon = useMemo((): IconData => {@@ -29,34 +37,38 @@ export const EntityLink = ({ entity }: EntityLinkProps) => {});}, [entity, getIcon]);- const url = useMemo(() => modelToUrl(entity), [entity]);- if (url == null) {- return null;- }+ const url = useMemo(() => getEntityUrl(entity), [entity]);const statusIcon = getSyncStatusIcon(entity.sync_status);const statusColor = getSyncStatusColor(entity.sync_status);return (<Group gap="sm" wrap="nowrap" px="sm" className={S.entityLink}>- <Anchor- href={url}- target="_blank"- size="sm"- c="text-secondary"- td="none"- classNames={{ root: S.anchor }}- display="flex"- >- <Icon- name={entityIcon.name}- size={16}- mr="sm"+ {url != null ? (+ <Anchor+ href={url}+ target="_blank"+ size="sm"c="text-secondary"- className={S.icon}- />- {entity.name}- </Anchor>+ td="none"+ classNames={{ root: S.anchor }}+ display="flex"+ >+ <Icon+ name={entityIcon.name}+ size={16}+ mr="sm"+ c="text-secondary"+ className={S.icon}+ />+ {entity.name}+ </Anchor>+ ) : (+ <Text size="sm" c="text-secondary" display="flex">+ <Icon name={entityIcon.name} size={16} mr="sm" c="text-secondary" />+ {entity.name}+ </Text>+ )}<Icon name={statusIcon} size={16} c={statusColor} ml="auto" /></Group>);diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-configs.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-configs.tsindex 7a25d239f6f..2856321e173 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-configs.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-configs.ts@@ -1,5 +1,6 @@import type { UnknownAction } from "@reduxjs/toolkit";+import { actionApi } from "metabase/api/action";import { cardApi } from "metabase/api/card";import { collectionApi } from "metabase/api/collection";import { dashboardApi } from "metabase/api/dashboard";@@ -81,6 +82,16 @@ export const MODEL_MUTATION_CONFIGS: ModelMutationConfig[] = [},},+ {+ modelType: "action",+ createEndpoints: [actionApi.endpoints.createAction.matchFulfilled],+ updateEndpoints: [actionApi.endpoints.updateAction.matchFulfilled],+ deleteEndpoints: [actionApi.endpoints.deleteAction.matchFulfilled],+ invalidation: {+ type: InvalidationType.Always,+ },+ },+// Collections have special handling{modelType: "collection",diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.tsindex 887a10b456f..6abc1fc3f70 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts@@ -11,6 +11,7 @@ import {} from "__support__/server-mocks";import { seedApiQueryCache } from "__support__/state";import { Api } from "metabase/api";+import { actionApi } from "metabase/api/action";import { cardApi } from "metabase/api/card";import { collectionApi } from "metabase/api/collection";import { dashboardApi } from "metabase/api/dashboard";@@ -773,6 +774,76 @@ describe("remote-sync-listener-middleware", () => {});});+ describe("action listeners", () => {+ afterEach(() => {+ fetchMock.clearHistory();+ });++ const subscribeAndSettle = async (+ store: ReturnType<typeof createTestStore>,+ ) => {+ store.dispatch(+ remoteSyncApi.endpoints.getRemoteSyncChanges.initiate(undefined),+ );+ await waitForCondition(() =>+ fetchMock.callHistory.done("remote-sync-dirty"),+ );+ };++ const dirtyCallCount = () =>+ fetchMock.callHistory.calls("remote-sync-dirty").length;++ // Model actions are tracked by remote sync, so creating one must refresh+ // the dirty state that enables the push button (GHY-4722).+ it("invalidates when a model action is created", async () => {+ fetchMock.post("path:/api/action", { id: 1, model_id: 10 });+ setupRemoteSyncDirtyEndpoint();++ const store = createTestStore();+ await subscribeAndSettle(store);++ store.dispatch(+ actionApi.endpoints.createAction.initiate({+ name: "Create",+ type: "implicit",+ kind: "row/create",+ model_id: 10,+ }),+ );++ await waitForCondition(() => dirtyCallCount() > 1);+ expect(dirtyCallCount()).toBeGreaterThan(1);+ });++ it("invalidates when a model action is updated", async () => {+ fetchMock.put("path:/api/action/1", { id: 1, model_id: 10 });+ setupRemoteSyncDirtyEndpoint();++ const store = createTestStore();+ await subscribeAndSettle(store);++ store.dispatch(+ actionApi.endpoints.updateAction.initiate({ id: 1, name: "Renamed" }),+ );++ await waitForCondition(() => dirtyCallCount() > 1);+ expect(dirtyCallCount()).toBeGreaterThan(1);+ });++ it("invalidates when a model action is deleted", async () => {+ fetchMock.delete("path:/api/action/1", { id: 1, model_id: 10 });+ setupRemoteSyncDirtyEndpoint();++ const store = createTestStore();+ await subscribeAndSettle(store);++ store.dispatch(actionApi.endpoints.deleteAction.initiate(1));++ await waitForCondition(() => dirtyCallCount() > 1);+ expect(dirtyCallCount()).toBeGreaterThan(1);+ });+ });+describe("glossary listeners", () => {afterEach(() => {fetchMock.clearHistory();diff --git a/frontend/src/metabase-types/api/remote-sync.ts b/frontend/src/metabase-types/api/remote-sync.tsindex f2321e216ca..2fd456ab97a 100644--- a/frontend/src/metabase-types/api/remote-sync.ts+++ b/frontend/src/metabase-types/api/remote-sync.ts@@ -4,6 +4,7 @@ import type { UserId } from "./user";import type { CardDisplayType } from "./visualization";export type RemoteSyncEntityModel =+ | "action"| "card"| "dataset"| "metric"diff --git a/src/metabase/actions/events.clj b/src/metabase/actions/events.cljindex cb243e9b70b..c0554ea46c1 100644--- a/src/metabase/actions/events.clj+++ b/src/metabase/actions/events.clj@@ -8,6 +8,10 @@(events/derive! :event/action.success ::event)(events/derive! :event/action.failure ::event)+(events/derive! :event/action-create ::event)+(events/derive! :event/action-update ::event)+(events/derive! :event/action-delete ::event)+(defn publish-action-invocation!"Publish the details of action of how a"[action-kw {:keys [invocation-id user-id] :as _context} inputs]diff --git a/src/metabase/actions_rest/api.clj b/src/metabase/actions_rest/api.cljindex 5e3c40ab410..0914e558ed9 100644--- a/src/metabase/actions_rest/api.clj+++ b/src/metabase/actions_rest/api.clj@@ -9,6 +9,7 @@[metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.eid-translation.core :as eid-translation]+ [metabase.events.core :as events][metabase.lib.core :as lib][metabase.lib.schema.id :as lib.schema.id][metabase.permissions.core :as perms]@@ -76,8 +77,9 @@(analytics/track-event! :snowplow/action{:event :action-deleted:type (:type action)- :action_id action-id}))- (actions-rest.db/delete-action! action-id)+ :action_id action-id})+ (actions-rest.db/delete-action! action-id)+ (events/publish-event! :event/action-delete {:object action :user-id api/*current-user-id*}))api/generic-204-no-content)(api.macros/defendpoint :post "/" :- ::actions.schema/action@@ -111,11 +113,12 @@:type action-type:action_id action-id:num_parameters (count parameters)})- (if action-id- (actions/select-action :id action-id)- ;; t2/insert! does not return a value when used with h2- ;; so we return the most recently updated http action.- (last (actions/select-actions nil :type action-type)))))+ (u/prog1 (if action-id+ (actions/select-action :id action-id)+ ;; t2/insert! does not return a value when used with h2+ ;; so we return the most recently updated http action.+ (last (actions/select-actions nil :type action-type)))+ (events/publish-event! :event/action-create {:object <> :user-id api/*current-user-id*}))));; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to;; use our API + we will need it when we make auto-TypeScript-signature generation happen@@ -144,6 +147,7 @@(check-native-query-perms! (:database_id action) dataset-query))(actions/update! (assoc action :id id) existing-action))(let [{:keys [parameters type] :as action} (actions/select-action :id id)]+ (events/publish-event! :event/action-update {:object action :user-id api/*current-user-id*})(analytics/track-event! :snowplow/action{:event :action-updated:type typediff --git a/src/metabase/events/schema.clj b/src/metabase/events/schema.cljindex f479e668a6a..d962785a73f 100644--- a/src/metabase/events/schema.clj+++ b/src/metabase/events/schema.clj@@ -128,6 +128,17 @@[:email ms/Email][:first_name {:optional true} [:maybe :string]]]]]]])+;; action events++(mr/def ::action+ [:map {:closed true}+ [:user-id [:maybe pos-int?]]+ [:object [:fn #(t2/instance-of? :model/Action %)]]])++(mr/def :event/action-create ::action)+(mr/def :event/action-update ::action)+(mr/def :event/action-delete ::action)+;; segment events(mr/def ::segmentdiff --git a/src/metabase/queries/db.clj b/src/metabase/queries/db.cljindex fd2943258cb..5cad5184d2a 100644--- a/src/metabase/queries/db.clj+++ b/src/metabase/queries/db.clj@@ -354,6 +354,19 @@:join [:implicit_action [:= :action.id :implicit_action.action_id]]:where [:= :action.model_id model-id]}))+(mu/defn action-ids-for-model+ "The IDs of the Actions of the model Card with `model-id`, leaving out archived Actions when `skip-archived`."+ [model-id :- ms/PositiveInt+ skip-archived :- [:maybe :boolean]]+ (if skip-archived+ (t2/select-pks-set :model/Action :model_id model-id :archived false)+ (t2/select-pks-set :model/Action :model_id model-id)))++(mu/defn actions-for-model+ "The Actions of the model Card with `model-id`."+ [model-id :- ms/PositiveInt]+ (t2/select :model/Action :model_id model-id))+(mu/defn delete-actions!"Delete the Actions with `action-ids`, returning the number deleted."[action-ids :- [:set ::lib.schema.id/action]]diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.cljindex a6a18d92ab1..742b08c24cd 100644--- a/src/metabase/queries/models/card.clj+++ b/src/metabase/queries/models/card.clj@@ -1294,10 +1294,9 @@(doseq [{dep-id :id, dep-query :dataset_query} cards-to-update](queries.db/update-card! dep-id {:dataset_query (assoc dep-query :database new-db-id)}))))))-(defn update-card!- "Update a Card. Metadata is fetched asynchronously. If it is ready before [[metadata-sync-wait-ms]] elapses it will be- included, otherwise the metadata will be saved to the database asynchronously."- [{:keys [card-before-update card-updates actor delete-old-dashcards?]}]+(defn- update-card-in-db!+ "Write `card-updates` to the Card and the rows that depend on it, in one transaction."+ [card-before-update card-updates actor delete-old-dashcards?];; don't block our precious core.async thread, run the actual DB updates on a separate thread(t2/with-transaction [_conn](api/maybe-reconcile-collection-position! (select-keys card-before-update [:collection_id :collection_position]) (select-keys card-updates [:collection_id :collection_position]))@@ -1325,7 +1324,33 @@(update-associated-parameters! card-before-update card-updates)(catch Throwable e(log/errorf "Update of dependent card parameters failed!: %s" (ex-message e))))- (collection/check-for-remote-sync-update card-before-update))+ (collection/check-for-remote-sync-update card-before-update)))++(defn- retired-action-events+ "The `[topic action]` pairs that announce how an update to a model retired its actions: `:event/action-delete` with+ the old action for each of `actions-before` absent from `actions-after`, and `:event/action-update` with the new+ action for each one that became archived."+ [actions-before actions-after]+ (let [id->after (m/index-by :id actions-after)]+ (for [before actions-before+ :let [after (id->after (:id before))]+ :when (or (nil? after) (and (:archived after) (not (:archived before))))]+ (if after+ [:event/action-update after]+ [:event/action-delete before]))))++(defn update-card!+ "Update a Card. Metadata is fetched asynchronously. If it is ready before [[metadata-sync-wait-ms]] elapses it will be+ included, otherwise the metadata will be saved to the database asynchronously. Publishes `:event/card-update`, plus+ an action event for each action of a model that the update deletes or archives."+ [{:keys [card-before-update card-updates actor delete-old-dashcards?]}]+ ;; The card hooks delete or archive a model's actions without events, so compare the actions before and after.+ (let [actions-before (when (= :model (keyword (:type card-before-update)))+ (queries.db/actions-for-model (:id card-before-update)))]+ (update-card-in-db! card-before-update card-updates actor delete-old-dashcards?)+ (when (seq actions-before)+ (doseq [[topic action] (retired-action-events actions-before (queries.db/actions-for-model (:id card-before-update)))]+ (events/publish-event! topic {:object action :user-id api/*current-user-id*}))));; Fetch the updated Card from the DB(let [card (queries.db/card (:id card-before-update))];;; TODO -- this should be triggered indirectly by `:event/card-update`@@ -1516,7 +1541,7 @@(defmethod serdes/deserialization-dependencies "Card" [card](card-deps false card))-(defmethod serdes/descendants "Card" [_model-name id _opts]+(defmethod serdes/descendants "Card" [_model-name id {:keys [skip-archived]}](let [card (queries.db/card id)query (not-empty (:dataset_query card))source-cards (some-> query lib/all-source-card-ids)@@ -1532,7 +1557,11 @@(for [card-id parameters-card-id]{["Card" card-id] {"Card" id}})(for [snippet-id snippets]- {["NativeQuerySnippet" snippet-id] {"Card" id}})))))+ {["NativeQuerySnippet" snippet-id] {"Card" id}})+ ;; An Action points at its model, but the model doesn't reference its actions, so list them here.+ (when (= :model (:type card))+ (for [action-id (queries.db/action-ids-for-model id skip-archived)]+ {["Action" action-id] {"Card" id}}))))))(defmethod serdes/extract-query "Card"[model-name {:keys [collection-set filter-column filter-ids] :as opts}]diff --git a/src/metabase/queries_rest/api/card.clj b/src/metabase/queries_rest/api/card.cljindex fa0638c70e7..2e8d41ee22b 100644--- a/src/metabase/queries_rest/api/card.clj+++ b/src/metabase/queries_rest/api/card.clj@@ -665,9 +665,13 @@"Hard delete a Card. To soft delete, use `PUT /api/queries/:id`"[{:keys [id]} :- [:map {:closed true}[:id ms/PositiveInt]]]- (let [card (api/write-check :model/Card id)]+ (let [card (api/write-check :model/Card id)+ ;; the database deletes the card's actions through the foreign key, with no events of their own+ actions (queries-rest.db/actions-for-model id)](queries-rest.db/delete-card! id)- (events/publish-event! :event/card-delete {:object card :user-id api/*current-user-id*}))+ (events/publish-event! :event/card-delete {:object card :user-id api/*current-user-id*})+ (doseq [action actions]+ (events/publish-event! :event/action-delete {:object action :user-id api/*current-user-id*})))api/generic-204-no-content);;; -------------------------------------------- Bulk Collections Update ---------------------------------------------diff --git a/src/metabase/queries_rest/db.clj b/src/metabase/queries_rest/db.cljindex 840e520d96f..53674aeb810 100644--- a/src/metabase/queries_rest/db.clj+++ b/src/metabase/queries_rest/db.clj@@ -143,6 +143,11 @@changes :- (mut/select-keys ::queries.schema/card.update [:collection_position :collection_id :public_uuid :made_public_by_id])](t2/update! :model/Card card-id changes))+(mu/defn actions-for-model+ "The Actions of the model Card with `model-id`."+ [model-id :- ::lib.schema.id/card]+ (t2/select :model/Action :model_id model-id))+(mu/defn delete-card!"Delete the Card with `card-id`."[card-id :- ::lib.schema.id/card]diff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.cljindex 0be84711f1c..eb3ef330e42 100644--- a/test/metabase/queries/models/card_test.clj+++ b/test/metabase/queries/models/card_test.clj@@ -6,6 +6,7 @@[metabase.api.common :as api][metabase.audit-app.impl :as audit][metabase.config.core :as config]+ [metabase.events.core :as events][metabase.lib.convert :as lib.convert][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata]@@ -768,6 +769,87 @@(is (= {["Card" (:id card1)] {"Card" (:id card2)}}(serdes/descendants "Card" (:id card2) {}))))))+(deftest ^:parallel descendants-model-actions-test+ (testing "GHY-4722: a model's actions are its descendants, though the model doesn't reference them"+ (mt/with-temp [:model/Card {model-id :id} {:type :model+ :dataset_query {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :venues)}}}+ :model/Action {action-id :id} {:type :implicit :name "Live" :model_id model-id}+ :model/Action {archived-id :id} {:type :implicit :name "Archived" :model_id model-id :archived true}]+ (is (= {["Action" action-id] {"Card" model-id}+ ["Action" archived-id] {"Card" model-id}}+ (serdes/descendants "Card" model-id {})))+ (testing "with :skip-archived, archived actions are left out"+ (is (= {["Action" action-id] {"Card" model-id}}+ (serdes/descendants "Card" model-id {:skip-archived true})))))))++(defn- action-events-during!+ "The set of `[topic action-id archived?]` for the action events `thunk` publishes."+ [thunk]+ (let [published (atom #{})]+ (with-redefs [events/publish-event! (fn [topic {:keys [object]}]+ (when (#{:event/action-create :event/action-update :event/action-delete} topic)+ (swap! published conj [topic (:id object) (boolean (:archived object))])))]+ (thunk))+ @published))++(defn- do-with-model-actions!+ "Runs `f` with `{:model-id :implicit :query :archived}`: a model with an implicit action, a query action, and an+ already-archived query action."+ [f]+ (mt/with-temp [:model/Card {model-id :id} {:type :model :dataset_query (mt/mbql-query venues)}+ :model/Action {implicit :id} {:type :implicit :name "Create" :model_id model-id}+ :model/Action {query :id} {:type :query :name "Rename" :model_id model-id}+ :model/Action {archived :id} {:type :query :name "Old" :model_id model-id :archived true}]+ ;; the implicit_action row is what marks an action implicit to the queries that retire them+ (t2/insert! :model/ImplicitAction {:action_id implicit :kind "row/create"})+ (f {:model-id model-id :implicit implicit :query query :archived archived})))++(defn- update-model!+ "Updates the model Card with `model-id` through [[card/update-card!]], as the API does."+ [model-id changes]+ (mt/with-test-user :crowberto+ (card/update-card! {:card-before-update (t2/select-one :model/Card model-id)+ :card-updates changes})))++(defn- filtered-venues-query+ "A venues query with a filter, which does not support implicit actions."+ []+ (let [mp (mt/metadata-provider)]+ (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :venues :price)) 1)))))++(deftest model-becoming-question-publishes-action-events-test+ (testing "GHY-4722: update-card! announces the actions it archives and deletes when a model becomes a question"+ (do-with-model-actions!+ (fn [{:keys [model-id implicit query]}]+ (is (= #{[:event/action-update query true]+ [:event/action-delete implicit false]}+ (action-events-during! #(update-model! model-id {:type :question}))))))))++(deftest model-query-without-implicit-support-publishes-action-events-test+ (testing "GHY-4722: update-card! announces the implicit actions it deletes when a model query no longer supports them"+ (do-with-model-actions!+ (fn [{:keys [model-id implicit]}]+ (is (= #{[:event/action-delete implicit false]}+ (action-events-during! #(update-model! model-id {:dataset_query (filtered-venues-query)}))))))))++(deftest model-changes-outside-update-card-publish-no-action-events-test+ (testing "GHY-4722: a serdes load writes models with t2 directly, and must not publish action events (they would dirty the remote sync ledger during a pull)"+ (testing "a model becoming a question"+ (do-with-model-actions!+ (fn [{:keys [model-id]}]+ (is (= #{} (action-events-during! #(t2/update! :model/Card model-id {:type :question})))))))+ (testing "a model query that no longer supports implicit actions"+ (do-with-model-actions!+ (fn [{:keys [model-id]}]+ (is (= #{} (action-events-during! #(t2/update! :model/Card model-id {:dataset_query (filtered-venues-query)})))))))+ (testing "a deleted model"+ (do-with-model-actions!+ (fn [{:keys [model-id]}]+ (is (= #{} (action-events-during! #(t2/delete! :model/Card model-id)))))))))+(deftest ^:parallel extract-result-metadata-non-model-test(testing "non-model Card extraction drops :result_metadata entirely"(let [metadata (qp.preprocess/query->expected-cols (mt/mbql-query venues))diff --git a/test/metabase/queries_rest/api/card_test.clj b/test/metabase/queries_rest/api/card_test.cljindex befc69ed3d9..eaafaf0b8c3 100644--- a/test/metabase/queries_rest/api/card_test.clj+++ b/test/metabase/queries_rest/api/card_test.clj@@ -19,6 +19,7 @@[metabase.content-verification.models.moderation-review :as moderation-review][metabase.driver :as driver][metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]+ [metabase.events.core :as events][metabase.lib-be.core :as lib-be][metabase.lib.convert :as lib.convert][metabase.lib.core :as lib]@@ -2497,6 +2498,18 @@(mt/user-http-request :rasta :delete 204 (str "card/" (u/the-id card)))(t2/select-one :model/Card :id (u/the-id card)))))))+(deftest delete-model-publishes-action-delete-events-test+ (testing "GHY-4722: deleting a model announces the deletion of each of its actions, which the database removes with it"+ (mt/with-temp [:model/Card {model-id :id} {:type :model :dataset_query (mt/mbql-query venues)}+ :model/Action {query :id} {:type :query :name "Rename" :model_id model-id}+ :model/Action {archived :id} {:type :query :name "Old" :model_id model-id :archived true}]+ (let [published (atom #{})]+ (mt/with-dynamic-fn-redefs [events/publish-event! (fn [topic {:keys [object]}]+ (when (= :event/action-delete topic)+ (swap! published conj (:id object))))]+ (mt/user-http-request :crowberto :delete 204 (str "card/" model-id)))+ (is (= #{query archived} @published))))))+;; deleting a card that doesn't exist should return a 404 (#1957)(deftest deleting-a-card-that-doesnt-exist-should-return-a-404---1957-(is (= "Not found."