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.

SQLite / SQLite3 ActiveRecord Adapter should quote table names when renaming tables

#2272

OVERVIEW

ActiveRecord does not always quote table names when using SQLite3, which may cause migrations to break when using keywords as table names.

STEPS TO REPRODUCE

  1. Create a new rails project.
  2. Create the CreateReferences migration (see below).
  3. Run the db:migrate task.
  4. Create the RenameReferencesToItems migration (see below).
  5. Run the db:migrate task.

The CreateReferences migration:

create_table :references, :force => true do |t|
  t.string :foo
  t.string :bar
  t.timestamps
end

The RenameReferencesToItems:

rename_table :references, :items

ACTUAL RESULTS

Step 3 runs correctly. The "references" table is created.

Step 5 fails to run correctly. This is the output of the rake task:

==  RenameReferencesToItems: migrating =====
-- rename_table(:references, :items)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: near "references": syntax error: ALTER TABLE references RENAME TO items

EXPECTED RESULTS

Step 5 should not fail. It should rename the "references" table to "items". The generated SQL should be

ALTER TABLE "references" RENAME TO items

instead of

ALTER TABLE references RENAME TO items

AFFECTED VERSIONS

  • SQLite3: 3.6.11.
  • ActiveRecord: 2.3.2
  • Ruby: 1.8.7

Reported by Denis Defreyne · March 17th, 2009 @ 03:26 PM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Brian Hogan
    Brian Hogan
    • Tag set to 2.3.2, activerecord, patch, sql, sqlite, sqlite3

    Here's a patch with tests that resolves the issue. I've seen this come up for quite some time now and finally figured out how to write appropriate tests. Anyone wanna review this and apply it? :)

    March 20th, 2009 @ 03:00 AM

  2. Brian Hogan
    Brian Hogan

    How about we try this again with a patch that uses the quote_table_name mechanism instead of just single quotes? :)

    March 20th, 2009 @ 03:15 AM

  3. Brian Hogan
    Brian Hogan
    • Title changed from ActiveRecord should quote table names to SQLite / SQLite3 ActiveRecord Adapter should quote table names when renaming tables

    Changed title to make this more descriptive

    March 20th, 2009 @ 01:47 PM

  4. Brian Hogan
    Brian Hogan

    Thanks to tpope for noticing my tests sucked - Patch with better tests.

    March 22nd, 2009 @ 05:20 AM

  5. Pratik
    Pratik
    • State changed from new to resolved

    Resolved in b26c2c11ab47dbd4d1e26806d1e861edc7d697e0

    June 21st, 2009 @ 06:26 PM

  6. Repository
  7. Repository