🏗️ **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
176 lines
4.4 KiB
Go
176 lines
4.4 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.29.0
|
|
// source: versions.sql
|
|
|
|
package postgresql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
const createContentVersion = `-- name: CreateContentVersion :exec
|
|
INSERT INTO content_versions (content_id, site_id, value, type, created_by)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
`
|
|
|
|
type CreateContentVersionParams struct {
|
|
ContentID string `json:"content_id"`
|
|
SiteID string `json:"site_id"`
|
|
Value string `json:"value"`
|
|
Type string `json:"type"`
|
|
CreatedBy string `json:"created_by"`
|
|
}
|
|
|
|
func (q *Queries) CreateContentVersion(ctx context.Context, arg CreateContentVersionParams) error {
|
|
_, err := q.db.ExecContext(ctx, createContentVersion,
|
|
arg.ContentID,
|
|
arg.SiteID,
|
|
arg.Value,
|
|
arg.Type,
|
|
arg.CreatedBy,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const deleteOldVersions = `-- name: DeleteOldVersions :exec
|
|
DELETE FROM content_versions
|
|
WHERE created_at < $1 AND site_id = $2
|
|
`
|
|
|
|
type DeleteOldVersionsParams struct {
|
|
CreatedBefore int64 `json:"created_before"`
|
|
SiteID string `json:"site_id"`
|
|
}
|
|
|
|
func (q *Queries) DeleteOldVersions(ctx context.Context, arg DeleteOldVersionsParams) error {
|
|
_, err := q.db.ExecContext(ctx, deleteOldVersions, arg.CreatedBefore, arg.SiteID)
|
|
return err
|
|
}
|
|
|
|
const getAllVersionsForSite = `-- 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 = $1
|
|
ORDER BY cv.created_at DESC
|
|
LIMIT $2
|
|
`
|
|
|
|
type GetAllVersionsForSiteParams struct {
|
|
SiteID string `json:"site_id"`
|
|
LimitCount int32 `json:"limit_count"`
|
|
}
|
|
|
|
type GetAllVersionsForSiteRow struct {
|
|
VersionID int32 `json:"version_id"`
|
|
ContentID string `json:"content_id"`
|
|
SiteID string `json:"site_id"`
|
|
Value string `json:"value"`
|
|
Type string `json:"type"`
|
|
CreatedAt int64 `json:"created_at"`
|
|
CreatedBy string `json:"created_by"`
|
|
CurrentValue sql.NullString `json:"current_value"`
|
|
}
|
|
|
|
func (q *Queries) GetAllVersionsForSite(ctx context.Context, arg GetAllVersionsForSiteParams) ([]GetAllVersionsForSiteRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getAllVersionsForSite, arg.SiteID, arg.LimitCount)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetAllVersionsForSiteRow
|
|
for rows.Next() {
|
|
var i GetAllVersionsForSiteRow
|
|
if err := rows.Scan(
|
|
&i.VersionID,
|
|
&i.ContentID,
|
|
&i.SiteID,
|
|
&i.Value,
|
|
&i.Type,
|
|
&i.CreatedAt,
|
|
&i.CreatedBy,
|
|
&i.CurrentValue,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getContentVersion = `-- name: GetContentVersion :one
|
|
SELECT version_id, content_id, site_id, value, type, created_at, created_by
|
|
FROM content_versions
|
|
WHERE version_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetContentVersion(ctx context.Context, versionID int32) (ContentVersion, error) {
|
|
row := q.db.QueryRowContext(ctx, getContentVersion, versionID)
|
|
var i ContentVersion
|
|
err := row.Scan(
|
|
&i.VersionID,
|
|
&i.ContentID,
|
|
&i.SiteID,
|
|
&i.Value,
|
|
&i.Type,
|
|
&i.CreatedAt,
|
|
&i.CreatedBy,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getContentVersionHistory = `-- name: GetContentVersionHistory :many
|
|
SELECT version_id, content_id, site_id, value, type, created_at, created_by
|
|
FROM content_versions
|
|
WHERE content_id = $1 AND site_id = $2
|
|
ORDER BY created_at DESC
|
|
LIMIT $3
|
|
`
|
|
|
|
type GetContentVersionHistoryParams struct {
|
|
ContentID string `json:"content_id"`
|
|
SiteID string `json:"site_id"`
|
|
LimitCount int32 `json:"limit_count"`
|
|
}
|
|
|
|
func (q *Queries) GetContentVersionHistory(ctx context.Context, arg GetContentVersionHistoryParams) ([]ContentVersion, error) {
|
|
rows, err := q.db.QueryContext(ctx, getContentVersionHistory, arg.ContentID, arg.SiteID, arg.LimitCount)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ContentVersion
|
|
for rows.Next() {
|
|
var i ContentVersion
|
|
if err := rows.Scan(
|
|
&i.VersionID,
|
|
&i.ContentID,
|
|
&i.SiteID,
|
|
&i.Value,
|
|
&i.Type,
|
|
&i.CreatedAt,
|
|
&i.CreatedBy,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|