aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/migration/column_positioning_test.rb
blob: bdbad431e88f120e0ffea39ecca901b83c3c2863 (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
52
53
54
55
56
57
# frozen_string_literal: true
require "cases/helper"

module ActiveRecord
  class Migration
    class ColumnPositioningTest < ActiveRecord::TestCase
      attr_reader :connection
      alias :conn :connection

      def setup
        super

        @connection = ActiveRecord::Base.connection

        connection.create_table :testings, id: false do |t|
          t.column :first, :integer
          t.column :second, :integer
          t.column :third, :integer
        end
      end

      teardown do
        connection.drop_table :testings rescue nil
        ActiveRecord::Base.primary_key_prefix_type = nil
      end

      if current_adapter?(:Mysql2Adapter)
        def test_column_positioning
          assert_equal %w(first second third), conn.columns(:testings).map(&:name)
        end

        def test_add_column_with_positioning
          conn.add_column :testings, :new_col, :integer
          assert_equal %w(first second third new_col), conn.columns(:testings).map(&:name)
        end

        def test_add_column_with_positioning_first
          conn.add_column :testings, :new_col, :integer, first: true
          assert_equal %w(new_col first second third), conn.columns(:testings).map(&:name)
        end

        def test_add_column_with_positioning_after
          conn.add_column :testings, :new_col, :integer, after: :first
          assert_equal %w(first new_col second third), conn.columns(:testings).map(&:name)
        end

        def test_change_column_with_positioning
          conn.change_column :testings, :second, :integer, first: true
          assert_equal %w(second first third), conn.columns(:testings).map(&:name)

          conn.change_column :testings, :second, :integer, after: :third
          assert_equal %w(first third second), conn.columns(:testings).map(&:name)
        end
      end
    end
  end
end