刚刚从sqlite
切换到pg
。我注意到,虽然测试表清除得很好,但ID不会重新启动到1.换句话说,给定测试:
describe "" do
before do
# some dummy is created
Object.create()
puts "count = #{Object.count}"
puts "last object_id = #{Object.id}"
end
...
end
如果你进行了很多测试,你将获得以下输出:
count = 1
last object_id = 1
count = 1
last object_id = 2
count = 1
last object_id = 3
count = 1
last object_id = 4
虽然我觉得清除测试表并重新启动ID以便更好:
count = 1
last object_id = 1
count = 1
last object_id = 1
count = 1
last object_id = 1
count = 1
last object_id = 1
我认为这只是我spec_helper
文件中需要的内容?作为参考,下面是当前的帮助文件,我使用database_cleaner
gem。谢谢!
RSpec.configure do |config|
# Database cleaner set up below
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
# Clean up all jobs specs with truncation
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
答案 0 :(得分:0)
您的psql
版本可能低于8.4
,这就是它无法正常工作的原因。
使用psql --version
查找您的版本,并从数据库清理器源中获取8.4或更高版本。
@restart_identity ||= db_version >= 80400 ? 'RESTART IDENTITY' : ''
我正在运行9.6.2,工作正常。
> truncate users restart identity;
> insert into users (x) values (1);
> select * from users ;
# id | x
# ----+---
# 1 | 1
为确保数据库清理工作正常。
require 'rails/all'
require 'database_cleaner'
ActiveRecord::Base.establish_connection('postgres://localhost/test1')
class User < ActiveRecord::Base; end
User.count
# => 1
DatabaseCleaner.clean_with(:truncation, reset_ids: true)
重置串行列。
$ psql -d test1
> insert into users (x) values (1);
> select * from users ;
# id | x
# ----+---
# 1 | 1