在第一部分我們探討了 Dataform CompilationResult 和 WorkflowInvocation 物件。接下來讓我們快速看一下一些我覺得有用的其他 API 端點.
工作區管理 - delete_workspace 和create_workspace
delete_workspace 是大型團隊使用的 Dataform 實例所必需的,因為它允許工作區在合併後自動刪除,節省了每半年一次的「大家能否告訴我哪些工作區已經完成了?」的通訊。這可以整合到 GitHub Actions 等 CI/CD 工具中。
from google.cloud import dataform_v1
client = dataform_v1.DataformClient()
workspace_path = client.workspace_path(
PROJECT_ID,
REGION,
REPOSITORY_ID,
WORKSPACE_ID,
)
try:
operation = client.delete_workspace(name=workspace_path)
另外還有create_workspace - 非常有用,可在 Dataform UI 外的新分支上進行程式碼變更時自動建立工作空間.
Git 操作 - push_git_changes 和 pull_git_changes
另一組有用的端點,允許在 Dataform UI 外自動套用程式碼變更:
from google.cloud import dataform_v1
client = dataform_v1.DataformClient()
workspace_path = client.workspace_path(
project_id,
region,
repository_id,
workspace_id,
)
# ------------------------------------------------------------
# 1. Pull latest changes from Git into the workspace
# ------------------------------------------------------------
print("Pulling latest Git changes into workspace...")
pull_request = dataform_v1.PullGitChangesRequest(
name=workspace_path
)
pull_operation = client.pull_git_changes(request=pull_request)
print("Pull initiated:", pull_operation.operation.name)
# Optional: wait for completion (simplified polling)
pull_result = pull_operation.result()
print("Pull completed successfully")
# ------------------------------------------------------------
# (Optional) 2. Make programmatic changes here
# ------------------------------------------------------------
# e.g. update config blocks, inject variables, generate models, format SQL, etc.
print("Applying automated workspace changes...")
# ------------------------------------------------------------
# 3. Push workspace changes back to Git
# ------------------------------------------------------------
print("Pushing workspace changes to Git...")
push_request = dataform_v1.PushGitChangesRequest(
name=workspace_path
)
push_operation = client.push_git_changes(request=push_request)
push_result = push_operation.result()
print("Push completed successfully")
print("Workspace successfully synced with Git")












