Class: Spanner::Translator::Rules::Translation::EnsurePrimaryKeyColumnsExist

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

Overview

In the create_table block, add t.time :committed_at with allow_commit_timestamp: true before the first t.index or at the end of the block if no t.index is found.

Constant Summary collapse

ID_COLUMN_TEMPLATE =
%(t.%<type>s "%<colname>s", null: false # generated\n  )

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_block(node) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spanner/translator/rules/translation/ensure_primary_key_columns_exist.rb', line 15

def on_block(node)
  # only pay attention to create_table blocks
  return unless node.send_node.method_name == :create_table

  # get primary_key hash argument value
  specified_primary_keys = value_of(node.send_node.primary_key_option.value)
  return unless specified_primary_keys

  # get primary_key column name[s]
  undefined_columns = undefined_primary_key_columns node.body.children, specified_primary_keys

  # otherwise, insert at beginning of block
  undefined_columns.each do |colname|
    @rewriter.insert_before(node.body.loc.expression, id_column_source(colname))
  end
end