Skip to content

Commit 7620085

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

File tree

18 files changed

+150
-17
lines changed

18 files changed

+150
-17
lines changed

core/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.5
1+
1.2.6

framework/changelog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!-- The pattern we follow here is to keep the changelog for the latest version -->
22
<!-- Old changelogs are automatically attached to the GitHub releases -->
33

4-
- Upgrade dependency: core to 1.2.5
5-
- Feat: User table added to config store.
4+
- Upgrade dependency: core to 1.2.6
5+
- Feat: Moved the migrator package to a more general location and added database migrations for the logstore to standardize object type values.

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: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
updateSQL := `
63+
UPDATE logs
64+
SET object_type = CASE object_type
65+
WHEN 'chat.completion' THEN 'chat_completion'
66+
WHEN 'text.completion' THEN 'text_completion'
67+
WHEN 'completion' THEN 'text_completion'
68+
WHEN 'list' THEN 'embedding'
69+
WHEN 'audio.speech' THEN 'speech'
70+
WHEN 'audio.transcription' THEN 'transcription'
71+
WHEN 'chat.completion.chunk' THEN 'chat_completion_stream'
72+
WHEN 'audio.speech.chunk' THEN 'speech_stream'
73+
WHEN 'audio.transcription.chunk' THEN 'transcription_stream'
74+
WHEN 'response' THEN 'responses'
75+
WHEN 'responses.response' THEN 'responses'
76+
WHEN 'response.completion.chunk' THEN 'responses_stream'
77+
ELSE object_type
78+
END
79+
WHERE object_type IN (
80+
'chat.completion', 'text.completion', 'completion', 'list',
81+
'audio.speech', 'audio.transcription', 'chat.completion.chunk',
82+
'audio.speech.chunk', 'audio.transcription.chunk',
83+
'response', 'response.completion.chunk', 'responses.response'
84+
)`
85+
86+
result := tx.Exec(updateSQL)
87+
if result.Error != nil {
88+
return fmt.Errorf("failed to update object_type values: %w", result.Error)
89+
}
90+
91+
return nil
92+
},
93+
Rollback: func(tx *gorm.DB) error {
94+
tx = tx.WithContext(ctx)
95+
96+
// Use a single CASE statement for efficient bulk rollback
97+
rollbackSQL := `
98+
UPDATE logs
99+
SET object_type = CASE object_type
100+
WHEN 'chat_completion' THEN 'chat.completion'
101+
WHEN 'text_completion' THEN 'text.completion'
102+
WHEN 'embedding' THEN 'list'
103+
WHEN 'speech' THEN 'audio.speech'
104+
WHEN 'transcription' THEN 'audio.transcription'
105+
WHEN 'chat_completion_stream' THEN 'chat.completion.chunk'
106+
WHEN 'speech_stream' THEN 'audio.speech.chunk'
107+
WHEN 'transcription_stream' THEN 'audio.transcription.chunk'
108+
WHEN 'responses' THEN 'response'
109+
WHEN 'responses_stream' THEN 'response.completion.chunk'
110+
ELSE object_type
111+
END
112+
WHERE object_type IN (
113+
'chat_completion', 'text_completion', 'embedding', 'speech',
114+
'transcription', 'chat_completion_stream', 'speech_stream',
115+
'transcription_stream', 'responses', 'responses_stream'
116+
)`
117+
118+
result := tx.Exec(rollbackSQL)
119+
if result.Error != nil {
120+
return fmt.Errorf("failed to rollback object_type values: %w", result.Error)
121+
}
122+
123+
return nil
124+
},
125+
}})
126+
127+
err := m.Migrate()
128+
if err != nil {
129+
return fmt.Errorf("error while running object column migration: %s", err.Error())
130+
}
131+
return nil
132+
}

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.

framework/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.5
1+
1.1.6

plugins/governance/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.3.5
1+
1.3.6

0 commit comments

Comments
 (0)