Skip to content

Commit 78c3efb

Browse files
committed
litesream fix, add test case
1 parent 5c6c3e1 commit 78c3efb

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

lib/generators/templates/litestream.rake.erb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
LITESTREAM_CONFIG = ENV["LITESTREAM_CONFIG"] || Rails.root.join("tmp/litestream.yml").to_s
23

34
LITESTREAM_TEMPLATE = <<-EOF
@@ -36,7 +37,7 @@ namespace :litestream do
3637
end
3738

3839
@dbs.each do |db|
39-
next if File.exist?(db) or !ENV["BUCKET_NAME"]
40+
next if File.exist?(db) || !ENV["BUCKET_NAME"]
4041
system "litestream restore -config #{LITESTREAM_CONFIG} -if-replica-exists #{db}"
4142
exit $?.exitstatus unless $?.exitstatus == 0
4243
end
@@ -45,8 +46,12 @@ namespace :litestream do
4546
task :run do
4647
require "shellwords"
4748

48-
exec(*%w[bundle exec litestream replicate -config],
49-
LITESTREAM_CONFIG, "-exec", Shellwords.join(ARGV[1..-1]))
49+
if ENV["BUCKET_NAME"]
50+
exec(*%w[bundle exec litestream replicate -config],
51+
LITESTREAM_CONFIG, "-exec", Shellwords.join(ARGV[1..]))
52+
else
53+
exec(*ARGV[1..])
54+
end
5055
end
5156
end
5257

test/base.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ def check_puma
130130
assert_equal expected, results
131131
end
132132

133+
def check_raketask
134+
results = IO.read("lib/tasks/litestream.rake")
135+
136+
IO.write("#{@results}/litestream.rake", results) if @capture
137+
138+
expected = IO.read("#{@results}/litestream.rake")
139+
140+
assert_equal expected, results
141+
end
142+
133143
def teardown
134144
return if ENV["TEST_KEEP"]
135145
Dir.chdir ".."
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
LITESTREAM_CONFIG = ENV["LITESTREAM_CONFIG"] || Rails.root.join("tmp/litestream.yml").to_s
3+
4+
LITESTREAM_TEMPLATE = <<-EOF
5+
# This is the configuration file for litestream.
6+
#
7+
# For more details, see: https://litestream.io/reference/config/
8+
#
9+
dbs:
10+
<% for db in @dbs -%>
11+
- path: <%= db %>
12+
replicas:
13+
- type: s3
14+
endpoint: $AWS_ENDPOINT_URL_S3
15+
bucket: $BUCKET_NAME
16+
path: storage/<%= File.basename(db) %>
17+
access-key-id: $AWS_ACCESS_KEY_ID
18+
secret-access-key: $AWS_SECRET_ACCESS_KEY
19+
<% end -%>
20+
EOF
21+
22+
namespace :litestream do
23+
task prepare: "db:load_config" do
24+
require "erubi"
25+
26+
@dbs =
27+
ActiveRecord::Base
28+
.configurations
29+
.configs_for(env_name: "production", include_hidden: true)
30+
.select { |config| [ "sqlite3", "litedb" ].include? config.adapter }
31+
.map(&:database)
32+
33+
result = eval(Erubi::Engine.new(LITESTREAM_TEMPLATE).src)
34+
35+
unless File.exist?(LITESTREAM_CONFIG) && File.read(LITESTREAM_CONFIG) == result
36+
File.write(LITESTREAM_CONFIG, result)
37+
end
38+
39+
@dbs.each do |db|
40+
next if File.exist?(db) || !ENV["BUCKET_NAME"]
41+
system "litestream restore -config #{LITESTREAM_CONFIG} -if-replica-exists #{db}"
42+
exit $?.exitstatus unless $?.exitstatus == 0
43+
end
44+
end
45+
46+
task :run do
47+
require "shellwords"
48+
49+
if ENV["BUCKET_NAME"]
50+
exec(*%w[bundle exec litestream replicate -config],
51+
LITESTREAM_CONFIG, "-exec", Shellwords.join(ARGV[1..]))
52+
else
53+
exec(*ARGV[1..])
54+
end
55+
end
56+
end
57+
58+
namespace :db do
59+
task prepare: "litestream:prepare"
60+
end

test/test_litestream.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ def test_litestream
1414
check_dockerfile
1515
check_entrypoint
1616
check_toml
17+
check_raketask
1718
end
1819
end

0 commit comments

Comments
 (0)