From 1d08b9a18b9215901099ba973d2d341a4f5d6c54 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sun, 8 Sep 2024 10:04:03 +0200 Subject: [PATCH] feat: SQLite support --- lib/active_storage/service/db_service.rb | 6 +++++- lib/active_storage/service/db_service_rails70.rb | 2 +- spec/dummy61/config/database.yml | 6 ++++++ spec/dummy70/config/database.yml | 6 ++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/active_storage/service/db_service.rb b/lib/active_storage/service/db_service.rb index 3bd7458..57e672d 100644 --- a/lib/active_storage/service/db_service.rb +++ b/lib/active_storage/service/db_service.rb @@ -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 @@ -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 diff --git a/lib/active_storage/service/db_service_rails70.rb b/lib/active_storage/service/db_service_rails70.rb index c7926fa..be76b92 100644 --- a/lib/active_storage/service/db_service_rails70.rb +++ b/lib/active_storage/service/db_service_rails70.rb @@ -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 diff --git a/spec/dummy61/config/database.yml b/spec/dummy61/config/database.yml index eeef139..3b300de 100644 --- a/spec/dummy61/config/database.yml +++ b/spec/dummy61/config/database.yml @@ -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 %> diff --git a/spec/dummy70/config/database.yml b/spec/dummy70/config/database.yml index eeef139..3b300de 100644 --- a/spec/dummy70/config/database.yml +++ b/spec/dummy70/config/database.yml @@ -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 %>