Sha256: e41e6648aae1754f1dabf9d0c2d865c48e64ca23991b121ca4228f2a10ec0c48

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 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 :schema_name, :table_name, :name, :type, :limit, :ordinal_position,
                    :allow_commit_timestamp, :default, :default_function, :generated,
                    :primary_key, :nullable

      def initialize \
          table_name,
          name,
          type,
          schema_name: "",
          limit: nil,
          ordinal_position: nil,
          nullable: true,
          allow_commit_timestamp: nil,
          default: nil,
          default_function: nil,
          generated: nil,
          primary_key: false
        @schema_name = schema_name.to_s
        @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 = primary_key
      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

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.6.3 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.6.2 lib/activerecord_spanner_adapter/table/column.rb
activerecord-spanner-adapter-1.6.1 lib/activerecord_spanner_adapter/table/column.rb