Skip to content

Commit 47d012a

Browse files
committed
Make the v3.5.0 upgrade more robust
* If tables have already been renamed, skip the rename and continue. * Allows modules to handle migrations in a less brittle fashion.
1 parent 368eb7d commit 47d012a

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lib/cms/upgrades/v3_5_0.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ def v3_5_0_apply_namespace_to_block(module_name, model_class_name)
2121
table_prefix = module_name.underscore
2222
model_name = model_class_name.underscore
2323

24-
rename_table model_name.pluralize, "#{table_prefix}_#{model_name.pluralize}"
25-
rename_table "#{model_name}_versions", "#{table_prefix}_#{model_name}_versions"
24+
old_content_table = model_name.pluralize
25+
new_content_table = "#{table_prefix}_#{model_name.pluralize}"
26+
rename_table old_content_table, new_content_table if have_not_renamed(old_content_table, new_content_table)
27+
28+
29+
old_versions_table = "#{model_name}_versions"
30+
new_versions_table = "#{table_prefix}_#{model_name}_versions"
31+
rename_table old_versions_table, new_versions_table if have_not_renamed(old_versions_table, new_versions_table)
2632
v3_5_0_standardize_version_id_column(table_prefix, model_name)
2733
v3_5_0_namespace_model_data(module_name, model_class_name)
2834
v3_5_0_update_connector_namespaces(module_name, model_class_name)
@@ -53,6 +59,21 @@ def v3_5_0_update_connector_namespaces(module_name, model_class_name)
5359
def v3_5_0_namespace_model(module_name, model_class_name)
5460
"#{module_name}::#{model_class_name}"
5561
end
62+
63+
private
64+
65+
# If we already renamed these tables, they should be skipped
66+
def have_not_renamed(old_table, new_table)
67+
unless table_exists?(old_table)
68+
puts "Table #{old_table} does not exist. Skipping rename."
69+
return false
70+
end
71+
unless !table_exists?(new_table)
72+
puts "Table #{new_table} already exists. Skipping rename."
73+
return false
74+
end
75+
true
76+
end
5677
end
5778

5879
# Add additional methods to ActiveRecord::Migration when this file is required.

0 commit comments

Comments
 (0)