Sha256: d7a862ce3923372a34742e96c189eec9ac924cd8008a36f8dd523af069346e2f

Contents?: true

Size: 1.5 KB

Versions: 41

Compression:

Stored size: 1.5 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.
  #
  # This is enabled by default. To disable this functionality set
  # `use_metadata_table` to false in your database configuration.
  class InternalMetadata < ActiveRecord::Base # :nodoc:
    self.record_timestamps = true

    class << self
      def enabled?
        ActiveRecord::Base.connection.use_metadata_table?
      end

      def primary_key
        "key"
      end

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

      def []=(key, value)
        return unless enabled?

        find_or_initialize_by(key: key).update!(value: value)
      end

      def [](key)
        return unless enabled?

        where(key: key).pick(:value)
      end

      # Creates an internal metadata table with columns +key+ and +value+
      def create_table
        return unless enabled?

        unless connection.table_exists?(table_name)
          connection.create_table(table_name, id: false) do |t|
            t.string :key, **connection.internal_string_options_for_primary_key
            t.string :value
            t.timestamps
          end
        end
      end

      def drop_table
        return unless enabled?

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

Version data entries

41 entries across 39 versions & 5 rubygems

Version Path
activerecord-7.0.8.6 lib/active_record/internal_metadata.rb
activerecord-7.0.8.5 lib/active_record/internal_metadata.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/active_record/internal_metadata.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/internal_metadata.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/internal_metadata.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/internal_metadata.rb
activerecord-7.0.8.4 lib/active_record/internal_metadata.rb
activerecord-7.0.8.1 lib/active_record/internal_metadata.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/internal_metadata.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/internal_metadata.rb
activerecord-7.0.8 lib/active_record/internal_metadata.rb
activerecord-7.0.7.2 lib/active_record/internal_metadata.rb
activerecord-7.0.7.1 lib/active_record/internal_metadata.rb
activerecord-7.0.7 lib/active_record/internal_metadata.rb
activerecord-7.0.6 lib/active_record/internal_metadata.rb
activerecord-7.0.5.1 lib/active_record/internal_metadata.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/internal_metadata.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/internal_metadata.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/internal_metadata.rb
activerecord-7.0.5 lib/active_record/internal_metadata.rb