Module: Webdack::UUIDMigration::Helpers

Includes:
SchemaHelpers
Defined in:
lib/webdack/uuid_migration/helpers.rb

Instance Method Summary collapse

Methods included from SchemaHelpers

#create_foreign_keys, #drop_foreign_keys, #extract_foreign_key_action, #foreign_keys_into

Instance Method Details

#column_to_uuid(table, column, seed: nil) ⇒ none

Converts a column to UUID, migrates all data by left padding with 0's

Parameters:

  • (defaults to: nil)

Returns:



39
40
41
42
# File 'lib/webdack/uuid_migration/helpers.rb', line 39

def column_to_uuid(table, column, seed: nil)
  execute %Q{ALTER TABLE #{table}
           ALTER COLUMN #{column} SET DATA TYPE UUID USING (#{to_uuid_pg(column, seed)})}
end

#columns_to_uuid(table, *columns, seed: nil) ⇒ none

Converts columns to UUID, migrates all data by left padding with 0's

Parameters:

  • (defaults to: nil)

Returns:



51
52
53
54
55
# File 'lib/webdack/uuid_migration/helpers.rb', line 51

def columns_to_uuid(table, *columns, seed: nil)
  columns.each do |column|
    column_to_uuid(table, column, seed: seed)
  end
end

#int_to_uuid(num) ⇒ String

Convert an Integer to UUID formatted string by left padding with 0's

Parameters:

Returns:



61
62
63
# File 'lib/webdack/uuid_migration/helpers.rb', line 61

def int_to_uuid(num)
  '00000000-0000-0000-0000-%012d' % num.to_i
end

#polymorphic_column_data_for_uuid(table, column, *entities, seed: nil) ⇒ Object

Convert data values to UUID format for polymorphic associations. Useful when only few of associated entities have switched to UUID primary keys. Before calling this ensure that the corresponding column_id has been changed to :string (VARCHAR(36) or larger)

See Polymorphic References in README

Parameters:

  • it will change data in corresponding _id

  • data referring these entities will be converted

  • (defaults to: nil)


76
77
78
79
80
81
82
# File 'lib/webdack/uuid_migration/helpers.rb', line 76

def polymorphic_column_data_for_uuid(table, column, *entities, seed: nil)
  list_of_entities = entities.map { |e| "'#{e}'" }.join(', ')
  execute %Q{
            UPDATE #{table} SET #{column}_id= #{to_uuid_pg("#{column}_id", seed)}
              WHERE #{column}_type in (#{list_of_entities})
          }
end

#primary_key_and_all_references_to_uuid(table, seed: nil) ⇒ Object

Note:

Works only with Rails 4.2 or newer



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/webdack/uuid_migration/helpers.rb', line 99

def primary_key_and_all_references_to_uuid(table, seed: nil)
  fk_specs = foreign_keys_into(table)

  drop_foreign_keys(fk_specs)

  primary_key_to_uuid(table, seed: seed)

  fk_specs.each do |fk_spec|
    columns_to_uuid fk_spec[:from_table], fk_spec[:column], seed: seed
  end

  create_foreign_keys(fk_specs.deep_dup)
end

#primary_key_to_uuid(table, options = {}) ⇒ none

Converts primary key from Serial Integer to UUID, migrates all data by left padding with 0's sets gen_random_uuid() as default for the column

Options Hash (options):

  • :primary_key (Symbol)

    if not supplied queries the schema (should work most of the times)

  • :default (String)

    mechanism to generate UUID for new records, default gen_random_uuid(), which is Rails 4.0.0 default as well

  • :seed (String)

    used as namespace to generate UUIDv5 from the existing ID

Parameters:

  • (defaults to: {})

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/webdack/uuid_migration/helpers.rb', line 18

def primary_key_to_uuid(table, options = {})
  default = options[:default] || 'gen_random_uuid()'
  seed = options[:seed]

  column = connection.primary_key(table)

  execute %Q{ALTER TABLE #{table}
           ALTER COLUMN #{column} DROP DEFAULT,
           ALTER COLUMN #{column} SET DATA TYPE UUID USING (#{to_uuid_pg(column, seed)}),
           ALTER COLUMN #{column} SET DEFAULT #{default}}

  execute %Q{DROP SEQUENCE IF EXISTS #{table}_#{column}_seq} rescue nil
end