Module: Uuidable::V1MigrationHelpers

Defined in:
lib/uuidable/v1_migration_helpers.rb

Constant Summary collapse

NEW_POSTFIX =
'__new'
OLD_POSTFIX =
'__old'

Instance Method Summary collapse

Instance Method Details

#indexes_with_columns(table_name, columns) ⇒ Object



138
139
140
# File 'lib/uuidable/v1_migration_helpers.rb', line 138

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

#uuidable_drop_all_pre_v1_uuid_columns!Object

WARNING: this is irreversible migration! It will drop all *uuid__old columns and their indexes in all tables!



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/uuidable/v1_migration_helpers.rb', line 109

def uuidable_drop_all_pre_v1_uuid_columns!
  tables.each do |table_name|
    indexes = indexes(table_name)
    change_table table_name, bulk: true do |t|
      connection.columns(table_name).each do |column|
        next unless column.name.include?(OLD_POSTFIX)

        indexes.each do |ind|
          next unless ind.columns.include?(column.name)

          t.remove_index name: ind.name
        end

        t.remove column.name
      end
    end
  end
end

#uuidable_migrate_all_pre_v1_uuid_columns!Object



96
97
98
99
100
# File 'lib/uuidable/v1_migration_helpers.rb', line 96

def uuidable_migrate_all_pre_v1_uuid_columns!
  tables.each do |table_name|
    uuidable_migrate_uuid_columns_to_v1 table_name
  end
end

#uuidable_migrate_uuid_columns_to_v1(table_name, columns_options = {}, **opts) ⇒ Object

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+.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/uuidable/v1_migration_helpers.rb', line 11

def uuidable_migrate_uuid_columns_to_v1(table_name, columns_options = {}, **opts)
  columns_options.stringify_keys!
  uuid_columns = connection.columns(table_name).select do |column|
    (columns_options.blank? || columns_options.key?(column.name)) &&
      valid_column_for_migration?(column, **opts)
  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|
      options = columns_options[column.name] || { null: column.null }
      t.column :"#{column.name}#{NEW_POSTFIX}", :binary, **COLUMN_OPTIONS.merge(options).merge(after: column.name)
    end
  end

  update = uuid_columns.map do |column, _opts|
    "      `\#{table_name}`.`\#{column.name}\#{NEW_POSTFIX}` = IF(\n        IS_UUID(`\#{table_name}`.`\#{column.name}`),\n        UUID_TO_BIN(`\#{table_name}`.`\#{column.name}`),\n        `\#{table_name}`.`\#{column.name}`\n      )\n    SQL\n  end.join(', ')\n\n  execute \"UPDATE `\#{table_name}` SET \#{update}\"\n\n  change_table table_name, bulk: true do |t|\n    uuid_columns.each do |column|\n      t.rename column.name, :\"\#{column.name}\#{OLD_POSTFIX}\"\n      t.rename :\"\#{column.name}\#{NEW_POSTFIX}\", column.name\n    end\n\n    indexes.each do |ind|\n      t.remove_index name: ind.name\n    end\n  end\n\n  # recreate indexes with new column\n  change_table table_name, bulk: true do |t|\n    indexes.each do |ind|\n      t.index ind.columns, name: ind.name, unique: ind.unique\n    end\n  end\nend\n"

#uuidable_rollback_all_pre_v1_uuid_columns!Object



102
103
104
105
106
# File 'lib/uuidable/v1_migration_helpers.rb', line 102

def uuidable_rollback_all_pre_v1_uuid_columns!
  tables.each do |table_name|
    uuidable_rollback_uuid_columns_from_v1 table_name
  end
end

#uuidable_rollback_uuid_columns_from_v1(table_name, *columns) ⇒ Object

WARNING: will only work until *__old columns is not deleted!



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/uuidable/v1_migration_helpers.rb', line 61

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

    indexes.each do |ind|
      t.index ind.columns, name: ind.name, unique: ind.unique
    end
  end
end

#valid_column_for_migration?(column, limit: 36, skip_type_check: false) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
# File 'lib/uuidable/v1_migration_helpers.rb', line 128

def valid_column_for_migration?(column, limit: 36, skip_type_check: false)
  column.name.include?('uuid') &&
    !column.name.include?(NEW_POSTFIX) &&
    !column.name.include?(OLD_POSTFIX) &&
    (skip_type_check || (
      column.type == :binary &&
      column.limit == limit
    ))
end