Skip to content

Commit

Permalink
Merge pull request #296 from blackcandy-org/pg-ci
Browse files Browse the repository at this point in the history
Fix #295, add CI against PostgreSQL database
  • Loading branch information
aidewoode committed Aug 16, 2023
2 parents 42b7780 + 1f88bb1 commit 753eb1c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 39 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,33 @@ name: CI

on: [push, pull_request]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test_lint:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
database_adapter:
- postgresql
- sqlite
env:
DB_ADAPTER: ${{ matrix.database_adapter }}
DB_URL: postgresql://postgres:postgres@localhost/black_candy_test
services:
postgres:
image: postgres:latest
ports: ['5432:5432']
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 3
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ docker run -v /media_data:/media_data -e MEDIA_PATH=/media_data ghcr.io/blackcan
Black Candy use SQLite as database by default. Because SQLite can simplify the process of installation, and it's an ideal choice for self hosted small server. If you think SQLite is not enough or you are using some cloud service like heroku to host Black Candy, you can also use PostgreSQL as database.

```shell
docker run -e DATABASE_ADAPTER=postgresql -e DATABASE_URL=postgresql://yourdatabaseurl ghcr.io/blackcandy-org/blackcandy:edge
docker run -e DB_ADAPTER=postgresql -e DB_URL=postgresql://yourdatabaseurl ghcr.io/blackcandy-org/blackcandy:edge
```

