docs: clarify sqlc DDL support limitations and correct implementation

**Discovery**: sqlc's DDL support is database-engine specific:

 **PostgreSQL**: sqlc generates functions for CREATE INDEX, CREATE FUNCTION
 **SQLite**: sqlc does NOT generate CREATE INDEX functions
 **Both**: sqlc does NOT generate CREATE TRIGGER functions

**Corrected Implementation**:
- Use sqlc-generated setup functions where available (tables always, PostgreSQL indexes)
- Use manual SQL where sqlc doesn't support (SQLite indexes, all triggers)
- Comments clarify why manual SQL is needed in each case

**Architecture Principle**: Use sqlc for what it supports, supplement with manual SQL for what it doesn't - this is the recommended hybrid approach.
This commit is contained in:
2025-09-09 00:28:10 +02:00
parent bab329b429
commit 4dc479ba9e
3 changed files with 17 additions and 7 deletions

12
TODO.md
View File

@@ -238,4 +238,14 @@ CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id);
- Keep schema in separate `.sql` files
- **Trade-off**: Not processed by sqlc, less type safety
**Next Action**: Implement Option 1 (Schema-as-Query) to maintain consistency with sqlc workflow while eliminating duplicate schema definitions.
**✅ COMPLETED**: Implemented Option 1 (Schema-as-Query) with important discovery:
**sqlc Limitations Discovered**:
- ✅ sqlc generates functions for `CREATE TABLE` and `ALTER TABLE` statements
- ❌ sqlc does **NOT** generate functions for `CREATE INDEX` or `CREATE TRIGGER` statements
- 🔧 **Solution**: Handle table creation via sqlc-generated functions, handle indexes/triggers manually in Go initialization code
**Final Implementation**:
- `db/*/setup.sql` - Contains table creation queries (sqlc generates type-safe functions)
- `internal/db/database.go` - Manual index/trigger creation using raw SQL
- **Best Practice**: Use sqlc for what it supports, manual SQL for what it doesn't