active_storage_db 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a465e1b93b8ab6436f800b2a0ff6e38f049fae621587924d9b2299f3fa06946
4
- data.tar.gz: 78583f7c6aa00b0a8c7ab1d98f05d4d34e8ff8a1846f0ed70a0c3e16e97275c7
3
+ metadata.gz: a9903d366094ded799bb376d3fb568b9195a6bf4390ae7ae16b6ed7b21451d7a
4
+ data.tar.gz: b18c7ec5b77ce9d5dbf97b8af37d9d2fc9954702772f540b4ef77f8762cf0a38
5
5
  SHA512:
6
- metadata.gz: 76ef220615b3e3704a99803a8bbe3af211855278d3532a97bbc9f325a5455ff393dde657180368d10bdecce8592afe99a41be7f6fe28e325d3671fa3b414087a
7
- data.tar.gz: c555f306068194447e708c743abdc829f8043728585da32bbf504d689571d38a72725dbe97382665bd20fdaa95848b48ee018d45ab5d22692c1736ad9bcc59cb
6
+ metadata.gz: 25c3f9aeb95587dd6ecfdc51486f8f53848f6c154a8f4141d6baa63b06b4bafd3fa4bd42a026ecd82c972f775eb86f9de2d91c65380c9ba554866b1c41de5bc1
7
+ data.tar.gz: 1bffc3ecaaa58f9a082d71a8eafb36d07db806142fae8b399dbcd0cc1b879d278cc4cc8865e76aee3bede4c1bf2457f14b449b069926560260bfa75d3c02efae
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  [![specs Postgres](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_postgres_71.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_postgres_71.yml)
9
9
  [![specs MySQL](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_mysql_71.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_mysql_71.yml)
10
10
 
11
- An Active Storage service upload/download plugin that stores files in a PostgreSQL or MySQL database.
11
+ An Active Storage service upload/download plugin that stores files in a PostgreSQL or MySQL database. Experimental support also for MSSQL.
12
12
 
13
13
  Main features:
14
14
  - attachment data stored in a binary field (or blob);
@@ -8,6 +8,7 @@ module ActiveStorage
8
8
  # Wraps a DB table as an Active Storage service. See ActiveStorage::Service
9
9
  # for the generic API documentation that applies to all services.
10
10
  class Service::DBService < Service
11
+ # :nocov:
11
12
  if Rails::VERSION::MAJOR >= 7
12
13
  include ActiveStorage::DBServiceRails70
13
14
  elsif Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 1
@@ -15,6 +16,7 @@ module ActiveStorage
15
16
  else
16
17
  include ActiveStorage::DBServiceRails60
17
18
  end
19
+ # :nocov:
18
20
 
19
21
  def initialize(public: false, **)
20
22
  @chunk_size = ENV.fetch('ASDB_CHUNK_SIZE') { 1.megabytes }
@@ -43,7 +45,10 @@ module ActiveStorage
43
45
 
44
46
  def download_chunk(key, range)
45
47
  instrument :download_chunk, key: key, range: range do
46
- record = object_for(key, fields: "SUBSTRING(data FROM #{range.begin + 1} FOR #{range.size}) AS chunk")
48
+ from = range.begin + 1
49
+ size = range.size
50
+ args = adapter_sqlserver? ? "data, #{from}, #{size}" : "data FROM #{from} FOR #{size}"
51
+ record = object_for(key, fields: "SUBSTRING(#{args}) AS chunk")
47
52
  raise(ActiveStorage::FileNotFoundError) unless record
48
53
 
49
54
  record.chunk
@@ -97,6 +102,10 @@ module ActiveStorage
97
102
 
98
103
  private
99
104
 
105
+ def adapter_sqlserver?
106
+ @adapter_sqlserver ||= ActiveStorageDB::File.connection.adapter_name == 'SQLServer'
107
+ end
108
+
100
109
  def generate_url(key, expires_in:, filename:, content_type:, disposition:)
101
110
  content_disposition = content_disposition_with(type: disposition, filename: filename)
102
111
  verified_key_with_expiration = ActiveStorage.verifier.generate(
@@ -142,7 +151,8 @@ module ActiveStorage
142
151
  end
143
152
 
144
153
  def stream(key)
145
- size = object_for(key, fields: 'OCTET_LENGTH(data) AS size')&.size || raise(ActiveStorage::FileNotFoundError)
154
+ data_size = adapter_sqlserver? ? 'DATALENGTH(data)' : 'OCTET_LENGTH(data)'
155
+ size = object_for(key, fields: "#{data_size} AS size")&.size || raise(ActiveStorage::FileNotFoundError)
146
156
  (size / @chunk_size.to_f).ceil.times.each do |i|
147
157
  range = (i * @chunk_size..((i + 1) * @chunk_size) - 1)
148
158
  yield download_chunk(key, range)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStorageDB
4
- VERSION = '1.2.0'
4
+ VERSION = '1.3.0'
5
5
  end
@@ -19,12 +19,12 @@ end
19
19
  namespace :asdb do
20
20
  desc 'ActiveStorageDB: list attachments ordered by blob id desc'
21
21
  task list: [:environment] do |_t, _args|
22
- query = ::ActiveStorage::Blob.order(id: :desc).limit(100)
22
+ query = ActiveStorage::Blob.order(id: :desc).limit(100)
23
23
  digits = query.ids.inject(0) { |ret, id| size = id.to_s.size; [size, ret].max }
24
24
 
25
- ::ActiveStorage::Tasks.print_blob_header(digits: digits)
25
+ ActiveStorage::Tasks.print_blob_header(digits: digits)
26
26
  query.each do |blob|
27
- ::ActiveStorage::Tasks.print_blob(blob, digits: digits)
27
+ ActiveStorage::Tasks.print_blob(blob, digits: digits)
28
28
  end
29
29
  end
30
30
 
@@ -34,7 +34,7 @@ namespace :asdb do
34
34
  destination = args[:destination]&.strip || Dir.pwd
35
35
  abort('Required arguments: source blob id, destination path') if blob_id.blank? || destination.blank?
36
36
 
37
- blob = ::ActiveStorage::Blob.find_by(id: blob_id)
37
+ blob = ActiveStorage::Blob.find_by(id: blob_id)
38
38
  abort('Source file not found') unless blob
39
39
 
40
40
  destination = "#{destination}/#{blob.filename}" if Dir.exist?(destination)
@@ -50,12 +50,12 @@ namespace :asdb do
50
50
  filename = args[:filename]&.strip
51
51
  abort('Required arguments: filename') if filename.blank?
52
52
 
53
- blobs = ::ActiveStorage::Blob.where('filename LIKE ?', "%#{filename}%").order(id: :desc)
53
+ blobs = ActiveStorage::Blob.where('filename LIKE ?', "%#{filename}%").order(id: :desc)
54
54
  if blobs.any?
55
55
  digits = blobs.ids.inject(0) { |ret, id| size = id.to_s.size; [size, ret].max }
56
- ::ActiveStorage::Tasks.print_blob_header(digits: digits)
56
+ ActiveStorage::Tasks.print_blob_header(digits: digits)
57
57
  blobs.each do |blob|
58
- ::ActiveStorage::Tasks.print_blob(blob, digits: digits)
58
+ ActiveStorage::Tasks.print_blob(blob, digits: digits)
59
59
  end
60
60
  else
61
61
  puts 'No results'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Roccoberton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-22 00:00:00.000000000 Z
11
+ date: 2023-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activestorage
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  - !ruby/object:Gem::Version
113
113
  version: '0'
114
114
  requirements: []
115
- rubygems_version: 3.4.19
115
+ rubygems_version: 3.4.21
116
116
  signing_key:
117
117
  specification_version: 4
118
118
  summary: ActiveStorage DB Service