Module: MigrationExtensions::EncodingChanges

Defined in:
lib/migration_extensions/encoding_changes.rb

Overview

Include in an ActiveRecord::Migration to add the ability to easily migrate schema encodings using change_encoding

Constant Summary collapse

ENCODING_COLLATIONS =

Default collation for listed character encodings

{
  'latin1' => 'latin1_swedish_ci',
  'utf8mb4' => 'utf8mb4_unicode_ci',
  'utf8' => 'utf8_general_ci'
}.freeze
DEFAULT_TARGET_ROW_FORMAT =
'DYNAMIC'
DEFAULT_SOURCE_ROW_FORMAT =
'COMPACT'

Instance Method Summary collapse

Instance Method Details

#alter_encoding(table, row_format, character_set, collation) ⇒ Object

rubocop:enable Metrics/MethodLength



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/migration_extensions/encoding_changes.rb', line 58

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

#change_encoding(table, from:, to:) ⇒ void

Note:

DYNAMIC row formats are the 5.7 defaults, and allow for larger indexes. This is important as otherwise it reduces the max size of an indexed varchar column from 255 to 191 when dealing with utf8mb4 characters.

This method returns an undefined value.

Converts the table to a new character_encoding. Can be used in a reversible 'change' migration rubocop:todo Metrics/MethodLength

Examples:

migrating a table from latin1 to utf8mb4

change_encoding 'study_metadata', from: 'latin1', to: 'utf8mb4'

Parameters:

  • table (String)

    The name of the table to convert

  • to (String)

    The target character set of the converted table, or a hash of options

  • from (String, Hash)

    The current character set, or a hash of options. Used in the event of a down migration.

Options Hash (from:):

  • character_set: (String)

    The current character set of the table

  • collation: (String)

    The current colaltion set of the table. Default based on ENCODING_COLLATIONS

  • row_format: (String)

    The current row_format of the table (COMPACT by default)

Options Hash (to:):

  • character_set: (String)

    The target character set of the converted table

  • collation: (String)

    The target colaltion set of the converted table. Default based on ENCODING_COLLATIONS

  • row_format: (String)

    The row_format of the target table (DYNAMIC by default)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/migration_extensions/encoding_changes.rb', line 36

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end