@@ -125,6 +125,12 @@ func triggerMigrations(ctx context.Context, db *gorm.DB) error {
125125 if err := migrationAddUseForBatchAPIColumnAndS3BucketsConfig (ctx , db ); err != nil {
126126 return err
127127 }
128+ if err := migrationAddModelConfigTable (ctx , db ); err != nil {
129+ return err
130+ }
131+ if err := migrationAddProviderGovernanceColumns (ctx , db ); err != nil {
132+ return err
133+ }
128134 return nil
129135}
130136
@@ -2080,3 +2086,112 @@ func migrationAddUseForBatchAPIColumnAndS3BucketsConfig(ctx context.Context, db
20802086 }
20812087 return nil
20822088}
2089+
2090+ // migrationAddModelConfigTable adds the governance_model_configs table
2091+ func migrationAddModelConfigTable (ctx context.Context , db * gorm.DB ) error {
2092+ m := migrator .New (db , migrator .DefaultOptions , []* migrator.Migration {{
2093+ ID : "add_model_config_table" ,
2094+ Migrate : func (tx * gorm.DB ) error {
2095+ tx = tx .WithContext (ctx )
2096+ migrator := tx .Migrator ()
2097+ if ! migrator .HasTable (& tables.TableModelConfig {}) {
2098+ if err := migrator .CreateTable (& tables.TableModelConfig {}); err != nil {
2099+ return err
2100+ }
2101+ }
2102+ return nil
2103+ },
2104+ Rollback : func (tx * gorm.DB ) error {
2105+ tx = tx .WithContext (ctx )
2106+ migrator := tx .Migrator ()
2107+ if err := migrator .DropTable (& tables.TableModelConfig {}); err != nil {
2108+ return err
2109+ }
2110+ return nil
2111+ },
2112+ }})
2113+ err := m .Migrate ()
2114+ if err != nil {
2115+ return fmt .Errorf ("error while running add model config table migration: %s" , err .Error ())
2116+ }
2117+ return nil
2118+ }
2119+
2120+ // migrationAddProviderGovernanceColumns adds budget_id and rate_limit_id columns to config_providers table
2121+ func migrationAddProviderGovernanceColumns (ctx context.Context , db * gorm.DB ) error {
2122+ m := migrator .New (db , migrator .DefaultOptions , []* migrator.Migration {{
2123+ ID : "add_provider_governance_columns" ,
2124+ Migrate : func (tx * gorm.DB ) error {
2125+ tx = tx .WithContext (ctx )
2126+ migrator := tx .Migrator ()
2127+ provider := & tables.TableProvider {}
2128+
2129+ // Add budget_id column if it doesn't exist
2130+ if ! migrator .HasColumn (provider , "budget_id" ) {
2131+ if err := migrator .AddColumn (provider , "BudgetID" ); err != nil {
2132+ return fmt .Errorf ("failed to add budget_id column: %w" , err )
2133+ }
2134+ // Create index for budget_id
2135+ if ! migrator .HasIndex (provider , "idx_provider_budget" ) {
2136+ if err := tx .Exec ("CREATE INDEX IF NOT EXISTS idx_provider_budget ON config_providers (budget_id)" ).Error ; err != nil {
2137+ return fmt .Errorf ("failed to create budget_id index: %w" , err )
2138+ }
2139+ }
2140+ }
2141+
2142+ // Add rate_limit_id column if it doesn't exist
2143+ if ! migrator .HasColumn (provider , "rate_limit_id" ) {
2144+ if err := migrator .AddColumn (provider , "RateLimitID" ); err != nil {
2145+ return fmt .Errorf ("failed to add rate_limit_id column: %w" , err )
2146+ }
2147+ // Create index for rate_limit_id
2148+ if ! migrator .HasIndex (provider , "idx_provider_rate_limit" ) {
2149+ if err := tx .Exec ("CREATE INDEX IF NOT EXISTS idx_provider_rate_limit ON config_providers (rate_limit_id)" ).Error ; err != nil {
2150+ return fmt .Errorf ("failed to create rate_limit_id index: %w" , err )
2151+ }
2152+ }
2153+ }
2154+
2155+ return nil
2156+ },
2157+ Rollback : func (tx * gorm.DB ) error {
2158+ tx = tx .WithContext (ctx )
2159+ migrator := tx .Migrator ()
2160+ provider := & tables.TableProvider {}
2161+
2162+ // Drop indexes first
2163+ if migrator .HasIndex (provider , "idx_provider_rate_limit" ) {
2164+ if err := tx .Exec ("DROP INDEX IF EXISTS idx_provider_rate_limit" ).Error ; err != nil {
2165+ return fmt .Errorf ("failed to drop rate_limit_id index: %w" , err )
2166+ }
2167+ }
2168+
2169+ if migrator .HasIndex (provider , "idx_provider_budget" ) {
2170+ if err := tx .Exec ("DROP INDEX IF EXISTS idx_provider_budget" ).Error ; err != nil {
2171+ return fmt .Errorf ("failed to drop budget_id index: %w" , err )
2172+ }
2173+ }
2174+
2175+ // Drop rate_limit_id column if it exists
2176+ if migrator .HasColumn (provider , "rate_limit_id" ) {
2177+ if err := migrator .DropColumn (provider , "RateLimitID" ); err != nil {
2178+ return fmt .Errorf ("failed to drop rate_limit_id column: %w" , err )
2179+ }
2180+ }
2181+
2182+ // Drop budget_id column if it exists
2183+ if migrator .HasColumn (provider , "budget_id" ) {
2184+ if err := migrator .DropColumn (provider , "BudgetID" ); err != nil {
2185+ return fmt .Errorf ("failed to drop budget_id column: %w" , err )
2186+ }
2187+ }
2188+
2189+ return nil
2190+ },
2191+ }})
2192+ err := m .Migrate ()
2193+ if err != nil {
2194+ return fmt .Errorf ("error while running add provider governance columns migration: %s" , err .Error ())
2195+ }
2196+ return nil
2197+ }
0 commit comments