Skip to content

Commit ef942a6

Browse files
committed
fix: OCTET_LENGTH on old SQLite
Add SQLite-specific branch using `LENGTH(data)` instead of `OCTET_LENGTH(data)`. `OCTET_LENGTH` was only added in SQLite 3.43.0 (Aug 2023); older versions would crash at runtime during streaming downloads.
1 parent cf32b9a commit ef942a6

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

lib/active_storage/service/db_service.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,23 @@ def object_for(key, fields: nil)
184184
end
185185

186186
def stream(key)
187-
data_size = adapter_sqlserver? ? "DATALENGTH(data)" : "OCTET_LENGTH(data)"
188-
size = object_for(key, fields: "#{data_size} AS size")&.size || raise(ActiveStorage::FileNotFoundError)
187+
size = object_for(key, fields: data_size)&.size || raise(ActiveStorage::FileNotFoundError)
189188
(size / @chunk_size.to_f).ceil.times.each do |i|
190189
range = (i * @chunk_size)..(((i + 1) * @chunk_size) - 1)
191190
yield download_chunk(key, range)
192191
end
193192
end
194193

194+
def data_size
195+
if adapter_sqlserver?
196+
"DATALENGTH(data) AS size"
197+
elsif adapter_sqlite?
198+
"LENGTH(data) AS size"
199+
else
200+
"OCTET_LENGTH(data) AS size"
201+
end
202+
end
203+
195204
def url_helpers
196205
@url_helpers ||= ::ActiveStorageDB::Engine.routes.url_helpers
197206
end

0 commit comments

Comments
 (0)