diff --git a/.dir-locals.el b/.dir-locals.el
index 20ebf6a8d0d5dce346c9d62b9dce41342af8fa49..c6ef3523caff2e5bf3ce802bf28b55c977c182e8 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -48,6 +48,7 @@
   (eval . (put-clojure-indent 'mt/format-rows-by 1))
   (eval . (put-clojure-indent 'mt/query 1))
   (eval . (put-clojure-indent 'mt/test-drivers 1))
+  (eval . (put-clojure-indent 'mt/test-driver 1))
   (eval . (put-clojure-indent 'prop/for-all 1))
   (eval . (put-clojure-indent 'qp.streaming/streaming-response 1))
   (eval . (put-clojure-indent 'u/select-keys-when 1))
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
index f1a1fd94781d7c0eb2915cdc0eb79fe2eb1f06f1..cc97df9a30402b03f491cd46d583ce2f7b7af57f 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
@@ -332,13 +332,18 @@
   (let [db-id (u/the-id database)]
     (log/infof (trs "DB {0} had hardcoded dataset-id; changing to an inclusion pattern and updating table schemas"
                     db-id))
-    (db/execute! {:update MetabaseTable
-                  :set    {:schema dataset-id}
-                  :where  [:and
-                           [:= :db_id db-id]
-                           [:or
-                            [:= :schema nil]
-                            [:not= :schema dataset-id]]]})
+    (try
+      (db/execute! {:update MetabaseTable
+                    :set    {:schema dataset-id}
+                    :where  [:and
+                             [:= :db_id db-id]
+                             [:or
+                              [:= :schema nil]
+                              [:not= :schema dataset-id]]]})
+      ;; if we are upgrading to the sdk driver after having downgraded back to the old driver we end up with
+      ;; duplicated tables with nil schema. Happily only in the "dataset-id" schema and not all schemas. But just
+      ;; leave them with nil schemas and they will get deactivated in sync.
+      (catch Exception _e))
     (let [updated-db (-> (assoc-in database [:details :dataset-filters-type] "inclusion")
                          (assoc-in [:details :dataset-filters-patterns] dataset-id)
                          (m/dissoc-in [:details :dataset-id]))]
diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj
index bb896a1d4cc96b8b99c306a502841d4a7d553705..3fc363cb382902a5ae3ec245d04b9585598e2304 100644
--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj
+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj
@@ -335,8 +335,24 @@
                 :name   tbl-nm
                 :fields #{{:name "int_col", :database-type "INTEGER", :base-type :type/Integer, :database-position 0}
                           {:name "array_col", :database-type "INTEGER", :base-type :type/Array, :database-position 1}}}
-              (driver/describe-table :bigquery-cloud-sdk (mt/db) {:name tbl-nm, :schema "v3_test_data"}))
-          "`describe-table` should detect the correct base-type for array type columns")))))
+               (driver/describe-table :bigquery-cloud-sdk (mt/db) {:name tbl-nm, :schema "v3_test_data"}))
+               "`describe-table` should detect the correct base-type for array type columns")))))
+
+(deftest sync-inactivates-old-duplicate-tables
+  (testing "If on the new driver, then downgrade, then upgrade again (#21981)"
+    (mt/test-driver :bigquery-cloud-sdk
+      (mt/dataset avian-singles
+        (try
+          (let [synced-tables (db/select Table :db_id (mt/id))]
+            (is (= 2 (count synced-tables)))
+            (db/insert-many! Table (map #(dissoc % :id :schema) synced-tables))
+            (sync/sync-database! (mt/db) {:scan :schema})
+            (let [synced-tables (db/select Table :db_id (mt/id))]
+              (is (partial= {true [{:name "messages"} {:name "users"}]
+                             false [{:name "messages"} {:name "users"}]}
+                            (-> (group-by :active (doto synced-tables tap>))
+                                (update-vals #(sort-by :name %)))))))
+          (finally (db/delete! Table :db_id (mt/id) :active false)))))))
 
 (deftest retry-certain-exceptions-test
   (mt/test-driver :bigquery-cloud-sdk