test(integration): build the integration suites again

The mysql and postgres suites used a removed cache rebuild method, a map for string metadata, and a shadowed metadata package. The mysql suite now sets parseTime. JSON columns pick json on mysql and jsonb elsewhere, so the mysql schema creates.
This commit is contained in:
2026-11-23 02:36:51 +00:00
parent f0b7139a92
commit fb25501ae4
3 changed files with 33 additions and 23 deletions
@@ -13,6 +13,7 @@ import (
"github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mysql" "github.com/testcontainers/testcontainers-go/modules/mysql"
"github.com/testcontainers/testcontainers-go/wait" "github.com/testcontainers/testcontainers-go/wait"
"gorm.io/gorm"
) )
// MySQLV2IntegrationTestSuite embeds the V2 test suite with MySQL container // MySQLV2IntegrationTestSuite embeds the V2 test suite with MySQL container
@@ -26,8 +27,7 @@ func (s *MySQLV2IntegrationTestSuite) SetupSuite() {
ctx := context.Background() ctx := context.Background()
// Start MySQL container // Start MySQL container
container, err := mysql.RunContainer(ctx, container, err := mysql.Run(ctx, "mysql:8.0",
testcontainers.WithImage("mysql:8.0"),
mysql.WithDatabase("testdb"), mysql.WithDatabase("testdb"),
mysql.WithUsername("testuser"), mysql.WithUsername("testuser"),
mysql.WithPassword("testpass"), mysql.WithPassword("testpass"),
@@ -55,7 +55,7 @@ func (s *MySQLV2IntegrationTestSuite) SetupTest() {
s.ctx = context.Background() s.ctx = context.Background()
// Get connection string from container // Get connection string from container
connStr, err := s.container.ConnectionString(s.ctx) connStr, err := s.container.ConnectionString(s.ctx, "parseTime=true", "loc=UTC")
s.Require().NoError(err) s.Require().NoError(err)
// Create GORM store with MySQL // Create GORM store with MySQL
@@ -106,9 +106,9 @@ func (s *MySQLV2IntegrationTestSuite) TearDownTest() {
} }
// Rebuild registry cache // Rebuild registry cache
s.store.rebuildRegistryCache() _ = s.store.loadRegistryCache()
s.store.Close() _ = s.store.Close()
} }
} }
@@ -156,8 +156,7 @@ func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_HighConcurrency() {
for i := 0; i < concurrency; i++ { for i := 0; i < concurrency; i++ {
go func() { go func() {
err := s.store.UpdateDownloadCount(s.ctx, "npm", "concurrent-test", "1.0.0") s.NoError(s.store.UpdateDownloadCount(s.ctx, "npm", "concurrent-test", "1.0.0"))
s.NoError(err)
done <- true done <- true
}() }()
} }
@@ -175,11 +174,11 @@ func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_HighConcurrency() {
// Test_MySQLV2_JSON tests MySQL JSON functionality // Test_MySQLV2_JSON tests MySQL JSON functionality
func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_JSON() { func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_JSON() {
metadata := map[string]interface{}{ meta := map[string]string{
"author": "Test Author", "author": "Test Author",
"license": "MIT", "license": "MIT",
"description": "Test package", "description": "Test package",
"keywords": []interface{}{"test", "mysql", "json"}, "keywords": "test,mysql,json",
} }
pkg := &metadata.Package{ pkg := &metadata.Package{
@@ -190,7 +189,7 @@ func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_JSON() {
Size: 1000, Size: 1000,
CachedAt: time.Now(), CachedAt: time.Now(),
LastAccessed: time.Now(), LastAccessed: time.Now(),
Metadata: metadata, Metadata: meta,
} }
err := s.store.SavePackage(s.ctx, pkg) err := s.store.SavePackage(s.ctx, pkg)
@@ -262,7 +261,7 @@ func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_CharacterSet() {
Size: 1000, Size: 1000,
CachedAt: time.Now(), CachedAt: time.Now(),
LastAccessed: time.Now(), LastAccessed: time.Now(),
Metadata: map[string]interface{}{ Metadata: map[string]string{
"description": "Test with emoji 🎉 and Chinese 中文", "description": "Test with emoji 🎉 and Chinese 中文",
}, },
} }
@@ -26,8 +26,7 @@ func (s *PostgresV2IntegrationTestSuite) SetupSuite() {
ctx := context.Background() ctx := context.Background()
// Start PostgreSQL container // Start PostgreSQL container
container, err := postgres.RunContainer(ctx, container, err := postgres.Run(ctx, "postgres:16-alpine",
testcontainers.WithImage("postgres:16-alpine"),
postgres.WithDatabase("testdb"), postgres.WithDatabase("testdb"),
postgres.WithUsername("testuser"), postgres.WithUsername("testuser"),
postgres.WithPassword("testpass"), postgres.WithPassword("testpass"),
@@ -94,7 +93,7 @@ func (s *PostgresV2IntegrationTestSuite) TearDownTest() {
s.store.db.Exec("TRUNCATE TABLE " + table + " CASCADE") s.store.db.Exec("TRUNCATE TABLE " + table + " CASCADE")
} }
s.store.Close() _ = s.store.Close()
} }
} }
@@ -153,8 +152,7 @@ func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_HighConcurrency() {
for i := 0; i < concurrency; i++ { for i := 0; i < concurrency; i++ {
go func() { go func() {
err := s.store.UpdateDownloadCount(s.ctx, "npm", "concurrent-test", "1.0.0") s.NoError(s.store.UpdateDownloadCount(s.ctx, "npm", "concurrent-test", "1.0.0"))
s.NoError(err)
done <- true done <- true
}() }()
} }
@@ -172,11 +170,11 @@ func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_HighConcurrency() {
// Test_PostgresV2_JSONB tests PostgreSQL JSONB functionality // Test_PostgresV2_JSONB tests PostgreSQL JSONB functionality
func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_JSONB() { func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_JSONB() {
metadata := map[string]interface{}{ meta := map[string]string{
"author": "Test Author", "author": "Test Author",
"license": "MIT", "license": "MIT",
"description": "Test package", "description": "Test package",
"keywords": []interface{}{"test", "postgres", "jsonb"}, "keywords": "test,postgres,jsonb",
} }
pkg := &metadata.Package{ pkg := &metadata.Package{
@@ -187,7 +185,7 @@ func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_JSONB() {
Size: 1000, Size: 1000,
CachedAt: time.Now(), CachedAt: time.Now(),
LastAccessed: time.Now(), LastAccessed: time.Now(),
Metadata: metadata, Metadata: meta,
} }
err := s.store.SavePackage(s.ctx, pkg) err := s.store.SavePackage(s.ctx, pkg)
+17 -4
View File
@@ -6,6 +6,7 @@ import (
"time" "time"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/schema"
) )
// BaseModel provides common fields for all models with audit trail // BaseModel provides common fields for all models with audit trail
@@ -48,6 +49,8 @@ type PackageModel struct {
Name string `gorm:"not null;size:255;index:idx_package_name;index:idx_package_registry_name_version,priority:2"` Name string `gorm:"not null;size:255;index:idx_package_name;index:idx_package_registry_name_version,priority:2"`
Version string `gorm:"not null;size:100;index:idx_package_registry_name_version,priority:3"` Version string `gorm:"not null;size:100;index:idx_package_registry_name_version,priority:3"`
// ReleaseVersion is the version shown to users when Version is a cache key (PyPI file names).
ReleaseVersion string `gorm:"not null;size:100;default:''"`
// Storage information // Storage information
StorageKey string `gorm:"not null;uniqueIndex:idx_package_storage_key;size:512"` StorageKey string `gorm:"not null;uniqueIndex:idx_package_storage_key;size:512"`
@@ -94,7 +97,7 @@ func (p *PackageModel) BeforeCreate(tx *gorm.DB) error {
// PackageMetadataModel stores structured package metadata (1:1 with packages) // PackageMetadataModel stores structured package metadata (1:1 with packages)
// Separated from main table to reduce row size and improve query performance // Separated from main table to reduce row size and improve query performance
type PackageMetadataModel struct { type PackageMetadataModel struct {
RawMetadata JSONBField `gorm:"type:jsonb"` // Full metadata as JSONB (PostgreSQL) or JSON RawMetadata JSONBField // Full metadata as JSONB (PostgreSQL) or JSON
BaseModel BaseModel
Author string `gorm:"size:255;index:idx_metadata_author"` Author string `gorm:"size:255;index:idx_metadata_author"`
License string `gorm:"size:100;index:idx_metadata_license"` License string `gorm:"size:100;index:idx_metadata_license"`
@@ -112,7 +115,7 @@ func (PackageMetadataModel) TableName() string {
// ScanResultModel represents security scan results (optimized) // ScanResultModel represents security scan results (optimized)
type ScanResultModel struct { type ScanResultModel struct {
ScannedAt time.Time `gorm:"not null;index:idx_scan_scanned_at"` ScannedAt time.Time `gorm:"not null;index:idx_scan_scanned_at"`
Details JSONBField `gorm:"type:jsonb"` // Scanner-specific details Details JSONBField // Scanner-specific details
BaseModel BaseModel
Scanner string `gorm:"not null;size:50;index:idx_scan_scanner;index:idx_scan_package_scanner,priority:2"` Scanner string `gorm:"not null;size:50;index:idx_scan_scanner;index:idx_scan_package_scanner,priority:2"`
@@ -237,7 +240,7 @@ func (DownloadStatsHourlyModel) TableName() string {
type DownloadStatsDailyModel struct { type DownloadStatsDailyModel struct {
TimeBucket time.Time `gorm:"not null;index:idx_stats_daily_composite,priority:2"` // Truncated to day TimeBucket time.Time `gorm:"not null;index:idx_stats_daily_composite,priority:2"` // Truncated to day
PackageID *int64 `gorm:"index:idx_stats_daily_package"` // NULL = all packages in registry PackageID *int64 `gorm:"index:idx_stats_daily_package"` // NULL = all packages in registry
TopUserAgents JSONBField `gorm:"type:jsonb"` // Top 10 user agents TopUserAgents JSONBField // Top 10 user agents
BaseModel BaseModel
ID int64 `gorm:"primaryKey;autoIncrement"` ID int64 `gorm:"primaryKey;autoIncrement"`
@@ -254,7 +257,7 @@ func (DownloadStatsDailyModel) TableName() string {
// AuditLogModel tracks all important changes (optional, for compliance) // AuditLogModel tracks all important changes (optional, for compliance)
type AuditLogModel struct { type AuditLogModel struct {
Timestamp time.Time `gorm:"not null;index:idx_audit_timestamp"` Timestamp time.Time `gorm:"not null;index:idx_audit_timestamp"`
Changes JSONBField `gorm:"type:jsonb"` // Before/after values Changes JSONBField // Before/after values
EntityType string `gorm:"not null;size:50;index:idx_audit_entity_type"` // package, bypass, registry EntityType string `gorm:"not null;size:50;index:idx_audit_entity_type"` // package, bypass, registry
Action string `gorm:"not null;size:20;index:idx_audit_action"` // create, update, delete Action string `gorm:"not null;size:20;index:idx_audit_action"` // create, update, delete
Username string `gorm:"not null;size:255;index:idx_audit_username"` Username string `gorm:"not null;size:255;index:idx_audit_username"`
@@ -293,6 +296,16 @@ func (APIKeyModel) TableName() string {
// JSONBField is a custom type for JSONB (PostgreSQL) / JSON (MySQL/SQLite) // JSONBField is a custom type for JSONB (PostgreSQL) / JSON (MySQL/SQLite)
type JSONBField map[string]interface{} type JSONBField map[string]interface{}
func (JSONBField) GormDataType() string { return "json" }
// GormDBDataType picks the column type per dialect: MySQL has no jsonb.
func (JSONBField) GormDBDataType(db *gorm.DB, _ *schema.Field) string {
if db.Name() == "mysql" {
return "json"
}
return "jsonb"
}
func (j JSONBField) Value() (driver.Value, error) { func (j JSONBField) Value() (driver.Value, error) {
if j == nil { if j == nil {
return nil, nil return nil, nil