Skip to content

Commit dce46cb

Browse files
committed
chore(rails): add postgresql to test/CI
1 parent 5c3c53d commit dce46cb

File tree

9 files changed

+134
-34
lines changed

9 files changed

+134
-34
lines changed

.devcontainer/docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ services:
1616
<<: *sentry-build
1717
entrypoint: ".devcontainer/run --service dev"
1818
command: "sleep infinity"
19+
links:
20+
- redis
21+
- postgres
1922
depends_on:
2023
- redis
24+
- postgres
2125

2226
sentry-test:
2327
<<: *sentry-build
@@ -27,6 +31,15 @@ services:
2731
- "${SENTRY_E2E_RAILS_APP_PORT}:4000"
2832
- "${SENTRY_E2E_SVELTE_APP_PORT}:4001"
2933

34+
postgres:
35+
image: postgres:15
36+
environment:
37+
- POSTGRES_USER=postgres
38+
- POSTGRES_PASSWORD=postgres
39+
- POSTGRES_DB=sentry
40+
ports:
41+
- "5432:5432"
42+
3043
redis:
3144
image: redis:latest
3245
environment:

.github/workflows/sentry_rails_test.yml

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,80 @@ jobs:
1717
defaults:
1818
run:
1919
working-directory: sentry-rails
20-
name: Ruby ${{ matrix.ruby_version }} & Rails ${{ matrix.rails_version }}, options - ${{ toJson(matrix.options) }}
20+
name: "Ruby ${{ matrix.ruby_version }} & Rails ${{ matrix.rails_version }} (${{ matrix.database }}) - ${{ toJson(matrix.options) }}"
2121
runs-on: ubuntu-latest
2222
timeout-minutes: 10
23+
services:
24+
postgres:
25+
image: postgres:15
26+
env:
27+
POSTGRES_USER: postgres
28+
POSTGRES_PASSWORD: postgres
29+
POSTGRES_DB: sentry
30+
ports:
31+
- 5432:5432
32+
options: >-
33+
--health-cmd pg_isready
34+
--health-interval 10s
35+
--health-timeout 5s
36+
--health-retries 5
2337
env:
2438
RUBYOPT: ${{ matrix.options.rubyopt }}
2539
BUNDLE_GEMFILE: ${{ github.workspace }}/sentry-rails/Gemfile
2640
BUNDLE_WITHOUT: rubocop
2741
RAILS_VERSION: ${{ matrix.rails_version }}
42+
DATABASE: ${{ matrix.database }}
43+
DATABASE_HOST: "localhost"
2844
JRUBY_OPTS: "--debug" # for more accurate test coverage
2945
strategy:
3046
fail-fast: false
3147
matrix:
3248
ruby_version: ${{ fromJson(inputs.versions) }}
3349
rails_version: [6.1.0, 7.0.0, 7.1.0]
50+
database:
51+
- "sqlite3"
52+
- "postgresql"
3453
include:
3554
- ruby_version: "2.7"
36-
rails_version: 5.2.0
55+
rails_version: "5.2.0"
56+
database: "sqlite3"
3757
- ruby_version: "2.7"
38-
rails_version: 6.0.0
58+
rails_version: "6.0.0"
59+
database: "sqlite3"
3960
- ruby_version: "2.7"
40-
rails_version: 6.1.0
61+
rails_version: "6.1.0"
62+
database: "sqlite3"
4163
- ruby_version: "3.1"
42-
rails_version: 7.2.0
64+
rails_version: "7.2.0"
65+
database: "sqlite3"
4366
- ruby_version: "3.2"
44-
rails_version: 7.2.0
67+
rails_version: "7.2.0"
68+
database: "sqlite3"
4569
- ruby_version: "3.3"
46-
rails_version: 7.2.0
70+
rails_version: "7.2.0"
71+
database: "sqlite3"
4772
- ruby_version: "3.4"
48-
rails_version: 7.2.0
73+
rails_version: "7.2.0"
74+
database: "sqlite3"
75+
- ruby_version: "3.2"
76+
rails_version: "7.1.0"
77+
database: "sqlite3"
78+
options:
79+
rubyopt: "--enable-frozen-string-literal --debug=frozen-string-literal"
80+
- ruby_version: "3.2"
81+
rails_version: "7.1.0"
4982
- ruby_version: "3.2"
5083
rails_version: "8.0.0"
84+
database: "sqlite3"
5185
- ruby_version: "3.3"
5286
rails_version: "8.0.0"
87+
database: "sqlite3"
5388
- ruby_version: "3.4"
5489
rails_version: "8.0.0"
90+
database: "postgresql"
5591
- ruby_version: "3.4"
5692
rails_version: "8.1.0"
57-
- ruby_version: "3.2"
58-
rails_version: 7.1.0
59-
options:
60-
rubyopt: "--enable-frozen-string-literal --debug=frozen-string-literal"
61-
- ruby_version: "3.2"
62-
rails_version: 7.1.0
93+
database: "postgresql"
6394
exclude:
6495
- ruby_version: head
6596
- ruby_version: jruby-head
@@ -70,12 +101,7 @@ jobs:
70101

