Skip to content

Commit 063c22b

Browse files
Fix/microgateway postgres blob schema (#381)
1 parent 5860375 commit 063c22b

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

microgateway/internal/database/connection_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
package database
33

44
import (
5+
"sync"
56
"testing"
67
"time"
78

89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
11+
"gorm.io/driver/postgres"
12+
"gorm.io/gorm/schema"
1013
)
1114

1215
func TestConnect_SQLite(t *testing.T) {
@@ -37,7 +40,7 @@ func TestConnect_SQLite(t *testing.T) {
3740
t.Run("InvalidDSN", func(t *testing.T) {
3841
invalidConfig := config
3942
invalidConfig.DSN = "/invalid/path/to/db.sqlite"
40-
43+
4144
_, err := Connect(invalidConfig)
4245
assert.Error(t, err)
4346
})
@@ -54,7 +57,27 @@ func TestConnect_UnsupportedDatabase(t *testing.T) {
5457
assert.Contains(t, err.Error(), "unsupported database type")
5558
}
5659

57-
// TestMigrate removed - GORM handles migrations automatically
60+
func TestControlPayload_PostgresPayloadColumnType(t *testing.T) {
61+
modelSchema, err := schema.Parse(&ControlPayload{}, &sync.Map{}, schema.NamingStrategy{})
62+
require.NoError(t, err)
63+
64+
payloadField := modelSchema.LookUpField("Payload")
65+
require.NotNil(t, payloadField)
66+
67+
columnType := postgres.Dialector{}.DataTypeOf(payloadField)
68+
assert.Equal(t, "bytea", columnType)
69+
}
70+
71+
func TestAnalyticsEvent_PostgresInteractionTypeColumnType(t *testing.T) {
72+
modelSchema, err := schema.Parse(&AnalyticsEvent{}, &sync.Map{}, schema.NamingStrategy{})
73+
require.NoError(t, err)
74+
75+
interactionTypeField := modelSchema.LookUpField("InteractionType")
76+
require.NotNil(t, interactionTypeField)
77+
78+
columnType := postgres.Dialector{}.DataTypeOf(interactionTypeField)
79+
assert.Equal(t, "text", columnType)
80+
}
5881

5982
func TestDatabaseConfig_Validation(t *testing.T) {
6083
tests := []struct {
@@ -90,7 +113,7 @@ func TestDatabaseConfig_Validation(t *testing.T) {
90113
t.Run(tt.name, func(t *testing.T) {
91114
// Test connection (may fail due to actual DB not available, but config should be valid)
92115
_, err := Connect(tt.config)
93-
116+
94117
if tt.hasError {
95118
assert.Error(t, err)
96119
} else {
@@ -124,4 +147,4 @@ func TestGormLogger(t *testing.T) {
124147
// but we can verify the function doesn't panic
125148
})
126149
}
127-
}
150+
}

microgateway/internal/database/models.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ type AnalyticsEvent struct {
174174
UserID uint `gorm:"index:idx_analytics_user"` // User who made the request
175175
Name string // Model name (e.g., "gpt-4", "claude-3-opus")
176176
Vendor string // LLM vendor (e.g., "openai", "anthropic")
177-
InteractionType string `gorm:"type:string;default:'proxy'"` // "chat" or "proxy"
177+
InteractionType string `gorm:"type:text;default:'proxy'"` // "chat" or "proxy"
178178
Choices int // Number of choices in response
179179
ToolCalls int // Number of tool calls made
180180
ChatID string // Chat session identifier
@@ -383,7 +383,7 @@ func (pkv *PluginKV) IsExpired() bool {
383383
type ControlPayload struct {
384384
ID uint `gorm:"primaryKey"`
385385
PluginID uint `gorm:"not null;index:idx_control_payload_plugin"`
386-
Payload []byte `gorm:"type:blob;not null"`
386+
Payload []byte `gorm:"not null"`
387387
CorrelationID string `gorm:"size:255;index:idx_control_payload_correlation"`
388388
Metadata datatypes.JSON `gorm:"type:json"`
389389
Sent bool `gorm:"default:false;index:idx_control_payload_sent"`
@@ -560,4 +560,4 @@ func (OAuthClientEdge) TableName() string { return "oauth_clients" }
560560
func (AccessTokenEdge) TableName() string { return "access_tokens" }
561561
func (AppTool) TableName() string { return "app_tools" }
562562
func (AppDatasource) TableName() string { return "app_datasources" }
563-
func (ToolFilter) TableName() string { return "tool_filters" }
563+
func (ToolFilter) TableName() string { return "tool_filters" }

0 commit comments

Comments
 (0)