Skip to content
This repository
tree: 6cde635f34
Fetching contributors…

Cannot retrieve contributors at this time

file 176 lines (137 sloc) 7.465 kb
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
require 'cases/helper'

module ActiveRecord
  class Migration
    class IndexTest < ActiveRecord::TestCase
      attr_reader :connection, :table_name

      def setup
        super
        @connection = ActiveRecord::Base.connection
        @table_name = :testings

        connection.create_table table_name do |t|
          t.column :foo, :string, :limit => 100
          t.column :bar, :string, :limit => 100

          t.string :first_name
          t.string :last_name, :limit => 100
          t.string :key, :limit => 100
          t.boolean :administrator
        end
      end

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

      def test_rename_index
        skip "not supported on openbase" if current_adapter?(:OpenBaseAdapter)

        # keep the names short to make Oracle and similar behave
        connection.add_index(table_name, [:foo], :name => 'old_idx')
        connection.rename_index(table_name, 'old_idx', 'new_idx')

        # if the adapter doesn't support the indexes call, pick defaults that let the test pass
        refute connection.index_name_exists?(table_name, 'old_idx', false)
        assert connection.index_name_exists?(table_name, 'new_idx', true)
      end

      def test_double_add_index
        skip "not supported on openbase" if current_adapter?(:OpenBaseAdapter)

        connection.add_index(table_name, [:foo], :name => 'some_idx')
        assert_raises(ArgumentError) {
          connection.add_index(table_name, [:foo], :name => 'some_idx')
        }
      end

      def test_remove_nonexistent_index
        skip "not supported on openbase" if current_adapter?(:OpenBaseAdapter)

        # we do this by name, so OpenBase is a wash as noted above
        assert_raise(ArgumentError) { connection.remove_index(table_name, "no_such_index") }
      end

      def test_add_index_name_length_limit
        good_index_name = 'x' * connection.index_name_length
        too_long_index_name = good_index_name + 'x'

        assert_raises(ArgumentError) {
          connection.add_index(table_name, "foo", :name => too_long_index_name)
        }

        refute connection.index_name_exists?(table_name, too_long_index_name, false)
        connection.add_index(table_name, "foo", :name => good_index_name)

        assert connection.index_name_exists?(table_name, good_index_name, false)
        connection.remove_index(table_name, :name => good_index_name)
      end

      def test_index_symbol_names
        connection.add_index table_name, :foo, :name => :symbol_index_name
        assert connection.index_exists?(table_name, :foo, :name => :symbol_index_name)

        connection.remove_index table_name, :name => :symbol_index_name
        refute connection.index_exists?(table_name, :foo, :name => :symbol_index_name)
      end

      def test_index_exists
        connection.add_index :testings, :foo

        assert connection.index_exists?(:testings, :foo)
        assert !connection.index_exists?(:testings, :bar)
      end

      def test_index_exists_on_multiple_columns
        connection.add_index :testings, [:foo, :bar]

        assert connection.index_exists?(:testings, [:foo, :bar])
      end

      def test_unique_index_exists
        connection.add_index :testings, :foo, :unique => true

        assert connection.index_exists?(:testings, :foo, :unique => true)
      end

      def test_named_index_exists
        connection.add_index :testings, :foo, :name => "custom_index_name"

        assert connection.index_exists?(:testings, :foo, :name => "custom_index_name")
      end

      def test_add_index_attribute_length_limit
        connection.add_index :testings, [:foo, :bar], :length => {:foo => 10, :bar => nil}

        assert connection.index_exists?(:testings, [:foo, :bar])
      end

      def test_add_index
        connection.add_index("testings", "last_name")
        connection.remove_index("testings", "last_name")

        # Orcl nds shrt indx nms. Sybs 2.
        # OpenBase does not have named indexes. You must specify a single column name
        unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter)
          connection.add_index("testings", ["last_name", "first_name"])
          connection.remove_index("testings", :column => ["last_name", "first_name"])

          # Oracle adapter cannot have specified index name larger than 30 characters
          # Oracle adapter is shortening index name when just column list is given
          unless current_adapter?(:OracleAdapter)
            connection.add_index("testings", ["last_name", "first_name"])
            connection.remove_index("testings", :name => :index_testings_on_last_name_and_first_name)
            connection.add_index("testings", ["last_name", "first_name"])
            connection.remove_index("testings", "last_name_and_first_name")
          end
          connection.add_index("testings", ["last_name", "first_name"])
          connection.remove_index("testings", ["last_name", "first_name"])

          connection.add_index("testings", ["last_name"], :length => 10)
          connection.remove_index("testings", "last_name")

          connection.add_index("testings", ["last_name"], :length => {:last_name => 10})
          connection.remove_index("testings", ["last_name"])

          connection.add_index("testings", ["last_name", "first_name"], :length => 10)
          connection.remove_index("testings", ["last_name", "first_name"])

          connection.add_index("testings", ["last_name", "first_name"], :length => {:last_name => 10, :first_name => 20})
          connection.remove_index("testings", ["last_name", "first_name"])
        end

        # quoting
        # Note: changed index name from "key" to "key_idx" since "key" is a Firebird reserved word
        # OpenBase does not have named indexes. You must specify a single column name
        unless current_adapter?(:OpenBaseAdapter)
          connection.add_index("testings", ["key"], :name => "key_idx", :unique => true)
          connection.remove_index("testings", :name => "key_idx", :unique => true)
        end

        # Sybase adapter does not support indexes on :boolean columns
        # OpenBase does not have named indexes. You must specify a single column
        unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter)
          connection.add_index("testings", %w(last_name first_name administrator), :name => "named_admin")
          connection.remove_index("testings", :name => "named_admin")
        end

        # Selected adapters support index sort order
        if current_adapter?(:SQLite3Adapter, :MysqlAdapter, :Mysql2Adapter, :PostgreSQLAdapter)
          connection.add_index("testings", ["last_name"], :order => {:last_name => :desc})
          connection.remove_index("testings", ["last_name"])
          connection.add_index("testings", ["last_name", "first_name"], :order => {:last_name => :desc})
          connection.remove_index("testings", ["last_name", "first_name"])
          connection.add_index("testings", ["last_name", "first_name"], :order => {:last_name => :desc, :first_name => :asc})
          connection.remove_index("testings", ["last_name", "first_name"])
          connection.add_index("testings", ["last_name", "first_name"], :order => :desc)
          connection.remove_index("testings", ["last_name", "first_name"])
        end
      end

    end
  end
end
Something went wrong with that request. Please try again.