Repository: rails/rails
File: activerecord/test/cases/migration/rename_table_test.rb (Ruby)

Code before:
        connection.execute "INSERT INTO #{table_name} (id, url) VALUES (123, 'http://rubyonrails.com')"
        assert_equal "http://rubyonrails.com", connection.select_value("SELECT url FROM #{table_name} WHERE id=123")
      ensure
        if renamed
          connection.rename_table :references, :test_models
          connection.rename_table :old_references, :references
        end
      end

      def test_rename_table
        rename_table :test_models, :octopi

        connection.execute "INSERT INTO octopi (#{connection.quote_column_name('id')}, #{connection.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"

        assert_equal "http://www.foreverflying.com/octopus-black7.jpg", connection.select_value("SELECT url FROM octopi WHERE id=1")
      end

      def test_rename_table_raises_for_long_table_names
        name_limit = connection.table_name_length
        long_name = "a" * (name_limit + 1)
        short_name = "a" * name_limit

        error = assert_raises(ArgumentError) do
          connection.rename_table :test_models, long_name
        end
        assert_equal "Table name '#{long_name}' is too long \(#{name_limit + 1} characters\); the limit is #{name_limit} characters", error.message

        connection.rename_table :test_models, short_name
        assert connection.table_exists?(short_name)
      ensure
        connection.drop_table short_name, if_exists: true
      end

      def test_rename_table_with_an_index
        add_index :test_models, :url

        rename_table :test_models, :octopi

        connection.execute "INSERT INTO octopi (#{connection.quote_column_name('id')}, #{connection.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"

        assert_equal "http://www.foreverflying.com/octopus-black7.jpg", connection.select_value("SELECT url FROM octopi WHERE id=1")
        index = connection.indexes(:octopi).first
        assert_includes index.columns, "url"
        assert_equal "index_octopi_on_url", index.name
      end

      def test_rename_table_with_long_table_name_and_index
        long_name = "a" * connection.table_name_length

        add_index :test_models, :url
        rename_table :test_models, long_name

Review comment (refactor):
```suggestion
        assert_equal "Table name '#{long_name}' is too long \(#{long_name.length} characters\); the limit is #{name_limit} characters", error.message
```

Code after:
        connection.execute "INSERT INTO #{table_name} (id, url) VALUES (123, 'http://rubyonrails.com')"
        assert_equal "http://rubyonrails.com", connection.select_value("SELECT url FROM #{table_name} WHERE id=123")
      ensure
        if renamed
          connection.rename_table :references, :test_models
          connection.rename_table :old_references, :references
        end
      end

      def test_rename_table
        rename_table :test_models, :octopi

        connection.execute "INSERT INTO octopi (#{connection.quote_column_name('id')}, #{connection.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"

        assert_equal "http://www.foreverflying.com/octopus-black7.jpg", connection.select_value("SELECT url FROM octopi WHERE id=1")
      end

      def test_rename_table_raises_for_long_table_names
        name_limit = connection.table_name_length
        long_name = "a" * (name_limit + 1)
        short_name = "a" * name_limit

        error = assert_raises(ArgumentError) do
          connection.rename_table :test_models, long_name
        end
        assert_equal "Table name '#{long_name}' is too long \(#{long_name.length} characters\); the limit is #{name_limit} characters", error.message

        connection.rename_table :test_models, short_name
        assert connection.table_exists?(short_name)
      ensure
        connection.drop_table short_name, if_exists: true
      end

      def test_rename_table_with_an_index
        add_index :test_models, :url

        rename_table :test_models, :octopi

        connection.execute "INSERT INTO octopi (#{connection.quote_column_name('id')}, #{connection.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"

        assert_equal "http://www.foreverflying.com/octopus-black7.jpg", connection.select_value("SELECT url FROM octopi WHERE id=1")
        index = connection.indexes(:octopi).first
        assert_includes index.columns, "url"
        assert_equal "index_octopi_on_url", index.name
      end

      def test_rename_table_with_long_table_name_and_index
        long_name = "a" * connection.table_name_length

        add_index :test_models, :url
        rename_table :test_models, long_name