class ActiveRecord::ConnectionAdapters::Spanner::SchemaCreation

Private Instance Methods

add_column_options!(sql, options) click to toggle source

rubocop:enable Naming/MethodName, Metrics/AbcSize

# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 111
def add_column_options! sql, options
  if options[:null] == false || options[:primary_key] == true
    sql << " NOT NULL"
  end

  if !options[:allow_commit_timestamp].nil? &&
     options[:column].sql_type == "TIMESTAMP"
    sql << " OPTIONS (allow_commit_timestamp = "\
           "#{options[:allow_commit_timestamp]})"
  end

  if (as = options[:as])
    sql << " AS (#{as})"

    sql << " STORED" if options[:stored]
    unless options[:stored]
      raise ArgumentError, "" \
        "Cloud Spanner currently does not support generated columns without the STORED option." \
        "Specify 'stored: true' option for `#{options[:column].name}`"
    end
  end

  sql
end
visit_AddColumnDefinition(o) click to toggle source
# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 64
def visit_AddColumnDefinition o
  # Overridden to add the optional COLUMN keyword. The keyword is only optional
  # on real Cloud Spanner, the emulator requires the COLUMN keyword to be included.
  +"ADD COLUMN #{accept o.column}"
end
visit_ChangeColumnDefinition(o) click to toggle source
# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 75
def visit_ChangeColumnDefinition o
  sql = +"ALTER TABLE #{quote_table_name o.table_name} ALTER COLUMN "
  sql << accept(o.column)
  sql
end
visit_ColumnDefinition(o) click to toggle source
# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 57
def visit_ColumnDefinition o
  o.sql_type = type_to_sql o.type, **o.options
  column_sql = +"#{quote_column_name o.name} #{o.sql_type}"
  add_column_options! column_sql, column_options(o)
  column_sql
end
visit_DropColumnDefinition(o) click to toggle source
# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 70
def visit_DropColumnDefinition o
  "ALTER TABLE #{quote_table_name o.table_name} DROP" \
   " COLUMN #{quote_column_name o.name}"
end
visit_DropIndexDefinition(o) click to toggle source
# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 81
def visit_DropIndexDefinition o
  "DROP INDEX #{quote_table_name o.name}"
end
visit_DropTableDefinition(o) click to toggle source
# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 53
def visit_DropTableDefinition o
  "DROP TABLE #{quote_table_name o.name}"
end
visit_IndexDefinition(o) click to toggle source
# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 85
def visit_IndexDefinition o
  sql = +"CREATE"
  sql << " UNIQUE" if o.unique
  sql << " NULL_FILTERED" if o.null_filtered
  sql << " INDEX #{quote_table_name o.name} "

  columns_sql = o.columns_with_order.map do |c, order|
    order_sql = +quote_column_name(c)
    order_sql << " DESC" if order == "DESC"
    order_sql
  end

  sql << "ON #{quote_table_name o.table} (#{columns_sql.join ', '})"

  if o.storing.any?
    storing = o.storing.map { |s| quote_column_name s }
    sql << " STORING (#{storing.join ', '})"
  end
  if o.interleave_in
    sql << ", INTERLEAVE IN #{quote_table_name o.interleave_in}"
  end
  sql
end
visit_TableDefinition(o) click to toggle source

rubocop:disable Naming/MethodName, Metrics/AbcSize

# File lib/active_record/connection_adapters/spanner/schema_creation.rb, line 15
def visit_TableDefinition o
  create_sql = +"CREATE TABLE #{quote_table_name o.name} "
  statements = o.columns.map { |c| accept c }

  o.foreign_keys.each do |to_table, options|
    statements << foreign_key_in_create(o.name, to_table, options)
  end

  create_sql << "(#{statements.join ', '}) " if statements.any?

  primary_keys = if o.primary_keys
                   o.primary_keys
                 else
                   pk_names = o.columns.each_with_object [] do |c, r|
                     if c.type == :primary_key || c.primary_key?
                       r << c.name
                     end
                   end
                   PrimaryKeyDefinition.new pk_names
                 end

  if o.interleave_in?
    parent_names = o.columns.each_with_object [] do |c, r|
      if c.type == :parent_key
        r << c.name
      end
    end
    primary_keys.name = parent_names.concat primary_keys.name
    create_sql << accept(primary_keys)
    create_sql << ", INTERLEAVE IN PARENT #{quote_table_name o.interleave_in_parent}"
    create_sql << " ON DELETE #{o.on_delete}" if o.on_delete
  else
    create_sql << accept(primary_keys)
  end

  create_sql
end