Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord/sqlite3 truncation strategy & sqlite_sequence #66

Closed
earnold opened this issue Jul 11, 2011 · 6 comments
Closed

ActiveRecord/sqlite3 truncation strategy & sqlite_sequence #66

earnold opened this issue Jul 11, 2011 · 6 comments

Comments

@earnold
Copy link

earnold commented Jul 11, 2011

I'm using sqlite and ActiveRecord and the truncation strategy, and I noticed there was some funny behavior with sqlite_sequence not getting cleared. (I have some seed data with hard coded IDs, thus the problem.) I can't think of any reason why this table shouldn't be cleared, and likewise, it DOES get cleared if you use the transaction strategy. I think these two strategies should have the same effective result.

I created a small work-around for this:

earnold@98d324a

Does it seem appropriate for you?

Thanks,

Evan

@bmabey
Copy link
Contributor

bmabey commented Jul 11, 2011

On 7/11/11 11:35 AM, earnold wrote:

I'm using sqlite and ActiveRecord and the truncation strategy, and I noticed there was some funny behavior with sqlite_sequence not getting cleared. (I have some seed data with hard coded IDs, thus the problem.) I can't think of any reason why this table shouldn't be cleared, and likewise, it DOES get cleared if you use the transaction strategy. I think these two strategies should have the same effective result.

I created a small work-around for this:

earnold@98d324a

Does it seem appropriate for you?

Thanks,

Evan

I'm not too familiar with sqlite.. what is special about the
sqlite_sequence table?

@earnold
Copy link
Author

earnold commented Jul 11, 2011

In short, "SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCE table."

Therefore, if you have an entity called lawyers and you create a new one, Sqlite gives it a new ID +1 over the number on the sqlite_sequence table. When you truncate this table, it effectively says that no entities have ever been created (which is more or less true, given the total truncation).

Full details: http://www.sqlite.org/autoinc.html

@earnold earnold closed this as completed Jul 11, 2011
@earnold earnold reopened this Jul 11, 2011
@earnold
Copy link
Author

earnold commented Jul 11, 2011

Sorry! Didn't mean to close this!

@jhollinger
Copy link
Contributor

I second this! In my apps I have been monkey-patching database_cleaner with the following code to get the expected behavior (i.e. table ids being reset to 1, like MySQL's TRUNCATE command does):

class DataMapper::Adapters::SqliteAdapter
  alias_method :broken_truncate_table, :truncate_table
  def truncate_table(table_name)
    broken_truncate_table(table_name)
    execute("DELETE FROM sqlite_sequence") # Reset the table's ids
  end
end

Since SQLite has no TRUNCATE command, the only way to duplicate MySQL's (desirable) behavior is to delete a table's row from sqlite_sequence.

I'd be happy to submit a patch, though since it would only be one line, it's probably just as easy for anyone to do it.

@sobrinho
Copy link
Contributor

@bmabey this issue can be closed due to #84 ;)

@bmabey
Copy link
Contributor

bmabey commented Jul 13, 2012

Thanks!

@bmabey bmabey closed this as completed Jul 13, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants