bricksentry 0.1.15__tar.gz → 0.1.18__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bricksentry-0.1.15 → bricksentry-0.1.18}/CHANGELOG.md +23 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/PKG-INFO +24 -9
- {bricksentry-0.1.15 → bricksentry-0.1.18}/README.md +23 -8
- bricksentry-0.1.18/deploy/databricks-app/requirements.txt +1 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/pyproject.toml +1 -1
- bricksentry-0.1.18/scripts/render_demo_image.py +115 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/server.json +2 -2
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/__init__.py +1 -1
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/demo.py +80 -15
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/history.py +8 -4
- bricksentry-0.1.18/src/bricksentry/job_demo.py +71 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/templates/job-check/databricks_template_schema.json +23 -5
- {bricksentry-0.1.15 → bricksentry-0.1.18}/templates/job-check/template/{{.project_name}}/README.md.tmpl +27 -3
- bricksentry-0.1.18/templates/job-check/template/{{.project_name}}/resources/bricksentry_check.job.yml.tmpl +112 -0
- bricksentry-0.1.18/templates/job-check/template/{{.project_name}}/src/bricksentry_demo.py +7 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_0114.py +113 -13
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_demo.py +6 -0
- bricksentry-0.1.15/deploy/databricks-app/requirements.txt +0 -1
- bricksentry-0.1.15/templates/job-check/template/{{.project_name}}/resources/bricksentry_check.job.yml.tmpl +0 -54
- {bricksentry-0.1.15 → bricksentry-0.1.18}/.gitignore +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/CONTRIBUTING.md +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/LICENSE +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/SECURITY.md +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/action.yml +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/deploy/databricks-app/README.md +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/deploy/databricks-app/app.yaml +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/deploy/databricks-app/bricksentry.yml +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/deploy/databricks-app/databricks.yml +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/deploy/databricks-app/grants.sql +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/bricksentry.yml +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/bricksentry_check_my_table.ipynb +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/bricksentry_check_my_table.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/bricksentry_hr_walkthrough.ipynb +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/bricksentry_hr_walkthrough.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/bricksentry_quickstart.ipynb +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/bricksentry_quickstart.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/github-workflow.yml +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/databricks/policy.yml +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/examples/demo/agent_loop_demo.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/scripts/build_notebook.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/__main__.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/agent_tools.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/assess.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/backends/__init__.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/backends/base.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/backends/databricks_backend.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/backends/duckdb_backend.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/changes.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/cli.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/config.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/diff.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/explain.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/github.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/job.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/lineage.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/mcp_server.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/measures.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/models.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/notify.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/policy.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/policy_templates.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/preview.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/py.typed +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/src/bricksentry/report.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/templates/job-check/README.md +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/templates/job-check/template/{{.project_name}}/.gitignore +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/templates/job-check/template/{{.project_name}}/databricks.yml.tmpl +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/templates/job-check/template/{{.project_name}}/src/bricksentry_check.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/conftest.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_0110.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_0112.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_018.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_019.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_assess_policy.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_check_notebook.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_databricks_and_uc.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_diff.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_hr_notebook.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_notebook.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_notify_templates.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_preview_agents.py +0 -0
- {bricksentry-0.1.15 → bricksentry-0.1.18}/tests/test_sample_guard_and_store.py +0 -0
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.18 (2026-11-12)
|
|
4
|
+
|
|
5
|
+
See a block in your own workspace within minutes of `bundle init`.
|
|
6
|
+
|
|
7
|
+
- The job template asks whether to add a **demo job** (default yes). `databricks bundle run bricksentry_demo` creates a 20,000-row demo table in your scratch schema, runs a MERGE whose ON condition compares region case-sensitively (so 25 orders are inserted again as duplicates), then checks that change. The run ends red with BLOCK, the rules that fired and the `RESTORE` that undoes it, plus the Slack alert if you set one up. It writes only to `bricksentry_demo_*` tables.
|
|
8
|
+
- `python -m bricksentry.job_demo`: the task that makes the demo change.
|
|
9
|
+
- Slack is now presented as optional in the template prompt, and the success message lists the next commands.
|
|
10
|
+
- The template README no longer says the trigger is paused in the dev target: the job sets `pause_status: UNPAUSED` on its trigger, which development mode respects.
|
|
11
|
+
|
|
12
|
+
## 0.1.17 (2026-11-12)
|
|
13
|
+
|
|
14
|
+
- `bricksentry demo` now ends with a pipeline commit: a nightly MERGE with a wrong match condition lands, `check-last-change` finds it in the table history (skipping the OPTIMIZE after it), blocks it for 25 duplicate keys, prints the `RESTORE` and applies it. It replaces the old step 2, which only described a rollback because DuckDB has no table history; the demo now keeps versions itself. It still runs offline in about a second and ends with the one-command Databricks setup.
|
|
15
|
+
- The README opens with three steps, each on more real data: the 30-second demo on your laptop (`uvx --from "bricksentry[duckdb]" bricksentry demo`, nothing to install first), checking every change to your tables on Databricks with the bundle template, and stopping bad changes inside your pipeline job, in CI or for AI agents.
|
|
16
|
+
- New demo screenshot, generated from the real output by `scripts/render_demo_image.py`.
|
|
17
|
+
|
|
18
|
+
## 0.1.16 (2026-11-11)
|
|
19
|
+
|
|
20
|
+
From the live test of 0.1.15 on Databricks serverless:
|
|
21
|
+
|
|
22
|
+
- Fix: two checks of the same table at the same time (two jobs watching it, or a job and a notebook) used the same scratch clone names, so one check's cleanup could drop the other's clone mid-diff (`TABLE_OR_VIEW_NOT_FOUND`). Each check now uses its own clone names.
|
|
23
|
+
- The job template's task also sets `disable_auto_optimization: true`. On serverless, auto-optimization retries failed tasks even with `max_retries: 0`, so a `block` was still run twice.
|
|
24
|
+
- The job template now pins the bricksentry version (new `bricksentry_version` prompt, default the current release). Correction to the 0.1.15 release notes: a job deployed with `>=0.1.14` does **not** pick up new releases by itself, because serverless resolves a job's dependencies once and caches them. To upgrade, change the version in the job's YAML and redeploy; the generated README says so.
|
|
25
|
+
|
|
3
26
|
## 0.1.15 (2026-11-11)
|
|
4
27
|
|
|
5
28
|
From the first live test of the 0.1.14 job template on Databricks serverless (deployed with `databricks bundle deploy`, started by the table update trigger and by hand):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: bricksentry
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.18
|
|
4
4
|
Summary: Change-safety for Databricks pipelines and the AI agents that edit them: data diffs, Unity Catalog blast radius, policy verdicts and rollback plans.
|
|
5
5
|
Project-URL: Homepage, https://bricksentry.org
|
|
6
6
|
Project-URL: Documentation, https://bricksentry.org/docs
|
|
@@ -55,22 +55,37 @@ Description-Content-Type: text/markdown
|
|
|
55
55
|
|
|
56
56
|
Change-safety for Databricks pipelines and the AI agents that edit them: know what a change does to your data before it ships.
|
|
57
57
|
|
|
58
|
-
**[Try it in 30 seconds](#try-it-in-30-seconds-
|
|
58
|
+
**[Try it in 30 seconds](#1-try-it-in-30-seconds-on-your-laptop)** · **[Check every change on Databricks](#2-check-every-change-to-your-tables-on-databricks)** · **[Docs: bricksentry.org](https://bricksentry.org)** · **[LinkedIn](https://www.linkedin.com/company/bricksentry-org/)**
|
|
59
59
|
|
|
60
60
|
Built by Brahmareddy, Databricks Champion. Open source under Apache-2.0.
|
|
61
61
|
|
|
62
62
|
*Why the name?* Bricks plus entry. Your tables and pipelines are the bricks of the lakehouse, and every change to them is an entry. bricksentry checks each entry, reports what it does, and stands guard before it lands.
|
|
63
63
|
|
|
64
|
-
##
|
|
64
|
+
## Three steps, each on more real data
|
|
65
|
+
|
|
66
|
+
### 1. Try it in 30 seconds, on your laptop
|
|
65
67
|
|
|
66
68
|
```bash
|
|
67
|
-
|
|
68
|
-
bricksentry demo
|
|
69
|
+
uvx --from "bricksentry[duckdb]" bricksentry demo
|
|
69
70
|
```
|
|
70
71
|
|
|
71
|
-
|
|
72
|
+
(Or `pip install "bricksentry[duckdb]"` and then `bricksentry demo`.) No Databricks account and nothing leaves your machine: it runs on a 20,000-row revenue table in local DuckDB. An AI agent previews a DELETE on a clone (production untouched), gets its pull request **blocked** for a join that duplicates 40 orders, fixes it, and is routed to the owners of a certified table downstream. Then a nightly pipeline commits a bad MERGE anyway: bricksentry finds it in the table history, skips the OPTIMIZE after it, **blocks** it for 25 duplicate keys, prints the `RESTORE` and applies it.
|
|
73
|
+
|
|
74
|
+

|
|
75
|
+
|
|
76
|
+
### 2. Check every change to your tables, on Databricks
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
databricks bundle init https://github.com/bricksentry/bricksentry --template-dir templates/job-check
|
|
80
|
+
cd bricksentry_check && databricks bundle deploy
|
|
81
|
+
databricks bundle run bricksentry_demo # optional: see a block on a demo table in about 2 minutes
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
One command and a few prompts give you a serverless job that runs after every commit to the tables you pick: the same check as the demo's last step, on your data, with the `RESTORE` that undoes a bad change and an optional Slack alert. The optional demo job writes a known-bad MERGE to a demo table in your scratch schema and checks it, so your first run ends red with a block. [Details below](#check-every-pipeline-run-automatically-asset-bundle).
|
|
85
|
+
|
|
86
|
+
### 3. Stop bad changes before anything reads them
|
|
72
87
|
|
|
73
|
-
|
|
88
|
+
Put the check inside your pipeline job, right after the task that writes the table: a `block` fails that task and the tasks downstream don't run. Or run it on pull requests with the [GitHub Action](#for-data-engineers-cli-and-ci), and give AI agents the same checks through the [MCP server](#for-ai-agents-the-mcp-server).
|
|
74
89
|
|
|
75
90
|
## Run it in your Databricks workspace
|
|
76
91
|
|
|
@@ -83,14 +98,14 @@ Point the [check-my-table notebook](examples/databricks/bricksentry_check_my_tab
|
|
|
83
98
|
|
|
84
99
|
If it shows you something, please [share what it found](https://github.com/bricksentry/bricksentry/issues/new?template=it_caught_this.yml) (no table names needed). Real results decide what gets built next.
|
|
85
100
|
|
|
86
|
-
### Check every pipeline run automatically (Asset Bundle
|
|
101
|
+
### Check every pipeline run automatically (Asset Bundle)
|
|
87
102
|
|
|
88
103
|
```bash
|
|
89
104
|
databricks bundle init https://github.com/bricksentry/bricksentry --template-dir templates/job-check
|
|
90
105
|
cd bricksentry_check && databricks bundle deploy -t prod
|
|
91
106
|
```
|
|
92
107
|
|
|
93
|
-
Answer a few prompts (tables to watch, SQL warehouse, scratch schema, policy, optional Slack webhook secret). You get a serverless job with a **table update trigger**: whenever a watched table changes, it checks the change, sends the alert and logs the `RESTORE` that undoes it. To stop downstream tasks when a change is bad, add the same check as a task inside your pipeline job, right after the write (the generated README shows the YAML): with `--since {{job.start_time.iso_datetime}}` it checks everything the run wrote, and a `block` verdict fails the task so the tasks after it don't run.
|
|
108
|
+
Answer a few prompts (tables to watch, key columns if they have no primary key, SQL warehouse, scratch schema, whether to add the demo job, policy, optional Slack webhook secret). You get a serverless job with a **table update trigger**: whenever a watched table changes, it checks the change, sends the alert and logs the `RESTORE` that undoes it. To stop downstream tasks when a change is bad, add the same check as a task inside your pipeline job, right after the write (the generated README shows the YAML): with `--since {{job.start_time.iso_datetime}}` it checks everything the run wrote, and a `block` verdict fails the task so the tasks after it don't run.
|
|
94
109
|
|
|
95
110
|
From the command line, the same check is one command:
|
|
96
111
|
|
|
@@ -8,22 +8,37 @@
|
|
|
8
8
|
|
|
9
9
|
Change-safety for Databricks pipelines and the AI agents that edit them: know what a change does to your data before it ships.
|
|
10
10
|
|
|
11
|
-
**[Try it in 30 seconds](#try-it-in-30-seconds-
|
|
11
|
+
**[Try it in 30 seconds](#1-try-it-in-30-seconds-on-your-laptop)** · **[Check every change on Databricks](#2-check-every-change-to-your-tables-on-databricks)** · **[Docs: bricksentry.org](https://bricksentry.org)** · **[LinkedIn](https://www.linkedin.com/company/bricksentry-org/)**
|
|
12
12
|
|
|
13
13
|
Built by Brahmareddy, Databricks Champion. Open source under Apache-2.0.
|
|
14
14
|
|
|
15
15
|
*Why the name?* Bricks plus entry. Your tables and pipelines are the bricks of the lakehouse, and every change to them is an entry. bricksentry checks each entry, reports what it does, and stands guard before it lands.
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## Three steps, each on more real data
|
|
18
|
+
|
|
19
|
+
### 1. Try it in 30 seconds, on your laptop
|
|
18
20
|
|
|
19
21
|
```bash
|
|
20
|
-
|
|
21
|
-
bricksentry demo
|
|
22
|
+
uvx --from "bricksentry[duckdb]" bricksentry demo
|
|
22
23
|
```
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
(Or `pip install "bricksentry[duckdb]"` and then `bricksentry demo`.) No Databricks account and nothing leaves your machine: it runs on a 20,000-row revenue table in local DuckDB. An AI agent previews a DELETE on a clone (production untouched), gets its pull request **blocked** for a join that duplicates 40 orders, fixes it, and is routed to the owners of a certified table downstream. Then a nightly pipeline commits a bad MERGE anyway: bricksentry finds it in the table history, skips the OPTIMIZE after it, **blocks** it for 25 duplicate keys, prints the `RESTORE` and applies it.
|
|
26
|
+
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
### 2. Check every change to your tables, on Databricks
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
databricks bundle init https://github.com/bricksentry/bricksentry --template-dir templates/job-check
|
|
33
|
+
cd bricksentry_check && databricks bundle deploy
|
|
34
|
+
databricks bundle run bricksentry_demo # optional: see a block on a demo table in about 2 minutes
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
One command and a few prompts give you a serverless job that runs after every commit to the tables you pick: the same check as the demo's last step, on your data, with the `RESTORE` that undoes a bad change and an optional Slack alert. The optional demo job writes a known-bad MERGE to a demo table in your scratch schema and checks it, so your first run ends red with a block. [Details below](#check-every-pipeline-run-automatically-asset-bundle).
|
|
38
|
+
|
|
39
|
+
### 3. Stop bad changes before anything reads them
|
|
25
40
|
|
|
26
|
-
|
|
41
|
+
Put the check inside your pipeline job, right after the task that writes the table: a `block` fails that task and the tasks downstream don't run. Or run it on pull requests with the [GitHub Action](#for-data-engineers-cli-and-ci), and give AI agents the same checks through the [MCP server](#for-ai-agents-the-mcp-server).
|
|
27
42
|
|
|
28
43
|
## Run it in your Databricks workspace
|
|
29
44
|
|
|
@@ -36,14 +51,14 @@ Point the [check-my-table notebook](examples/databricks/bricksentry_check_my_tab
|
|
|
36
51
|
|
|
37
52
|
If it shows you something, please [share what it found](https://github.com/bricksentry/bricksentry/issues/new?template=it_caught_this.yml) (no table names needed). Real results decide what gets built next.
|
|
38
53
|
|
|
39
|
-
### Check every pipeline run automatically (Asset Bundle
|
|
54
|
+
### Check every pipeline run automatically (Asset Bundle)
|
|
40
55
|
|
|
41
56
|
```bash
|
|
42
57
|
databricks bundle init https://github.com/bricksentry/bricksentry --template-dir templates/job-check
|
|
43
58
|
cd bricksentry_check && databricks bundle deploy -t prod
|
|
44
59
|
```
|
|
45
60
|
|
|
46
|
-
Answer a few prompts (tables to watch, SQL warehouse, scratch schema, policy, optional Slack webhook secret). You get a serverless job with a **table update trigger**: whenever a watched table changes, it checks the change, sends the alert and logs the `RESTORE` that undoes it. To stop downstream tasks when a change is bad, add the same check as a task inside your pipeline job, right after the write (the generated README shows the YAML): with `--since {{job.start_time.iso_datetime}}` it checks everything the run wrote, and a `block` verdict fails the task so the tasks after it don't run.
|
|
61
|
+
Answer a few prompts (tables to watch, key columns if they have no primary key, SQL warehouse, scratch schema, whether to add the demo job, policy, optional Slack webhook secret). You get a serverless job with a **table update trigger**: whenever a watched table changes, it checks the change, sends the alert and logs the `RESTORE` that undoes it. To stop downstream tasks when a change is bad, add the same check as a task inside your pipeline job, right after the write (the generated README shows the YAML): with `--since {{job.start_time.iso_datetime}}` it checks everything the run wrote, and a `block` verdict fails the task so the tasks after it don't run.
|
|
47
62
|
|
|
48
63
|
From the command line, the same check is one command:
|
|
49
64
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
bricksentry[databricks,mcp]==0.1.18
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "bricksentry"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.18"
|
|
8
8
|
description = "Change-safety for Databricks pipelines and the AI agents that edit them: data diffs, Unity Catalog blast radius, policy verdicts and rollback plans."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""Render `bricksentry demo` output as docs/images/demo.png (terminal-window style).
|
|
2
|
+
|
|
3
|
+
python scripts/render_demo_image.py # needs: playwright + chromium
|
|
4
|
+
|
|
5
|
+
Runs the demo with colors on, converts the ANSI output to HTML and screenshots it.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import html
|
|
11
|
+
import io
|
|
12
|
+
import re
|
|
13
|
+
import sys
|
|
14
|
+
from contextlib import redirect_stdout
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
OUT = Path(__file__).resolve().parents[1] / "docs" / "images" / "demo.png"
|
|
18
|
+
COMMAND = 'uvx --from "bricksentry[duckdb]" bricksentry demo'
|
|
19
|
+
|
|
20
|
+
BASIC = {30: "#1e293b", 31: "#e5484d", 32: "#22c55e", 33: "#e5a93b", 34: "#60a5fa", 35: "#c084fc",
|
|
21
|
+
36: "#22c4c4", 37: "#e2e8f0", 90: "#7d8794", 91: "#f87171", 92: "#4ade80", 93: "#facc15",
|
|
22
|
+
94: "#93c5fd", 95: "#e879f9", 96: "#67e8f9", 97: "#ffffff"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def capture() -> str:
|
|
26
|
+
import click
|
|
27
|
+
|
|
28
|
+
from bricksentry.demo import run_demo
|
|
29
|
+
|
|
30
|
+
buf = io.StringIO()
|
|
31
|
+
orig = click.echo
|
|
32
|
+
|
|
33
|
+
def echo(message=None, **kw): # keep ANSI codes when writing to a StringIO
|
|
34
|
+
kw["color"] = True
|
|
35
|
+
return orig(message, **kw)
|
|
36
|
+
|
|
37
|
+
click.echo = echo
|
|
38
|
+
try:
|
|
39
|
+
with redirect_stdout(buf):
|
|
40
|
+
run_demo()
|
|
41
|
+
finally:
|
|
42
|
+
click.echo = orig
|
|
43
|
+
return buf.getvalue()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def ansi_to_html(text: str) -> str:
|
|
47
|
+
out: list[str] = []
|
|
48
|
+
state: dict[str, str] = {}
|
|
49
|
+
pos = 0
|
|
50
|
+
for m in re.finditer(r"\x1b\[([0-9;]*)m", text):
|
|
51
|
+
out.append(_span(text[pos:m.start()], state))
|
|
52
|
+
codes = [int(c) for c in m.group(1).split(";") if c] or [0]
|
|
53
|
+
i = 0
|
|
54
|
+
while i < len(codes):
|
|
55
|
+
c = codes[i]
|
|
56
|
+
if c == 0:
|
|
57
|
+
state = {}
|
|
58
|
+
elif c == 1:
|
|
59
|
+
state["font-weight"] = "700"
|
|
60
|
+
elif c == 4:
|
|
61
|
+
state["text-decoration"] = "underline"
|
|
62
|
+
elif c in BASIC:
|
|
63
|
+
state["color"] = BASIC[c]
|
|
64
|
+
elif c in (38, 48) and codes[i + 1:i + 2] == [2]:
|
|
65
|
+
r, g, b = codes[i + 2:i + 5]
|
|
66
|
+
key = "color" if c == 38 else "background"
|
|
67
|
+
state[key] = f"rgb({r},{g},{b})"
|
|
68
|
+
if c == 48:
|
|
69
|
+
state["padding"] = "0 2px"
|
|
70
|
+
i += 4
|
|
71
|
+
i += 1
|
|
72
|
+
pos = m.end()
|
|
73
|
+
out.append(_span(text[pos:], state))
|
|
74
|
+
return "".join(out)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _span(s: str, state: dict[str, str]) -> str:
|
|
78
|
+
if not s:
|
|
79
|
+
return ""
|
|
80
|
+
s = html.escape(s)
|
|
81
|
+
if not state:
|
|
82
|
+
return s
|
|
83
|
+
return f'<span style="{";".join(f"{k}:{v}" for k, v in state.items())}">{s}</span>'
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
PAGE = """<!doctype html><html><head><meta charset="utf-8"><style>
|
|
87
|
+
body{{margin:0;background:transparent}}
|
|
88
|
+
.win{{width:1180px;background:#0f172a;border-radius:12px;overflow:hidden;font:15px/1.6 'DejaVu Sans Mono',monospace;color:#e2e8f0}}
|
|
89
|
+
.bar{{background:#1a2740;padding:10px 16px;display:flex;gap:8px;align-items:center;color:#94a3b8;font-size:13px}}
|
|
90
|
+
.dot{{width:12px;height:12px;border-radius:50%;display:inline-block}}
|
|
91
|
+
pre{{margin:0;padding:18px 24px 26px;white-space:pre-wrap;font:inherit}}
|
|
92
|
+
.cmd{{color:#a5b4fc}}
|
|
93
|
+
</style></head><body><div class="win"><div class="bar"><span class="dot" style="background:#ff5f57"></span>
|
|
94
|
+
<span class="dot" style="background:#e5a93b"></span><span class="dot" style="background:#28c840"></span>
|
|
95
|
+
<span style="margin-left:12px">bricksentry demo</span></div><pre><span class="cmd">$ {cmd}</span>
|
|
96
|
+
|
|
97
|
+
{body}</pre></div></body></html>"""
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def main() -> None:
|
|
101
|
+
from playwright.sync_api import sync_playwright
|
|
102
|
+
|
|
103
|
+
body = ansi_to_html(capture().rstrip("\n"))
|
|
104
|
+
page_html = PAGE.format(cmd=html.escape(COMMAND), body=body)
|
|
105
|
+
with sync_playwright() as p:
|
|
106
|
+
b = p.chromium.launch()
|
|
107
|
+
page = b.new_page(device_scale_factor=2, viewport={"width": 1180, "height": 800})
|
|
108
|
+
page.set_content(page_html)
|
|
109
|
+
page.locator(".win").screenshot(path=str(OUT), omit_background=True)
|
|
110
|
+
b.close()
|
|
111
|
+
print(f"wrote {OUT}", file=sys.stderr)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
if __name__ == "__main__":
|
|
115
|
+
main()
|
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
"source": "github"
|
|
9
9
|
},
|
|
10
10
|
"websiteUrl": "https://bricksentry.org",
|
|
11
|
-
"version": "0.1.
|
|
11
|
+
"version": "0.1.18",
|
|
12
12
|
"packages": [
|
|
13
13
|
{
|
|
14
14
|
"registryType": "pypi",
|
|
15
15
|
"identifier": "bricksentry-mcp",
|
|
16
|
-
"version": "0.1.
|
|
16
|
+
"version": "0.1.18",
|
|
17
17
|
"runtimeHint": "uvx",
|
|
18
18
|
"transport": {
|
|
19
19
|
"type": "stdio"
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
"""``bricksentry demo``: the whole
|
|
1
|
+
"""``bricksentry demo``: the whole safety loop in about 30 seconds, locally.
|
|
2
2
|
|
|
3
3
|
Runs on an in-memory DuckDB database with a 20,000-row orders table and a small
|
|
4
4
|
lineage graph, so it needs no Databricks workspace and no data leaves your machine.
|
|
5
|
+
Steps 1-4: an AI agent previews a delete, gets its pull request blocked, fixes it.
|
|
6
|
+
Step 5: a bad pipeline commit lands anyway; ``check-last-change`` catches it from the
|
|
7
|
+
table history and the undo is applied. DuckDB has no table history, so the demo keeps
|
|
8
|
+
versions itself; on Databricks the same check reads Delta history.
|
|
5
9
|
"""
|
|
6
10
|
|
|
7
11
|
from __future__ import annotations
|
|
@@ -16,6 +20,7 @@ from bricksentry.assess import Sentry
|
|
|
16
20
|
from bricksentry.config import Config
|
|
17
21
|
|
|
18
22
|
DOCS_URL = "https://bricksentry.org/docs"
|
|
23
|
+
BUNDLE_CMD = "databricks bundle init https://github.com/bricksentry/bricksentry --template-dir templates/job-check"
|
|
19
24
|
|
|
20
25
|
CONFIG: dict[str, Any] = {
|
|
21
26
|
"sandbox_schema": "sandbox",
|
|
@@ -65,6 +70,13 @@ FROM sales.orders
|
|
|
65
70
|
|
|
66
71
|
DELETE_SQL = "DELETE FROM sales.orders WHERE status <> 'paid'"
|
|
67
72
|
|
|
73
|
+
# A nightly pipeline whose MERGE matches on the wrong column: 25 existing orders come
|
|
74
|
+
# back as new rows (duplicate keys) instead of updating in place.
|
|
75
|
+
BAD_MERGE_SQL = """
|
|
76
|
+
INSERT INTO sales.orders
|
|
77
|
+
SELECT order_id, region, round(amount * 1.05, 2), status FROM sales.orders WHERE order_id % 800 = 0
|
|
78
|
+
"""
|
|
79
|
+
|
|
68
80
|
# Same verdict colors as the website: pass green, warn amber, needs_human orange, block red.
|
|
69
81
|
_COLORS: dict[str, Any] = {"pass": (29, 160, 100), "warn": (214, 160, 40), "needs_human": (235, 115, 40), "block": (210, 40, 50)}
|
|
70
82
|
|
|
@@ -97,12 +109,45 @@ def _hits(rule_hits: list[dict[str, Any]]) -> None:
|
|
|
97
109
|
_line(click.style(f"[{h['verdict']}]", fg=_COLORS.get(h["verdict"], "white")) + f" {h['rule']}: {h['reason']}")
|
|
98
110
|
|
|
99
111
|
|
|
112
|
+
def _versioned_backend():
|
|
113
|
+
"""DuckDB plus a minimal table history (snapshots), so step 5 can run offline."""
|
|
114
|
+
from bricksentry.backends.duckdb_backend import DuckDBBackend
|
|
115
|
+
|
|
116
|
+
class VersionedDuckDB(DuckDBBackend):
|
|
117
|
+
supports_time_travel = True
|
|
118
|
+
|
|
119
|
+
def __init__(self) -> None:
|
|
120
|
+
super().__init__()
|
|
121
|
+
self._history: list[dict[str, Any]] = []
|
|
122
|
+
|
|
123
|
+
def commit(self, table: str, operation: str, sql: str | None = None) -> int:
|
|
124
|
+
if sql:
|
|
125
|
+
self.execute(sql)
|
|
126
|
+
v = len(self._history)
|
|
127
|
+
self.execute(f"CREATE SCHEMA IF NOT EXISTS _history; "
|
|
128
|
+
f"CREATE OR REPLACE TABLE _history.{table.replace('.', '__')}_v{v} AS SELECT * FROM {table}")
|
|
129
|
+
self._history.append({"version": v, "operation": operation, "timestamp": ""})
|
|
130
|
+
return v
|
|
131
|
+
|
|
132
|
+
def history(self, table: str, limit: int = 50) -> list[dict[str, Any]]:
|
|
133
|
+
return list(reversed(self._history))[:limit]
|
|
134
|
+
|
|
135
|
+
def clone_version(self, source: str, target: str, version: int) -> None:
|
|
136
|
+
self.execute(f"CREATE OR REPLACE TABLE {target} AS "
|
|
137
|
+
f"SELECT * FROM _history.{source.replace('.', '__')}_v{int(version)}")
|
|
138
|
+
|
|
139
|
+
def restore(self, table: str, version: int) -> int:
|
|
140
|
+
self.execute(f"CREATE OR REPLACE TABLE {table} AS "
|
|
141
|
+
f"SELECT * FROM _history.{table.replace('.', '__')}_v{int(version)}")
|
|
142
|
+
return self.commit(table, "RESTORE")
|
|
143
|
+
|
|
144
|
+
return VersionedDuckDB()
|
|
145
|
+
|
|
146
|
+
|
|
100
147
|
def run_demo(report_path: str | None = None) -> dict[str, Any]:
|
|
101
148
|
"""Run the demo, print it, and return the key results (used by tests)."""
|
|
102
149
|
try:
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
backend = DuckDBBackend()
|
|
150
|
+
backend = _versioned_backend()
|
|
106
151
|
except ImportError:
|
|
107
152
|
# Raised without the inner traceback so a notebook shows one clear line.
|
|
108
153
|
raise ImportError(
|
|
@@ -112,6 +157,7 @@ def run_demo(report_path: str | None = None) -> dict[str, Any]:
|
|
|
112
157
|
|
|
113
158
|
backend.execute("CREATE SCHEMA sales; CREATE SCHEMA sales_pr_42")
|
|
114
159
|
backend.execute(SEED_SQL)
|
|
160
|
+
backend.commit("sales.orders", "CREATE TABLE AS SELECT")
|
|
115
161
|
sentry = Sentry(Config.from_dict({**CONFIG, "assessment_store": False}), backend=backend)
|
|
116
162
|
previous = agent_tools._SENTRY
|
|
117
163
|
agent_tools.set_sentry(sentry)
|
|
@@ -121,7 +167,8 @@ def run_demo(report_path: str | None = None) -> dict[str, Any]:
|
|
|
121
167
|
click.style("bricksentry demo", bold=True)
|
|
122
168
|
+ click.style(" · 20,000-row sales.orders on local DuckDB; nothing leaves your machine", fg="bright_black")
|
|
123
169
|
)
|
|
124
|
-
click.echo(click.style("An AI agent
|
|
170
|
+
click.echo(click.style("An AI agent changes a revenue table, then a pipeline does. Watch what bricksentry says.",
|
|
171
|
+
fg="bright_black"))
|
|
125
172
|
total = 5
|
|
126
173
|
|
|
127
174
|
_step(1, total, "Before editing: what depends on sales.orders?", "whats_downstream")
|
|
@@ -146,12 +193,7 @@ def run_demo(report_path: str | None = None) -> dict[str, Any]:
|
|
|
146
193
|
walk("sales.orders", "")
|
|
147
194
|
results["downstream_count"] = ds["count"]
|
|
148
195
|
|
|
149
|
-
_step(2, total, "
|
|
150
|
-
_line("On Databricks this returns the exact command, e.g.", fg="bright_black")
|
|
151
|
-
_line("RESTORE TABLE `main`.`sales`.`orders` TO VERSION AS OF 41")
|
|
152
|
-
_line("(DuckDB has no table history, so the demo skips it.)", fg="bright_black")
|
|
153
|
-
|
|
154
|
-
_step(3, total, "Agent wants to delete refunded orders. It previews first", "preview_write")
|
|
196
|
+
_step(2, total, "Agent wants to delete refunded orders. It previews first", "preview_write")
|
|
155
197
|
_line(DELETE_SQL, fg="bright_black")
|
|
156
198
|
p = agent_tools.preview_write(DELETE_SQL, "sales.orders")
|
|
157
199
|
_line(_verdict(p["verdict"]) + " " + _short(p["tables"][0]["summary"]))
|
|
@@ -161,7 +203,7 @@ def run_demo(report_path: str | None = None) -> dict[str, Any]:
|
|
|
161
203
|
results["preview_verdict"] = p["verdict"]
|
|
162
204
|
results["production_rows_after_preview"] = prod_rows
|
|
163
205
|
|
|
164
|
-
_step(
|
|
206
|
+
_step(3, total, "Agent opens PR #42 instead. Its join has a fan-out bug", "assess_change")
|
|
165
207
|
backend.execute(BUGGY_SQL)
|
|
166
208
|
a = agent_tools.assess_change(["sales.orders"], pr="42")
|
|
167
209
|
_line(_verdict(a["verdict"]) + f" risk {a['risk_tier']}")
|
|
@@ -170,7 +212,7 @@ def run_demo(report_path: str | None = None) -> dict[str, Any]:
|
|
|
170
212
|
_line("next_step: " + gate["next_step"], fg="cyan")
|
|
171
213
|
results["buggy_verdict"] = a["verdict"]
|
|
172
214
|
|
|
173
|
-
_step(
|
|
215
|
+
_step(4, total, "Agent fixes the join, rebuilds and checks again", "assess_change → safe_to_ship")
|
|
174
216
|
backend.execute(FIXED_SQL)
|
|
175
217
|
a2 = agent_tools.assess_change(["sales.orders"], pr="42")
|
|
176
218
|
explanation = agent_tools.explain_diff(assessment_id=a2["id"])["explanations"]["sales.orders"]
|
|
@@ -181,6 +223,26 @@ def run_demo(report_path: str | None = None) -> dict[str, Any]:
|
|
|
181
223
|
results["fixed_verdict"] = a2["verdict"]
|
|
182
224
|
results["notify"] = gate2["notify"]
|
|
183
225
|
|
|
226
|
+
_step(5, total, "Later, a nightly pipeline commits a bad MERGE", "check-last-change")
|
|
227
|
+
backend.commit("sales.orders", "MERGE", BAD_MERGE_SQL)
|
|
228
|
+
backend.commit("sales.orders", "OPTIMIZE")
|
|
229
|
+
a3, change = sentry.check_change("sales.orders")
|
|
230
|
+
t3 = a3.tables[0]
|
|
231
|
+
_line(f"History: v2 OPTIMIZE (skipped, no data change) · v1 MERGE, checked against v{change.base_version}",
|
|
232
|
+
fg="bright_black")
|
|
233
|
+
_line(_verdict(a3.verdict.value) + " " + _short(t3.summary.split(": ", 1)[-1]))
|
|
234
|
+
_hits([{"verdict": h.verdict.value, "rule": h.rule, "reason": h.reason} for h in t3.hits
|
|
235
|
+
if h.verdict.value in ("block", "needs_human")])
|
|
236
|
+
undo = t3.rollback.replace('"', "")
|
|
237
|
+
_line("Undo: " + undo, fg="cyan")
|
|
238
|
+
backend.restore("sales.orders", change.base_version)
|
|
239
|
+
rows = backend.scalar("SELECT COUNT(*) FROM sales.orders")
|
|
240
|
+
dups = backend.scalar("SELECT COUNT(*) - COUNT(DISTINCT order_id) FROM sales.orders")
|
|
241
|
+
_line(f"Applied the undo: sales.orders is back to {rows:,} rows, {dups} duplicate keys.", fg="green")
|
|
242
|
+
results["commit_verdict"] = a3.verdict.value
|
|
243
|
+
results["undo"] = undo
|
|
244
|
+
results["rows_after_undo"] = rows
|
|
245
|
+
|
|
184
246
|
if report_path:
|
|
185
247
|
from bricksentry.report import to_markdown
|
|
186
248
|
|
|
@@ -189,8 +251,11 @@ def run_demo(report_path: str | None = None) -> dict[str, Any]:
|
|
|
189
251
|
click.echo(f"Wrote {report_path}: the pull-request comment a reviewer would see.")
|
|
190
252
|
|
|
191
253
|
click.echo()
|
|
192
|
-
click.echo(click.style("The agent was stopped
|
|
193
|
-
|
|
254
|
+
click.echo(click.style("The agent was stopped, fixed its change and handed the call to the owners. "
|
|
255
|
+
"The bad commit was caught and undone.", bold=True))
|
|
256
|
+
click.echo("On Databricks, check every change to your tables with one command:")
|
|
257
|
+
click.echo(" " + click.style(BUNDLE_CMD, fg="cyan"))
|
|
258
|
+
click.echo("Docs: " + click.style(DOCS_URL, underline=True))
|
|
194
259
|
finally:
|
|
195
260
|
agent_tools.set_sentry(previous)
|
|
196
261
|
return results
|
|
@@ -9,6 +9,7 @@ version after), diffs them, runs lineage and policy, and returns the assessment
|
|
|
9
9
|
from __future__ import annotations
|
|
10
10
|
|
|
11
11
|
import re
|
|
12
|
+
import uuid
|
|
12
13
|
from dataclasses import dataclass
|
|
13
14
|
from datetime import datetime, timezone
|
|
14
15
|
from typing import TYPE_CHECKING, Any
|
|
@@ -123,9 +124,9 @@ def pick_change(
|
|
|
123
124
|
str(h.get("timestamp") or ""), [h["version"]])
|
|
124
125
|
|
|
125
126
|
|
|
126
|
-
def _scratch_name(schema: str, table: str, version: int) -> str:
|
|
127
|
+
def _scratch_name(schema: str, table: str, version: int, run: str) -> str:
|
|
127
128
|
short = re.sub(r"[^A-Za-z0-9_]", "_", table.split(".")[-1])[:40]
|
|
128
|
-
return f"{schema}.bs_check_{short}_v{version}"
|
|
129
|
+
return f"{schema}.bs_check_{short}_v{version}_{run}"
|
|
129
130
|
|
|
130
131
|
|
|
131
132
|
def check_change(
|
|
@@ -154,8 +155,11 @@ def check_change(
|
|
|
154
155
|
"or add a PRIMARY KEY constraint to the table.")
|
|
155
156
|
|
|
156
157
|
scratch = sentry.config.sandbox_schema
|
|
157
|
-
|
|
158
|
-
|
|
158
|
+
# Unique per check, so two checks of the same table at the same time (two jobs, or a job
|
|
159
|
+
# and a notebook) never drop each other's clones.
|
|
160
|
+
run = uuid.uuid4().hex[:8]
|
|
161
|
+
base = _scratch_name(scratch, table, change.base_version, run)
|
|
162
|
+
cand = _scratch_name(scratch, table, change.version, run)
|
|
159
163
|
db.ensure_schema(scratch)
|
|
160
164
|
try:
|
|
161
165
|
for target, v in ((base, change.base_version), (cand, change.version)):
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Make a known-bad change to a demo table, so the first bricksentry check you see is a block.
|
|
2
|
+
|
|
3
|
+
Used by the ``bricksentry_demo`` job in the bundle template (``templates/job-check``)::
|
|
4
|
+
|
|
5
|
+
python -m bricksentry.job_demo --table main.bricksentry_scratch.bricksentry_demo_orders --warehouse-id abc123
|
|
6
|
+
|
|
7
|
+
It (re)creates a 20,000-row orders table, then runs a MERGE whose ON condition compares
|
|
8
|
+
region case-sensitively ('apac' vs 'APAC'). Nothing matches, so 25 orders that should have
|
|
9
|
+
been updated are inserted again as duplicates. The next task checks that change.
|
|
10
|
+
Only the table named by ``--table`` is written to.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import argparse
|
|
16
|
+
import os
|
|
17
|
+
import re
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
SEED_SQL = """CREATE OR REPLACE TABLE {t} AS
|
|
21
|
+
SELECT id AS order_id,
|
|
22
|
+
element_at(array('APAC', 'EMEA', 'AMER'), CAST(1 + id % 3 AS INT)) AS region,
|
|
23
|
+
CAST(round(20 + (id % 97) * 3.1, 2) AS DOUBLE) AS amount,
|
|
24
|
+
CASE WHEN id % 10 = 0 THEN 'refunded' ELSE 'paid' END AS status
|
|
25
|
+
FROM range(1, 20001)"""
|
|
26
|
+
|
|
27
|
+
# Late price corrections for 25 orders. The bug: the source has region in lower case and
|
|
28
|
+
# the ON condition compares it case-sensitively, so no row matches and all 25 are inserted.
|
|
29
|
+
BAD_MERGE_SQL = """MERGE INTO {t} AS t
|
|
30
|
+
USING (
|
|
31
|
+
SELECT order_id, lower(region) AS region, round(amount * 1.05, 2) AS amount, status
|
|
32
|
+
FROM {t} WHERE order_id % 800 = 0
|
|
33
|
+
) AS s
|
|
34
|
+
ON t.order_id = s.order_id AND t.region = s.region
|
|
35
|
+
WHEN MATCHED THEN UPDATE SET t.amount = s.amount
|
|
36
|
+
WHEN NOT MATCHED THEN INSERT (order_id, region, amount, status)
|
|
37
|
+
VALUES (s.order_id, upper(s.region), s.amount, s.status)"""
|
|
38
|
+
|
|
39
|
+
_NAME = re.compile(r"[A-Za-z0-9_]+\.[A-Za-z0-9_]+\.[A-Za-z0-9_]+")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def run(argv: list[str] | None = None, backend: Any = None, out=print) -> int:
|
|
43
|
+
p = argparse.ArgumentParser(prog="python -m bricksentry.job_demo", description=__doc__.split("\n\n")[0])
|
|
44
|
+
p.add_argument("--table", required=True, help="catalog.schema.table for the demo (created or replaced)")
|
|
45
|
+
p.add_argument("--warehouse-id", default=os.environ.get("DATABRICKS_WAREHOUSE_ID"))
|
|
46
|
+
args = p.parse_args(argv)
|
|
47
|
+
if not _NAME.fullmatch(args.table):
|
|
48
|
+
raise SystemExit(f"--table must be catalog.schema.table, got {args.table!r}")
|
|
49
|
+
if not args.table.split(".")[-1].startswith("bricksentry_demo"):
|
|
50
|
+
raise SystemExit("For safety the demo only writes to a table whose name starts with bricksentry_demo.")
|
|
51
|
+
if backend is None: # pragma: no cover - needs Databricks
|
|
52
|
+
from bricksentry.backends.databricks_backend import DatabricksBackend
|
|
53
|
+
|
|
54
|
+
backend = DatabricksBackend(warehouse_id=args.warehouse_id)
|
|
55
|
+
t = backend.qualified(args.table)
|
|
56
|
+
backend.ensure_schema(".".join(args.table.split(".")[:2]))
|
|
57
|
+
backend.execute(SEED_SQL.format(t=t))
|
|
58
|
+
out(f"Created {args.table}: 20,000 orders, one row per order_id.")
|
|
59
|
+
backend.execute(BAD_MERGE_SQL.format(t=t))
|
|
60
|
+
out("Ran a MERGE with a bug: its ON condition compares region case-sensitively ('apac' vs 'APAC'),")
|
|
61
|
+
out("so 25 orders it should have updated were inserted again as duplicates.")
|
|
62
|
+
out("The next task checks this change. Expect BLOCK (duplicate-keys), and the RESTORE that undoes it.")
|
|
63
|
+
return 0
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def main(argv: list[str] | None = None) -> None: # pragma: no cover - thin wrapper
|
|
67
|
+
run(argv) # no sys.exit on success: Databricks marks SystemExit(0) as a failed task
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
if __name__ == "__main__": # pragma: no cover
|
|
71
|
+
main()
|