Skip to content

Commit

Permalink
Use SQLite as default database
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Nov 16, 2022
1 parent f9027d6 commit 4d972e6
Show file tree
Hide file tree
Showing 25 changed files with 100 additions and 198 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ on: [push, pull_request]
jobs:
test_lint:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11
ports: ['5432:5432']
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
Expand All @@ -29,9 +18,6 @@ jobs:
- run: yarn install
- name: Test and Lint
env:
DB_HOST: localhost
DB_USER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
run: |
sudo apt-get update
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-*

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
Expand Down
11 changes: 7 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ gem "cssbundling-rails", "~> 1.1.0"
# Bundle and transpile JavaScript in Rails
gem "jsbundling-rails", "~> 1.0.0"

# Use postgresql as the database for Active Record
gem "pg", "~> 1.3.2"
if ENV.fetch("DATABASE_ADAPTER", "sqlite") == "postgresql"
gem "pg", "~> 1.3.2"
else
gem "sqlite3", "~> 1.5.0"
end

# Use Puma as the app server
gem "puma", "~> 6.0.0"
Expand Down Expand Up @@ -49,8 +52,8 @@ gem "httparty", "~> 0.17.0"
# For browser detection
gem "browser", "~> 2.6.1", require: "browser/browser"

# For PostgreSQL's full text search
gem "pg_search", "~> 2.3.2"
# For searching
gem "ransack", "~> 3.2.1"

# For sortable list
gem "acts_as_list", "~> 1.0.2"
Expand Down
14 changes: 8 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ GEM
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
pg (1.3.5)
pg_search (2.3.6)
activerecord (>= 5.2)
activesupport (>= 5.2)
propshaft (0.6.4)
actionpack (>= 7.0.0)
activesupport (>= 7.0.0)
Expand Down Expand Up @@ -228,6 +224,10 @@ GEM
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
ransack (3.2.1)
activerecord (>= 6.1.5)
activesupport (>= 6.1.5)
i18n
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
Expand Down Expand Up @@ -270,6 +270,8 @@ GEM
simplecov-html (0.12.3)
simplecov-lcov (0.8.0)
simplecov_json_formatter (0.1.4)
sqlite3 (1.5.3)
mini_portile2 (~> 2.8.0)
ssrf_filter (1.1.1)
standard (1.17.0)
rubocop (= 1.38.0)
Expand Down Expand Up @@ -326,15 +328,15 @@ DEPENDENCIES
listen (~> 3.7.1)
memory_profiler (~> 0.9.13)
pagy (~> 5.6.6)
pg (~> 1.3.2)
pg_search (~> 2.3.2)
propshaft (~> 0.6.4)
puma (~> 6.0.0)
rails (~> 7.0.0)
ransack (~> 3.2.1)
redis (~> 4.0)
sidekiq (~> 7.0.0)
simplecov (~> 0.21.2)
simplecov-lcov (~> 0.8.0)
sqlite3 (~> 1.5.0)
standard (~> 1.7)
stimulus-rails (~> 1.0.2)
turbo-rails (~> 1.3.0)
Expand Down
2 changes: 1 addition & 1 deletion app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Album < ApplicationRecord
has_many :songs, -> { order(:tracknum) }, inverse_of: :album, dependent: :destroy
belongs_to :artist, touch: true

search_by :name, associations: :artist
search_by :name, associations: {artist: :name}

def title
is_unknown? ? I18n.t("label.unknown_album") : name
Expand Down
17 changes: 2 additions & 15 deletions app/models/concerns/global_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,10 @@ def instance
first_or_create!(singleton_guard: 0)
end

def has_settings(*settings)
self::AVAILABLE_SETTINGS.push(*settings)

store_accessor :values, *settings

settings.each do |setting|
define_singleton_method(setting) do
setting_value = instance.send(setting)
setting_value || ENV[setting.to_s.upcase]
end
end
end

def has_setting(setting, type: :string, default: nil)
self::AVAILABLE_SETTINGS.push(setting)

store_accessor :values, setting
store :values, accessors: setting

define_method("#{setting}=") do |value|
setting_value = ScopedSetting.convert_setting_value(type, value)
Expand All @@ -45,7 +32,7 @@ def has_setting(setting, type: :string, default: nil)

define_singleton_method(setting) do
setting_value = instance.send(setting)
setting_value || default
setting_value || default || ENV[setting.to_s.upcase]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/scoped_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module ScopedSetting
def has_setting(setting, type: :string, default: nil)
self::AVAILABLE_SETTINGS.push(setting)

store_accessor :settings, setting
store :settings, accessors: setting

define_method(setting) do
setting_value = ScopedSetting.convert_setting_value(type, super())
Expand All @@ -30,7 +30,7 @@ def self.convert_setting_value(type, value)
when :integer
value.to_i
else
value
value.to_s
end
end
end
23 changes: 5 additions & 18 deletions app/models/concerns/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,17 @@
module Searchable
extend ActiveSupport::Concern

