Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
erlingur opened this issue Sep 15, 2021 · 3 comments
Open

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

erlingur opened this issue Sep 15, 2021 · 3 comments

Comments

@erlingur
Copy link

erlingur commented Sep 15, 2021

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
@Michoels
Copy link

Michoels commented Jan 1, 2024

This problem is still present in 2024 with Rails 7

@aidanharan
Copy link
Contributor

PRs welcome.

@erlingur
Copy link
Author

erlingur commented Jan 2, 2024

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants