Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmetal committed Feb 8, 2024
1 parent bab6efa commit 4265819
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions lib/uuidable/v1_migration_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def uuidable_migrate_uuid_columns_to_v1(table_name, columns_options = {}, **opts

return if uuid_columns.blank?

indexes = indexes_with_columns(table_name, uuid_columns)

change_table table_name, bulk: true do |t|
uuid_columns.each do |column|
options = columns_options[column.name] || { null: column.null }
Expand All @@ -36,43 +38,59 @@ def uuidable_migrate_uuid_columns_to_v1(table_name, columns_options = {}, **opts

execute "UPDATE `#{table_name}` SET #{update}"

# bulk will not rename indexes so we could just reindex them
change_table table_name, bulk: true do |t|
uuid_columns.each do |column|
t.rename column.name, :"#{column.name}#{OLD_POSTFIX}"
t.rename :"#{column.name}#{NEW_POSTFIX}", column.name
end

indexes.each do |ind|
t.remove_index name: ind.name
end
end

# reindex indexes
connection.execute "OPTIMIZE TABLE `#{table_name}`"
# recreate indexes with new column
change_table table_name, bulk: true do |t|
indexes.each do |ind|
t.index ind.columns, name: ind.name, unique: ind.unique
end
end
end

# WARNING: will only work until *__old columns is not deleted!
def uuidable_rollback_uuid_columns_from_v1(table_name, *columns)
columns.map!(&:to_s)
indexes = indexes(table_name)

uuid_columns = connection.columns(table_name).select do |column|
(columns.blank? || columns.include?(column.name)) &&
valid_column_for_migration?(column, limit: 16)
end

return if uuid_columns.blank?

indexes = indexes_with_columns(table_name, uuid_columns)

change_table table_name, bulk: true do |t|
uuid_columns.each do |column|
t.rename column.name, :"#{column.name}#{NEW_POSTFIX}"
t.rename :"#{column.name}#{OLD_POSTFIX}", column.name
end

indexes.each do |ind|
t.remove_index name: ind.name
end
end

change_table table_name, bulk: true do |t|
uuid_columns.each do |column|
t.remove :"#{column.name}#{NEW_POSTFIX}"
end
end

# reindex indexes
connection.execute "OPTIMIZE TABLE `#{table_name}`"
indexes.each do |ind|
t.index name: ind.name, unique: ind.unique
end
end
end

def uuidable_migrate_all_pre_v1_uuid_columns!
Expand Down Expand Up @@ -116,6 +134,10 @@ def valid_column_for_migration?(column, limit: 36, skip_type_check: false)
column.limit == limit
))
end

def indexes_with_columns(table_name, columns)
indexes(table_name).select { |ind| (ind.columns & columns.map(&:name)).any? }
end
end
end
# rubocop:enable all

0 comments on commit 4265819

Please sign in to comment.