Class: Spanner::Translator::Rules::Translation::AddExplicitPrimaryKey

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

Overview

Based on the create_table statement, add an explicit column creation for the default primary key when the default is "id" and not already defined.

Scenarios:

create_table "users" do |t|
create_table "users", id: { type: :bigint, unsigned: true, default: nil } do |t|

Other scenarios should be ignored.

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



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spanner/translator/rules/translation/add_explicit_primary_key.rb', line 20

def on_send(node)
  capture_primary_key_argument(node)

  return if @seen["primary_key"]
  return unless node.sending_t? # we assume every create_table has at least one t. statement
  return if @primary_key_argument&.value

  primary_key_name = "id"

  @rewriter.insert_before(node.loc.expression,
                          "t.integer \"#{primary_key_name}\", null: false # generated\n  ")
  @seen["primary_key"] = true
end