Class: RubyLlmAgents::UpgradeGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/ruby_llm_agents/upgrade_generator.rb

Overview

Upgrade generator for ruby_llm-agents

Usage:

rails generate ruby_llm_agents:upgrade

This will create any missing migrations for upgrading from older versions. It handles all upgrade scenarios:

  • v0.x/v1.x -> v2.0: Splits detail columns from executions to execution_details, removes deprecated columns, renames tenant_budgets to tenants

  • v2.0 -> latest: No-ops safely if already up to date

Instance Method Summary collapse

Instance Method Details

#create_add_assistant_prompt_migrationObject

Add assistant_prompt column to execution_details (v3.0 -> v3.1 upgrade)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/ruby_llm_agents/upgrade_generator.rb', line 83

def create_add_assistant_prompt_migration
  if column_exists?(:ruby_llm_agents_execution_details, :assistant_prompt)
    say_status :skip, "assistant_prompt column already exists on execution_details", :yellow
    return
  end

  unless table_exists?(:ruby_llm_agents_execution_details)
    say_status :skip, "execution_details table does not exist yet", :yellow
    return
  end

  say_status :upgrade, "Adding assistant_prompt to execution_details", :blue
  migration_template(
    "add_assistant_prompt_migration.rb.tt",
    File.join(db_migrate_path, "add_assistant_prompt_to_execution_details.rb")
  )
end

#create_add_dashboard_performance_indexes_migrationObject

Add dashboard performance indexes



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/generators/ruby_llm_agents/upgrade_generator.rb', line 102

def create_add_dashboard_performance_indexes_migration
  unless table_exists?(:ruby_llm_agents_executions)
    say_status :skip, "executions table does not exist yet", :yellow
    return
  end

  if index_exists?(:ruby_llm_agents_executions, [:status, :created_at])
    say_status :skip, "dashboard performance indexes already exist", :yellow
    return
  end

  say_status :upgrade, "Adding dashboard performance indexes", :blue
  migration_template(
    "add_dashboard_performance_indexes_migration.rb.tt",
    File.join(db_migrate_path, "add_dashboard_performance_indexes.rb")
  )
end

#create_add_usage_counters_migrationObject

Add usage counter columns to tenants (v3.x upgrade)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/ruby_llm_agents/upgrade_generator.rb', line 64

def create_add_usage_counters_migration
  unless table_exists?(:ruby_llm_agents_tenants)
    say_status :skip, "tenants table does not exist yet", :yellow
    return
  end

  if column_exists?(:ruby_llm_agents_tenants, :monthly_cost_spent)
    say_status :skip, "usage counter columns already exist on tenants", :yellow
    return
  end

  say_status :upgrade, "Adding usage counter columns to tenants", :blue
  migration_template(
    "add_usage_counters_to_tenants_migration.rb.tt",
    File.join(db_migrate_path, "add_usage_counters_to_ruby_llm_agents_tenants.rb")
  )
end

#create_overrides_migrationObject

Create overrides table for dashboard-managed agent settings



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/generators/ruby_llm_agents/upgrade_generator.rb', line 121

def create_overrides_migration
  if table_exists?(:ruby_llm_agents_overrides)
    say_status :skip, "ruby_llm_agents_overrides table already exists", :yellow
    return
  end

  say_status :upgrade, "Creating agent overrides table", :blue
  migration_template(
    "create_overrides_migration.rb.tt",
    File.join(db_migrate_path, "create_ruby_llm_agents_overrides.rb")
  )
end

#create_rename_tenant_budgets_migrationObject

Rename tenant_budgets to tenants (v1.x -> v2.0 upgrade)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/ruby_llm_agents/upgrade_generator.rb', line 43

def create_rename_tenant_budgets_migration
  # Skip if already using new table name
  if table_exists?(:ruby_llm_agents_tenants)
    say_status :skip, "ruby_llm_agents_tenants table already exists", :yellow
    return
  end

  # Only run if old table exists (needs upgrade)
  unless table_exists?(:ruby_llm_agents_tenant_budgets)
    say_status :skip, "No tenant_budgets table to upgrade", :yellow
    return
  end

  say_status :upgrade, "Renaming tenant_budgets to tenants", :blue
  migration_template(
    "rename_tenant_budgets_to_tenants_migration.rb.tt",
    File.join(db_migrate_path, "rename_tenant_budgets_to_tenants.rb")
  )
end

#create_split_execution_details_migrationObject

Main upgrade: split execution_details from executions table

This single migration handles ALL schema transitions:

  • Creates execution_details table if missing

  • Migrates data from old columns on executions to execution_details

  • Removes deprecated columns (detail, niche, workflow, agent_version)

  • Adds any missing columns that should stay on executions



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/ruby_llm_agents/upgrade_generator.rb', line 30

def create_split_execution_details_migration
  if already_split?
    say_status :skip, "execution_details already split from executions", :yellow
    return
  end

  migration_template(
    "split_execution_details_migration.rb.tt",
    File.join(db_migrate_path, "split_execution_details_from_executions.rb")
  )
end

#show_post_upgrade_messageObject



156
157
158
159
160
161
162
163
164
# File 'lib/generators/ruby_llm_agents/upgrade_generator.rb', line 156

def show_post_upgrade_message
  say ""
  say "RubyLLM::Agents upgrade complete!", :green
  say ""
  say "Next steps:"
  say "  1. Run migrations: rails db:migrate"
  say "  2. Run your test suite to verify everything works"
  say ""
end

#suggest_config_consolidationObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/generators/ruby_llm_agents/upgrade_generator.rb', line 134

def suggest_config_consolidation
  ruby_llm_initializer = File.join(destination_root, "config/initializers/ruby_llm.rb")
  agents_initializer = File.join(destination_root, "config/initializers/ruby_llm_agents.rb")

  return unless File.exist?(ruby_llm_initializer) && File.exist?(agents_initializer)

  say ""
  say "Optional: You can now consolidate your API key configuration.", :yellow
  say ""
  say "Move your API keys from config/initializers/ruby_llm.rb"
  say "into config/initializers/ruby_llm_agents.rb:"
  say ""
  say "  RubyLLM::Agents.configure do |config|"
  say "    config.openai_api_key = ENV['OPENAI_API_KEY']"
  say "    config.anthropic_api_key = ENV['ANTHROPIC_API_KEY']"
  say "    # ... rest of your agent config"
  say "  end"
  say ""
  say "Then delete config/initializers/ruby_llm.rb if it only contained API keys."
  say ""
end