From 2f5c4bd08b1f86dc41efaf3d42815581d21eb848 Mon Sep 17 00:00:00 2001
From: colin-rogers-dbt <111200756+colin-rogers-dbt@users.noreply.github.com>
Date: Sun, 12 Nov 2023 09:59:06 -0800
Subject: [PATCH] fix 409 duplicate batch issue (#1020)

* use dynamic schema in test_grant_access_to.py

* use dynamic schema in test_grant_access_to.py

* revert setup

* fix partitioninb

* skip TestPythonBatchIdModels

* add changie

* run python integration tests separately

* run python integration tests separately

* cleanup _get_batch_id

* add space to pipe

* fix integration.yml

* add extra changie

* use dynamic schema in test_grant_access_to.py

* use dynamic schema in test_grant_access_to.py

* revert setup

* replace deterministic default batch_id with uuid

* add changie

(cherry picked from commit 0c5422c6df8f542611ee04e0c59c64ca0e0335a1)
---
 .changes/unreleased/Fixes-20231111-150959.yaml | 6 ++++++
 dbt/adapters/bigquery/python_submissions.py    | 4 ++--
 tests/functional/adapter/test_python_model.py  | 3 ---
 3 files changed, 8 insertions(+), 5 deletions(-)
 create mode 100644 .changes/unreleased/Fixes-20231111-150959.yaml

diff --git a/.changes/unreleased/Fixes-20231111-150959.yaml b/.changes/unreleased/Fixes-20231111-150959.yaml
new file mode 100644
index 000000000..3d9f245a6
--- /dev/null
+++ b/.changes/unreleased/Fixes-20231111-150959.yaml
@@ -0,0 +1,6 @@
+kind: Fixes
+body: replace deterministic batch_id with uuid
+time: 2023-11-11T15:09:59.243797-08:00
+custom:
+  Author: colin-rogers-dbt
+  Issue: "1006"
diff --git a/dbt/adapters/bigquery/python_submissions.py b/dbt/adapters/bigquery/python_submissions.py
index 114ebf979..6ea5a5564 100644
--- a/dbt/adapters/bigquery/python_submissions.py
+++ b/dbt/adapters/bigquery/python_submissions.py
@@ -1,3 +1,4 @@
+import uuid
 from typing import Dict, Union
 
 from dbt.events import AdapterLogger
@@ -126,8 +127,7 @@ def _get_job_client(self) -> dataproc_v1.BatchControllerClient:
 
     def _get_batch_id(self) -> str:
         model = self.parsed_model
-        default_batch_id = model["unique_id"].replace(".", "-").replace("_", "-")
-        default_batch_id += str(int(model["created_at"]))
+        default_batch_id = str(uuid.uuid4())
         return model["config"].get("batch_id", default_batch_id)
 
     def _submit_dataproc_job(self) -> Batch:
diff --git a/tests/functional/adapter/test_python_model.py b/tests/functional/adapter/test_python_model.py
index 1e1c2775c..bc5d8c91c 100644
--- a/tests/functional/adapter/test_python_model.py
+++ b/tests/functional/adapter/test_python_model.py
@@ -187,8 +187,6 @@ def model(dbt, spark):
 models:
   - name: python_array_batch_id
     description: A random table with a calculated column defined in python.
-    config:
-      batch_id: '{{ run_started_at.strftime("%Y-%m-%d-%H-%M-%S") }}-python-array'
     columns:
       - name: A
         description: Column A
@@ -216,7 +214,6 @@ def model(dbt, spark):
 """
 
 
-@pytest.mark.skip(reason="Currently failing as run_started_at is the same across dbt runs")
 class TestPythonBatchIdModels:
     @pytest.fixture(scope="class")
     def models(self):