Class: RubyLlmAgents::MultiTenancyGenerator

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

Overview

Multi-tenancy generator for ruby_llm-agents

Usage:

rails generate ruby_llm_agents:multi_tenancy

This will create migrations for:

- ruby_llm_agents_tenants table for per-tenant configuration
- Adding tenant columns to ruby_llm_agents_executions

For users upgrading from an older version:

- Renames ruby_llm_agents_tenant_budgets to ruby_llm_agents_tenants

Instance Method Summary collapse

Instance Method Details

#create_add_tenant_to_executions_migrationObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/generators/ruby_llm_agents/multi_tenancy_generator.rb', line 48

def create_add_tenant_to_executions_migration
  if column_exists?(:ruby_llm_agents_executions, :tenant_id)
    say_status :skip, "tenant_id column already exists on executions", :yellow
    return
  end

  migration_template(
    "add_tenant_to_executions_migration.rb.tt",
    File.join(db_migrate_path, "add_tenant_id_to_ruby_llm_agents_executions.rb")
  )
end

#create_tenants_migrationObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/ruby_llm_agents/multi_tenancy_generator.rb', line 26

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

  if table_exists?(:ruby_llm_agents_tenant_budgets)
    # Upgrade path: rename existing table
    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")
    )
  else
    # Fresh install: create new table
    migration_template(
      "create_tenants_migration.rb.tt",
      File.join(db_migrate_path, "create_ruby_llm_agents_tenants.rb")
    )
  end
end

#show_post_install_messageObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generators/ruby_llm_agents/multi_tenancy_generator.rb', line 60

def show_post_install_message
  say ""
  say "Multi-tenancy migrations created!", :green
  say ""
  say "Next steps:"
  say "  1. Run: rails db:migrate"
  say "  2. Add llm_tenant to your tenant model:"
  say ""
  say "     class Organization < ApplicationRecord"
  say "       include RubyLLM::Agents::LLMTenant"
  say ""
  say "       llm_tenant id: :id,            # Method for tenant_id"
  say "                  budget: true,       # Auto-create budget on creation"
  say "                  limits: {           # Optional default limits"
  say "                    daily_cost: 100,"
  say "                    monthly_cost: 1000"
  say "                  },"
  say "                  enforcement: :hard  # :none, :soft, or :hard"
  say "     end"
  say ""
  say "  3. Pass tenant to agents:"
  say ""
  say "     MyAgent.call(prompt, tenant: current_organization)"
  say ""
  say "  4. Query usage:"
  say ""
  say "     tenant = RubyLLM::Agents::Tenant.for(organization)"
  say "     tenant.cost_today        # => 12.34"
  say "     tenant.tokens_this_month # => 50000"
  say "     tenant.usage_summary     # => { cost: ..., tokens: ..., ... }"
  say ""
end