Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Table name not escaped on dropping index

#4809

While this works just fine:

add_index :values, [:field_id, :user_id]

this doesn't work:

remove_index :values, :column => [:field_id, :user_id]

failing with this error:

Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'values' at line 1: DROP INDEX index_values_on_field_id_and_user_id ON values

I think the issue is that values is not escaped at the end of that query, if it generated

DROP INDEX `index_values_on_field_id_and_user_id` ON `values`

it might work.

Reported by pupeno@pupeno.com · June 9th, 2010 @ 05:04 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. pupeno@pupeno.com
    pupeno@pupeno.com
    • Tag changed from mysql remove_index index to database, index, mysql, remove_index

    I workarrounded it this way:

    remove_index "`values`", :name => "index_values_on_field_id_and_user_id"
    

    June 9th, 2010 @ 05:06 PM

  2. pupeno@pupeno.com
    pupeno@pupeno.com
    • Tag changed from database, index, mysql, remove_index to database, index, mysql, patch, remove_index
    • Title changed from Table name not escaped on dropping index to [PATCH] Table name not escaped on dropping index

    I'm attaching the patch that fixes this issue. It contains a test that shows the issue. I'm quite sure the way I wrote the test is not good, I've created another test class and I created the test as small and simple as possible. If it is not the correct way, can you tell me how you'd like me to do it and I'll make new patches.

    June 12th, 2010 @ 02:37 AM

  3. pupeno@pupeno.com
  4. Paul Barry
    Paul Barry

    Looks good, it's definitely a bug that should be fixed. I've attached a new patch that slightly modifies the test so that it asserts that no exception is raised, rather than just stating that in a comment

    June 12th, 2010 @ 04:05 AM

  5. pupeno@pupeno.com
    pupeno@pupeno.com

    Thanks Paul, I just updated my repo with your patch.

    June 12th, 2010 @ 11:05 AM

  6. Étienne Barrié
    Étienne Barrié

    It's a regression from http://github.com/rails/rails/commit/3809c80cd55ac2838f050346800889... (sorry about that).

    I would have put the test along the other ones in MigrationTest, though. And the call to add_index could also be in an assert_nothing_raised block.

    But the patch as it is solves the problem and works under 1.8.7 and 1.9.1 on master and 2-3-stable (cherry-picking raises a conflict, but it's easy to solve).

    June 13th, 2010 @ 02:46 PM

  7. pupeno@pupeno.com
    pupeno@pupeno.com
    • Tag changed from database, index, mysql, patch, remove_index to database, index, migration, mysql, patch, remove_index

    Étienne,

    Paul Barry added the assert_nothing_raised block. I'll look into the conflict.

    Regarding MigrationTest, I should add the test here: activerecord/test/cases/migration_test.rb and then should I add a values table here: activerecord/test/schema/schema.rb?

    With that second part I had some trouble: http://groups.google.com/group/rubyonrails-core/browse_thread/threa...

    June 14th, 2010 @ 12:04 PM

  8. Étienne Barrié
    Étienne Barrié

    Paul added the assert_nothing_raised block to remove_index, but not to add_index.
    You added the test to migration_test.rb but in a new TestCase, outside of MigrationTest. I would have put it inside, at line 160 for example.
    And I don't think you need to add a new table, just add/remove indices with reserved names to an existing table, like it was done in test_add_index_length_limit for
    example.

    June 14th, 2010 @ 01:19 PM

  9. pupeno@pupeno.com
    pupeno@pupeno.com

    Étienne, oh, got it regarding assert_nothing_raised. I'll fix it.

    But the issue was the name of the table, not the name of the index. What wasn't being escaped was the name of the table, if the name of the table is not reserver it'll work just fine.

    June 14th, 2010 @ 01:48 PM

  10. pupeno@pupeno.com
    pupeno@pupeno.com

    New patch that applies cleanly (I actually couldn't find the conflict) and puts the add_index inside the no exceptions zone.

    June 15th, 2010 @ 10:39 PM

  11. Étienne Barrié
    Étienne Barrié

    The conflict appears when you cherry-pick (or rebase --onto) your commits from master to 2-3-stable. But it's just a matter of removing the conflict markers.

    June 16th, 2010 @ 08:42 AM

  12. pupeno@pupeno.com
    pupeno@pupeno.com

    Oh, right, I only worked in Rails 3 for this patch.

    June 16th, 2010 @ 08:50 AM

  13. Norman Clarke
    Norman Clarke
    • Tag changed from database, index, migration, mysql, patch, remove_index to database, index, migration, mysql, patch, remove_index, verified

    +1

    I just applied this patch in my copy of rails master; it applies cleanly and all of the tests which pass in master pass with the patch applied on SQLite3, MySQL and Postgres.

    June 29th, 2010 @ 01:19 PM

  14. Matt Jones
    Matt Jones
    • Importance changed from to Low

    Can you rebase the patch to a single commit, rather than a group of three?

    June 29th, 2010 @ 02:50 PM

  15. pupeno@pupeno.com
    pupeno@pupeno.com

    Matt,

    Yes, I can do that. Wouldn't that remove authorship from Paul Barry, and if so, is it acceptable for me to do it?

    June 29th, 2010 @ 03:57 PM

  16. Santiago Pastorino
    Santiago Pastorino
    • Assigned user set to José Valim
    • State changed from new to open
    • Milestone cleared.

    June 29th, 2010 @ 06:17 PM

  17. José Valim
    José Valim

    It's ok to be three commits. I will apply it soon.

    June 29th, 2010 @ 06:26 PM

  18. Repository
    Repository
    • State changed from open to resolved

    (from [21957b72ea394c679d9b17e75b570cc99596322d]) Test that adding an index also doesn't raise an exception.

    [#4809 state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/21957b72ea394c679d9b17e75b570c...

    June 29th, 2010 @ 08:19 PM

  19. Jeremy Kemper
  20. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    November 8th, 2010 @ 01:52 AM