|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is **test_factory**, a PostgreSQL extension that provides a framework for managing unit test data in databases. It solves the common problem of creating and maintaining test data by providing a system to register test data definitions once and retrieve them efficiently with automatic dependency resolution. |
| 8 | + |
| 9 | +## Build System & Development Commands |
| 10 | + |
| 11 | +This project uses PGXNtool for build management. Key commands: |
| 12 | + |
| 13 | +### Building and Installation |
| 14 | +```bash |
| 15 | +make # Build the extension |
| 16 | +make install # Install to PostgreSQL |
| 17 | +make clean # Clean build artifacts |
| 18 | +make distclean # Clean all generated files including META.json |
| 19 | +``` |
| 20 | + |
| 21 | +### Testing |
| 22 | +```bash |
| 23 | +make test # Run full test suite (install, then test) |
| 24 | +make installcheck # Run tests only (no clean/install) |
| 25 | +make results # Update expected test results (only after verifying tests pass!) |
| 26 | +``` |
| 27 | + |
| 28 | +### Distribution |
| 29 | +```bash |
| 30 | +make tag # Create git tag for current version |
| 31 | +make dist # Create distribution zip file |
| 32 | +make forcetag # Force recreate tag if it exists |
| 33 | +make forcedist # Force tag + distribution |
| 34 | +``` |
| 35 | + |
| 36 | +## Architecture & Key Components |
| 37 | + |
| 38 | +### Core API Functions |
| 39 | +- `tf.register(table_name, test_sets[])` - Register test data definitions for a table |
| 40 | +- `tf.get(table_type, set_name)` - Retrieve test data, creating it if it doesn't exist |
| 41 | +- `tf.tap(table_name, set_name)` - pgTAP integration wrapper for testing |
| 42 | + |
| 43 | +### Database Schema Organization |
| 44 | +- `tf` schema: User-facing API (functions, types) |
| 45 | +- `_tf` schema: Internal implementation (tables, security definer functions) |
| 46 | +- `_test_factory_test_data` schema: Cached test data storage |
| 47 | +- Uses dedicated `test_factory__owner` role for security isolation |
| 48 | + |
| 49 | +### Security Model |
| 50 | +- Role-based access with `test_factory__owner` for data management |
| 51 | +- Security definer functions with `search_path=pg_catalog` |
| 52 | +- Proper permission isolation between user and system operations |
| 53 | + |
| 54 | +### Key Data Structures |
| 55 | +```sql |
| 56 | +CREATE TYPE tf.test_set AS ( |
| 57 | + set_name text, -- Name to reference this test data set |
| 58 | + insert_sql text -- SQL command that returns test data rows |
| 59 | +); |
| 60 | +``` |
| 61 | + |
| 62 | +### Test Data Workflow |
| 63 | +1. **Registration**: Use `tf.register()` to define how test data is created |
| 64 | +2. **Retrieval**: Call `tf.get()` to obtain test data (creates on first call) |
| 65 | +3. **Caching**: Test data is stored permanently for fast subsequent access |
| 66 | +4. **Dependencies**: Test sets can reference other test sets via embedded `tf.get()` calls |
| 67 | + |
| 68 | +### Performance & Caching |
| 69 | +- Test data created once and cached in permanent tables |
| 70 | +- Subsequent `tf.get()` calls return cached data without recreation |
| 71 | +- Data remains available even if source tables are modified/truncated |
| 72 | +- Dependency resolution handled automatically during creation |
| 73 | + |
| 74 | +## File Structure Key Points |
| 75 | + |
| 76 | +### SQL Files |
| 77 | +- `sql/test_factory.sql` and `sql/test_factory--0.5.0.sql`: Main extension code |
| 78 | +- Complex role management and schema setup with proper cleanup |
| 79 | +- Security definer functions for safe cross-schema operations |
| 80 | + |
| 81 | +### Build Configuration |
| 82 | +- `META.in.json`: Template for PGXN metadata (processed to `META.json`) |
| 83 | +- `test_factory.control`: PostgreSQL extension control file |
| 84 | +- `Makefile`: Simple inclusion of pgxntool's build system |
| 85 | + |
| 86 | +## Development Workflow |
| 87 | + |
| 88 | +1. **Making Changes**: Modify source files in `sql/` directory |
| 89 | +2. **Testing**: Run `make test` to ensure all tests pass |
| 90 | +3. **Version Updates**: Update version in both `META.in.json` and `test_factory.control` |
| 91 | +4. **Distribution**: Use `make dist` to create release packages |
| 92 | + |
| 93 | +## Extension Architecture Details |
| 94 | + |
| 95 | +The extension handles complex bootstrapping during installation: |
| 96 | +- Creates temporary role tracking for safe installation |
| 97 | +- Sets up three schemas with proper ownership and permissions |
| 98 | +- Uses security definer pattern for controlled access to internal functions |
| 99 | +- Automatically restores original database role after installation |
| 100 | +- Implements dependency resolution through recursive `tf.get()` calls |
| 101 | + |
| 102 | +## Usage Patterns |
| 103 | + |
| 104 | +### Basic Registration |
| 105 | +```sql |
| 106 | +SELECT tf.register( |
| 107 | + 'customer', |
| 108 | + array[ |
| 109 | + row('base', 'INSERT INTO customer VALUES (DEFAULT, ''Test'', ''User'') RETURNING *')::tf.test_set |
| 110 | + ] |
| 111 | +); |
| 112 | +``` |
| 113 | + |
| 114 | +### With Dependencies |
| 115 | +```sql |
| 116 | +SELECT tf.register( |
| 117 | + 'invoice', |
| 118 | + array[ |
| 119 | + row('base', 'INSERT INTO invoice VALUES (DEFAULT, (tf.get(NULL::customer, ''base'')).customer_id, current_date) RETURNING *')::tf.test_set |
| 120 | + ] |
| 121 | +); |
| 122 | +``` |
| 123 | + |
| 124 | +### Data Retrieval |
| 125 | +```sql |
| 126 | +-- Gets customer test data, creating it if needed |
| 127 | +SELECT * FROM tf.get(NULL::customer, 'base'); |
| 128 | + |
| 129 | +-- Gets invoice test data, automatically creating dependent customer data |
| 130 | +SELECT * FROM tf.get(NULL::invoice, 'base'); |
| 131 | +``` |
0 commit comments