Skip to content

Commit c229f05

Browse files
committed
chore(rails): add postgresql to test/CI
1 parent a5cee1b commit c229f05

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ jobs:
2020
name: Ruby ${{ matrix.ruby_version }} & Rails ${{ matrix.rails_version }}, options - ${{ toJson(matrix.options) }}
2121
runs-on: ubuntu-latest
2222
timeout-minutes: 10
23+
services:
24+
postgres:
25+
image: postgres:15
26+
env:
27+
POSTGRES_PASSWORD: postgres
28+
POSTGRES_DB: sentry
29+
options: >-
30+
--health-cmd pg_isready
31+
--health-interval 10s
32+
--health-timeout 5s
33+
--health-retries 5
2334
env:
2435
RUBYOPT: ${{ matrix.options.rubyopt }}
2536
BUNDLE_GEMFILE: ${{ github.workspace }}/sentry-rails/Gemfile
@@ -31,6 +42,9 @@ jobs:
3142
matrix:
3243
ruby_version: ${{ fromJson(inputs.versions) }}
3344
rails_version: [6.1.0, 7.0.0, 7.1.0]
45+
db:
46+
- "sqlite3://./db/db.sqlite3"
47+
- "postgresql://postgres:postgres@localhost:5432/sentry"
3448
include:
3549
- ruby_version: "2.7"
3650
rails_version: 5.2.0
@@ -84,6 +98,8 @@ jobs:
8498

8599
- name: Build with Rails ${{ matrix.rails_version }}
86100
run: bundle exec rake
101+
env:
102+
DATABASE_URL: ${{ matrix.db }}
87103

88104
- name: Upload Coverage
89105
uses: codecov/codecov-action@v5

sentry-rails/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ end
2929
rails_version = Gem::Version.new(rails_version)
3030

3131
gem "rails", "~> #{rails_version}"
32+
gem "pg", platform: :ruby
3233

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

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def self.db_path
4949
@db_path ||= root_path.join("db", "db.sqlite3")
5050
end
5151

52+
def self.db_uri
53+
@db_url ||= URI(ENV["DATABASE_URL"] || "sqlite3://#{db_path}")
54+
end
55+
56+
def self.db_name
57+
db_uri.path[1..-1]
58+
end
59+
5260
def self.application_file
5361
@application_file ||= begin
5462
current = Dir[root_path.join("config/applications/rails-*.rb")]
@@ -61,14 +69,24 @@ def self.application_file
6169

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

7075
# We need to connect manually here
71-
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: db_path)
76+
case db_uri.scheme
77+
when "postgresql"
78+
ActiveRecord::Base.establish_connection(
79+
adapter: db_uri.scheme,
80+
host: db_uri.host,
81+
username: db_uri.user,
82+
password: db_uri.password,
83+
database: db_uri.path[1..-1]
84+
)
85+
else
86+
ActiveRecord::Base.establish_connection(
87+
adapter: db_uri.scheme, database: db_uri.path[1..-1]
88+
)
89+
end
7290

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

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
@@ -242,8 +242,8 @@
242242
expect(log_event).not_to be_nil
243243

244244
attributes = log_event[:attributes]
245-
expect(attributes[:db_system][:value]).to eq("sqlite3")
246-
expect(attributes[:db_name][:value]).to eq("db.sqlite3")
245+
expect(attributes[:db_system][:value]).to eq(Rails.application.db_uri.scheme)
246+
expect(attributes[:db_name][:value]).to eq(Rails.application.db_name)
247247
end
248248
end
249249
end

0 commit comments

Comments
 (0)