diff --git a/config/application.rb b/config/application.rb index 72778f2c..03ae0c5e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 @@ -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 diff --git a/config/cache.yml b/config/cache.yml new file mode 100644 index 00000000..9f00f061 --- /dev/null +++ b/config/cache.yml @@ -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 \ No newline at end of file diff --git a/config/database.yml b/config/database.yml index c83707eb..0d6da86b 100644 --- a/config/database.yml +++ b/config/database.yml @@ -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 %> diff --git a/db/cache_schema.rb b/db/cache_schema.rb new file mode 100644 index 00000000..6005a297 --- /dev/null +++ b/db/cache_schema.rb @@ -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 diff --git a/db/migrate/20241016135211_drop_solid_cache_entries_table.rb b/db/migrate/20241016135211_drop_solid_cache_entries_table.rb new file mode 100644 index 00000000..190d4934 --- /dev/null +++ b/db/migrate/20241016135211_drop_solid_cache_entries_table.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 59e8fe24..7dde3aa9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 @@ -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 diff --git a/test/lib/black_candy/config_test.rb b/test/lib/black_candy/config_test.rb index 3ab53f46..3641a3ec 100644 --- a/test/lib/black_candy/config_test.rb +++ b/test/lib/black_candy/config_test.rb @@ -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