Clean with transaction not working for 2 databases
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- postgresql, ruby
- Lĩnh vực
- databases, testing-qa
Hướng nghiên cứu
Bắt đầu với lib/database_cleaner/active_record/transaction.rb, đặc biệt là các đường dẫn start và clean của chiến lược transaction, rồi tái hiện thiết lập RSpec gồm hai example được mô tả trong issue. So sánh số lượng transaction được ghi log và trạng thái cơ sở dữ liệu của tenant trước và sau mỗi example. Được xem là hoàn tất khi sau cleanup, cơ sở dữ liệu của tenant không còn bệnh nhân nào từ example trước đó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I've used DatabaseCleaner for several years (thanks!) without issues. But my current setup seems to not work properly.
Basic setup:
- Grape 0.19.2
- ActiveRecord 5.1.3 (+ otr-activerecord 1.2.4)
- RSpec 3.6.0
- DatabaseCleaner 1.6.1
- Posgres 9.5
On noteworthy thing about the app here is that it's using multiple databases: One main database and several tenant databases. In the test environment, this currently means two databases in total (main + test tenant).
Here's the test setup:
TEST_TENANT = 'the_tenant'
TEST_TENANT_API_KEY = SecureRandom.uuid.gsub('-', '')
RSpec.configure do |config|
config.mock_with :rspec
config.expect_with :rspec
config.raise_errors_for_deprecations!
# config.order = 'random'
config.before(:suite) do
ApiApp.establish_connection # ApiApp is connected to the main DB
primary_cleaner.clean_with(:truncation)
tenant = ApiApp.where(identifier: TEST_TENANT).first_or_create!(
name: 'The tenant',
api_key: TEST_TENANT_API_KEY
)
# force full recreate of tenant DB
config = ActiveRecord::Base.configurations[tenant.identifier]
ActiveRecord::Tasks::DatabaseTasks.drop(config) rescue nil
ActiveRecord::Tasks::DatabaseTasks.create(config)
ActiveRecord::Tasks::DatabaseTasks.load_schema(config, :sql, 'db/structure.sql')
# seed it
AppSetter.with(TEST_TENANT) do
Dir["#{Dir.pwd}/spec/seeds/*.rb"].sort.each { |f| load f }
end
end
config.before(:each) do |example|
AppSetter.set(TEST_TENANT)
puts ['[RSpec] start before', "number of patients: #{Patient.count}", Patient.connection.instance_variable_get(:@config)[:database]].inspect
# primary_cleaner.start
tenant_cleaner.start
puts ['[RSpec] end before', "number of patients: #{Patient.count}", Patient.connection.instance_variable_get(:@config)[:database]].inspect
end
config.append_after(:each) do
puts ['[RSpec] start append_after', "number of patients: #{Patient.count}", Patient.connection.instance_variable_get(:@config)[:database]].inspect
# primary_cleaner.clean
tenant_cleaner.clean
puts ['[RSpec] end append_after', "number of patients: #{Patient.count}", Patient.connection.instance_variable_get(:@config)[:database]].inspect
puts '-' * 100
end
private
def primary_cleaner
@primary_cleaner ||= cleaner_for(connection: ENV['RACK_ENV'].to_sym)
end
def tenant_cleaner
@tenant_cleaner ||= cleaner_for(connection: TEST_TENANT.to_sym)
end
def cleaner_for(options)
DatabaseCleaner[:active_record, options].tap { |cleaner| cleaner.strategy = :transaction }
end
end
Now when I have 2 examples in RSpec which both use the same basic setup with a test patient (Patient.create!(email: '[email protected]', ...)), the uniqueness constraint on email fails because apparently the record exists already. The debugging code in the output (from the puts statements above) confirms this:
(1) ["[RSpec] start before", "number of patients: 0", "api_test_the_tenant"]
["[DatabaseCleaner] before start", "open transactions: 0", "api_test_the_tenant"]
(2) ["[DatabaseCleaner] after start", "open transactions: 1", "api_test_the_tenant"]
["[RSpec] end before", "number of patients: 0", "api_test_the_tenant"]
(3) ["[RSpec] start append_after", "number of patients: 1", "api_test_the_tenant"]
["[DatabaseCleaner] before clean", "open transactions: 1", "api_test_the_tenant"]
(4) ["[DatabaseCleaner] after clean", "open transactions: 0", "api_test_the_tenant"]
(5) ["[RSpec] end append_after", "number of patients: 1", "api_test_the_tenant"]
----------------------------------------------------------------------------------------------------
(6) ["[RSpec] start before", "number of patients: 1", "api_test_the_tenant"]
["[DatabaseCleaner] before start", "open transactions: 0", "api_test_the_tenant"]
["[DatabaseCleaner] after start", "open transactions: 1", "api_test_the_tenant"]
["[RSpec] end before", "number of patients: 1", "api_test_the_tenant"]
["[RSpec] start append_after", "number of patients: 1", "api_test_the_tenant"]
["[DatabaseCleaner] before clean", "open transactions: 1", "api_test_the_tenant"]
["[DatabaseCleaner] after clean", "open transactions: 0", "api_test_the_tenant"]
["[RSpec] end append_after", "number of patients: 1", "api_test_the_tenant"]
----------------------------------------------------------------------------------------------------
(I've numbered some lines to make it easier to follow. The [DatabaseCleaner] output comes from some puts statements I've put in the corresponding strategy in https://github.com/DatabaseCleaner/database_cleaner/blob/master/lib/database_cleaner/active_record/transaction.rb.)
(1) Before the first example (in the first before block), there are 0 patients in the tenant database => expected.
(2) tenant_cleaner.start has caused an open transaction in the tenant database => expected.
(3) Running the example has created a patient in the tenant database => expected.
(4) tenant_cleaner.clean rolls back the transaction => expected.
(5) There is still 1 patient in the tenant database => this is NOT expected.
(6) The before block of the next example confirms that 1 patient has remained in the tenant database => this is NOT expected.
Commenting in/out the cleaning of the primary database doesn't change anything (I wouldn't have expected it to either, but I tried it nonetheless).
Does anyone have any insights as to what's going wrong here?
Thanks a lot in advance.
- Ngôn ngữ chính
- Ruby
- Star
- 76
- Fork
- 76
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DatabaseCleaner/database_cleaner-active_record
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
DatabaseCleaner/database_cleaner-active_record#128 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
DatabaseCleaner/database_cleaner-active_record#127 · 3 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
-
Parallelized truncatesĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
Tất cả issue của DatabaseCleaner/database_cleaner-active_record
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
solana-foundation/pay-kit#341 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
TheOdinProject/curriculum#31427 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Mail processor needs SSL flagĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
cyclestreets/cyclescape#1107 · 1 reaction ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
Maintainer thường phản hồi trong vòng 1 ngày