Skip to content

Rails tests can't truncate tables when run in parallel #947

Open
@erlingur

Description

@erlingur

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions