Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ class MigrateToRubyLLMModelReferences < ActiveRecord::Migration<%= migration_ver

# Then check for any models in existing data that aren't in models.json
say_with_time "Checking for additional models in existing data" do
collect_and_create_models(chat_class, :<%= chat_model_name.tableize %>, model_class)
collect_and_create_models(message_class, :<%= message_model_name.tableize %>, model_class)
collect_and_create_models(chat_class, :<%= chat_table_name %>, model_class)
collect_and_create_models(message_class, :<%= message_table_name %>, model_class)
model_class.count
end

# Migrate foreign keys
migrate_foreign_key(:<%= chat_model_name.tableize %>, chat_class, model_class, :<%= model_model_name.underscore %>)
migrate_foreign_key(:<%= message_model_name.tableize %>, message_class, model_class, :<%= model_model_name.underscore %>)
migrate_foreign_key(:<%= chat_table_name %>, chat_class, model_class, :<%= model_model_name.underscore %>)
migrate_foreign_key(:<%= message_table_name %>, message_class, model_class, :<%= model_model_name.underscore %>)
end

def down
# Remove foreign key references
if column_exists?(:<%= message_model_name.tableize %>, :<%= model_model_name.underscore %>_id)
remove_reference :<%= message_model_name.tableize %>, :<%= model_model_name.underscore %>, foreign_key: true
if column_exists?(:<%= message_table_name %>, :<%= model_model_name.underscore %>_id)
remove_reference :<%= message_table_name %>, :<%= model_model_name.underscore %>, foreign_key: true
end

if column_exists?(:<%= chat_model_name.tableize %>, :<%= model_model_name.underscore %>_id)
remove_reference :<%= chat_model_name.tableize %>, :<%= model_model_name.underscore %>, foreign_key: true
if column_exists?(:<%= chat_table_name %>, :<%= model_model_name.underscore %>_id)
remove_reference :<%= chat_table_name %>, :<%= model_model_name.underscore %>, foreign_key: true
end

# Restore original model_id string columns
if column_exists?(:<%= message_model_name.tableize %>, :model_id_string)
rename_column :<%= message_model_name.tableize %>, :model_id_string, :model_id
if column_exists?(:<%= message_table_name %>, :model_id_string)
rename_column :<%= message_table_name %>, :model_id_string, :model_id
end

if column_exists?(:<%= chat_model_name.tableize %>, :model_id_string)
rename_column :<%= chat_model_name.tableize %>, :model_id_string, :model_id
if column_exists?(:<%= chat_table_name %>, :model_id_string)
rename_column :<%= chat_table_name %>, :model_id_string, :model_id
end
end

Expand Down Expand Up @@ -134,4 +134,4 @@ class MigrateToRubyLLMModelReferences < ActiveRecord::Migration<%= migration_ver
model_class.find_by(model_id: model_id)
end
end
end
end
14 changes: 12 additions & 2 deletions lib/generators/ruby_llm/upgrade_to_v1_7_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,28 @@ def parse_model_mappings
@model_names
end

def table_name_for(model_name)
# Convert namespaced model names to proper table names
# e.g., "Assistant::Chat" -> "assistant_chats" (not "assistant/chats")
model_name.underscore.pluralize.tr('/', '_')
end

%i[chat message tool_call model].each do |type|
define_method("#{type}_model_name") do
@model_names ||= parse_model_mappings
@model_names[type]
end

define_method("#{type}_table_name") do
table_name_for(send("#{type}_model_name"))
end
end

def create_migration_file
# First check if models table exists, if not create it
unless table_exists?(model_model_name.tableize)
unless table_exists?(table_name_for(model_model_name))
migration_template 'create_models_migration.rb.tt',
"db/migrate/create_#{model_model_name.tableize}.rb",
"db/migrate/create_#{table_name_for(model_model_name)}.rb",
migration_version: migration_version,
model_model_name: model_model_name

Expand Down