Skip to content

Commit aafa5b2

Browse files
authored
Reset ids option (#71)
Implement resetting ids for deletion strategy.
1 parent 4afa131 commit aafa5b2

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Deleti
6161

6262
* `:cache_tables` - When set to `true` the list of tables to truncate or delete from will only be read from the DB once, otherwise it will be read before each cleanup run. Set this to `false` if (1) you create and drop tables in your tests, or (2) you change Postgres schemas (`ActiveRecord::Base.connection.schema_search_path`) in your tests (for example, in a multitenancy setup with each tenant in a different Postgres schema). Defaults to `true`.
6363

64+
* `:reset_ids` - Only valid for deletion strategy, when set to `true` resets ids to 1 after each table is cleaned.
65+
6466
## Adapter configuration options
6567

6668
`#db` defaults to the default ActiveRecord database, but can be specified manually in a few ways:

lib/database_cleaner/active_record/deletion.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,27 @@ def clean
1919
def delete_tables(connection, table_names)
2020
table_names.each do |table_name|
2121
delete_table(connection, table_name)
22+
reset_id_sequence(connection, table_name) if @reset_ids
2223
end
2324
end
2425

2526
def delete_table connection, table_name
2627
connection.execute("DELETE FROM #{connection.quote_table_name(table_name)} WHERE 1=1")
2728
end
2829

30+
def reset_id_sequence connection, table_name
31+
case connection.adapter_name
32+
when 'Mysql2'
33+
connection.execute("ALTER TABLE #{table_name} AUTO_INCREMENT = 1;")
34+
when 'SQLite'
35+
connection.execute("delete from sqlite_sequence where name='#{table_name}';")
36+
when 'PostgreSQL'
37+
connection.reset_pk_sequence!(table_name)
38+
else
39+
raise "reset_id option not supported for #{connection.adapter_name}"
40+
end
41+
end
42+
2943
def tables_to_truncate(connection)
3044
if information_schema_exists?(connection)
3145
@except += connection.database_cleaner_view_cache + migration_storage_names

lib/database_cleaner/active_record/truncation.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ module DatabaseCleaner
55
module ActiveRecord
66
class Truncation < Base
77
def initialize(opts={})
8-
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :cache_tables]).empty?
9-
raise ArgumentError, "The only valid options are :only, :except, :pre_count, and :cache_tables. You specified #{opts.keys.join(',')}."
8+
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :cache_tables, :reset_ids]).empty?
9+
raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids, and :cache_tables. You specified #{opts.keys.join(',')}."
1010
end
1111

1212
@only = Array(opts[:only]).dup
1313
@except = Array(opts[:except]).dup
1414

15+
@reset_ids = opts[:reset_ids]
1516
@pre_count = opts[:pre_count]
1617
@cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
1718
end

spec/database_cleaner/active_record/deletion_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
expect(User.create.id).to eq 3
4343
end
4444

45+
context "reset_ids option set to true" do
46+
subject(:strategy) { described_class.new(reset_ids: true) }
47+
it "should reset AUTO_INCREMENT index of table" do
48+
strategy.clean
49+
expect(User.create.id).to eq 1
50+
end
51+
end
52+
4553
it "should delete from all tables except for schema_migrations" do
4654
expect { strategy.clean }
4755
.to_not change { connection.select_value("select count(*) from schema_migrations;").to_i }

0 commit comments

Comments
 (0)