Skip to content

Commit

Permalink
feat: SQLite support
Browse files Browse the repository at this point in the history
  • Loading branch information
blocknotes committed Sep 8, 2024
1 parent 885f6e7 commit 1d08b9a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/active_storage/service/db_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def download_chunk(key, range)
instrument :download_chunk, key: key, range: range do
from = range.begin + 1
size = range.size
args = adapter_sqlserver? ? "data, #{from}, #{size}" : "data FROM #{from} FOR #{size}"
args = adapter_sqlserver? || adapter_sqlite? ? "data, #{from}, #{size}" : "data FROM #{from} FOR #{size}"
record = object_for(key, fields: "SUBSTRING(#{args}) AS chunk")
raise(ActiveStorage::FileNotFoundError) unless record

Expand Down Expand Up @@ -102,6 +102,10 @@ def headers_for_direct_upload(_key, content_type:, **)

private

def adapter_sqlite?
@adapter_sqlite ||= ActiveStorageDB::File.connection.adapter_name == 'SQLite'
end

def adapter_sqlserver?
@adapter_sqlserver ||= ActiveStorageDB::File.connection.adapter_name == 'SQLServer'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_storage/service/db_service_rails70.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def compose(source_keys, destination_key, **)
if buffer
buffer << data
else
buffer = data
buffer = +data
end
end
::ActiveStorageDB::File.create!(ref: destination_key, data: buffer) if buffer
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy61/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ test: &test
encoding: utf8
min_messages: warning
pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>
<% when 'sqlite' %>
test: &test
adapter: sqlite3
database: db/test.sqlite3
encoding: utf8
pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>
<% else %>
<% raise "Invalid or empty env DB_TEST #{ENV['DB_TEST']}" %>
<% end %>
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy70/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ test: &test
encoding: utf8
min_messages: warning
pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>
<% when 'sqlite' %>
test: &test
adapter: sqlite3
database: db/test.sqlite3
encoding: utf8
pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>
<% else %>
<% raise "Invalid or empty env DB_TEST #{ENV['DB_TEST']}" %>
<% end %>
Expand Down

0 comments on commit 1d08b9a

Please sign in to comment.