Skip to content

Commit 50c4012

Browse files
committed
feat: added migrator to log store
1 parent 2bf3a1d commit 50c4012

File tree

6 files changed

+139
-4
lines changed

6 files changed

+139
-4
lines changed

framework/configstore/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/maximhq/bifrost/framework/configstore/migrator"
7+
"github.com/maximhq/bifrost/framework/migrator"
88
"gorm.io/gorm"
99
)
1010

framework/configstore/rdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"fmt"
88

99
"github.com/maximhq/bifrost/core/schemas"
10-
"github.com/maximhq/bifrost/framework/configstore/migrator"
10+
"github.com/maximhq/bifrost/framework/migrator"
1111
"github.com/maximhq/bifrost/framework/logstore"
1212
"github.com/maximhq/bifrost/framework/vectorstore"
1313
"gorm.io/gorm"

framework/configstore/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77

88
"github.com/maximhq/bifrost/core/schemas"
9-
"github.com/maximhq/bifrost/framework/configstore/migrator"
9+
"github.com/maximhq/bifrost/framework/migrator"
1010
"github.com/maximhq/bifrost/framework/logstore"
1111
"github.com/maximhq/bifrost/framework/vectorstore"
1212
"gorm.io/gorm"

framework/logstore/migrations.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package logstore
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/maximhq/bifrost/framework/migrator"
8+
"gorm.io/gorm"
9+
)
10+
11+
// Migrate performs the necessary database migrations.
12+
func triggerMigrations(ctx context.Context, db *gorm.DB) error {
13+
if err := migrationInit(ctx, db); err != nil {
14+
return err
15+
}
16+
if err := migrationUpdateObjectColumnValues(ctx, db); err != nil {
17+
return err
18+
}
19+
20+
return nil
21+
}
22+
23+
// migrationInit is the first migration
24+
func migrationInit(ctx context.Context, db *gorm.DB) error {
25+
m := migrator.New(db, migrator.DefaultOptions, []*migrator.Migration{{
26+
ID: "init",
27+
Migrate: func(tx *gorm.DB) error {
28+
tx = tx.WithContext(ctx)
29+
migrator := tx.Migrator()
30+
if !migrator.HasTable(&Log{}) {
31+
if err := migrator.CreateTable(&Log{}); err != nil {
32+
return err
33+
}
34+
}
35+
36+
return nil
37+
},
38+
Rollback: func(tx *gorm.DB) error {
39+
tx = tx.WithContext(ctx)
40+
migrator := tx.Migrator()
41+
// Drop children first, then parents (adjust if your actual FKs differ)
42+
if err := migrator.DropTable(&Log{}); err != nil {
43+
return err
44+
}
45+
return nil
46+
},
47+
}})
48+
err := m.Migrate()
49+
if err != nil {
50+
return fmt.Errorf("error while running db migration: %s", err.Error())
51+
}
52+
return nil
53+
}
54+
55+
// migrationUpdateObjectColumnValues updates the object column values from old format to new format
56+
func migrationUpdateObjectColumnValues(ctx context.Context, db *gorm.DB) error {
57+
m := migrator.New(db, migrator.DefaultOptions, []*migrator.Migration{{
58+
ID: "update_object_column_values",
59+
Migrate: func(tx *gorm.DB) error {
60+
tx = tx.WithContext(ctx)
61+
62+
// Use a single CASE statement for efficient bulk update
63+
// This is much more efficient than multiple UPDATE statements
64+
updateSQL := `
65+
UPDATE logs
66+
SET object_type = CASE object_type
67+
WHEN 'chat.completion' THEN 'chat_completion'
68+
WHEN 'text.completion' THEN 'text_completion'
69+
WHEN 'completion' THEN 'text_completion'
70+
WHEN 'list' THEN 'embedding'
71+
WHEN 'audio.speech' THEN 'speech'
72+
WHEN 'audio.transcription' THEN 'transcription'
73+
WHEN 'chat.completion.chunk' THEN 'chat_completion_stream'
74+
WHEN 'audio.speech.chunk' THEN 'speech_stream'
75+
WHEN 'audio.transcription.chunk' THEN 'transcription_stream'
76+
WHEN 'response' THEN 'responses'
77+
WHEN 'responses.response' THEN 'responses'
78+
WHEN 'response.completion.chunk' THEN 'responses_stream'
79+
ELSE object_type
80+
END
81+
WHERE object_type IN (
82+
'chat.completion', 'text.completion', 'completion', 'list',
83+
'audio.speech', 'audio.transcription', 'chat.completion.chunk',
84+
'audio.speech.chunk', 'audio.transcription.chunk',
85+
'response', 'response.completion.chunk', 'responses.response'
86+
)`
87+
88+
result := tx.Exec(updateSQL)
89+
if result.Error != nil {
90+
return fmt.Errorf("failed to update object_type values: %w", result.Error)
91+
}
92+
93+
return nil
94+
},
95+
Rollback: func(tx *gorm.DB) error {
96+
tx = tx.WithContext(ctx)
97+
98+
// Use a single CASE statement for efficient bulk rollback
99+
rollbackSQL := `
100+
UPDATE logs
101+
SET object_type = CASE object_type
102+
WHEN 'chat_completion' THEN 'chat.completion'
103+
WHEN 'text_completion' THEN 'text.completion'
104+
WHEN 'embedding' THEN 'list'
105+
WHEN 'speech' THEN 'audio.speech'
106+
WHEN 'transcription' THEN 'audio.transcription'
107+
WHEN 'chat_completion_stream' THEN 'chat.completion.chunk'
108+
WHEN 'speech_stream' THEN 'audio.speech.chunk'
109+
WHEN 'transcription_stream' THEN 'audio.transcription.chunk'
110+
WHEN 'responses' THEN 'response'
111+
WHEN 'responses_stream' THEN 'response.completion.chunk'
112+
ELSE object_type
113+
END
114+
WHERE object_type IN (
115+
'chat_completion', 'text_completion', 'embedding', 'speech',
116+
'transcription', 'chat_completion_stream', 'speech_stream',
117+
'transcription_stream', 'responses', 'responses_stream'
118+
)`
119+
120+
result := tx.Exec(rollbackSQL)
121+
if result.Error != nil {
122+
return fmt.Errorf("failed to rollback object_type values: %w", result.Error)
123+
}
124+
125+
return nil
126+
},
127+
}})
128+
129+
err := m.Migrate()
130+
if err != nil {
131+
return fmt.Errorf("error while running object column migration: %s", err.Error())
132+
}
133+
return nil
134+
}

framework/logstore/sqlite.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ func newSqliteLogStore(ctx context.Context, config *SQLiteConfig, logger schemas
2222
if err != nil {
2323
return nil, err
2424
}
25-
if err := db.WithContext(ctx).AutoMigrate(&Log{}); err != nil {
25+
// Run migrations
26+
if err := triggerMigrations(ctx, db); err != nil {
2627
return nil, err
2728
}
2829
return &RDBLogStore{db: db, logger: logger}, nil
File renamed without changes.

0 commit comments

Comments
 (0)