Module: Webdack::UUIDMigration::Helpers
- Includes:
- SchemaHelpers
- Defined in:
- lib/webdack/uuid_migration/helpers.rb
Instance Method Summary collapse
-
#column_to_uuid(table, column, seed: nil) ⇒ none
Converts a column to UUID, migrates all data by left padding with 0’s.
-
#columns_to_uuid(table, *columns, seed: nil) ⇒ none
Converts columns to UUID, migrates all data by left padding with 0’s.
-
#int_to_uuid(num) ⇒ String
Convert an Integer to UUID formatted string by left padding with 0’s.
-
#polymorphic_column_data_for_uuid(table, column, *entities, seed: nil) ⇒ Object
Convert data values to UUID format for polymorphic associations.
- #primary_key_and_all_references_to_uuid(table, seed: nil) ⇒ Object
-
#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.
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
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
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
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
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
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
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, = {}) default = [:default] || 'gen_random_uuid()' seed = [: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 |