Skip to content
Back to skills

Collection

ASecurity

SCD Type 2, Closure Table, Shared Family Budget patterns

  • 24 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 8, 2026
databasesgosqlapidatabasebackend

Works with

  • api

Security analysis

A100/100

Pro scans all 21 files and shows the line behind each finding

Scanned September 8, 2026

npx -y skills add mattnigh/skills_collection --skill collection --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Collection?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Collection
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mattnigh-collection-27d4e560/badge)](https://www.skillsdirectory.com/skills/mattnigh-collection-27d4e560)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: Advanced Patterns
description: SCD Type 2, Closure Table, Shared Family Budget patterns
version: 3.0.0
author: Family Budget Team
tags: [scd-type-2, closure-table, shared-budget, hierarchy, versioning]
dependencies: [db-management, api-development]
architecture_refs:
  - $ref: ../../docs/architecture/database/history.yaml
  - $ref: ../../docs/architecture/database/hierarchy.yaml
  - $ref: ../../docs/architecture/guides/change-checklist.yaml#/checklists/add_history_tracking
---

# Advanced Patterns Skill

Реализация продвинутых архитектурных паттернов: SCD Type 2, Closure Table, Shared Family Budget.

## When to Use

- Добавить SCD Type 2 versioning к dimension таблице
- Создать Closure Table для иерархии
- Реализовать Shared Family Budget endpoint
- Обновить History tracking

## Architecture Context

**References:**
- History Tables: [$ref](../../docs/architecture/database/history.yaml)
- Hierarchy: [$ref](../../docs/architecture/database/hierarchy.yaml)
- Change Checklist: [$ref](../../docs/architecture/guides/change-checklist.yaml#/checklists/add_history_tracking)

**Key Patterns:**

### SCD Type 2 (Slowly Changing Dimension Type 2)

**Purpose:** Track full history of dimension changes

**Current Implementation:** SCD Type 1 (main tables) + SCD Type 2 (history tables)

**Main Tables (SCD Type 1 - In-place updates):**
- `Article`, `User`, `FinancialCenter`, `CostCenter` - NO versioning fields
- Updates modify existing row directly
- Stable PK for fact table FK references

**History Tables (SCD Type 2 - Full versioning):**
- `ArticleHistory`, `UserHistory`, etc. - ALL changes tracked
- Fields: `is_current`, `valid_from`, `valid_to`, `change_type`
- Audit trail with complete snapshot of each version

**Why This Hybrid?**
- Simple queries on main tables (no is_current filter needed)
- Complete audit history in separate tables
- Stable PKs for fact table relationships

### Closure Table

**Purpose:** O(1) hierarchical queries without recursion

**Table Structure:**
```sql
CREATE TABLE t_d_article_hierarchy (
    ancestor_id INT,      -- Parent (or self)
    descendant_id INT,    -- Child (or self)
    depth INT,            -- 0 = self, 1 = direct child, 2+ = nested
    PRIMARY KEY (ancestor_id, descendant_id)
);
```

**Queries:**
- Get all descendants: `WHERE ancestor_id = X`
- Get all ancestors: `WHERE descendant_id = X`
- Get direct children: `WHERE ancestor_id = X AND depth = 1`

### Shared Family Budget

**Purpose:** 2-5 family members see ALL transactions (full transparency)

**Rules:**
- **Fact tables**: NO user_id filtering (all see all)
- **Dimension tables**: Admin-only CREATE/UPDATE/DELETE, all can READ
- **user_id**: Audit trail only (who created/modified)

Reference: `_shared/validation-logic.md#3-shared-budget-model-consistency`

## Commands

### Command: add-scd2-history

**Usage:**
```
Добавь SCD Type 2 history tracking к модели <ModelName>.
```

**What It Does:**
1. Create History table model with ALL fields from main table
2. Add `is_current`, `valid_from`, `valid_to`, `change_type` fields
3. Create service functions for history tracking
4. Create Alembic migration for History table

**Template Reference:**
- `templates/scd-type-2-service.py` - History service
- `examples/article-history.md` - Real Article history implementation

**Critical:** History table MUST have ALL fields from main table!

Reference: `_shared/validation-logic.md#2-history-table-field-completeness`

### Command: add-closure-table

**Usage:**
```
Добавь Closure Table для модели <ModelName> hierarchy.
```

**What It Does:**
1. Create Hierarchy table (ancestor_id, descendant_id, depth)
2. Create indexes on (ancestor_id, depth), (descendant_id, depth)
3. Initialize self-references (depth=0)
4. Create HierarchyService for CRUD operations

**Template Reference:**
- `templates/closure-table.py` - Hierarchy model
- `templates/hierarchy-service.py` - HierarchyService implementation

### Command: implement-shared-budget

**Usage:**
```
Реализуй Shared Budget pattern для endpoint <endpoint-name>.
```

**What It Does:**
1. Remove user_id filtering from fact queries
2. Add admin checks for dimension CREATE/UPDATE/DELETE
3. Keep user_id for audit trail only

**Template Reference:**
- `templates/shared-budget-endpoint.py` - Shared Budget pattern

## Validation Checklist

### SCD Type 2 History
- [ ] History table has ALL fields from main table
- [ ] NO NULL in NOT NULL columns
- [ ] `change_type` set (CREATE/UPDATE/DELETE)
- [ ] `valid_from`, `valid_to`, `is_current` set correctly
- [ ] Service layer updates history on changes

### Closure Table
- [ ] Hierarchy table created (ancestor_id, descendant_id, depth)
- [ ] Indexes on (ancestor_id, depth), (descendant_id, depth), (depth)
- [ ] Self-references initialized (depth=0)
- [ ] HierarchyService handles updates
- [ ] NO direct SQL updates to closure table

### Shared Budget
- [ ] Fact tables: NO user_id filtering
- [ ] Dimension tables: Admin-only CREATE/UPDATE/DELETE
- [ ] user_id used for audit trail only

## Common Mistakes

**Forgot History field:**
- **Symptom**: IntegrityError: null value in column "record_type"
- **Fix**: Copy ALL fields from main table to History table
- **Reference**: `_shared/validation-logic.md#2`

**User_id filtering in Shared Budget:**
- **Symptom**: Users can't see family transactions
- **Fix**: Remove `.where(BudgetFact.user_id == current_user.id)`
- **Reference**: `_shared/validation-logic.md#3`

**Direct UPDATE on dimension (instead of history):**
- **Symptom**: No history record created, audit trail broken
- **Fix**: Use service layer to create history records
- **Reference**: `_shared/validation-logic.md#5`

## Related Skills

- **db-management**: Create models and migrations
- **api-development**: Implement endpoints with patterns

## Quick Links

- History Tables Architecture: [$ref](../../docs/architecture/database/history.yaml)
- Hierarchy Architecture: [$ref](../../docs/architecture/database/hierarchy.yaml)
- Article Example: `backend/app/models/article.py`, `backend/app/models/article_history.py`

Files in this skill

  • 0Chan-smc__claude-code-workflow-lab__claude__skills__frontend-dev-guidelines__SKILL.md15.1 KB
  • 17hz__nextjs-template__claude__skills__example-skill__SKILL.md316 B
  • 1ambda__dataops-platform__claude__skills__context-synthesis__SKILL.md3.5 KB
  • 1natsu172__dotfiles__claude__skills__git-analysis__SKILL.md5.4 KB
  • 1natsu172__dotfiles__claude__skills__github-pr-best-practices__SKILL.md7.7 KB
  • 23Maestro__prospect-pipeline__claude__skills__npid-fastapi-skill.md26.1 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-code-javascript__SKILL.md15.7 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-code-python__SKILL.md17.5 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-expression-syntax__SKILL.md9.4 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-mcp-tools-expert__SKILL.md12.5 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-node-configuration__SKILL.md16.6 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-workflow-patterns__SKILL.md11.2 KB
  • 3x-Projetos__claude-memory-framework__claude__skills__scientist__SKILL.md14.8 KB
  • 5MinFutures__futures-arena__claude__skills__migration-tracker__SKILL.md16.2 KB
  • 5MinFutures__futures-arena__claude__skills__planning-guidelines__SKILL.md11.8 KB
  • 92Bilal26__TaskPilotAI__claude__skills__assessment-builder__SKILL.md17.5 KB
  • 92Bilal26__TaskPilotAI__claude__skills__book-scaffolding__SKILL.md19.1 KB
  • 92Bilal26__TaskPilotAI__claude__skills__code-validation-sandbox__SKILL.md6.2 KB
  • 92Bilal26__TaskPilotAI__claude__skills__exercise-designer__SKILL.md18.1 KB
  • 92Bilal26__TaskPilotAI__claude__skills__learning-objectives__SKILL.md24.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in databases →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.