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