Skip to content

Commit c493c26

Browse files
committed
Add support for SQLite
Rails 8 ships with production-grade SQLite, which means active_record_doctor should support it, too. This commit adds a test suite for SQLite3. This required making a few changes: 1. Making the database name used by the test suite depend on the adapter as SQLite uses an in-memory database. 2. Dedicated require_* methods for skipping test cases that are relevant to the current database under test. 3. Splitting a few test cases into SQLite-specific code and code for other databases. 4. Not testing against Ruby 3.0 as the SQLite gem requires 3.1. 5. Adding a GitHub Actions workflow for SQLite. Additionally, short_primary_key is effectively disabled in SQLite.
1 parent d7509b6 commit c493c26

20 files changed

+240
-122
lines changed

.github/workflows/mysql.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,8 @@ jobs:
6363
- 3306:3306
6464
strategy:
6565
matrix:
66-
ruby: ["3.0", "3.1", "3.2", "3.3"]
66+
ruby: ["3.1", "3.2", "3.3"]
6767
active_record: ["7.0", "7.1", "7.2"]
68-
exclude:
69-
- ruby: "3.0"
70-
active_record: "7.2"
7168
env:
7269
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/Gemfile.activerecord-${{ matrix.active_record }}.x
7370
steps:

.github/workflows/postgresql.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,8 @@ jobs:
6363
- 5432:5432
6464
strategy:
6565
matrix:
66-
ruby: ["3.0", "3.1", "3.2", "3.3"]
66+
ruby: ["3.1", "3.2", "3.3"]
6767
active_record: ["7.0", "7.1", "7.2"]
68-
exclude:
69-
- ruby: "3.0"
70-
active_record: "7.2"
7168
env:
7269
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/Gemfile.activerecord-${{ matrix.active_record }}.x
7370
steps:

.github/workflows/sqlite.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Test SQLite
2+
on: [push, pull_request]
3+
4+
jobs:
5+
test-latest:
6+
name: "Active Record 8.0 + Ruby 3.3"
7+
runs-on: ubuntu-latest
8+
env:
9+
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/Gemfile.activerecord-8.0.x
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: "3.3"
15+
bundler-cache: true
16+
- name: Run the test suite against SQLite
17+
run: bundle exec rake test:sqlite3
18+
19+
test-supported:
20+
name: "Active Record ${{ matrix.active_record }} + Ruby ${{ matrix.ruby }}"
21+
needs: [test-latest]
22+
runs-on: ubuntu-latest
23+
strategy:
24+
matrix:
25+
ruby: ["3.1", "3.2", "3.3"]
26+
active_record: ["7.0", "7.1", "7.2"]
27+
env:
28+
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/Gemfile.activerecord-${{ matrix.active_record }}.x
29+
steps:
30+
- uses: actions/checkout@v4
31+
- uses: ruby/setup-ruby@v1
32+
with:
33+
ruby-version: ${{ matrix.ruby }}
34+
rubygems: ${{ matrix.rubygems }}
35+
bundler-cache: true
36+
- name: Run the test suite against SQLite
37+
run: bundle exec rake test:sqlite3

Rakefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ RuboCop::RakeTask.new
2424
require "rake/testtask"
2525

2626
namespace :test do
27-
["postgresql", "mysql2"].each do |adapter|
27+
["postgresql", "mysql2", "sqlite3"].each do |adapter|
2828
Rake::TestTask.new(adapter) do |t|
29-
t.deps = ["set_#{adapter}_env"]
29+
t.deps = ["prepare_#{adapter}"]
3030
t.libs = ["lib", "test"]
3131
t.ruby_opts = ["-rsetup"]
3232
t.pattern = "test/**/*_test.rb"
@@ -36,10 +36,12 @@ namespace :test do
3636
t.warning = false
3737
end
3838

39-
task("set_#{adapter}_env") { ENV["DATABASE_ADAPTER"] = adapter }
39+
task :"prepare_#{adapter}" do
40+
ENV["DATABASE_ADAPTER"] = adapter
41+
end
4042
end
4143
end
4244

43-
task test: ["test:postgresql", "test:mysql2"]
45+
task test: ["test:postgresql", "test:mysql2", "test:sqlite3"]
4446

4547
task default: :test

active_record_doctor.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ Gem::Specification.new do |s|
2828
s.add_development_dependency "railties", ACTIVE_RECORD_SPEC
2929
s.add_development_dependency "rake", "~> 13.2.1"
3030
s.add_development_dependency "rubocop", "~> 1.68.0"
31+
s.add_development_dependency "sqlite3", "~> 2.2.0"
3132
s.add_development_dependency "transient_record", "~> 2.0.0"
3233
end

gemfiles/Gemfile.activerecord-7.0.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ gemspec path: File.join(File.dirname(__FILE__), "..")
44
gem "activerecord", "~> 7.0.0"
55
gem "pg", "~> 1.5.9"
66
gem "mysql2", "~> 0.5.6"
7+
gem "sqlite3", "~> 1.4"

gemfiles/Gemfile.activerecord-7.1.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ gemspec path: File.join(File.dirname(__FILE__), "..")
44
gem "activerecord", "~> 7.1.0"
55
gem "pg", "~> 1.5.9"
66
gem "mysql2", "~> 0.5.6"
7+
gem "sqlite3", "~> 1.4"

gemfiles/Gemfile.activerecord-7.2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ gemspec path: File.join(File.dirname(__FILE__), "..")
44
gem "activerecord", "~> 7.2.0"
55
gem "pg", "~> 1.5.9"
66
gem "mysql2", "~> 0.5.6"
7+
gem "sqlite3", "~> 1.4"

lib/active_record_doctor/detectors/short_primary_key_type.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def message(table:, column:)
2020
end
2121

2222
def detect
23+
return if ActiveRecordDoctor::Utils.sqlite?
24+
2325
each_table(except: config(:ignore_tables)) do |table|
2426
column = primary_key(table)
2527
next if column.nil?

lib/active_record_doctor/utils.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ def mysql?(connection = ActiveRecord::Base.connection)
1111
connection.adapter_name == "Mysql2"
1212
end
1313

14+
def sqlite?(connection = ActiveRecord::Base.connection)
15+
connection.adapter_name == "SQLite"
16+
end
17+
1418
def expression_indexes_unsupported?(connection = ActiveRecord::Base.connection)
15-
# Active Record is unable to correctly parse expression indexes for MySQL.
16-
mysql?(connection) && ActiveRecord::VERSION::STRING < "7.1"
19+
sqlite?(connection) ||
20+
# Active Record is unable to correctly parse expression indexes for MySQL.
21+
(mysql?(connection) && ActiveRecord::VERSION::STRING < "7.1")
1722
end
1823
end
1924
end

0 commit comments

Comments
 (0)