Class: Spanner::Translator::Rules::Translation::EnumColumnToStringWithConstraint

Inherits:
BaseRule
  • Object
show all
Defined in:
lib/spanner/translator/rules/translation/enum_column_to_string_with_constraint.rb

Overview

replace t.column enums with the appropriate t.string and the correct check constraints

Constant Summary

Constants inherited from BaseRule

BaseRule::RAILS_COLUMN_TYPES

Instance Attribute Summary

Attributes inherited from BaseRule

#translation_options

Instance Method Summary collapse

Methods inherited from BaseRule

#any_index?, #capture_column_nullity, #capture_primary_key_argument, #capture_table_name, #conditionally_propagate_argument, #create_range, #initialize, #unless_seen

Constructor Details

This class inherits a constructor from Spanner::Translator::Rules::BaseRule

Instance Method Details

#on_send(node) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spanner/translator/rules/translation/enum_column_to_string_with_constraint.rb', line 11

def on_send(node)
  capture_table_name(node)
  return unless node.t_column?

  constraint_name = generate_constraint_name(node)
  unless_seen(constraint_name) do
    @rewriter.replace(node.loc.expression,
                      "t.string #{new_arguments_for_column_definition(node)} # generated")
    constraint = %["#{column_name(node)} IN (#{choices_for_enum_string(node)})"]
    @rewriter.insert_after(node.last_sibling.source_range,
                           "\n  t.check_constraint #{constraint}, name: \"#{constraint_name}\" # generated")
  end
end