activerecord-sqlserver-adapter icon indicating copy to clipboard operation
activerecord-sqlserver-adapter copied to clipboard

Rails tests can't truncate tables when run in parallel

Open erlingur opened this issue 4 years ago • 3 comments

Issue

Using Minitest and fixtures I will get an error ActiveRecord::StatementInvalid: TinyTds::Error: Cannot truncate table 'active_storage_blobs' because it is being referenced by a FOREIGN KEY constraint. when I try to run any tests.

Expected behavior

Tests should run without an error.

Actual behavior

When I have in my test helper parallelize(workers: :number_of_processors) it will give the above error. Changing it to parallelize(workers: 1) makes it work. I think it has something to do with the timing of disabling the referential integrity but I'm not sure.

How to reproduce

Set up a clean Rails app, add something like ActiveStorage, create a couple of models like User and Post. Create a test for the user and run it.

I've created a demo repo here where this error occurs: https://github.com/erlingur/sqlserver_test You should be able to clone it, point it at an SQL server and run rails test to make it blow up (possibly have to run it twice).

I created the app by literally doing the following:

rails new sqlserver_test
bundle add activerecord-sqlserver-adapter
# Change database.yml
rails db:create
rails g model User name
rails g model Post title user:belongs_to
rails active_storage:install
rails db:migrate
# Create the single User test
rails test

Details

  • Rails version: 6.1.4.1
  • SQL Server adapter version: 6.1.2.1
  • TinyTDS version: 2.1.5
  • FreeTDS details:
Compile-time settings (established with the "configure" script)
                            Version: freetds v1.3.3
             freetds.conf directory: /opt/homebrew/etc
     MS db-lib source compatibility: no
        Sybase binary compatibility: yes
                      Thread safety: yes
                      iconv library: yes
                        TDS version: 7.3
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: yes
                            OpenSSL: yes
                             GnuTLS: no
                               MARS: yes

erlingur avatar Sep 15 '21 23:09 erlingur

This problem is still present in 2024 with Rails 7

Michoels avatar Jan 01 '24 13:01 Michoels

PRs welcome.

aidanharan avatar Jan 01 '24 14:01 aidanharan

At the time I gave it a pretty good try at finding the issue and trying to fix it but I was unsuccessful. I'll try it again soon, hopefully I'm a little smarter now three years later :)

erlingur avatar Jan 02 '24 13:01 erlingur