71102
steps:
72103
- uses: actions/checkout@v4
73-
- name: Install sqlite and ImageMagick
74-
run: |
75-
# See https://github.community/t5/GitHub-Actions/ubuntu-latest-Apt-repository-list-issues/td-p/41122/page/2
76-
for apt_file in `grep -lr microsoft /etc/apt/sources.list.d/`; do sudo rm $apt_file; done
77-
sudo apt-get update
78-
sudo apt-get install libsqlite3-dev imagemagick
104+
79105
- name: Set up Ruby ${{ matrix.ruby_version }}
80106
uses: ruby/setup-ruby@v1
81107
with:

sentry-rails/Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ gem "sentry-ruby", path: "../sentry-ruby"
1111

1212
platform :jruby do
1313
gem "activerecord-jdbcmysql-adapter"
14+
gem "activerecord-jdbcpostgresql-adapter"
1415
gem "jdbc-sqlite3"
1516
end
1617

@@ -29,6 +30,7 @@ end
2930
rails_version = Gem::Version.new(rails_version)
3031

3132
gem "rails", "~> #{rails_version}"
33+
gem "pg", platform: :ruby
3234

3335
if rails_version >= Gem::Version.new("8.1.0")
3436
gem "rspec-rails", "~> 8.0.0"

sentry-rails/spec/dummy/test_rails_app/config/application.rb

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,50 @@ def self.schema_file
4545
@schema_file ||= root_path.join("db/schema.rb")
4646
end
4747

48+
def self.database_url
49+
@__database_url__ ||=
50+
begin
51+
url = ENV["DATABASE_URL"]
52+
53+
if url
54+
url
55+
else
56+
url =
57+
case ENV["DATABASE"].to_s
58+
when "sqlite3" then "sqlite3://#{db_path}"
59+
when "postgresql" then "postgresql://postgres:postgres@#{db_host}:5432/sentry"
60+
else
61+
raise "Unsupported database for tests: #{ENV["DATABASE"].inspect}"
62+
end
63+
64+
# Set it too to make AR happy
65+
ENV["DATABASE_URL"] = url
66+
67+
url
68+
end
69+
end
70+
end
71+
4872
def self.db_path
4973
@db_path ||= root_path.join("db", "db.sqlite3")
5074
end
5175

76+
def self.db_host
77+
@db_host ||= (ENV["DATABASE_HOST"] || "postgres")
78+
end
79+
80+
def self.db_uri
81+
@db_url ||= URI(database_url)
82+
end
83+
84+
def self.db_system
85+
@db_system ||= db_uri.scheme
86+
end
87+
88+
def self.db_name
89+
@db_name ||= File.basename(db_uri.path[1..-1])
90+
end
91+
5292
def self.application_file
5393
@application_file ||= begin
5494
current = Dir[root_path.join("config/applications/rails-*.rb")]
@@ -61,14 +101,29 @@ def self.application_file
61101

62102
def self.load_test_schema
63103
@__schema_loaded__ ||= begin
64-
# This is more reliable than setting config/database.yml
65-
ENV["DATABASE_URL"] = "sqlite3://#{db_path}"
66-
67104
# Silence migrations output
68105
ActiveRecord::Migration.verbose = false
69106

