feat: migrate tags and contributors from TEXT to TEXT[] arrays
Convert tags and contributors columns from comma-separated strings to PostgreSQL TEXT[] arrays for better data normalization and query performance. Database Changes: - schema.sql: Change tags/contributors from TEXT to TEXT[] - schema.sql: Add GIN indexes for fast array searches - queries.sql: Update search queries to use ANY() operator - queries.sql: Update fuzzy search with unnest() for arrays Generated Code (sqlc): - models.go: Auto-generated with []string types for tags/contributors - queries.sql.go: Auto-generated with proper array handling Handler Changes: - media.go: Update request structs to use []string for tags/contributors - media.go: Remove pgtype.Text wrapping, use direct array assignment - media.go: Add tag normalization in CreateMediaItemHandler - collections.go: Update tags evaluation to join arrays for comparison - collections.go: Add strings import for Join() function Service Changes: - ebook_scanner.go: Update EbookMetadata struct to use []string - ebook_scanner.go: Remove string Join(), assign arrays directly - collection_service.go: Update tags rule evaluation to join arrays - collection_service.go: Add strings import New Utilities: - internal/utils/tags.go: Create NormalizeTags(), JoinTags(), SplitTags() - Normalizes tags by trimming, lowercasing, removing duplicates/empties API Documentation: - bruno/media-items/Create Media Item.bru: Update examples to use arrays - bruno/media-items/Update Media Item.bru: Update examples to use arrays - Update docs: tags/contributors now array of string Breaking Change: - JSON format changes from "tags": "tag1,tag2" to "tags": ["tag1", "tag2"] - Tests already use array format (no changes needed) Benefits: - GIN indexes enable faster array searches - Normalization prevents data quality issues (case, duplicates) - Array operations use PostgreSQL native operators (ANY, &&, unnest) - Better separation of concerns (no string parsing in application)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NormalizeTags normalizes an array of tags by:
|
||||
// 1. Converting to lowercase
|
||||
// 2. Trimming whitespace
|
||||
// 3. Removing duplicates
|
||||
// 4. Removing empty strings
|
||||
func NormalizeTags(tags []string) []string {
|
||||
seen := make(map[string]struct{})
|
||||
var normalized []string
|
||||
|
||||
for _, tag := range tags {
|
||||
// Trim whitespace
|
||||
tag = strings.TrimSpace(tag)
|
||||
|
||||
// Skip empty tags
|
||||
if tag == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Convert to lowercase
|
||||
tag = strings.ToLower(tag)
|
||||
|
||||
// Check for duplicates
|
||||
if _, exists := seen[tag]; !exists {
|
||||
seen[tag] = struct{}{}
|
||||
normalized = append(normalized, tag)
|
||||
}
|
||||
}
|
||||
|
||||
return normalized
|
||||
}
|
||||
|
||||
// JoinTags converts a string array to a comma-separated string
|
||||
// Maintained for backward compatibility with external systems
|
||||
func JoinTags(tags []string) string {
|
||||
return strings.Join(tags, ", ")
|
||||
}
|
||||
|
||||
// SplitTags converts a comma-separated string to a normalized array
|
||||
func SplitTags(tags string) []string {
|
||||
if tags == "" {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
parts := strings.Split(tags, ",")
|
||||
return NormalizeTags(parts)
|
||||
}
|
||||
Reference in New Issue
Block a user