From 818ac62ddbceea6b0f3efc316af47fc5fb6b2737 Mon Sep 17 00:00:00 2001 From: Will Cosgrove Date: Wed, 10 Sep 2025 12:02:03 -0500 Subject: [PATCH] Fix namespaced model table names in upgrade generator The upgrade generator was using .tableize which produces invalid table names for namespaced models (e.g., 'assistant/chats' instead of 'assistant_chats'). This fix uses .underscore.pluralize.tr('/', '_') to properly convert namespaced model names to valid table names. Fixes #397 --- .../upgrade_to_v1_7/templates/migration.rb.tt | 26 +++++++++---------- .../ruby_llm/upgrade_to_v1_7_generator.rb | 14 ++++++++-- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt b/lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt index bd6d7b1dd..49dca139b 100644 --- a/lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt +++ b/lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt @@ -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 @@ -134,4 +134,4 @@ class MigrateToRubyLLMModelReferences < ActiveRecord::Migration<%= migration_ver model_class.find_by(model_id: model_id) end end -end \ No newline at end of file +end diff --git a/lib/generators/ruby_llm/upgrade_to_v1_7_generator.rb b/lib/generators/ruby_llm/upgrade_to_v1_7_generator.rb index 363d5f6c9..e467b2b6a 100644 --- a/lib/generators/ruby_llm/upgrade_to_v1_7_generator.rb +++ b/lib/generators/ruby_llm/upgrade_to_v1_7_generator.rb @@ -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