70107
# We need to connect manually here
71-
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: db_path)
108+
case db_uri.scheme
109+
when "postgresql"
110+
ActiveRecord::Base.establish_connection(
111+
adapter: db_uri.scheme,
112+
host: db_uri.host,
113+
username: db_uri.user,
114+
password: db_uri.password,
115+
database: db_name
116+
)
117+
when "sqlite3"
118+
ActiveRecord::Base.establish_connection(
119+
adapter: db_uri.scheme,
120+
database: db_path.to_s
121+
)
122+
else
123+
ActiveRecord::Base.establish_connection(
124+
adapter: db_uri.scheme, database: db_name
125+
)
126+
end
72127

73128
# Load schema from db/schema.rb into the current connection
74129
require Test::Application.schema_file

sentry-rails/spec/dummy/test_rails_app/db/.gitkeep

Whitespace-only changes.

sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@
274274
expect(log_event).not_to be_nil
275275

276276
attributes = log_event[:attributes]
277-
expect(attributes[:db_system][:value]).to eq("sqlite3")
278-
expect(attributes[:db_name][:value]).to eq("db.sqlite3")
277+
expect(attributes[:db_system][:value]).to eq(Rails.application.db_uri.scheme)
278+
expect(attributes[:db_name][:value]).to eq(Rails.application.db_name)
279279
end
280280
end
281281
end

sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
expect(span[:trace_id]).to eq(transaction.dig(:contexts, :trace, :trace_id))
4343

4444
data = span[:data]
45-
expect(data["db.name"]).to include("db")
46-
expect(data["db.system"]).to eq("sqlite3")
45+
expect(data["db.name"]).to include(Rails.application.db_name)
46+
expect(data["db.system"]).to eql(Rails.application.db_system)
4747
end
4848

4949
context "when query source location is avaialble", skip: RUBY_VERSION.to_f < 3.2 || Rails.version.to_f < 7.1 do
@@ -74,7 +74,7 @@ def foo
7474

7575
span = transaction[:spans][0]
7676
data = span[:data]
77-
expect(data["db.name"]).to include("db")
77+
expect(data["db.name"]).to include(Rails.application.db_name)
7878
expect(data["code.filepath"]).to eq(nil)
7979
expect(data["code.lineno"]).to eq(nil)
8080
expect(data["code.function"]).to eq(nil)
@@ -116,7 +116,7 @@ def foo
116116

117117
span = transaction[:spans][0]
118118
data = span[:data]
119-
expect(data["db.name"]).to include("db.sqlite3")
119+
expect(data["db.name"]).to include(Rails.application.db_name)
120120
expect(data["code.filepath"]).to eq(nil)
121121
expect(data["code.lineno"]).to eq(nil)
122122
expect(data["code.function"]).to eq(nil)
@@ -148,8 +148,8 @@ def foo
148148
expect(cached_query_span[:tags]).to include({ cached: true })
149149

150150
data = cached_query_span[:data]
151-
expect(data["db.name"]).to include("db.sqlite3")
152-
expect(data["db.system"]).to eq("sqlite3")
151+
expect(data["db.name"]).to include(Rails.application.db_name)
152+
expect(data["db.system"]).to eql(Rails.application.db_system)
153153
end
154154
end
155155

sentry-rails/spec/sentry/rails/tracing_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
second_span = transaction[:spans][1]
8787
expect(second_span[:op]).to eq("db.sql.active_record")
8888
expect(second_span[:origin]).to eq("auto.db.rails")
89-
expect(second_span[:description].squeeze("\s")).to eq(
90-
'SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ?'
89+
expect(second_span[:description].squeeze("\s")).to match(
90+
/SELECT "posts"\.\* FROM "posts" WHERE "posts"\."id" = (\?|\$1) LIMIT (\?|\$2)/
9191
)
9292
expect(second_span[:parent_span_id]).to eq(first_span[:span_id])
9393

sentry-rails/spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
ENV["RAILS_ENV"] = "test"
66

7+
# We run tests against sqlite3 by default
8+
# See SentryRailsTest application for more info
9+
ENV["DATABASE"] ||= "sqlite3"
10+
711
begin
812
require "debug/prelude"
913
rescue LoadError

0 commit comments

Comments
 (0)