@mriekki/lean 0.4.2 → 0.4.4

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.
@@ -0,0 +1,397 @@
1
+ # Database Learning Workbench
2
+
3
+ The Database Learning Workbench is the reusable Lean learning contract behind `/db-lab`. It makes database behavior that frameworks and ORMs often hide visible enough to inspect, explain, debug, and defend.
4
+
5
+ It is project-aware rather than a generic SQL course: use the **current** repository's domain, schema, migrations, ORM mappings, queries, and safe development/test database evidence as the curriculum.
6
+
7
+ Learning remains optional and never changes engineering delivery state.
8
+
9
+ ## Quick start
10
+
11
+ Use `/coach` for explanation, adaptive questioning, and gap assessment. Use `/db-lab` when the learning needs concrete database evidence.
12
+
13
+ ```text
14
+ /coach database design
15
+ /db-lab
16
+ /db-lab schema
17
+ /db-lab changes <work-item>
18
+ /db-lab trace <query/file/symbol>
19
+ /db-lab plan <query>
20
+ /db-lab debug <symptom/query>
21
+ /db-lab migration <migration>
22
+ /db-lab interview database
23
+ ```
24
+
25
+ Invoking `/db-lab` means **inspect current evidence and show the current view**. The learner should not need to remember to refresh, rebuild, or manually maintain an ER diagram.
26
+
27
+ Natural-language arguments are preferred over rigid syntax. Examples:
28
+
29
+ ```text
30
+ /db-lab show me how Recipes and Ingredients are related
31
+ /db-lab changes WI-24
32
+ /db-lab trace the EF query used by GetWeeklyMealPlan
33
+ /db-lab why is this query scanning MealPlanItems
34
+ /db-lab show which indexes this query actually uses
35
+ /db-lab walk me through the latest migration without applying it
36
+ /db-lab give me a database-design interview based on this domain
37
+ ```
38
+
39
+ ## Current-by-default artifact lifecycle
40
+
41
+ Generated workbench HTML/JSON is a disposable projection:
42
+
43
+ ```text
44
+ current safe database/repository evidence
45
+ → inspect schema / ORM / migrations / queries
46
+ → build current learning model
47
+ → generate current interactive view
48
+ ```
49
+
50
+ The database/repository evidence remains authoritative. An older browser tab is not authoritative. Run the lab again and it produces the current view.
51
+
52
+ When a previous disposable snapshot is available it may help explain what changed, but never outranks current physical/ORM/migration evidence.
53
+
54
+ ## SQLite interactive workbench
55
+
56
+ Greenfield consumers that include the Core learning payload receive:
57
+
58
+ ```text
59
+ tools/lean-db-lab/sqlite-workbench.py
60
+ ```
61
+
62
+ The tool is dependency-free Python and opens SQLite databases read-only. It generates a self-contained interactive `index.html` and machine-readable `workbench.json`.
63
+
64
+ ### PowerShell
65
+
66
+ From the repository root, using a **known safe development/test** SQLite file:
67
+
68
+ ```powershell
69
+ Set-Location D:\Projects\<repo>
70
+ $out = Join-Path $env:TEMP "lean-db-lab"
71
+ python .\tools\lean-db-lab\sqlite-workbench.py export `
72
+ --db .\.data\app.db `
73
+ --out $out
74
+ Start-Process (Join-Path $out "index.html")
75
+ ```
76
+
77
+ `.data/app.db` is only an example. Use the actual project development/test database path. Never point the learning tool at Production.
78
+
79
+ ### macOS/Linux
80
+
81
+ ```bash
82
+ cd /path/to/repo
83
+ out="${TMPDIR:-/tmp}/lean-db-lab"
84
+ python3 tools/lean-db-lab/sqlite-workbench.py export \
85
+ --db .data/app.db \
86
+ --out "$out"
87
+ open "$out/index.html" 2>/dev/null || xdg-open "$out/index.html"
88
+ ```
89
+
90
+ ### Query-to-ERD evidence
91
+
92
+ Create a disposable query-evidence JSON file from captured or otherwise verified SQL:
93
+
94
+ ```json
95
+ [
96
+ {
97
+ "name": "Get weekly meal plan",
98
+ "sql": "SELECT r.Id, r.Name FROM MealPlanItems mpi JOIN Recipes r ON r.Id = mpi.RecipeId WHERE mpi.MealPlanId = :mealPlanId",
99
+ "parameters": {
100
+ "mealPlanId": 42
101
+ }
102
+ }
103
+ ]
104
+ ```
105
+
106
+ Run the workbench with that evidence:
107
+
108
+ ```powershell
109
+ $out = Join-Path $env:TEMP "lean-db-lab"
110
+ python .\tools\lean-db-lab\sqlite-workbench.py export `
111
+ --db .\.data\app.db `
112
+ --queries .\tmp\db-lab-queries.json `
113
+ --out $out
114
+ Start-Process (Join-Path $out "index.html")
115
+ ```
116
+
117
+ Each invocation produces the current view. The query-evidence file and generated workbench stay disposable/local unless the project explicitly chooses a sanitized durable artifact.
118
+
119
+ ## What the interactive viewer shows
120
+
121
+ The SQLite workbench supports:
122
+
123
+ - tables and views;
124
+ - columns, types, nullability, and primary-key participation;
125
+ - foreign-key relationships and delete/update actions;
126
+ - indexes, uniqueness, origin, partial-index status, key columns, and stored DDL when available;
127
+ - junction-table candidates, clearly labeled as a heuristic rather than domain truth;
128
+ - clickable tables, columns, and indexes with a learning/details panel;
129
+ - query selection and highlighting of referenced tables;
130
+ - relationship-edge highlighting when both related tables participate;
131
+ - qualified query-column highlighting when resolvable;
132
+ - `EXPLAIN QUERY PLAN` evidence for parameterized `SELECT`/`WITH` queries;
133
+ - a distinction between indexes selected by the planner and indexes that are merely plausible candidates/relevant to highlighted columns.
134
+
135
+ The viewer does not claim to understand arbitrary SQL perfectly. SQL-to-table/column mapping includes heuristic parsing, while planner-selected indexes come from SQLite's own query-plan evidence.
136
+
137
+ ## What to expect from `/db-lab`
138
+
139
+ A useful session follows evidence rather than guesses:
140
+
141
+ ```text
142
+ repository intent
143
+ → ORM model
144
+ → physical schema
145
+ → query / executed SQL
146
+ → query plan
147
+ → indexes / constraints / transaction behavior
148
+ → explanation or exercise
149
+ ```
150
+
151
+ Explicitly state which layers were verified and which were inferred.
152
+
153
+ Where useful, distinguish:
154
+
155
+ ```text
156
+ DOMAIN MODEL
157
+ ORM MODEL
158
+ PHYSICAL MODEL
159
+ ```
160
+
161
+ ## Change-focused learning
162
+
163
+ Prompt:
164
+
165
+ ```text
166
+ /db-lab changes WI-24
167
+ ```
168
+
169
+ Use the accepted/delivered change set plus current database evidence. Explain relevant changes to:
170
+
171
+ - entities/tables and columns;
172
+ - primary/foreign/unique/check constraints;
173
+ - nullability and cascade behavior;
174
+ - indexes and access paths;
175
+ - ORM mappings and translation behavior;
176
+ - migrations/DDL and data-safety implications;
177
+ - query paths;
178
+ - transactions/concurrency.
179
+
180
+ A changed entity/migration/query file proves that database-related code was touched. It does **not** by itself prove that the physical database changed. Verify the physical layer when safe evidence is available.
181
+
182
+ A good learning summary connects the change to its consequence:
183
+
184
+ ```text
185
+ added relationship
186
+ → new FK/invariant
187
+ → new join/access path
188
+ → possible index consideration
189
+ → migration/data compatibility question
190
+ ```
191
+
192
+ ## Entity Framework to raw SQL
193
+
194
+ For EF Core, connect:
195
+
196
+ ```text
197
+ LINQ / EF expression
198
+ → translation preview such as ToQueryString() when appropriate
199
+ → captured actual DbCommand when available
200
+ → parameters
201
+ → tables and relationships
202
+ → provider query plan
203
+ → indexes selected by the planner
204
+ → result materialization/tracking
205
+ ```
206
+
207
+ Translation preview and an actually executed command are different evidence. Label them separately. Use supported logging/interception for executed-command evidence; the SQLite workbench consumes captured SQL and does not replace EF command capture.
208
+
209
+ Apply the same principle to other ORMs using framework-supported mechanisms.
210
+
211
+ ## Query-to-ERD highlighting
212
+
213
+ For a query touching `MealPlans`, `MealPlanItems`, and `Recipes`, highlight those tables. If both ends of an FK relationship participate, emphasize the edge. Highlight resolvable qualified columns.
214
+
215
+ If `EXPLAIN QUERY PLAN` says an index was used, mark it **planner selected**. An index whose leading column appears relevant but was not selected can be shown as **candidate/relevant**, never as used.
216
+
217
+ If the interactive viewer is unavailable, use a temporary Mermaid/text diagram and clearly label the fallback.
218
+
219
+ ## Query-plan debugging
220
+
221
+ Preferred learning sequence:
222
+
223
+ ```text
224
+ show query/access pattern
225
+ → developer predicts plan/index behavior
226
+ → inspect provider-native plan
227
+ → identify scan/search/join evidence
228
+ → developer proposes a change
229
+ → predict changed plan
230
+ → compare safely when a sandbox is available
231
+ ```
232
+
233
+ Do not teach "add an index" without connecting it to a concrete access pattern, selectivity/order needs, write cost and plan evidence.
234
+
235
+ ## Migration review
236
+
237
+ Prompt:
238
+
239
+ ```text
240
+ /db-lab migration AddRecipeRatings
241
+ ```
242
+
243
+ Connect:
244
+
245
+ ```text
246
+ migration intent
247
+ ↔ migration/ORM operations
248
+ ↔ generated SQL/DDL when available
249
+ ↔ schema before
250
+ ↔ schema after
251
+ ↔ data-safety concerns
252
+ ```
253
+
254
+ Useful questions:
255
+
256
+ - Is existing data compatible with a new non-null column?
257
+ - Is a backfill required?
258
+ - Does a uniqueness constraint match the invariant?
259
+ - Is a table rebuild/destructive operation involved?
260
+ - What rollback limitations exist?
261
+
262
+ Do not apply the migration in Database Lab mode.
263
+
264
+ ## Database-design interview
265
+
266
+ Prompt:
267
+
268
+ ```text
269
+ /db-lab interview database
270
+ ```
271
+
272
+ Use the project domain for familiarity but perturb requirements so memorizing the schema does not solve the exercise.
273
+
274
+ Drive:
275
+
276
+ ```text
277
+ requirements
278
+ → entities
279
+ → relationships/cardinality
280
+ → keys/constraints
281
+ → normalization
282
+ → access patterns
283
+ → indexes/plans
284
+ → transactions/concurrency
285
+ → evolution/scaling
286
+ → design defense
287
+ ```
288
+
289
+ Ask why a junction table exists, why a key was chosen, what deletion means historically, which query justifies an index, which invariant belongs in a database constraint, and what changes under concurrent edits.
290
+
291
+ ## Database competency model
292
+
293
+ ```text
294
+ relational modeling
295
+ keys + integrity constraints
296
+ normalization + data anomalies
297
+ SQL query construction
298
+ indexes + access patterns
299
+ query plans + performance reasoning
300
+ transactions + concurrency
301
+ ORM translation + abstraction leaks
302
+ schema evolution + migrations
303
+ database debugging
304
+ design defense + interview communication
305
+ ```
306
+
307
+ Use L1-L6 per competency rather than one undifferentiated SQL score.
308
+
309
+ ## Cross-lab learning
310
+
311
+ System Lab owns system context; Database Lab owns database depth.
312
+
313
+ ```text
314
+ /system-lab runtime checkout
315
+ → identify Orders DB
316
+ → /db-lab trace <order query>
317
+
318
+ /db-lab plan <query>
319
+ → database evidence looks healthy but end-to-end latency is high
320
+ → /system-lab performance checkout
321
+ ```
322
+
323
+ ## Delivery-completion visibility
324
+
325
+ Successful delivery may visibly recommend:
326
+
327
+ ```text
328
+ ★ /db-lab changes WI-24
329
+ This work changed database structure or behavior.
330
+ Explore affected relationships, queries, indexes, constraints, and ORM behavior.
331
+ ```
332
+
333
+ When database-specific evidence was not materially touched, `/db-lab` may still appear as an available option:
334
+
335
+ ```text
336
+ ○ /db-lab
337
+ Explore the current database schema, relationships, queries, indexes, and ORM behavior.
338
+ ```
339
+
340
+ Do not tell the learner to rebuild or refresh a diagram. Current evidence is part of invoking the lab.
341
+
342
+ After successful merge/default-branch CI, `/deliver` may prepare a disposable SQLite workbench automatically only when the accepted change is DB-relevant, the tool exists, and a safe local development/test SQLite database path is already known. Never guess the path, create/apply migrations, mutate data, or touch Production just to prepare learning output. Failure remains non-blocking.
343
+
344
+ ## Provider / ORM architecture
345
+
346
+ The workbench stays provider/ORM-adapter oriented rather than application-specific.
347
+
348
+ ```text
349
+ Database adapter
350
+ SQLite concrete path
351
+ PostgreSQL / SQL Server / ... future
352
+
353
+ ORM evidence
354
+ EF Core first concrete path
355
+ Hibernate / SQLAlchemy / ... future
356
+
357
+ Canonical learning model
358
+ tables
359
+ columns
360
+ constraints
361
+ relationships
362
+ indexes
363
+ queries
364
+ query-plan steps
365
+ migrations
366
+
367
+ Interactive viewer
368
+ schema navigation
369
+ learning panels
370
+ query-to-ERD highlighting
371
+ ```
372
+
373
+ SQLite + EF Core is a first concrete learning path, not a permanent architectural restriction.
374
+
375
+ ## IDE tooling
376
+
377
+ Database extensions can improve the human exploration loop, but `/db-lab` must not depend on manipulating their UI.
378
+
379
+ For SQLite projects, Bootstrap may recommend SQLTools plus its SQLite driver so the developer can independently browse/query the same local database. The reusable lab obtains authoritative evidence from repository/framework/provider-supported mechanisms.
380
+
381
+ ## Safety and fallbacks
382
+
383
+ Database Lab is read-only learning by default.
384
+
385
+ Do not:
386
+
387
+ - connect to Production for learning exercises;
388
+ - mutate application data;
389
+ - apply/revert migrations;
390
+ - create/drop indexes;
391
+ - change code to prove a point;
392
+ - claim an index was used without plan evidence;
393
+ - manufacture executed SQL when only ORM source is available.
394
+
395
+ The SQLite workbench opens the database with SQLite `mode=ro`. Generated HTML/JSON are disposable learning artifacts and should normally go to temp/cache rather than tracked project paths.
396
+
397
+ If evidence is missing, say so and continue with the strongest safe layer available. A missing database runtime, ORM trace, interactive viewer, or post-delivery artifact should degrade the experience; it should never block learning or engineering delivery.