From co-dev
Analyze, document, map, or scan the database schema. Use when the user wants to analyze the database, document the database, generate schema docs, map the database, create DB documentation, or inspect the database structure. Generates a docs/DB.md file with complete database schema documentation. Auto-detects language/framework. Supports MySQL, PostgreSQL, SQLite, MongoDB, Elasticsearch, Redis, and BigQuery.
npx claudepluginhub cloud-officer/claude-code-plugin-dev --plugin co-devThis skill is limited to using the following tools:
Analyze this project and generate a `docs/DB.md` file with **complete database schema documentation** for running queries.
Creates isolated Git worktrees for feature branches with prioritized directory selection, gitignore safety checks, auto project setup for Node/Python/Rust/Go, and baseline verification.
Executes implementation plans in current session by dispatching fresh subagents per independent task, with two-stage reviews: spec compliance then code quality.
Dispatches parallel agents to independently tackle 2+ tasks like separate test failures or subsystems without shared state or dependencies.
Analyze this project and generate a docs/DB.md file with complete database schema documentation for running queries.
This skill uses database MCP tools when available and falls back to CLI commands if they are unavailable or return errors.
| Database | MCP Tools | CLI Fallback |
|---|---|---|
| PostgreSQL | mcp__postgres__list_tables, describe_table, list_schemas, query | psql |
| MySQL | mcp__mysql__mysql_query | mysql |
| MongoDB | mcp__mongodb__list-databases, list-collections, collection-schema, find | mongosh |
| Redis | mcp__redis__info, dbsize, scan_keys, type, get, hgetall, json_get | redis-cli |
| SQLite | No MCP — CLI only | sqlite3 |
| BigQuery | mcp__bigquery__query, list_tables, get_table_schema | bq |
| Elasticsearch | No MCP — CLI only | curl |
Prefer MCP tools when available — they handle connection management and provide structured output. For schema analysis, MCP tools like mcp__postgres__list_tables and mcp__postgres__describe_table are especially useful. If MCP tools return errors, fall back to the CLI.
IMPORTANT: Document ALL tables/collections/indices. Do not filter or skip any tables. Developers need full schema documentation, not just "important" tables.
This skill assumes database connection environment variables are already set. The following variables are used:
MYSQL_HOST - Database hostMYSQL_PORT - Database portMYSQL_USER - Database userMYSQL_PASS - Database passwordMYSQL_DB - Database namePGHOST - Database hostPGPORT - Database portPGUSER - Database userPGPASSWORD - Database passwordPGDATABASE - Database nameMONGODB_URI - Full connection URI (e.g., mongodb://localhost:27017/dbname)ES_URL - Elasticsearch URL (e.g., http://localhost:9200)ES_API_KEY - Optional API key for authenticationREDIS_URL - Redis connection URL (e.g., redis://localhost:6379)BQ_PROJECT - GCP project IDBQ_DATASETS - Comma-separated list of BigQuery datasets (e.g., archive_2023,archive_2024,archive_2025)Use these exact command formats:
MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB" -e "SQL_QUERY"
psql -c "SQL_QUERY"
mongosh "$MONGODB_URI" --eval "JS_CODE"
curl -s "$ES_URL/index/_endpoint" -H "Content-Type: application/json" -d 'JSON_BODY'
redis-cli -u "$REDIS_URL" COMMAND
bq query --use_legacy_sql=false --format=prettyjson --project_id="$BQ_PROJECT" "STANDARD_SQL_QUERY"
bq ls --project_id="$BQ_PROJECT" "$DATASET"
bq show --schema --project_id="$BQ_PROJECT" "$DATASET.table_name"
Before starting, check if docs/DB.md already exists.
If the file exists:
If the file does not exist:
Check for these indicators:
Symfony (Doctrine ORM):
composer.json with doctrine/orm or doctrine/doctrine-bundlesrc/Entity/ directoryconfig/packages/doctrine.yamlmigrations/ directoryLaravel (Eloquent):
composer.json with laravel/frameworkapp/Models/ directorydatabase/migrations/config/database.phpDoctrine ODM (MongoDB):
composer.json with doctrine/mongodb-odmsrc/Document/ directoryDjango:
manage.py in rootsettings.py with DATABASES configmodels.py files in app directories*/migrations/ directoriesFlask/SQLAlchemy:
requirements.txt or pyproject.toml with sqlalchemy or flask-sqlalchemymodels.py or models/ directoryalembic/ or migrations/ for AlembicFastAPI:
requirements.txt with fastapi and sqlalchemyDjango + MongoDB (Djongo/MongoEngine):
settings.py with djongo or mongoenginePyMongo/Motor:
requirements.txt with pymongo or motorRuby on Rails (ActiveRecord):
Gemfile with railsapp/models/ directorydb/migrate/ directorydb/schema.rb or db/structure.sqlconfig/database.ymlMongoid (MongoDB):
Gemfile with mongoidconfig/mongoid.ymlGORM:
go.mod with gorm.io/gormgorm: tagsmodels/ or internal/models/ directorysqlx/database-sql:
go.mod with github.com/jmoiron/sqlxMongoDB (mongo-driver):
go.mod with go.mongodb.org/mongo-driverent:
go.mod with entgo.io/entent/schema/ directoryTypeORM:
package.json with typeormsrc/entity/ or entities/ directoryormconfig.json or data-source.tsPrisma:
prisma/schema.prisma filepackage.json with @prisma/clientSequelize:
package.json with sequelizemodels/ directorymigrations/ directoryMongoose (MongoDB):
package.json with mongoosenew Schema()Drizzle:
package.json with drizzle-ormdrizzle/ directory or schema filesKnex.js:
package.json with knexknexfile.js or knexfile.tsmigrations/ directorySpring Boot + JPA/Hibernate:
pom.xml or build.gradle with spring-boot-starter-data-jpa@Entity annotated classesapplication.properties or application.yml with spring.datasourcesrc/main/java/**/entity/ or **/model/ directoriesSpring Data MongoDB:
pom.xml with spring-boot-starter-data-mongodb@Document annotated classesEntity Framework Core:
*.csproj with Microsoft.EntityFrameworkCoreDbContext classesMigrations/ directoryappsettings.json with connection stringsMongoDB.Driver:
*.csproj with MongoDB.DriverDiesel:
Cargo.toml with dieseldiesel.toml configmigrations/ directoryschema.rsSeaORM:
Cargo.toml with sea-ormentity/ directorySQLx:
Cargo.toml with sqlx.sqlx/ directory or migrations/Based on framework detection, identify which databases are used:
SQL Databases (MySQL/PostgreSQL/SQLite):
.env, .env.example)MongoDB:
Elasticsearch:
fos_elastica.yaml, elasticsearch.yml, or similarRedis:
BigQuery:
BQ_PROJECT and BQ_DATASETS environment variables setgoogle-cloud-bigquery in Python, @google-cloud/bigquery in Node.js)| Framework | Entity Location | Migration Location | Schema Command |
|---|---|---|---|
| Symfony/Doctrine | src/Entity/ | migrations/ | php bin/console doctrine:mapping:info |
| Laravel/Eloquent | app/Models/ | database/migrations/ | php artisan model:show |
| Django | */models.py | */migrations/ | python manage.py inspectdb |
| Rails/ActiveRecord | app/models/ | db/migrate/ | Read db/schema.rb |
| TypeORM | src/entity/ | migrations/ | Check entity decorators |
| Prisma | prisma/schema.prisma | Prisma migrations | Read schema.prisma directly |
| Spring JPA | **/entity/ | Flyway/Liquibase | Check @Entity classes |
| EF Core | Models/ or Entities/ | Migrations/ | Check DbContext |
| GORM | models/ | Migration files | Check struct tags |
| Diesel | src/models.rs | migrations/ | Read schema.rs |
Look for:
IMPORTANT: ORM entities/models may not cover all tables. Join tables, framework-generated tables (sessions, migrations, jobs, cache), and raw SQL tables may not have model classes. You MUST also enumerate all tables directly from the schema file (e.g., db/schema.rb, schema.prisma) or the live database in Step 7, then cross-reference to ensure no table is missing from the documentation.
| Framework | Document Location | Schema Definition |
|---|---|---|
| Doctrine ODM | src/Document/ | @ODM\ annotations |
| Mongoose | models/ | new Schema({...}) |
| MongoEngine | models.py | Document class fields |
| Mongoid | app/models/ | field :name, type: |
| Spring Data MongoDB | **/document/ | @Document annotation |
Look for:
Find across all frameworks:
deleted_at, is_deleted)tenant_id, organization_id)Create the directory if needed:
mkdir -p docs
Write an initial docs/DB.md with the appropriate template based on detected database type(s).
Before connecting to the database, verify the required environment variables are set and the CLI tool is available.
How to check: Run a simple connectivity test using the CLI tool. If it fails, output the appropriate setup instructions below and ask the user to configure it.
MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB" -e "SELECT 1"
Required environment variables:
MYSQL_HOST - Database hostMYSQL_PORT - Database portMYSQL_USER - Database userMYSQL_PASS - Database passwordMYSQL_DB - Database namepsql -c "SELECT 1"
Required environment variables:
PGHOST - Database hostPGPORT - Database portPGUSER - Database userPGPASSWORD - Database passwordPGDATABASE - Database namemongosh "$MONGODB_URI" --eval "db.runCommand({ping: 1})"
Required environment variables:
MONGODB_URI - Full connection URIcurl -s "$ES_URL/_cluster/health"
# Or with API key:
curl -s -H "Authorization: ApiKey $ES_API_KEY" "${ES_URL}/_cluster/health"
Required environment variables:
ES_URL - Elasticsearch URLES_API_KEY - Optional API keyredis-cli -u "$REDIS_URL" PING
Required environment variables:
REDIS_URL - Redis connection URLbq query --use_legacy_sql=false --project_id="$BQ_PROJECT" "SELECT 1"
Required environment variables:
BQ_PROJECT - GCP project IDBQ_DATASETS - Comma-separated list of datasetsIf this fails: Tell the user to run gcloud auth application-default login and gcloud auth application-default set-quota-project $BQ_PROJECT.
After outputting instructions: Ask the user to confirm when they have set the environment variables. Wait for their confirmation before proceeding to step 7.
If the user declines or cannot provide database credentials: Skip steps 7 and 8. Proceed directly to step 9 using only the code-based analysis from steps 3-4. The verification status in docs/DB.md MUST reflect this (see verification timestamp formats below).
Connect via CLI to gather live data and verify the schema analysis.
CRITICAL: Enumerate ALL tables/collections/indices first. Before doing anything else in this step, list every table (or collection/index) in the database. Compare this list against what you documented from code in Steps 3-4. Any table present in the database but missing from your documentation MUST be added. Do NOT skip join tables, migration tracking tables, session tables, queue tables, or any other table — every single table must appear in the final documentation.
Performance safeguards for large tables:
COUNT(*) on large tablesList ALL tables and row counts (uses estimates, instant). Every table returned here MUST appear in docs/DB.md:
MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB" -e "
SELECT table_name, table_rows
FROM information_schema.tables
WHERE table_schema = DATABASE()
ORDER BY table_rows DESC;"
Check indexes:
MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB" -e "SHOW INDEX FROM table_name;"
Get date ranges for time-series tables:
MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB" -e "
SELECT MIN(created_at) as earliest, MAX(created_at) as latest FROM orders;"
List ALL tables and row counts (uses estimates, instant). Every table returned here MUST appear in docs/DB.md:
psql -c "SELECT schemaname, relname, n_live_tup FROM pg_stat_user_tables ORDER BY n_live_tup DESC;"
Check indexes:
psql -c "SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'table_name';"
Get date ranges:
psql -c "SELECT MIN(created_at) as earliest, MAX(created_at) as latest FROM orders;"
List ALL collections and document counts. Every collection returned here MUST appear in docs/DB.md:
mongosh "$MONGODB_URI" --eval "db.getCollectionNames().forEach(c => print(c + ': ' + db[c].estimatedDocumentCount()))"
List indexes:
mongosh "$MONGODB_URI" --eval "db.collection.getIndexes()"
Get date ranges:
mongosh "$MONGODB_URI" --eval "db.orders.aggregate([
{ \$group: { _id: null, earliest: { \$min: '\$createdAt' }, latest: { \$max: '\$createdAt' } } }
])"
Sample document structure:
mongosh "$MONGODB_URI" --eval "db.collection.findOne()"
List ALL indices and document counts. Every index returned here MUST appear in docs/DB.md:
curl -s "$ES_URL/_cat/indices?v&h=index,docs.count,store.size"
Get mapping:
curl -s "$ES_URL/index_name/_mapping" | jq
Get database size:
redis-cli -u "$REDIS_URL" DBSIZE
Sample key patterns:
redis-cli -u "$REDIS_URL" SCAN 0 MATCH "user:*" COUNT 10
Check TTLs:
redis-cli -u "$REDIS_URL" TTL key_name
List ALL datasets (from BQ_DATASETS env var). Every dataset listed MUST be documented:
for ds in $(echo "$BQ_DATASETS" | tr ',' ' '); do
echo "=== Dataset: $ds ==="
bq ls --project_id="$BQ_PROJECT" "$ds"
done
Get table schema:
bq show --schema --format=prettyjson --project_id="$BQ_PROJECT" "$DATASET.table_name"
Get table info (row count, size, partitioning):
bq show --project_id="$BQ_PROJECT" "$DATASET.table_name"
Get date ranges:
bq query --use_legacy_sql=false --project_id="$BQ_PROJECT" "
SELECT MIN(created_at) as earliest, MAX(created_at) as latest
FROM \`$BQ_PROJECT.$DATASET.orders\`"
For each enum or status field identified, query the actual values and their distribution.
Use safe sampling for large tables (>1M rows):
For small tables (<1M rows) - full count is OK:
MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB" -e "
SELECT status, COUNT(*) as count FROM orders GROUP BY status ORDER BY count DESC;"
For large tables (>1M rows) - use sampling:
MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB" -e "
SELECT status, COUNT(*) as count FROM orders
WHERE created_at >= NOW() - INTERVAL 30 DAY
GROUP BY status ORDER BY count DESC;"
For very large tables - just get distinct values:
MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB" -e "
SELECT DISTINCT status FROM orders LIMIT 20;"
For small tables:
psql -c "SELECT status, COUNT(*) as count FROM orders GROUP BY status ORDER BY count DESC;"
For large tables - use sampling:
psql -c "SELECT status, COUNT(*) as count FROM orders
WHERE created_at >= NOW() - INTERVAL '30 days'
GROUP BY status ORDER BY count DESC;"
For small collections:
mongosh "$MONGODB_URI" --eval "db.orders.aggregate([
{ \$group: { _id: '\$status', count: { \$sum: 1 } } },
{ \$sort: { count: -1 } }
])"
For large collections - use sampling:
mongosh "$MONGODB_URI" --eval "db.orders.aggregate([
{ \$sample: { size: 10000 } },
{ \$group: { _id: '\$status', count: { \$sum: 1 } } },
{ \$sort: { count: -1 } }
])"
Elasticsearch aggregations are generally safe - they use approximate counts:
curl -s "$ES_URL/orders/_search" -H "Content-Type: application/json" -d '{
"size": 0,
"aggs": {
"status_values": {
"terms": { "field": "status.keyword", "size": 20 }
}
}
}'
For enum sampling (always use LIMIT or APPROX functions to control cost):
bq query --use_legacy_sql=false --project_id="$BQ_PROJECT" "
SELECT status, COUNT(*) as count
FROM \`$BQ_PROJECT.$DATASET.orders\`
GROUP BY status
ORDER BY count DESC
LIMIT 20;"
For very large tables — use APPROX_COUNT_DISTINCT or sample:
bq query --use_legacy_sql=false --project_id="$BQ_PROJECT" "
SELECT status, APPROX_COUNT_DISTINCT(id) as approx_count
FROM \`$BQ_PROJECT.$DATASET.orders\`
GROUP BY status
ORDER BY approx_count DESC;"
Update the docs/DB.md file with the live data gathered.
Completeness check: Before writing, verify that every table/collection/index returned by the database in Step 7 has a row in the "All Tables" (or "All Collections" / "All Indices") section. If any are missing, add them now. There must be a 1:1 correspondence between database objects and documented rows.
Large Table Warnings: For tables with >1M rows, add a row to the "Large Table Warnings" section. For tables with >10M rows, mark them as "VERY LARGE — always filter by date/indexed column" and list the specific indexed columns to filter on.
Common Business Questions: Scan the codebase for BI dashboards, report generators, analytics endpoints, and recurring query patterns. Document these as common business questions with the correct tables, joins, and filters. This helps future query-db users avoid common mistakes.
Add row/document counts to table/collection listings:
| Table | Purpose | Rows | Key Fields |
|---|---|---|---|
| orders | Customer orders | ~1.2M | status, created_at |
Replace enum guesses with actual values and counts:
| Table.Field | Value | Meaning | Count |
|---|---|---|---|
| orders.status | 1 | Completed | 850,000 |
| orders.status | 0 | Pending | 120,000 |
Document actual indexes:
| Table | Index | Columns | Notes |
|---|---|---|---|
| orders | idx_orders_created | created_at | Use for date range queries |
Add date ranges:
| Table.Field | Range |
|---|---|
| orders.created_at | 2019-01-15 to present |
Add verification timestamp at the top of the file using the appropriate format:
If database connection was available (steps 7-8 completed):
# Database Schema Documentation
> **Last verified**: YYYY-MM-DD — verified against live database
If NO database connection was available (steps 7-8 skipped):
# Database Schema Documentation
> **Last verified**: YYYY-MM-DD — derived from code analysis only (not verified against live database)
# Database Schema Documentation
> **Last verified**: YYYY-MM-DD — verified against live database / derived from code analysis only (not verified against live database)
## Database Type
MySQL / PostgreSQL (select one)
## CLI Command
<!-- Used by query-db skill -->
- MySQL: `MYSQL_PWD="$MYSQL_PASS" mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DB"`
- PostgreSQL: `psql`
## Framework
[Detected framework, e.g., "Symfony/Doctrine", "Django", "Rails/ActiveRecord"]
## Database Overview
Brief description of what data this system holds.
## All Tables
<!-- List EVERY table in the database, no exceptions. -->
| Table | Purpose | Key Fields for Filtering/Grouping |
|-------|---------|-----------------------------------|
| (one row per table — list ALL of them) | | |
## Field Mappings & Enums
| Table.Field | Value | Meaning |
|-------------|-------|---------|
| order.status | 0 | Pending |
| ... | ... | ... |
## Relationships
- `order.user_id → user.id`
- `order_item.order_id → order.id`
## Date/Time Fields
| Table.Field | Purpose | Notes |
|-------------|---------|-------|
| order.created_at | Order creation | Use for daily/monthly reports |
## Money/Numeric Fields
| Table.Field | Unit | Notes |
|-------------|------|-------|
| order.total | cents | Divide by 100 for display |
## Soft Deletes
Tables using soft delete pattern:
- `users.deleted_at`
- `orders.deleted_at`
**Important**: Add `WHERE deleted_at IS NULL` to exclude soft-deleted records.
## Multi-Tenancy
If applicable, note tenant isolation:
- Filter by `organization_id` or `tenant_id`
## Framework / Infrastructure Tables
Tables managed by the framework (not domain models). Still included for completeness:
- Migration tracking: `...`
- Sessions: `...`
- Job queues: `...`
- Cache: `...`
## Large Table Warnings
<!-- For tables >1M rows, add a warning row. For >10M rows, mark "VERY LARGE — always filter by date/indexed column". -->
| Table | Rows | Required Safeguards |
|-------|------|---------------------|
| (list tables with >1M rows — add specific safeguards for each) | | |
## Query Anti-Patterns
Common mistakes that cause slow or incorrect queries:
| # | Anti-Pattern | Why It's Bad | Do Instead |
|---|-------------|--------------|------------|
| 1 | `SELECT * FROM large_table` without WHERE | Full table scan on millions of rows | Always filter by indexed column or date range |
| 2 | `COUNT(*)` on large tables without date filter | Scans entire table; can take minutes | Add `WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)` |
| 3 | Unfiltered JOIN between two large tables | Creates cartesian-like explosion | Add date range filters on both sides of the JOIN |
| 4 | `GROUP BY` on non-indexed columns of large tables | Full scan + temp table sort | Use indexed columns or filter to reduce dataset first |
| 5 | Using application tables instead of BI/analytics tables | Soft deletes cause undercounting; slower queries | Check if a denormalized analytics table exists |
## Common Business Questions
<!-- Capture common BI/analytics questions and the correct way to answer them. Look for report generators, dashboard endpoints, and analytics code in the codebase. -->
| # | Question | Tables Involved | Key Filters |
|---|----------|----------------|-------------|
| (document common questions found in analytics code, dashboards, or report generators) | | | |
## Common Query Patterns
### Daily Order Summary
~~~sql
SELECT DATE(created_at) as day, COUNT(*) as orders, SUM(total)/100 as revenue
FROM orders
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
AND deleted_at IS NULL
GROUP BY DATE(created_at);
~~~
# Database Schema Documentation
> **Last verified**: YYYY-MM-DD — verified against live database / derived from code analysis only (not verified against live database)
## Database Type
MongoDB
## CLI Command
<!-- Used by query-db skill -->
`mongosh "$MONGODB_URI"`
## Framework
[Detected framework, e.g., "Mongoose", "Doctrine ODM", "MongoEngine"]
## Database Overview
Brief description of what data this system holds.
## All Collections
<!-- List EVERY collection in the database, no exceptions. -->
| Collection | Purpose | Key Fields for Filtering/Grouping |
|------------|---------|-----------------------------------|
| (one row per collection — list ALL of them) | | |
## Field Mappings & Enums
| Collection.Field | Value | Meaning |
|------------------|-------|---------|
| orders.status | "pending" | Awaiting processing |
| ... | ... | ... |
## References (Relationships)
- `orders.customerId → customers._id`
- `orderItems.orderId → orders._id`
## Embedded Documents
| Collection | Embedded Field | Structure |
|------------|----------------|-----------|
| orders | items | Array of {productId, quantity, price} |
## Date Fields
| Collection.Field | Purpose |
|------------------|---------|
| orders.createdAt | Order creation timestamp |
## Indexes
List important indexes for query optimization.
## Query Anti-Patterns
| # | Anti-Pattern | Why It's Bad | Do Instead |
|---|-------------|--------------|------------|
| 1 | `db.collection.find({})` without limit | Returns all documents; can exhaust memory | Always add `.limit()` or use `$match` in aggregation |
| 2 | `$lookup` between two large collections | Effectively an unindexed nested loop join | Filter both collections first with `$match`, ensure foreign field is indexed |
| 3 | Large `allowDiskUse` aggregations without `$match` | Scans entire collection to disk | Add `$match` as first pipeline stage |
## Common Aggregation Patterns
### Daily Revenue
~~~javascript
db.orders.aggregate([
{ $match: { createdAt: { $gte: ISODate("2024-01-01") } } },
{ $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } },
total: { $sum: "$total" }, count: { $sum: 1 } } },
{ $sort: { _id: -1 } }
])
~~~
# Database Schema Documentation
> **Last verified**: YYYY-MM-DD — verified against live database / derived from code analysis only (not verified against live database)
## Database Type
Elasticsearch
## CLI Command
<!-- Used by query-db skill -->
`curl -s "$ES_URL"`
## Framework
[Detected framework, e.g., "FOSElastica", "elasticsearch-py", "elastic4s"]
## Index Overview
Brief description of what data is indexed.
## All Indices
| Index | Purpose | Key Fields |
|-------|---------|------------|
| products | Product catalog | name, category, price, stock |
| ... | ... | ... |
## Field Mappings
| Index.Field | Type | Notes |
|-------------|------|-------|
| products.price | scaled_float | Factor 100 (cents) |
| products.name | text + keyword | Use .keyword for aggregations |
## Date Fields
| Index.Field | Format |
|-------------|--------|
| orders.timestamp | epoch_millis |
## Nested Objects
| Index | Nested Field | Structure |
|-------|--------------|-----------|
| orders | items | Array of order line items |
## Query Anti-Patterns
| # | Anti-Pattern | Why It's Bad | Do Instead |
|---|-------------|--------------|------------|
| 1 | Large `size` value (>10000) | Heap pressure, slow response | Use `scroll` or `search_after` for pagination |
| 2 | Deep pagination with `from` + `size` | ES limits `from + size` to 10000 by default | Use `search_after` for deep pagination |
| 3 | `match_all` without `size: 0` on large indices | Returns all documents | Use `size: 0` for aggregation-only queries |
## Common Query Patterns
### Category Aggregation
~~~json
{
"size": 0,
"aggs": {
"by_category": {
"terms": { "field": "category.keyword" },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
}
}
~~~
# Database Schema Documentation
> **Last verified**: YYYY-MM-DD — verified against live database / derived from code analysis only (not verified against live database)
## Database Type
Redis
## CLI Command
<!-- Used by query-db skill -->
`redis-cli -u "$REDIS_URL"`
## Framework
[Detected framework, e.g., "ioredis", "redis-py", "Predis"]
## Data Overview
Brief description of what data is stored.
## Key Patterns
| Pattern | Type | Purpose |
|---------|------|---------|
| `user:{id}` | Hash | User profile data |
| `user:{id}:sessions` | Set | Active session IDs |
| `orders:daily:{date}` | Sorted Set | Orders by timestamp |
| `cache:product:{id}` | String (JSON) | Product cache |
| `stats:pageviews` | HyperLogLog | Unique visitor count |
## Data Structures
### user:{id} (Hash)
| Field | Description |
|-------|-------------|
| email | User email |
| name | Display name |
| created_at | Unix timestamp |
### orders:daily:{date} (Sorted Set)
- Score: Unix timestamp
- Member: Order ID
## TTL Patterns
| Pattern | TTL | Notes |
|---------|-----|-------|
| `cache:*` | 3600 | 1 hour cache |
| `session:*` | 86400 | 24 hour sessions |
## Query Anti-Patterns
| # | Anti-Pattern | Why It's Bad | Do Instead |
|---|-------------|--------------|------------|
| 1 | `KEYS *` in production | Blocks Redis (single-threaded) for seconds on large databases | Use `SCAN 0 MATCH pattern COUNT 100` for iteration |
| 2 | `FLUSHDB` / `FLUSHALL` without confirmation | Deletes all data instantly | Use targeted `DEL` or `UNLINK` for specific keys |
## Common Query Patterns
### Get user with recent orders
~~~redis
HGETALL user:123
ZREVRANGE orders:user:123 0 9 WITHSCORES
~~~
### Daily active users
~~~redis
PFCOUNT stats:dau:2024-01-15
~~~
# Database Schema Documentation
> **Last verified**: YYYY-MM-DD — verified against live BigQuery / derived from code analysis only (not verified against live database)
## Database Type
BigQuery
## CLI Command
<!-- Used by query-db skill -->
`bq query --use_legacy_sql=false --format=prettyjson --project_id="$BQ_PROJECT"`
## Datasets
| Dataset | Period | Description |
|---------|--------|-------------|
| archive_2023 | 2023-01-01 to 2023-12-31 | Year 2023 archived data |
| archive_2024 | 2024-01-01 to 2024-12-31 | Year 2024 archived data |
| archive_2025 | 2025-01-01 to 2025-12-31 | Year 2025 archived data |
## All Tables (per dataset)
<!-- Datasets share the same table structure. List each table once; note if specific datasets differ. -->
| Table | Purpose | Key Fields for Filtering/Grouping |
|-------|---------|-----------------------------------|
| (one row per table — list ALL of them) | | |
## Field Mappings & Enums
| Dataset.Table.Field | Value | Meaning |
|---------------------|-------|---------|
| *.orders.status | 0 | Pending |
| ... | ... | ... |
## Relationships
- `orders.user_id → users.id`
- `order_items.order_id → orders.id`
## Date/Time Fields
| Table.Field | Purpose | Notes |
|-------------|---------|-------|
| orders.created_at | Order creation | TIMESTAMP type — use TIMESTAMP functions |
## Money/Numeric Fields
| Table.Field | Unit | Notes |
|-------------|------|-------|
| orders.total | cents | Divide by 100 for display |
## Partitioning & Clustering
| Dataset.Table | Partition Column | Clustering Columns | Notes |
|---------------|-----------------|-------------------|-------|
| *.orders | created_at | status, user_id | Always filter on created_at to reduce bytes scanned |
## Cross-Dataset Query Pattern
When querying across years, use UNION ALL:
~~~sql
WITH all_orders AS (
SELECT * FROM \`project.archive_2024.orders\`
UNION ALL
SELECT * FROM \`project.archive_2025.orders\`
)
SELECT DATE(created_at) as day, COUNT(*) as total
FROM all_orders
WHERE created_at >= '2024-06-01'
GROUP BY day
ORDER BY day DESC;
~~~
## Query Anti-Patterns
| # | Anti-Pattern | Why It's Bad | Do Instead |
|---|-------------|--------------|------------|
| 1 | Missing partition filter | Full table scan — expensive (billed by bytes scanned) | Always filter on partition column |
| 2 | `SELECT *` on wide tables | Scans all columns — BigQuery is columnar | Select only needed columns |
| 3 | `UNION ALL` across all datasets without date filter | Scans every year's data | Only include datasets relevant to the date range |
| 4 | Using `LIMIT` to reduce cost | LIMIT does NOT reduce bytes scanned | Use `WHERE` filters on partitioned/clustered columns |
| 5 | Not using `--dry_run` for large queries | No cost visibility before execution | Run `--dry_run` first to estimate bytes scanned |
## Cost Estimation
Before running queries on large tables, use `--dry_run`:
~~~bash
bq query --use_legacy_sql=false --dry_run --project_id="$BQ_PROJECT" "QUERY"
~~~
BigQuery pricing: ~$5/TB scanned. Use `--maximum_bytes_billed=1000000000` (1 GB) to cap cost.
## Common Query Patterns
### Daily Summary (Single Year)
~~~sql
SELECT DATE(created_at) as day, COUNT(*) as orders, SUM(total)/100 as revenue
FROM \`project.archive_2025.orders\`
WHERE created_at >= '2025-01-01'
GROUP BY day
ORDER BY day DESC;
~~~
### Cross-Year Comparison
~~~sql
WITH all_orders AS (
SELECT * FROM \`project.archive_2024.orders\`
UNION ALL
SELECT * FROM \`project.archive_2025.orders\`
)
SELECT
EXTRACT(YEAR FROM created_at) as year,
EXTRACT(MONTH FROM created_at) as month,
COUNT(*) as orders,
SUM(total)/100 as revenue
FROM all_orders
GROUP BY year, month
ORDER BY year, month;
~~~
If the project uses multiple databases, create sections for each:
# Database Schema Documentation
## Databases Used
1. PostgreSQL (primary data)
2. Redis (caching, sessions)
3. Elasticsearch (search)
---
## PostgreSQL
[Include full SQL template sections here]
---
## Redis
[Include full Redis template sections here]
---
## Elasticsearch
[Include full Elasticsearch template sections here]