Skip to content

Commit

Permalink
feat: replace failing functions for MSSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
blocknotes committed Nov 2, 2023
1 parent 2c46dd8 commit 8605467
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/active_storage/service/db_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def download(key, &block)

def download_chunk(key, range)
instrument :download_chunk, key: key, range: range do
record = object_for(key, fields: "SUBSTRING(data FROM #{range.begin + 1} FOR #{range.size}) AS chunk")
from = range.begin + 1
to = range.size
args = adapter_sqlserver? ? "data, #{from}, #{to}" : "data FROM #{from} FOR #{to}"
record = object_for(key, fields: "SUBSTRING(#{args}) AS chunk")
raise(ActiveStorage::FileNotFoundError) unless record

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

private

def adapter_sqlserver?
@adapter_sqlserver ||= ActiveStorageDB::File.connection.adapter_name == 'SQLServer'
end

def generate_url(key, expires_in:, filename:, content_type:, disposition:)
content_disposition = content_disposition_with(type: disposition, filename: filename)
verified_key_with_expiration = ActiveStorage.verifier.generate(
Expand Down Expand Up @@ -144,7 +151,8 @@ def object_for(key, fields: nil)
end

def stream(key)
size = object_for(key, fields: 'OCTET_LENGTH(data) AS size')&.size || raise(ActiveStorage::FileNotFoundError)
data_size = adapter_sqlserver? ? 'DATALENGTH(data)' : 'OCTET_LENGTH(data)'
size = object_for(key, fields: "#{data_size} AS size")&.size || raise(ActiveStorage::FileNotFoundError)
(size / @chunk_size.to_f).ceil.times.each do |i|
range = (i * @chunk_size..((i + 1) * @chunk_size) - 1)
yield download_chunk(key, range)
Expand Down
3 changes: 0 additions & 3 deletions spec/dummy70/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_02_02_010101) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down

0 comments on commit 8605467

Please sign in to comment.