Skip to content

Commit

Permalink
Separate the cache database
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Oct 17, 2024
1 parent 4482f57 commit c5da899
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 33 deletions.
7 changes: 5 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module BlackCandy
include BlackCandy::Configurable

has_config :db_url
has_config :cache_db_url
has_config :media_path
has_config :db_adapter, default: "sqlite"
has_config :nginx_sendfile, default: false
Expand All @@ -39,8 +40,10 @@ module BlackCandy
raise_config_validation_error "Unsupported database adapter."
end

if value == "postgresql" && ENV["RAILS_ENV"] == "production" && config.db_url.blank?
raise_config_validation_error "DB_URL is required if database adapter is postgresql"
if value == "postgresql" &&
ENV["RAILS_ENV"] == "production" &&
(config.db_url.blank? || config.cache_db_url.blank?)
raise_config_validation_error "DB_URL and CACHE_DB_URL are required if database adapter is postgresql"
end
end

Expand Down
16 changes: 16 additions & 0 deletions config/cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
default: &default
database: cache
store_options:
# Cap age of oldest cache entry to fulfill retention policies
# max_age: <%= 60.days.to_i %>
max_size: <%= 256.megabytes %>
namespace: <%= Rails.env %>

development:
<<: *default

test:
<<: *default

production:
<<: *default
66 changes: 48 additions & 18 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,62 @@ pg_default: &pg_default

<% if BlackCandy.config.db_adapter == "postgresql" %>
development:
<<: *pg_default
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
database: blackcandy_development
host: localhost
primary: &primary_development
<<: *pg_default
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
database: blackcandy_development
host: localhost
cache:
<<: *primary_development
database: blackcandy_development_cache
migrations_paths: db/cache_migrate

test:
<<: *pg_default
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
database: blackcandy_test
host: localhost
primary: &primary_test
<<: *pg_default
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
database: blackcandy_test
host: localhost
cache:
<<: *primary_test
database: blackcandy_test_cache
migrations_paths: db/cache_migrate

production:
<<: *pg_default
url: <%= BlackCandy.config.db_url %>
primary: &primary_production
<<: *pg_default
url: <%= BlackCandy.config.db_url %>
cache:
<<: *primary_production
url: <%= BlackCandy.config.cache_db_url %>
migrations_paths: db/cache_migrate
<% else %>
development:
<<: *sqlite_default
database: storage/development.sqlite3
primary:
<<: *sqlite_default
database: storage/development.sqlite3
cache:
<<: *sqlite_default
database: storage/development_cache.sqlite3
migrations_paths: db/cache_migrate

test:
<<: *sqlite_default
database: storage/test.sqlite3
primary:
<<: *sqlite_default
database: storage/test.sqlite3
cache:
<<: *sqlite_default
database: storage/test_cache.sqlite3
migrations_paths: db/cache_migrate

production:
<<: *sqlite_default
database: storage/production.sqlite3
primary:
<<: *sqlite_default
database: storage/production.sqlite3
cache:
<<: *sqlite_default
database: storage/production_cache.sqlite3
migrations_paths: db/cache_migrate
<% end %>
14 changes: 14 additions & 0 deletions db/cache_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

ActiveRecord::Schema[7.2].define(version: 1) do
create_table "solid_cache_entries", force: :cascade do |t|
t.binary "key", limit: 1024, null: false
t.binary "value", limit: 536870912, null: false
t.datetime "created_at", null: false
t.integer "key_hash", limit: 8, null: false
t.integer "byte_size", limit: 4, null: false
t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size"
t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size"
t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true
end
end
14 changes: 14 additions & 0 deletions db/migrate/20241016135211_drop_solid_cache_entries_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class DropSolidCacheEntriesTable < ActiveRecord::Migration[7.2]
def change
drop_table :solid_cache_entries do |t|
t.binary "key", limit: 1024, null: false
t.binary "value", limit: 536870912, null: false
t.datetime "created_at", null: false
t.integer "key_hash", limit: 8, null: false
t.integer "byte_size", limit: 4, null: false
t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size"
t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size"
t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true
end
end
end
13 changes: 1 addition & 12 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_02_06_051609) do
ActiveRecord::Schema[7.2].define(version: 2024_10_16_135211) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down Expand Up @@ -91,17 +91,6 @@
t.index ["singleton_guard"], name: "index_settings_on_singleton_guard", unique: true
end

create_table "solid_cache_entries", force: :cascade do |t|
t.binary "key", limit: 1024, null: false
t.binary "value", limit: 536870912, null: false
t.datetime "created_at", null: false
t.integer "key_hash", limit: 8, null: false
t.integer "byte_size", limit: 4, null: false
t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size"
t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size"
t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true
end

create_table "solid_queue_blocked_executions", force: :cascade do |t|
t.integer "job_id", null: false
t.string "queue_name", null: false
Expand Down
2 changes: 1 addition & 1 deletion test/lib/black_candy/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class BlackCandy::ConfigTest < ActiveSupport::TestCase
end
end

with_env("DB_ADAPTER" => "postgresql", "DB_URL" => "database_url", "RAILS_ENV" => "production") do
with_env("DB_ADAPTER" => "postgresql", "DB_URL" => "database_url", "CACHE_DB_URL" => "cache_db_url", "RAILS_ENV" => "production") do
assert_equal "postgresql", BlackCandy.config.db_adapter
end
end
Expand Down

0 comments on commit c5da899

Please sign in to comment.