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.

SQLiteAdapter fails with a dark message when creating columns when inside a transaction

#197

While running some tests which require modifying some fields in a DB table, I ran across a problem that made me lose quite some time. The error I got when running the test (under SQLite3) was:

ActiveRecord::StatementInvalid: SQLite3::SQLException: SQL logic error or missing database: VACUUM

Now, digging into ConnectionAdapters::SQLiteAdapter, I found:

def add_column(table_name, column_name, type, options = {}) #:nodoc:

super(table_name, column_name, type, options)

  1. See last paragraph on http://www.sqlite.org/lang_alter...

execute "VACUUM"

end

Now... Refering to its own documentation, SQLite will fail on vacuums when there is an active transaction (http://www.sqlite.org/lang_vacuu...)... So my tests fail miserably.

I would suggest either of the attached patches.

First version: Try to create the requested column; if it was called in mid-transaction, the DB won't be readable with older versions of MySQL, but it will succeed:

The second possibility is just to disallow adding columns while in a transaction:

Any of them will be better than just leaving the user scratching his head to understand where the error lies :)

Reported by Gunnar Wolf · May 14th, 2008 @ 06:55 PM

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Gunnar Wolf
    Gunnar Wolf

    UGH... This got formatted as hell. I'm uploading both versions of the patch.

    Here goes the first version: Try to make it work, just leaving out the vacuum

    May 14th, 2008 @ 06:54 PM

  2. Gunnar Wolf
  3. Pratik
    Pratik
    • State changed from new to incomplete
    • Assigned user set to Pratik

    Can you please add a test case verifying expected exception is raised for sqlite adapter ?

    Thanks.

    May 19th, 2008 @ 11:12 AM

  4. Gunnar Wolf
    Gunnar Wolf

    Here it is - I'm doing the test for the second case I mentioned (raising a StatementInvalid when adding a column inside a transaction).

    Stylistically it might be quite un-railish - But it does work as expected.

    May 19th, 2008 @ 05:33 PM

  5. Repository
    Repository
    • State changed from incomplete to resolved

    (from [089251581137b041828a7e6dcbf75ecbef55b4a3]) Ensure add_column gives valid error for sqlite. [Gunnar Wolf, Pratik] [#197 state:resolved]

    SQLite#add_column executes "VACUUM", which fails if inside a live transaction. This patch

    ensures a valid exception is raised if add_column is executed within a live transaction for

    sqlite adapter.

    http://github.com/rails/rails/co...

    May 20th, 2008 @ 11:14 AM