aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/internal_metadata.rb
blob: 10fee4dca2d2f37bcb22de3b8269777b01c00acb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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:
    # Keys in mysql are limited to 191 characters, due to this no adapter can
    # use a longer key
    KEY_LIMIT = 191

    class << self
      def primary_key
        "key"
      end

      def table_name
        "#{table_name_prefix}#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}"
      end

      def index_name
        "#{table_name_prefix}unique_#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}"
      end

      def []=(key, value)
        first_or_initialize(key: key).update_attributes!(value: value)
      end

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

      def table_exists?
        ActiveSupport::Deprecation.silence { connection.table_exists?(table_name) }
      end

      # Creates an internal metadata table with columns +key+ and +value+
      def create_table
        unless table_exists?
          connection.create_table(table_name, id: false) do |t|
            t.column :key,   :string, null: false, limit: KEY_LIMIT
            t.column :value, :string
            t.index  :key, unique: true, name: index_name

            t.timestamps
          end
        end
      end
    end
  end
end