- Notifications
You must be signed in to change notification settings - Fork928
docs: add comprehensive development documentation#18646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
ThomasK33 wants to merge1 commit intothomask33/06-27-feat_oauth2_implement_rfc_7591_7592_dynamic_client_registration_for_mcp_complianceChoose a base branch fromthomask33/06-27-docs_refactor_claude.md_to_use_import_system_and_comprehensive_workflows
base:thomask33/06-27-feat_oauth2_implement_rfc_7591_7592_dynamic_client_registration_for_mcp_compliance
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+1,157 −481
Draft
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
258 changes: 258 additions & 0 deletions.claude/docs/DATABASE.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
# Database Development Patterns | ||
## Database Work Overview | ||
### Database Generation Process | ||
1. Modify SQL files in `coderd/database/queries/` | ||
2. Run `make gen` | ||
3. If errors about audit table, update `enterprise/audit/table.go` | ||
4. Run `make gen` again | ||
5. Run `make lint` to catch any remaining issues | ||
## Migration Guidelines | ||
### Creating Migration Files | ||
**Location**: `coderd/database/migrations/` | ||
**Format**: `{number}_{description}.{up|down}.sql` | ||
- Number must be unique and sequential | ||
- Always include both up and down migrations | ||
### Helper Scripts | ||
| Script | Purpose | | ||
|--------|---------| | ||
| `./coderd/database/migrations/create_migration.sh "migration name"` | Creates new migration files | | ||
| `./coderd/database/migrations/fix_migration_numbers.sh` | Renumbers migrations to avoid conflicts | | ||
| `./coderd/database/migrations/create_fixture.sh "fixture name"` | Creates test fixtures for migrations | | ||
### Database Query Organization | ||
- **MUST DO**: Any changes to database - adding queries, modifying queries should be done in the `coderd/database/queries/*.sql` files | ||
- **MUST DO**: Queries are grouped in files relating to context - e.g. `prebuilds.sql`, `users.sql`, `oauth2.sql` | ||
- After making changes to any `coderd/database/queries/*.sql` files you must run `make gen` to generate respective ORM changes | ||
## Handling Nullable Fields | ||
Use `sql.NullString`, `sql.NullBool`, etc. for optional database fields: | ||
```go | ||
CodeChallenge: sql.NullString{ | ||
String: params.codeChallenge, | ||
Valid: params.codeChallenge != "", | ||
} | ||
``` | ||
Set `.Valid = true` when providing values. | ||
## Audit Table Updates | ||
If adding fields to auditable types: | ||
1. Update `enterprise/audit/table.go` | ||
2. Add each new field with appropriate action: | ||
- `ActionTrack`: Field should be tracked in audit logs | ||
- `ActionIgnore`: Field should be ignored in audit logs | ||
- `ActionSecret`: Field contains sensitive data | ||
3. Run `make gen` to verify no audit errors | ||
## In-Memory Database (dbmem) Updates | ||
### Critical Requirements | ||
When adding new fields to database structs: | ||
- **CRITICAL**: Update `coderd/database/dbmem/dbmem.go` in-memory implementations | ||
- The `Insert*` functions must include ALL new fields, not just basic ones | ||
- Common issue: Tests pass with real database but fail with in-memory database due to missing field mappings | ||
- Always verify in-memory database functions match the real database schema after migrations | ||
### Example Pattern | ||
```go | ||
// In dbmem.go - ensure ALL fields are included | ||
code := database.OAuth2ProviderAppCode{ | ||
ID: arg.ID, | ||
CreatedAt: arg.CreatedAt, | ||
// ... existing fields ... | ||
ResourceUri: arg.ResourceUri, // New field | ||
CodeChallenge: arg.CodeChallenge, // New field | ||
CodeChallengeMethod: arg.CodeChallengeMethod, // New field | ||
} | ||
``` | ||
## Database Architecture | ||
### Core Components | ||
- **PostgreSQL 13+** recommended for production | ||
- **Migrations** managed with `migrate` | ||
- **Database authorization** through `dbauthz` package | ||
### Authorization Patterns | ||
```go | ||
// Public endpoints needing system access (OAuth2 registration) | ||
app, err := api.Database.GetOAuth2ProviderAppByClientID(dbauthz.AsSystemRestricted(ctx), clientID) | ||
// Authenticated endpoints with user context | ||
app, err := api.Database.GetOAuth2ProviderAppByClientID(ctx, clientID) | ||
// System operations in middleware | ||
roles, err := db.GetAuthorizationUserRoles(dbauthz.AsSystemRestricted(ctx), userID) | ||
``` | ||
## Common Database Issues | ||
### Migration Issues | ||
1. **Migration conflicts**: Use `fix_migration_numbers.sh` to renumber | ||
2. **Missing down migration**: Always create both up and down files | ||
3. **Schema inconsistencies**: Verify against existing schema | ||
### Field Handling Issues | ||
1. **Nullable field errors**: Use `sql.Null*` types consistently | ||
2. **Missing audit entries**: Update `enterprise/audit/table.go` | ||
3. **dbmem inconsistencies**: Ensure in-memory implementations match schema | ||
### Query Issues | ||
1. **Query organization**: Group related queries in appropriate files | ||
2. **Generated code errors**: Run `make gen` after query changes | ||
3. **Performance issues**: Add appropriate indexes in migrations | ||
## Database Testing | ||
### Test Database Setup | ||
```go | ||
func TestDatabaseFunction(t *testing.T) { | ||
db := dbtestutil.NewDB(t) | ||
// Test with real database | ||
result, err := db.GetSomething(ctx, param) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, result) | ||
} | ||
``` | ||
### In-Memory Testing | ||
```go | ||
func TestInMemoryDatabase(t *testing.T) { | ||
db := dbmem.New() | ||
// Test with in-memory database | ||
result, err := db.GetSomething(ctx, param) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, result) | ||
} | ||
``` | ||
## Best Practices | ||
### Schema Design | ||
1. **Use appropriate data types**: VARCHAR for strings, TIMESTAMP for times | ||
2. **Add constraints**: NOT NULL, UNIQUE, FOREIGN KEY as appropriate | ||
3. **Create indexes**: For frequently queried columns | ||
4. **Consider performance**: Normalize appropriately but avoid over-normalization | ||
### Query Writing | ||
1. **Use parameterized queries**: Prevent SQL injection | ||
2. **Handle errors appropriately**: Check for specific error types | ||
3. **Use transactions**: For related operations that must succeed together | ||
4. **Optimize queries**: Use EXPLAIN to understand query performance | ||
### Migration Writing | ||
1. **Make migrations reversible**: Always include down migration | ||
2. **Test migrations**: On copy of production data if possible | ||
3. **Keep migrations small**: One logical change per migration | ||
4. **Document complex changes**: Add comments explaining rationale | ||
## Advanced Patterns | ||
### Complex Queries | ||
```sql | ||
-- Example: Complex join with aggregation | ||
SELECT | ||
u.id, | ||
u.username, | ||
COUNT(w.id) as workspace_count | ||
FROM users u | ||
LEFT JOIN workspaces w ON u.id = w.owner_id | ||
WHERE u.created_at > $1 | ||
GROUP BY u.id, u.username | ||
ORDER BY workspace_count DESC; | ||
``` | ||
### Conditional Queries | ||
```sql | ||
-- Example: Dynamic filtering | ||
SELECT * FROM oauth2_provider_apps | ||
WHERE | ||
($1::text IS NULL OR name ILIKE '%' || $1 || '%') | ||
AND ($2::uuid IS NULL OR organization_id = $2) | ||
ORDER BY created_at DESC; | ||
``` | ||
### Audit Patterns | ||
```go | ||
// Example: Auditable database operation | ||
func (q *sqlQuerier) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error) { | ||
// Implementation here | ||
// Audit the change | ||
if auditor := audit.FromContext(ctx); auditor != nil { | ||
auditor.Record(audit.UserUpdate{ | ||
UserID: arg.ID, | ||
Old: oldUser, | ||
New: newUser, | ||
}) | ||
} | ||
return newUser, nil | ||
} | ||
``` | ||
## Debugging Database Issues | ||
### Common Debug Commands | ||
```bash | ||
# Check database connection | ||
make test-postgres | ||
# Run specific database tests | ||
go test ./coderd/database/... -run TestSpecificFunction | ||
# Check query generation | ||
make gen | ||
# Verify audit table | ||
make lint | ||
``` | ||
### Debug Techniques | ||
1. **Enable query logging**: Set appropriate log levels | ||
2. **Use database tools**: pgAdmin, psql for direct inspection | ||
3. **Check constraints**: UNIQUE, FOREIGN KEY violations | ||
4. **Analyze performance**: Use EXPLAIN ANALYZE for slow queries | ||
### Troubleshooting Checklist | ||
- [ ] Migration files exist (both up and down) | ||
- [ ] `make gen` run after query changes | ||
- [ ] Audit table updated for new fields | ||
- [ ] In-memory database implementations updated | ||
- [ ] Nullable fields use `sql.Null*` types | ||
- [ ] Authorization context appropriate for endpoint type |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.