### How to Persist Data
Expand Down Expand Up @@ -172,9 +172,9 @@ services:
| REDIS_CACHE_URL | REDIS_URL | This environment variable can override the REDIS_URL, if you want to set different Redis URL for cache.|
| REDIS_SIDEKIQ_URL | REDIS_URL | This environment variable can override the REDIS_URL, if you want to set different Redis URL for Sidekiq. |
| REDIS_CABLE_URL | REDIS_URL | This environment variable can override the REDIS_URL, if you want to set different Redis URL for WebSockets. |
| DATABASE_URL | | The URL of PostgreSQL database. You must set this environment variable if you use PostgreSQL as database. |
| DB_URL | | The URL of PostgreSQL database. You must set this environment variable if you use PostgreSQL as database. |
| MEDIA_PATH | | You can use this environment variable to set media path for Black Candy, otherwise you can set media path in settings page. |
| DATABASE_ADAPTER | "sqlite" | There are two adapters are supported, "sqlite" and "postgresql".|
| DB_ADAPTER | "sqlite" | There are two adapters are supported, "sqlite" and "postgresql".|
| NGINX_SENDFILE | false | Whether enable Nginx sendfile. |
| EMBEDDED_SIDEKIQ | false | Whether enable embedded mode of Sidekiq. |
| EMBEDDED_SIDEKIQ_CONCURRENCY | 2 | The concurrency number of embedded Sidekiq. This value should not greater than 2. Because we should keep embedded Sidekiq concurrency very low. For more details, see this [document](https://github.com/mperham/sidekiq/wiki/Embedding) about embedded Sidekiq. |
Expand Down
27 changes: 16 additions & 11 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
default: &default
sqlite_default: &sqlite_default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

pg_default: &pg_default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= BlackCandy::Config.db_url %>

development:
<<: *default
<<: *sqlite_default
database: db/development.sqlite3

<% if BlackCandy::Config.db_adapter == "postgresql" %>
test:
<<: *default
database: db/test.sqlite3

<<: *pg_default

<% if BlackCandy::Config.database_adapter == "postgresql" %>
production:
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= BlackCandy::Config.database_url %>
<<: *pg_default
<% else %>
test:
<<: *sqlite_default
database: db/test.sqlite3

production:
<<: *default
<<: *sqlite_default
database: db/production.sqlite3
<% end %>
10 changes: 5 additions & 5 deletions lib/black_candy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def raise_validation_error(message)
has_config :redis_cache_url, default: proc { redis_url }
has_config :redis_sidekiq_url, default: proc { redis_url }
has_config :redis_cable_url, default: proc { redis_url }
has_config :database_url
has_config :db_url
has_config :media_path
has_config :database_adapter, default: "sqlite"
has_config :db_adapter, default: "sqlite"
has_config :nginx_sendfile, default: false
has_config :embedded_sidekiq, default: false
has_config :embedded_sidekiq_concurrency, default: 2
Expand All @@ -68,13 +68,13 @@ def raise_validation_error(message)
end
end

validate :database_adapter do |value|
validate :db_adapter do |value|
unless SUPPORTED_DATABASE_ADAPTERS.include?(value)
raise_validation_error "Unsupported database adapter."
end

if value == "postgresql" && database_url.blank?
raise_validation_error "DATABASE_URL is required if database adapter is postgresql"
if value == "postgresql" && db_url.blank?
raise_validation_error "DB_URL is required if database adapter is postgresql"
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/artists.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ artist2:
created_at: 2023-01-01

various_artists:
name: 'various_artists'
is_various: true
created_at: 2023-01-03
19 changes: 9 additions & 10 deletions test/lib/black_candy/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class BlackCandy::ConfigTest < ActiveSupport::TestCase
end

test "should get default value when can not find value from ENV" do
assert_nil ENV["DATABASE_ADAPTER"]
assert_equal "sqlite", BlackCandy::Config.database_adapter
with_env("DB_ADAPTER" => nil) do
assert_equal "sqlite", BlackCandy::Config.db_adapter
end
end

test "should get nginx_sendfile value as a boolean" do
Expand Down Expand Up @@ -85,24 +86,22 @@ class BlackCandy::ConfigTest < ActiveSupport::TestCase
end

test "should raise error when database_adapter is not supported" do
with_env("DATABASE_ADAPTER" => "invalid_adapter") do
with_env("DB_ADAPTER" => "invalid_adapter") do
assert_raises(BlackCandy::Config::ValidationError) do
BlackCandy::Config.database_adapter
BlackCandy::Config.db_adapter
end
end
end

test "should raise error when database_adapter is postgresql but database_url is not set" do
assert_nil ENV["DATABASE_URL"]

with_env("DATABASE_ADAPTER" => "postgresql") do
with_env("DB_ADAPTER" => "postgresql", "DB_URL" => nil) do
assert_raises(BlackCandy::Config::ValidationError) do
BlackCandy::Config.database_adapter
BlackCandy::Config.db_adapter
end
end

with_env("DATABASE_ADAPTER" => "postgresql", "DATABASE_URL" => "database_url") do
assert_equal "postgresql", BlackCandy::Config.database_adapter
with_env("DB_ADAPTER" => "postgresql", "DB_URL" => "database_url") do
assert_equal "postgresql", BlackCandy::Config.db_adapter
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/models/album_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class AlbumTest < ActiveSupport::TestCase
end

test "should sort by artist name" do
assert_equal %w[album4 album1 album2 album3], Album.sort_records(:artist_name).pluck(:name)
assert_equal %w[album3 album1 album2 album4], Album.sort_records(:artist_name, :desc).pluck(:name)
assert_equal %w[album1 album2 album3 album4], Album.sort_records(:artist_name).pluck(:name)
assert_equal %w[album4 album3 album1 album2], Album.sort_records(:artist_name, :desc).pluck(:name)
end

test "should sort by name by default" do
Expand Down
12 changes: 6 additions & 6 deletions test/models/artist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ class ArtistTest < ActiveSupport::TestCase
end

test "should sort by name" do
assert_equal %w[artist1 artist2], Artist.sort_records(:name).pluck(:name).compact
assert_equal %w[artist2 artist1], Artist.sort_records(:name, :desc).pluck(:name).compact
assert_equal %w[artist1 artist2 various_artists], Artist.sort_records(:name).pluck(:name).compact
assert_equal %w[various_artists artist2 artist1], Artist.sort_records(:name, :desc).pluck(:name).compact
end

test "should sort by created_at" do
assert_equal %w[artist2 artist1], Artist.sort_records(:created_at).pluck(:name).compact
assert_equal %w[artist1 artist2], Artist.sort_records(:created_at, :desc).pluck(:name).compact
assert_equal %w[artist2 artist1 various_artists], Artist.sort_records(:created_at).pluck(:name).compact
assert_equal %w[various_artists artist1 artist2], Artist.sort_records(:created_at, :desc).pluck(:name).compact
end

test "should sort by name by default" do
assert_equal %w[artist1 artist2], Artist.sort_records.pluck(:name).compact
assert_equal %w[artist1 artist2 various_artists], Artist.sort_records.pluck(:name).compact
end

test "should get sort options" do
Expand All @@ -40,6 +40,6 @@ class ArtistTest < ActiveSupport::TestCase
end

test "should use default sort when use invalid sort value" do
assert_equal %w[artist1 artist2], Artist.sort_records(:invalid).pluck(:name).compact
assert_equal %w[artist1 artist2 various_artists], Artist.sort_records(:invalid).pluck(:name).compact
end
end
6 changes: 4 additions & 2 deletions test/models/concerns/searchable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SearchableTest < ActiveSupport::TestCase
end

test "should support fuzzy search by model attribute" do
assert_equal Artist.where(name: %w[artist1 artist2]).ids.sort, Artist.search("artist").ids.sort
assert_equal Artist.where(name: %w[artist1 artist2 various_artists]).ids.sort, Artist.search("artist").ids.sort
end

test "should support search by model attribute with association" do
Expand All @@ -23,7 +23,9 @@ class SearchableTest < ActiveSupport::TestCase

test "should support fuzzy search by model attribute with association" do
album_ids = Artist.find_by(name: "artist1").albums.ids +
Artist.find_by(name: "artist2").albums.ids
Artist.find_by(name: "artist2").albums.ids +
Artist.find_by(name: "various_artists").albums.ids

assert_equal album_ids.sort, Album.search("artist").ids.sort
end

Expand Down

0 comments on commit 753eb1c

Please sign in to comment.