refactor: implement database-specific schema architecture with schema-as-query pattern
🏗️ **Major Database Schema Refactoring** **Problem Solved**: Eliminated model duplication and multiple sources of truth by: - Removed duplicate models (`internal/models/content.go`) - Replaced inlined schema strings with sqlc-generated setup functions - Implemented database-specific schemas with proper NOT NULL constraints **Key Improvements**: ✅ **Single Source of Truth**: Database schemas define all types, no manual sync needed ✅ **Clean Generated Types**: sqlc generates `string` and `int64` instead of `sql.NullString/sql.NullTime` ✅ **Schema-as-Query Pattern**: Setup functions generated by sqlc for type safety ✅ **Database-Specific Optimization**: SQLite INTEGER timestamps, PostgreSQL BIGINT timestamps ✅ **Cross-Database Compatibility**: Single codebase supports both SQLite and PostgreSQL **Architecture Changes**: - `db/sqlite/` - SQLite-specific schema and setup queries - `db/postgresql/` - PostgreSQL-specific schema and setup queries - `db/queries/` - Cross-database CRUD queries using `sqlc.arg()` syntax - `internal/db/database.go` - Database abstraction with runtime selection - `internal/api/models.go` - Clean API models for requests/responses **Version Control System**: Complete element-level history with user attribution and rollback **Verification**: ✅ Full API workflow tested (create → update → rollback → versions) **Production Ready**: Supports SQLite (development) → PostgreSQL (production) migration
This commit is contained in:
29
insertr-server/db/queries/versions.sql
Normal file
29
insertr-server/db/queries/versions.sql
Normal file
@@ -0,0 +1,29 @@
|
||||
-- name: CreateContentVersion :exec
|
||||
INSERT INTO content_versions (content_id, site_id, value, type, created_by)
|
||||
VALUES (sqlc.arg(content_id), sqlc.arg(site_id), sqlc.arg(value), sqlc.arg(type), sqlc.arg(created_by));
|
||||
|
||||
-- name: GetContentVersionHistory :many
|
||||
SELECT version_id, content_id, site_id, value, type, created_at, created_by
|
||||
FROM content_versions
|
||||
WHERE content_id = sqlc.arg(content_id) AND site_id = sqlc.arg(site_id)
|
||||
ORDER BY created_at DESC
|
||||
LIMIT sqlc.arg(limit_count);
|
||||
|
||||
-- name: GetContentVersion :one
|
||||
SELECT version_id, content_id, site_id, value, type, created_at, created_by
|
||||
FROM content_versions
|
||||
WHERE version_id = sqlc.arg(version_id);
|
||||
|
||||
-- name: GetAllVersionsForSite :many
|
||||
SELECT
|
||||
cv.version_id, cv.content_id, cv.site_id, cv.value, cv.type, cv.created_at, cv.created_by,
|
||||
c.value as current_value
|
||||
FROM content_versions cv
|
||||
LEFT JOIN content c ON cv.content_id = c.id AND cv.site_id = c.site_id
|
||||
WHERE cv.site_id = sqlc.arg(site_id)
|
||||
ORDER BY cv.created_at DESC
|
||||
LIMIT sqlc.arg(limit_count);
|
||||
|
||||
-- name: DeleteOldVersions :exec
|
||||
DELETE FROM content_versions
|
||||
WHERE created_at < sqlc.arg(created_before) AND site_id = sqlc.arg(site_id);
|
||||
Reference in New Issue
Block a user