Sha256: 8773b2ace645ae367ddb28c81e682fcf06fd621def38ee549e273f1da84672a2

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require "active_record/scoping/default"
require "active_record/scoping/named"

module ActiveRecord
  # This class is used to create a table that keeps track of values and keys such
  # as which environment migrations were run in.
  class InternalMetadata < ActiveRecord::Base # :nodoc:
    class << self
      def _internal?
        true
      end

      def primary_key
        "key"
      end

      def table_name
        "#{table_name_prefix}#{internal_metadata_table_name}#{table_name_suffix}"
      end

      def []=(key, value)
        find_or_initialize_by(key: key).update!(value: value)
      end

      def [](key)
        where(key: key).pluck(:value).first
      end

      def table_exists?
        connection.table_exists?(table_name)
      end

      # Creates an internal metadata table with columns +key+ and +value+
      def create_table
        unless table_exists?
          key_options = connection.internal_string_options_for_primary_key

          connection.create_table(table_name, id: false) do |t|
            t.string :key, key_options
            t.string :value
            t.timestamps
          end
        end
      end

      def drop_table
        connection.drop_table table_name, if_exists: true
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 4 rubygems

Version Path
activerecord-6.0.2.2 lib/active_record/internal_metadata.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/internal_metadata.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/internal_metadata.rb
activerecord-6.0.2.1 lib/active_record/internal_metadata.rb
activerecord-6.0.2 lib/active_record/internal_metadata.rb
activerecord-6.0.2.rc2 lib/active_record/internal_metadata.rb
activerecord-6.0.2.rc1 lib/active_record/internal_metadata.rb
activerecord-6.0.1 lib/active_record/internal_metadata.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/internal_metadata.rb
activerecord-6.0.1.rc1 lib/active_record/internal_metadata.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/internal_metadata.rb
activerecord-6.0.0 lib/active_record/internal_metadata.rb
activerecord-6.0.0.rc2 lib/active_record/internal_metadata.rb
activerecord-6.0.0.rc1 lib/active_record/internal_metadata.rb