aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/schema_migration.rb
blob: 6077144265e8669d1ca9d2a37f7b6919d80d2ad6 (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
require 'active_record/scoping/default'
require 'active_record/scoping/named'
require 'active_record/base'

module ActiveRecord
  class SchemaMigration < ActiveRecord::Base

    def self.table_name
      "#{Base.table_name_prefix}schema_migrations#{Base.table_name_suffix}"
    end

    def self.index_name
      "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"
    end

    def self.create_table(limit=nil)
      unless connection.table_exists?(table_name)
        version_options = {null: false}
        version_options[:limit] = limit if limit

        connection.create_table(table_name, id: false) do |t|
          t.column :version, :string, version_options
        end
        connection.add_index table_name, :version, unique: true, name: index_name
      end
    end

    def self.drop_table
      if connection.table_exists?(table_name)
        connection.remove_index table_name, name: index_name
        connection.drop_table(table_name)
      end
    end

    def version
      super.to_i
    end
  end
end