Skip to content

Commit

Permalink
Merge branch 'configuration'
Browse files Browse the repository at this point in the history
  • Loading branch information
fractaledmind committed Sep 6, 2024
2 parents 19f4f58 + 141c07b commit c1c666a
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 140 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## [Unreleased]

## [0.11.2] - 2024-09-06

- Simplify the getters to not use memoization

## [0.11.1] - 2024-09-06

- Ensure the litestream initializer handles `nil`s

## [0.11.0] - 2024-06-21

- Add a default username for the Litestream engine ([@fractaledmind](https://github.com/fractaledmind/litestream-ruby/commit/91c4de8b85be01f8cfd0cc2bf0027a6c0d9f3aaf))
Expand Down
8 changes: 6 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
PATH
remote: .
specs:
litestream (0.11.0)
activejob
litestream (0.11.2)
actionpack (>= 7.0)
actionview (>= 7.0)
activejob (>= 7.0)
activesupport (>= 7.0)
logfmt (>= 0.0.10)
railties (>= 7.0)
sqlite3

GEM
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/litestream/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
around_action :force_english_locale!

http_basic_authenticate_with name: Litestream.username, password: Litestream.password if Litestream.password
if Litestream.password
http_basic_authenticate_with(
name: Litestream.username,
password: Litestream.password
)
end

private

Expand Down
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

require "bundler/setup"
require "rails"
require "litestream"

# You can add fixtures and/or initialization code here to make experimenting
Expand Down
42 changes: 31 additions & 11 deletions lib/litestream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@
require "sqlite3"

module Litestream
VerificationFailure = Class.new(StandardError)

class << self
attr_accessor :configuration
attr_writer :configuration

def configuration
@configuration ||= Configuration.new
end

def deprecator
@deprecator ||= ActiveSupport::Deprecation.new("0.12.0", "Litestream")
end
end

def self.configure
deprecator.warn(
"Configuring Litestream via Litestream.configure is deprecated. Use Rails.application.configure { config.litestream.* = ... } instead.",
caller
)
self.configuration ||= Configuration.new
yield(configuration)
end
Expand All @@ -19,11 +33,7 @@ def initialize
end
end

VerificationFailure = Class.new(StandardError)

mattr_writer :username
mattr_writer :password
mattr_writer :queue
mattr_writer :username, :password, :queue, :replica_bucket, :replica_key_id, :replica_access_key

class << self
def verify!(database_path)
Expand Down Expand Up @@ -52,17 +62,27 @@ def verify!(database_path)
# use method instead of attr_accessor to ensure
# this works if variable set after Litestream is loaded
def username
@username ||= ENV["LITESTREAM_USERNAME"] || @@username || "litestream"
ENV["LITESTREAM_USERNAME"] || @@username || "litestream"
end

# use method instead of attr_accessor to ensure
# this works if variable set after Litestream is loaded
def password
@password ||= ENV["LITESTREAM_PASSWORD"] || @@password
ENV["LITESTREAM_PASSWORD"] || @@password
end

def queue
@queue ||= ENV["LITESTREAM_QUEUE"] || @@queue || "default"
ENV["LITESTREAM_QUEUE"] || @@queue || "default"
end

def replica_bucket
@@replica_bucket || configuration.replica_bucket
end

def replica_key_id
@@replica_key_id || configuration.replica_key_id
end

def replica_access_key
@@replica_access_key || configuration.replica_access_key
end

def replicate_process
Expand Down
8 changes: 3 additions & 5 deletions lib/litestream/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,9 @@ def execute(command, argv = {}, database = nil, async: false, tabled_output: fal
end

def prepare(command, argv = {}, database = nil)
if Litestream.configuration
ENV["LITESTREAM_REPLICA_BUCKET"] ||= Litestream.configuration.replica_bucket
ENV["LITESTREAM_ACCESS_KEY_ID"] ||= Litestream.configuration.replica_key_id
ENV["LITESTREAM_SECRET_ACCESS_KEY"] ||= Litestream.configuration.replica_access_key
end
ENV["LITESTREAM_REPLICA_BUCKET"] ||= Litestream.replica_bucket
ENV["LITESTREAM_ACCESS_KEY_ID"] ||= Litestream.replica_key_id
ENV["LITESTREAM_SECRET_ACCESS_KEY"] ||= Litestream.replica_access_key

args = {
"--config" => Rails.root.join("config", "litestream.yml").to_s
Expand Down
4 changes: 4 additions & 0 deletions lib/litestream/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ class Engine < ::Rails::Engine
Litestream.public_send(:"#{name}=", value)
end
end

initializer "deprecator" do |app|
app.deprecators[:litestream] = Litestream.deprecator
end
end
end
8 changes: 4 additions & 4 deletions lib/litestream/generators/litestream/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This allows you to configure Litestream using Rails encrypted credentials,
# or some other mechanism where the values are only avaialble at runtime.

Litestream.configure do |config|
Rails.application.configure do
# An example of using Rails encrypted credentials to configure Litestream.
# litestream_credentials = Rails.application.credentials.litestream

Expand All @@ -19,15 +19,15 @@
# any SFTP server.
# In this example, we are using Rails encrypted credentials to store the URL to
# our storage provider bucket.
# config.replica_bucket = litestream_credentials.replica_bucket
# config.litestream.replica_bucket = litestream_credentials&.replica_bucket

# Replica-specific authentication key.
# Litestream needs authentication credentials to access your storage provider bucket.
# In this example, we are using Rails encrypted credentials to store the access key ID.
# config.replica_key_id = litestream_credentials.replica_key_id
# config.litestream.replica_key_id = litestream_credentials&.replica_key_id

# Replica-specific secret key.
# Litestream needs authentication credentials to access your storage provider bucket.
# In this example, we are using Rails encrypted credentials to store the secret access key.
# config.replica_access_key = litestream_credentials.replica_access_key
# config.litestream.replica_access_key = litestream_credentials&.replica_access_key
end
2 changes: 1 addition & 1 deletion lib/litestream/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Litestream
VERSION = "0.11.0"
VERSION = "0.11.2"
end
11 changes: 3 additions & 8 deletions lib/tasks/litestream_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
namespace :litestream do
desc "Print the ENV variables needed for the Litestream config file"
task env: :environment do
if Litestream.configuration.nil?
warn "You have not configured the Litestream gem with any values to generate ENV variables"
next
end

puts "LITESTREAM_REPLICA_BUCKET=#{Litestream.configuration.replica_bucket}"
puts "LITESTREAM_ACCESS_KEY_ID=#{Litestream.configuration.replica_key_id}"
puts "LITESTREAM_SECRET_ACCESS_KEY=#{Litestream.configuration.replica_access_key}"
puts "LITESTREAM_REPLICA_BUCKET=#{Litestream.replica_bucket}"
puts "LITESTREAM_ACCESS_KEY_ID=#{Litestream.replica_key_id}"
puts "LITESTREAM_SECRET_ACCESS_KEY=#{Litestream.replica_access_key}"

true
end
Expand Down
8 changes: 7 additions & 1 deletion litestream.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ Gem::Specification.new do |spec|
# Uncomment to register a new dependency of your gem
spec.add_dependency "logfmt", ">= 0.0.10"
spec.add_dependency "sqlite3"
spec.add_dependency "activejob"
">= 7.0".tap do |rails_version|
spec.add_dependency "actionpack", rails_version
spec.add_dependency "actionview", rails_version
spec.add_dependency "activesupport", rails_version
spec.add_dependency "activejob", rails_version
spec.add_dependency "railties", rails_version
end
spec.add_development_dependency "rubyzip"
spec.add_development_dependency "rails"
spec.add_development_dependency "sqlite3"
Expand Down
6 changes: 3 additions & 3 deletions test/generators/test_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def after_teardown
end

assert_file "config/initializers/litestream.rb" do |content|
assert_match "config.replica_bucket = litestream_credentials.replica_bucket", content
assert_match "config.replica_key_id = litestream_credentials.replica_key_id", content
assert_match "config.replica_access_key = litestream_credentials.replica_access_key", content
assert_match "config.litestream.replica_bucket = litestream_credentials&.replica_bucket", content
assert_match "config.litestream.replica_key_id = litestream_credentials&.replica_key_id", content
assert_match "config.litestream.replica_access_key = litestream_credentials&.replica_access_key", content
end
end
end
Loading

0 comments on commit c1c666a

Please sign in to comment.