Sha256: f8a5c0bf68236f8b6905dd1283ef794c8b2088a28efbde52cf30f8976f39a318

Contents?: true

Size: 1.4 KB

Versions: 33

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module ConnectionAdapters
    # PostgreSQL-specific extensions to column definitions in a table.
    class PostgreSQLColumn < Column #:nodoc:
      delegate :array, :oid, :fmod, to: :sql_type_metadata
      alias :array? :array

      def initialize(*, max_identifier_length: 63, **)
        super
        @max_identifier_length = max_identifier_length
      end

      def serial?
        return unless default_function

        if %r{\Anextval\('"?(?<sequence_name>.+_(?<suffix>seq\d*))"?'::regclass\)\z} =~ default_function
          sequence_name_from_parts(table_name, name, suffix) == sequence_name
        end
      end

      protected
        attr_reader :max_identifier_length

      private
        def sequence_name_from_parts(table_name, column_name, suffix)
          over_length = [table_name, column_name, suffix].map(&:length).sum + 2 - max_identifier_length

          if over_length > 0
            column_name_length = [(max_identifier_length - suffix.length - 2) / 2, column_name.length].min
            over_length -= column_name.length - column_name_length
            column_name = column_name[0, column_name_length - [over_length, 0].min]
          end

          if over_length > 0
            table_name = table_name[0, table_name.length - over_length]
          end

          "#{table_name}_#{column_name}_#{suffix}"
        end
    end
  end
end

Version data entries

33 entries across 33 versions & 3 rubygems

Version Path
activerecord-5.2.8.1 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.8 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.7.1 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.7 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.6.3 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.6.2 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.6.1 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.6 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.4.6 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.5 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.4.5 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.4.4 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.4.3 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.4.2 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.4.1 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.4 lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.4.rc1 lib/active_record/connection_adapters/postgresql/column.rb
spiral_form-0.1.1 vendor/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/column.rb
spiral_form-0.1.0 vendor/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/column.rb
activerecord-5.2.3 lib/active_record/connection_adapters/postgresql/column.rb