Class: Spanner::Translator::Rules::Translation::AddTimestamps

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

COLUMN_DEF =
"t.timestamps # generated"

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

rubocop:disable Metrics/AbcSize



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spanner/translator/rules/translation/add_timestamps.rb', line 22

def on_block(node) # rubocop:disable Metrics/AbcSize
  # only pay attention to create_table blocks
  return unless node.send_node.method_name == :create_table

  # check all  already has `t.timestamps` bail out
  return if timestamp_child?(node.body.children)

  # try all body children to see if any are t.index, if so, insert before
  t_index = node.body.children.find { |child| child.is_a?(RuboCop::AST::SendNode) && child.t_index? }
  return @rewriter.insert_before(t_index.loc.expression, "#{COLUMN_DEF}\n  ") if t_index

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

#on_send(node) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/spanner/translator/rules/translation/add_timestamps.rb', line 13

def on_send(node)
  return unless node.t_datetime? && %w[created_at updated_at].include?(node.first_argument.value)

  full_line = Parser::Source::Range.new(@rewriter.source_buffer,
                                        node.loc.expression.begin_pos,
                                        node.loc.expression.end_pos)
  @rewriter.remove(full_line)
end