Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmetal committed Feb 7, 2024
1 parent 9fad44b commit b246b7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/uuidable/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def uuidable(as_param: true) # rubocop:disable Metrics/AbcSize, Metrics/Cyclomat
columns.select { |c| c.type == :binary && c.limit == 16 && c.name.include?('uuid') }.each do |column|
attribute column.name.to_sym, MySQLBinUUID::Type.new
end
rescue ::ActiveRecord::ConnectionNotEstablished, Mysql2::Error::ConnectionError # rubocop:disable Lint/SuppressedException
rescue ::ActiveRecord::ConnectionNotEstablished, Mysql2::Error::ConnectionError, ::ActiveRecord::NoDatabaseError # rubocop:disable Lint/SuppressedException
end
end

Expand Down
25 changes: 17 additions & 8 deletions lib/uuidable/v1_migration_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

module Uuidable
module V1MigrationHelpers
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:disable Metrics/AbcSize

# Will create uuid columns with new type, move data from pre-v1 column and then move pre-v1 to *__old.
# WARNING: will only work on MySQL 8+.
def uuidable_migrate_uuid_columns_to_v1(table_name, **columns)
columns = columns.stringify_keys.slice(connection.columns(table_name).select { |c| valid_column_for_migration?(c) }.map(&:name))

return unless columns.any?

change_table table_name, bulk: true do |t|
columns.each do |column, options|
t.column :"#{column}_new", :binary, **COLUMN_OPTIONS.merge(options).merge(after: column)
Expand Down Expand Up @@ -57,16 +61,13 @@ def uuidable_rollback_uuid_columns_from_v1(table_name, *columns)
end
end

def uuidable_migrate_all_pre_v1_uuid_columns!(skip: nil)
skip = Array.wrap(skip).each(&:to_s)
def uuidable_migrate_all_pre_v1_uuid_columns!
# skip = Array.wrap(skip).each(&:to_s)

tables.each do |table_name|
indexes = indexes(table_name)
uuid_columns = connection.columns(table_name).select do |column|
column.name.include?('uuid') &&
column.type == :binary &&
column.limit == 36 &&
!skip.include?("#{table_name}.#{column.name}")
valid_column_for_migration?(column)
end

next if uuid_columns.blank?
Expand Down Expand Up @@ -122,6 +123,14 @@ def uuidable_drop_all_pre_v1_uuid_columns!
end
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/AbcSize

def valid_column_for_migration?(column)
column.name.include?('uuid') &&
!column.name.include?('__old') &&
column.type == :binary &&
column.limit == 36 &&
!skip.include?("#{table_name}.#{column.name}")
end
end
end

0 comments on commit b246b7e

Please sign in to comment.