included do
include PgSearch::Model
end

class_methods do
def search_by(attr, options = {})
associations = Array(options[:associations])
associated_against = associations.index_with { attr }

search_options = {}.tap do |option|
option[:against] = attr
option[:using] = {
tsearch: {prefix: true},
trigram: {}
}
option[:associated_against] = associated_against if associations.present?
end

pg_search_scope "search_by_#{attr}", search_options
associations = Hash(options[:associations])
associations_attrs = associations.map { |name, attr| "#{name}_#{attr}" }
predicate = [attr].push(*associations_attrs).join("_or_").concat("_i_cont")

define_singleton_method :search do |query|
return self if query.blank?

send("search_by_#{attr}", query)
search_result = ransack({predicate.to_sym => query}).result
associations.present? ? search_result.includes(*associations.keys) : search_result
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class Setting < ApplicationRecord

AVAILABLE_BITRATE_OPTIONS = [128, 192, 320].freeze

has_settings :media_path, :discogs_token
has_setting :media_path
has_setting :discogs_token
has_setting :transcode_bitrate, type: :integer, default: 128
has_setting :allow_transcode_lossless, type: :boolean, default: false

Expand Down
2 changes: 1 addition & 1 deletion app/models/song.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Song < ApplicationRecord

before_destroy :remove_transcode_cache

search_by :name, associations: [:artist, :album]
search_by :name, associations: {artist: :name, album: :name}

def format
MediaFile.format(file_path)
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class User < ApplicationRecord

has_secure_token :api_token
has_setting :theme, default: DEFAULT_THEME
serialize :recently_played_album_ids, Array

before_create :downcase_email
after_create :create_buildin_playlists
Expand Down
89 changes: 14 additions & 75 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,86 +1,25 @@
# PostgreSQL. Versions 9.1 and up are supported.
#
# Install the pg driver:
# gem install pg
# On OS X with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV['DB_HOST'] %>
username: <%= ENV['DB_USER'] %>
timeout: 5000

development:
<<: *default
database: black_candy_development

# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: black_candy

# The password associated with the postgres role (username).
#password:

# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432

# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
database: db/development.sqlite3

# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: black_candy_test
database: db/test.sqlite3


# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
<% if ENV.fetch("DATABASE_ADAPTER", "sqlite") == "postgresql" %>
production:
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= ENV['DATABASE_URL'] %>
<% else %>
production:
<<: *default
database: black_candy_production
password: <%= ENV['DB_PASSWORD'] %>
database: db/production.sqlite3
<% end %>
2 changes: 1 addition & 1 deletion db/migrate/20200425023906_enable_trigram_extension.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class EnableTrigramExtension < ActiveRecord::Migration[6.0]
def change
enable_extension "pg_trgm" unless extension_enabled?("pg_trgm")
enable_extension "pg_trgm" if adapter_name.downcase == "postgresql" && !extension_enabled?("pg_trgm")
end
end
10 changes: 6 additions & 4 deletions db/migrate/20200915090637_use_hstore_for_settings.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
class UseHstoreForSettings < ActiveRecord::Migration[6.0]
def up
enable_extension "hstore" unless extension_enabled?("hstore")
enable_extension "hstore" if adapter_name.downcase == "postgresql" && !extension_enabled?("hstore")
add_column :settings, :values, :hstore
add_column :settings, :singleton_guard, :integer
add_column :users, :settings, :hstore
add_index :settings, :singleton_guard, unique: true
remove_columns :settings, :created_at, :updated_at

execute <<-SQL
if adapter_name.downcase == "postgresql" && extension_enabled?("hstore")
execute <<-SQL
/* Migrate user theme settings */
UPDATE users u
SET
Expand Down Expand Up @@ -48,7 +49,8 @@ def up
))
WHERE
singleton_guard = 0;
SQL
SQL
end

remove_columns :settings, :var, :value, :thing_id, :thing_type

Expand All @@ -73,6 +75,6 @@ def down
remove_column :settings, :values
remove_column :settings, :singleton_guard
remove_column :users, :settings
disable_extension "hstore" if extension_enabled?("hstore")
disable_extension "hstore" if adapter_name.downcase == "postgresql" && extension_enabled?("hstore")
end
end
7 changes: 7 additions & 0 deletions db/migrate/20221115023957_remove_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RemoveExtensions < ActiveRecord::Migration[7.0]
def change
disable_extension "pg_trgm"
disable_extension "hstore"
disable_extension "plpgsql"
end
end
11 changes: 11 additions & 0 deletions db/migrate/20221115061632_change_hstore_value_to_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ChangeHstoreValueToText < ActiveRecord::Migration[7.0]
def up
change_column :settings, :values, :text
change_column :users, :settings, :text
end

def down
change_column :settings, :values, :hstore
change_column :users, :settings, :hstore
end
end
Loading

0 comments on commit 4d972e6

Please sign in to comment.