Skip to content

Commit 1f84070

Browse files
takaokoujiclaude
andcommitted
Add Rails 7.1 compatibility (partial - 664/674 tests passing)
Implement Rails 7.1 compatibility fixes while maintaining backward compatibility with Rails 6.1 and 7.0: Changes: - Fix version-conditional app initialization in test_helper.rb - Rails 7.1+ requires TestApp.initialize! before rails/test_help - Rails 6.1/7.0 require opposite order - Fix schema compatibility in active_record.rb - Change t.string :length option to :limit (Rails 7.1 deprecation) - Change t.references :references option to foreign_key: false - Disable SQLite foreign key constraints in test environment - Rails 7.1 enables strict FK constraints by default - Added PRAGMA foreign_keys = OFF for test compatibility Backward compatibility verified: - Rails 6.1.7.10: ✅ 674/674 tests passing - Rails 7.0.10: ✅ 674/674 tests passing Rails 7.1.6 status: - 664/674 tests passing (98.5%) - 1 failure: RequestTest expecting 422, got 0 - 9 errors: "Invalid response code: 0" in controller tests Remaining work: - Investigate and fix response code 0 errors - Get all 674 tests passing in Rails 7.1.6 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2524b75 commit 1f84070

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

test/fixtures/active_record.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
end
5353

5454
create_table :posts, force: true do |t|
55-
t.string :title, length: 255
55+
t.string :title, limit: 255
5656
t.text :body
5757
t.integer :author_id
5858
t.integer :parent_post_id
@@ -324,8 +324,8 @@
324324

325325
create_table :related_things, force: true do |t|
326326
t.string :name
327-
t.references :from, references: :thing
328-
t.references :to, references: :thing
327+
t.references :from, foreign_key: false
328+
t.references :to, foreign_key: false
329329

330330
t.timestamps null: false
331331
end

test/test_db-shm

32 KB
Binary file not shown.

test/test_db-wal

3.95 MB
Binary file not shown.

test/test_helper.rb

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,10 @@
4747
ENV['DATABASE_URL'] ||= "sqlite3:test_db"
4848

4949
require 'active_record/railtie'
50-
require 'rails/test_help'
51-
require 'minitest/mock'
52-
require 'jsonapi-resources'
53-
require 'pry'
54-
55-
require File.expand_path('../helpers/value_matchers', __FILE__)
56-
require File.expand_path('../helpers/assertions', __FILE__)
57-
require File.expand_path('../helpers/functional_helpers', __FILE__)
58-
require File.expand_path('../helpers/configuration_helpers', __FILE__)
5950

51+
# Rails 7.1+ requires the application to be defined before requiring rails/test_help
6052
Rails.env = 'test'
6153

62-
I18n.load_path += Dir[File.expand_path("../../locales/*.yml", __FILE__)]
63-
I18n.enforce_available_locales = false
64-
65-
JSONAPI.configure do |config|
66-
config.json_key_format = :camelized_key
67-
end
68-
69-
ActiveSupport::Deprecation.silenced = true
70-
71-
puts "Testing With RAILS VERSION #{Rails.version}"
72-
7354
class TestApp < Rails::Application
7455
config.eager_load = false
7556
config.root = File.dirname(__FILE__)
@@ -91,6 +72,39 @@ class TestApp < Rails::Application
9172
end
9273
end
9374

75+
# Rails 7.1+ requires the application to be initialized before requiring rails/test_help
76+
# Earlier versions require the opposite order
77+
if Rails::VERSION::MAJOR >= 8 || (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1)
78+
TestApp.initialize!
79+
end
80+
81+
require 'rails/test_help'
82+
83+
# Initialize app for Rails < 7.1 (for Rails 7.1+ it was already initialized above)
84+
unless Rails::VERSION::MAJOR >= 8 || (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1)
85+
TestApp.initialize!
86+
end
87+
88+
require 'minitest/mock'
89+
require 'jsonapi-resources'
90+
require 'pry'
91+
92+
require File.expand_path('../helpers/value_matchers', __FILE__)
93+
require File.expand_path('../helpers/assertions', __FILE__)
94+
require File.expand_path('../helpers/functional_helpers', __FILE__)
95+
require File.expand_path('../helpers/configuration_helpers', __FILE__)
96+
97+
I18n.load_path += Dir[File.expand_path("../../locales/*.yml", __FILE__)]
98+
I18n.enforce_available_locales = false
99+
100+
JSONAPI.configure do |config|
101+
config.json_key_format = :camelized_key
102+
end
103+
104+
ActiveSupport::Deprecation.silenced = true
105+
106+
puts "Testing With RAILS VERSION #{Rails.version}"
107+
94108
DatabaseCleaner.allow_remote_database_url = true
95109
DatabaseCleaner.strategy = :transaction
96110

@@ -214,10 +228,15 @@ def show_queries
214228
end
215229
end
216230

217-
TestApp.initialize!
231+
# TestApp.initialize! already called earlier for Rails 7.1+ compatibility
218232

219233
require File.expand_path('../fixtures/active_record', __FILE__)
220234

235+
# Disable foreign key constraints for SQLite in test environment (Rails 7.1+)
236+
# This allows force: true to drop and recreate tables without constraint errors
237+
# Must be called after active_record.rb which sets up the schema
238+
ActiveRecord::Base.connection.execute("PRAGMA foreign_keys = OFF") if ActiveRecord::Base.connection.adapter_name == 'SQLite'
239+
221240
module Pets
222241
module V1
223242
class CatsController < JSONAPI::ResourceController

0 commit comments

Comments
 (0)