Sha256: 73d325e1cd79a1677cb5ade70f801811ebcaa94ee15a53dfd7c14f4e4e2ebe4d

Contents?: true

Size: 1.84 KB

Versions: 18

Compression:

Stored size: 1.84 KB

Contents

# Copyright 2020 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

require "activerecord_spanner_adapter/index/column"

module ActiveRecordSpannerAdapter
  class Index
    attr_accessor :table, :name, :columns, :type, :unique, :null_filtered,
                  :interleave_in, :storing, :state

    def initialize \
        table,
        name,
        columns,
        type: nil,
        unique: false,
        null_filtered: false,
        interleave_in: nil,
        storing: nil,
        state: nil
      @table = table.to_s
      @name = name.to_s
      @columns = Array(columns)
      @type = type
      @unique = unique
      @null_filtered = null_filtered
      @interleave_in = interleave_in unless interleave_in.to_s.empty?
      @storing = storing || []
      @state = state
    end

    def primary?
      @type == "PRIMARY_KEY"
    end

    def columns_by_position
      @columns.select(&:ordinal_position).sort do |c1, c2|
        c1.ordinal_position <=> c2.ordinal_position
      end
    end

    def column_names
      columns_by_position.map(&:name)
    end

    def orders
      columns_by_position.each_with_object({}) do |c, r|
        r[c.name] = c.desc? ? :desc : :asc
      end
    end

    def options
      {
        name: name,
        order: orders,
        unique: unique,
        interleave_in: interleave_in,
        null_filtered: null_filtered,
        storing: storing
      }.delete_if { |_, v| v.nil? }
    end

    def rename_column_options old_column, new_column
      opts = options

      opts[:order].transform_keys do |key|
        key.to_s == new_column.to_s
      end

      columns = column_names.map do |c|
        c.to_s == old_column.to_s ? new_column : c
      end

      { options: opts, columns: columns }
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.6.0 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.5.1 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.5.0 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.4.4 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.4.3 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.4.2 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.4.1 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.4.0 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.3.1 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.2.2 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.2.1 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.2.0 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.1.0 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.0.1 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-1.0.0 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-0.7.0 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-0.6.0 lib/activerecord_spanner_adapter/index.rb
activerecord-spanner-adapter-0.5.0 lib/activerecord_spanner_adapter/index.rb