Skip to content

Commit ad036e1

Browse files
authored
Fix namespaced model table names in upgrade generator (#398)
## Description This PR fixes issue #397 where the upgrade generator produces invalid table names for namespaced models. ## Problem When using custom model names with namespaces (like `Assistant::Chat`, `Assistant::Message`, etc.) and running the upgrade generator: ```bash rails g ruby_llm:upgrade_to_v1_7 chat:Assistant::Chat message:Assistant::Message ``` The migration would generate invalid table names like `:assistant/chats` instead of `:assistant_chats`. ## Solution The issue was that the generator was using `.tableize` which produces forward slashes for namespaced models. This has been fixed by using `.underscore.pluralize.tr('/', '_')` which properly converts namespaced model names to valid table names. ### Changes: - Added `table_name_for` helper method that correctly handles namespaced models - Added helper methods for each model type (`chat_table_name`, `message_table_name`, etc.) - Updated migration template to use the new helper methods instead of `.tableize` ## Testing Tested with namespaced models to confirm: - `Assistant::Chat` → `assistant_chats` ✓ - `Assistant::Message` → `assistant_messages` ✓ - `Assistant::ToolCall` → `assistant_tool_calls` ✓ - `Assistant::Model` → `assistant_models` ✓ Regular models continue to work as expected: - `Chat` → `chats` ✓ - `Message` → `messages` ✓ Fixes #397
1 parent 3d57b9e commit ad036e1

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@ class MigrateToRubyLLMModelReferences < ActiveRecord::Migration<%= migration_ver
66

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

1414
# Migrate foreign keys
15-
migrate_foreign_key(:<%= chat_model_name.tableize %>, chat_class, model_class, :<%= model_model_name.underscore %>)
16-
migrate_foreign_key(:<%= message_model_name.tableize %>, message_class, model_class, :<%= model_model_name.underscore %>)
15+
migrate_foreign_key(:<%= chat_table_name %>, chat_class, model_class, :<%= model_model_name.underscore %>)
16+
migrate_foreign_key(:<%= message_table_name %>, message_class, model_class, :<%= model_model_name.underscore %>)
1717
end
1818

1919
def down
2020
# Remove foreign key references
21-
if column_exists?(:<%= message_model_name.tableize %>, :<%= model_model_name.underscore %>_id)
22-
remove_reference :<%= message_model_name.tableize %>, :<%= model_model_name.underscore %>, foreign_key: true
21+
if column_exists?(:<%= message_table_name %>, :<%= model_model_name.underscore %>_id)
22+
remove_reference :<%= message_table_name %>, :<%= model_model_name.underscore %>, foreign_key: true
2323
end
2424

25-
if column_exists?(:<%= chat_model_name.tableize %>, :<%= model_model_name.underscore %>_id)
26-
remove_reference :<%= chat_model_name.tableize %>, :<%= model_model_name.underscore %>, foreign_key: true
25+
if column_exists?(:<%= chat_table_name %>, :<%= model_model_name.underscore %>_id)
26+
remove_reference :<%= chat_table_name %>, :<%= model_model_name.underscore %>, foreign_key: true
2727
end
2828

2929
# Restore original model_id string columns
30-
if column_exists?(:<%= message_model_name.tableize %>, :model_id_string)
31-
rename_column :<%= message_model_name.tableize %>, :model_id_string, :model_id
30+
if column_exists?(:<%= message_table_name %>, :model_id_string)
31+
rename_column :<%= message_table_name %>, :model_id_string, :model_id
3232
end
3333

34-
if column_exists?(:<%= chat_model_name.tableize %>, :model_id_string)
35-
rename_column :<%= chat_model_name.tableize %>, :model_id_string, :model_id
34+
if column_exists?(:<%= chat_table_name %>, :model_id_string)
35+
rename_column :<%= chat_table_name %>, :model_id_string, :model_id
3636
end
3737
end
3838

@@ -134,4 +134,4 @@ class MigrateToRubyLLMModelReferences < ActiveRecord::Migration<%= migration_ver
134134
model_class.find_by(model_id: model_id)
135135
end
136136
end
137-
end
137+
end

lib/generators/ruby_llm/upgrade_to_v1_7_generator.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,28 @@ def parse_model_mappings
4545
@model_names
4646
end
4747

48+
def table_name_for(model_name)
49+
# Convert namespaced model names to proper table names
50+
# e.g., "Assistant::Chat" -> "assistant_chats" (not "assistant/chats")
51+
model_name.underscore.pluralize.tr('/', '_')
52+
end
53+
4854
%i[chat message tool_call model].each do |type|
4955
define_method("#{type}_model_name") do
5056
@model_names ||= parse_model_mappings
5157
@model_names[type]
5258
end
59+
60+
define_method("#{type}_table_name") do
61+
table_name_for(send("#{type}_model_name"))
62+
end
5363
end
5464

5565
def create_migration_file
5666
# First check if models table exists, if not create it
57-
unless table_exists?(model_model_name.tableize)
67+
unless table_exists?(table_name_for(model_model_name))
5868
migration_template 'create_models_migration.rb.tt',
59-
"db/migrate/create_#{model_model_name.tableize}.rb",
69+
"db/migrate/create_#{table_name_for(model_model_name)}.rb",
6070
migration_version: migration_version,
6171
model_model_name: model_model_name
6272

0 commit comments

Comments
 (0)