Skip to content

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

[GHY-4747] Start unused built-in transform jobs inactive (#85535)

Tyler Pludecac530658581
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
index a08d3351387..9327273e019 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
@@ -3441,3 +3441,35 @@
(serdes.load/load-metabase! (ts/ingestion-in-memory [(glossary-file file-eid {:entity_id file-eid})]))
(is (=? [{:term "ARR" :entity_id file-eid}]
(t2/select :model/Glossary))))))))
+
+(deftest transform-job-active-is-local-test
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temp [:model/TransformJob {built-in-id :id} {:name "built-in"
+ :schedule "0 0 * * * ? *"
+ :built_in_type "hourly"
+ :active false}
+ :model/TransformJob {custom-id :id} {:name "custom"
+ :schedule "0 0 * * * ? *"
+ :active false}]
+ (testing "an export has no active"
+ (is (not (contains? (ts/extract-one "TransformJob" built-in-id) :active)))
+ (is (not (contains? (ts/extract-one "TransformJob" custom-id) :active))))
+ (let [import! (fn [job-id active]
+ (serdes.load/load-metabase!
+ (ts/ingestion-in-memory [(assoc (ts/extract-one "TransformJob" job-id) :active active)]))
+ (t2/select-one [:model/TransformJob :active :built_in_type] job-id))]
+ (testing "an import keeps active on an existing built-in job"
+ (is (=? {:active false :built_in_type "hourly"} (import! built-in-id true)))
+ (t2/update! :transform_job built-in-id {:active true})
+ (is (=? {:active true :built_in_type "hourly"} (import! built-in-id false))))
+ (testing "an import keeps active on an existing custom job"
+ (is (=? {:active false :built_in_type nil} (import! custom-id true)))
+ (t2/update! :transform_job custom-id {:active true})
+ (is (=? {:active true :built_in_type nil} (import! custom-id false))))
+ (testing "an import inserts a job with the column default for active"
+ (let [extracted (assoc (ts/extract-one "TransformJob" custom-id) :active false)]
+ (t2/delete! :model/TransformJob custom-id)
+ (serdes.load/load-metabase! (ts/ingestion-in-memory [extracted]))
+ (is (=? {:active true}
+ (t2/select-one [:model/TransformJob :active] :entity_id (:entity_id extracted))))
+ (t2/delete! :model/TransformJob :entity_id (:entity_id extracted))))))))
diff --git a/resources/migrations/2026/20261026_deactivate_unused_builtin_transform_jobs.yaml b/resources/migrations/2026/20261026_deactivate_unused_builtin_transform_jobs.yaml
new file mode 100644
index 00000000000..b49bc6d4e96
--- /dev/null
+++ b/resources/migrations/2026/20261026_deactivate_unused_builtin_transform_jobs.yaml
@@ -0,0 +1,27 @@
+## The built-in transform jobs from migration 057 exist on every instance and run any transform with a matching tag,
+## also transforms that remote sync pulls into a development instance. Built-in jobs that no transform uses become
+## inactive, so a user must enable them. Built-in jobs that a transform uses keep running.
+## When the app changes a built-in job, the TransformJob before-update hook sets built_in_type to NULL. A serdes import
+## never sets active: a job that the import inserts gets the column default (active).
+## The rollback activates all inactive built-in jobs.
+
+databaseChangeLog:
+ - changeSet:
+ id: pyz33j
+ author: tplude
+ comment: Deactivate the built-in transform jobs that no transform uses
+ changes:
+ - sql:
+ sql: >-
+ UPDATE transform_job SET active = FALSE
+ WHERE built_in_type IS NOT NULL
+ AND active = TRUE
+ AND NOT EXISTS (SELECT 1 FROM transform_job_transform_tag jt
+ JOIN transform_transform_tag tt ON tt.tag_id = jt.tag_id
+ WHERE jt.job_id = transform_job.id);
+ rollback:
+ - sql:
+ sql: >-
+ UPDATE transform_job SET active = TRUE
+ WHERE built_in_type IS NOT NULL
+ AND active = FALSE;
diff --git a/src/metabase/transforms/db.clj b/src/metabase/transforms/db.clj
index b3590b7dd82..7f8192856f0 100644
--- a/src/metabase/transforms/db.clj
+++ b/src/metabase/transforms/db.clj
@@ -184,6 +184,11 @@
[]
(t2/select :model/TransformJob :active true))
+(mu/defn inactive-job-ids
+ "The IDs of the inactive TransformJobs."
+ []
+ (t2/select-pks-set :model/TransformJob :active false))
+
(mu/defn job-snapshot
"The name, entity ID, and built-in type of the TransformJob with `job-id`."
[job-id :- ms/PositiveInt]
diff --git a/src/metabase/transforms/models/transform_job.clj b/src/metabase/transforms/models/transform_job.clj
index 1a11a5bff93..50ec32f59b6 100644
--- a/src/metabase/transforms/models/transform_job.clj
+++ b/src/metabase/transforms/models/transform_job.clj
@@ -200,9 +200,9 @@
(defmethod serdes/make-spec "TransformJob"
[_model-name opts]
- {:copy [:entity_id :built_in_type :schedule :ui_display_type :active]
- :skip []
- :defaults {:active true}
+ {:copy [:entity_id :built_in_type :schedule :ui_display_type]
+ :skip [;; admin-owned state of this instance
+ :active]
:transform {:name {:export str :import identity}
:description {:export str :import identity}
:job_tags (serdes/nested :model/TransformJobTransformTag :job_id (merge {:sort-by (juxt :position :entity_id)} opts))}})
diff --git a/src/metabase/transforms/schedule.clj b/src/metabase/transforms/schedule.clj
index 7b363eb77eb..1ae6f74bcd7 100644
--- a/src/metabase/transforms/schedule.clj
+++ b/src/metabase/transforms/schedule.clj
@@ -138,5 +138,6 @@
(defmethod task/init! ::RunTransform [_]
(log/info "Initializing transform job execution jobs")
+ (run! #(task/delete-trigger! (trigger-key %)) (transforms.db/inactive-job-ids))
(->> (transforms.db/active-jobs)
(run! initialize-job!)))
diff --git a/test/metabase/app_db/schema_migrations_test.clj b/test/metabase/app_db/schema_migrations_test.clj
index 9808cc7cd5f..11cbafae5f5 100644
--- a/test/metabase/app_db/schema_migrations_test.clj
+++ b/test/metabase/app_db/schema_migrations_test.clj
@@ -27,6 +27,7 @@
[metabase.app-db.core :as mdb]
[metabase.app-db.custom-migrations-test :as custom-migrations-test]
[metabase.app-db.liquibase :as liquibase]
+ [metabase.app-db.liquibase.rollback :as rollback]
[metabase.app-db.liquibase.versions :as versions]
[metabase.app-db.query :as mdb.query]
[metabase.app-db.schema-migrations-test.impl :as impl]
@@ -4207,3 +4208,74 @@
(is (= [] (t2/select :data_app_group_assignment)))
(migrate!)
(is (thrown? Exception (t2/select :data_app_group_assignment))))))
+
+(deftest deactivate-unused-builtin-transform-jobs-test
+ (testing "pyz33j deactivates the built-in jobs that no transform uses"
+ (impl/test-migrations ["pyz33j"] [migrate!]
+ (let [job-active (fn [] (t2/select-fn->fn :built_in_type :active :transform_job
+ {:where [:not= :built_in_type nil]}))
+ ;; Get the boundary before pyz33j runs. On MySQL, the version-tracking marker row that the run writes can
+ ;; have the same position as pyz33j, so previous-deployment-id after the run can be pyz33j's deployment.
+ boundary (with-open [conn (.getConnection (mdb/data-source))]
+ (liquibase/with-liquibase [liquibase conn]
+ (:deployment-id (first (versions/recorded-deployments conn (.getDatabase liquibase))))))]
+ (testing "with no transforms, all four built-in jobs become inactive"
+ (is (= {"hourly" true "daily" true "weekly" true "monthly" true} (job-active)))
+ (migrate!)
+ (is (= {"hourly" false "daily" false "weekly" false "monthly" false} (job-active))))
+ (testing "rollback activates the built-in jobs again"
+ (with-open [conn (.getConnection (mdb/data-source))]
+ (liquibase/with-liquibase [liquibase conn]
+ (rollback/rollback-to-deployment! conn liquibase boundary)))
+ (is (= {"hourly" true "daily" true "weekly" true "monthly" true} (job-active))))))))
+
+(deftest keep-used-builtin-transform-jobs-active-test
+ (testing "pyz33j keeps active the built-in jobs that a transform uses, and the jobs that are not built in"
+ (impl/test-migrations ["pyz33j"] [migrate!]
+ (let [user-id (t2/insert-returning-pk! :core_user {:first_name "Job"
+ :last_name "Owner"
+ :entity_id (u/generate-nano-id)
+ :date_joined :%now
+ :password "password"
+ :password_salt "salt"})
+ transform! (fn [tag-id]
+ (let [transform-id (t2/insert-returning-pk!
+ :transform {:name (mt/random-name)
+ :source "{}"
+ :target "{}"
+ :source_type "native"
+ :creator_id user-id
+ :entity_id (u/generate-nano-id)
+ :created_at :%now
+ :updated_at :%now})]
+ (t2/insert! :transform_transform_tag {:transform_id transform-id
+ :tag_id tag-id
+ :entity_id (u/generate-nano-id)
+ :position 0})))
+ builtin-tag (fn [built-in-type]
+ (t2/select-one-pk :transform_tag :built_in_type built-in-type))
+ builtin-job (fn [built-in-type]
+ (t2/select-one-pk :transform_job :built_in_type built-in-type))
+ custom-tag (t2/insert-returning-pk! :transform_tag {:name "nightly"
+ :entity_id (u/generate-nano-id)
+ :created_at :%now
+ :updated_at :%now})
+ job-active (fn [] (t2/select-fn->fn :name :active :transform_job))]
+ (t2/insert! :transform_job {:name "Custom job"
+ :description "A job without tags"
+ :schedule "0 0 * * * ? *"
+ :entity_id (u/generate-nano-id)})
+ (transform! (builtin-tag "hourly"))
+ (t2/insert! :transform_job_transform_tag {:job_id (builtin-job "daily")
+ :tag_id custom-tag
+ :entity_id (u/generate-nano-id)
+ :position 1})
+ (transform! custom-tag)
+ (migrate!)
+ (is (= {"Hourly job" true
+ "Daily job" true
+ "Weekly job" false
+ "Monthly job" false
+ "Custom job" true}
+ (job-active)))))))
diff --git a/test/metabase/transforms/schedule_test.clj b/test/metabase/transforms/schedule_test.clj
index 0baa067a846..60b3133920e 100644
--- a/test/metabase/transforms/schedule_test.clj
+++ b/test/metabase/transforms/schedule_test.clj
@@ -2,9 +2,11 @@
(:require
[clojure.test :refer :all]
[clojurewerkz.quartzite.conversion :as qc]
+ [metabase.task.core :as task]
[metabase.test :as mt]
[metabase.transforms.jobs :as transforms.jobs]
- [metabase.transforms.schedule])
+ [metabase.transforms.schedule :as transforms.schedule]
+ [toucan2.core :as t2])
(:import
(metabase.transforms.schedule RunTransforms)
(org.quartz JobDataMap)))
@@ -47,3 +49,21 @@
(is (= [job-id {:run-method :cron}]
@run-args)
"Should pass the job id and cron run method")))))))
+
+(deftest init-deletes-triggers-of-inactive-jobs-test
+ (testing "init! deletes the trigger of a job made inactive by a plain table update, accepts an inactive job with no trigger, and keeps active triggers"
+ (mt/with-temp-scheduler!
+ (mt/with-temp [:model/TransformJob {active-id :id :as active-job} {:active true :schedule "0 0 * * * ?"}
+ :model/TransformJob {inactive-id :id :as inactive-job} {:active true :schedule "0 0 * * * ?"}
+ :model/TransformJob {untriggered-id :id} {:active false :schedule "0 0 * * * ?"}]
+ (try
+ (run! transforms.schedule/initialize-job! [active-job inactive-job])
+ (is (some? (transforms.schedule/existing-trigger inactive-id)))
+ (is (nil? (transforms.schedule/existing-trigger untriggered-id)))
+ (t2/update! :transform_job {:id inactive-id} {:active false})
+ (task/init! ::transforms.schedule/RunTransform)
+ (is (nil? (transforms.schedule/existing-trigger inactive-id)))
+ (is (nil? (transforms.schedule/existing-trigger untriggered-id)))
+ (is (some? (transforms.schedule/existing-trigger active-id)))
+ (finally
+ (run! transforms.schedule/delete-job! [active-id inactive-id])))))))