Sha256: e2acc1ff55c60c8b760e9881f9d28e364664a3fe56b648cf8f744d570d22b960

Contents?: true

Size: 1.59 KB

Versions: 8

Compression:

Stored size: 1.59 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.

module ActiveRecordSpannerAdapter
  class Table
    class Column
      attr_accessor :table_name, :name, :type, :limit, :ordinal_position,
                    :allow_commit_timestamp, :default, :default_function, :generated,
                    :primary_key
      attr_writer :nullable

      def initialize \
          table_name,
          name,
          type,
          limit: nil,
          ordinal_position: nil,
          nullable: true,
          allow_commit_timestamp: nil,
          default: nil,
          default_function: nil,
          generated: nil
        @table_name = table_name.to_s
        @name = name.to_s
        @type = type
        @limit = limit
        @nullable = nullable != false
        @ordinal_position = ordinal_position
        @allow_commit_timestamp = allow_commit_timestamp
        @default = default
        @default_function = default_function
        @generated = generated == true
        @primary_key = false
      end

      def nullable
        return false if primary_key
        @nullable
      end

      def spanner_type
        return "#{type}(#{limit || 'MAX'})" if limit_allowed?
        type
      end

      def options
        {
          limit: limit,
          null: nullable,
          allow_commit_timestamp: allow_commit_timestamp
        }.delete_if { |_, v| v.nil? }
      end

      private

      def limit_allowed?
        ["BYTES", "STRING"].include? type
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.5.1 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.5.0 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.4.4 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.4.3 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.4.2 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.4.1 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.4.0 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.3.1 lib/activerecord_spanner_adapter/table/column.rb