Class: Spanner::Translator::Rules::Translation::AddCommittedAtTimestamp

Inherits:
BaseRule
  • Object
show all
Defined in:
lib/spanner/translator/rules/translation/add_committed_at_timestamp.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

COMMITTED_AT_COLUMN =
't.time "committed_at", null: false, allow_commit_timestamp: true'

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/spanner/translator/rules/translation/add_committed_at_timestamp.rb', line 13

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

  # try all body children to see if any are t.index, if so, insert before
  node.body.children.each do |child|
    next unless child.is_a?(RuboCop::AST::SendNode) && child.t_index?

    return @rewriter.insert_before(child.loc.expression, "#{COMMITTED_AT_COLUMN}\n  ")
  end

  # otherwise, insert at end of block
  @rewriter.insert_before(node.loc.end, "  #{COMMITTED_AT_COLUMN}\n")
end