active_storage_db 1.1.2 → 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: 33872893aefc8f1086a8c422819990e10d1d46d2b97bd87d67662fd153536243
4
- data.tar.gz: 0e662cd62a06ec353c9e02706c1c553500edafa146e4149d21fa57626d129d5a
3
+ metadata.gz: a9903d366094ded799bb376d3fb568b9195a6bf4390ae7ae16b6ed7b21451d7a
4
+ data.tar.gz: b18c7ec5b77ce9d5dbf97b8af37d9d2fc9954702772f540b4ef77f8762cf0a38
5
5
  SHA512:
6
- metadata.gz: a418655f7275cc54264b25733712cbe813eeede79eb7d08833c2e333427d7a9c1cb44f1c46b3a338cf20407abf0d7d9b59e2dfcdec0f1467b77f47ca85b7357e
7
- data.tar.gz: ac8d6c3c8fc9c44177d474c709f67ff5a46a8c1835c15e9f753773ac846851e23bd13ef2e8e0c3fd13185ea0d8f528f31aac8ae48275a46288bde22a6ad04dee
6
+ metadata.gz: 25c3f9aeb95587dd6ecfdc51486f8f53848f6c154a8f4141d6baa63b06b4bafd3fa4bd42a026ecd82c972f775eb86f9de2d91c65380c9ba554866b1c41de5bc1
7
+ data.tar.gz: 1bffc3ecaaa58f9a082d71a8eafb36d07db806142fae8b399dbcd0cc1b879d278cc4cc8865e76aee3bede4c1bf2457f14b449b069926560260bfa75d3c02efae
data/README.md CHANGED
@@ -5,15 +5,15 @@
5
5
  [![maintainability](https://api.codeclimate.com/v1/badges/92e1e703c308744a0f66/maintainability)](https://codeclimate.com/github/blocknotes/active_storage_db/maintainability)
6
6
 
7
7
  [![linters](https://github.com/blocknotes/active_storage_db/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/linters.yml)
8
- [![specs Postgres](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_postgres_70.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_postgres_70.yml)
9
- [![Specs MySQL](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_mysql_70.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_mysql_70.yml)
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
+ [![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
- - supports Rails _6.0_, _6.1_ and _7.0_;
14
+ - attachment data stored in a binary field (or blob);
15
15
  - all service methods implemented;
16
- - attachment data stored in a binary field (or blob).
16
+ - supports Rails _6_ and _7_.
17
17
 
18
18
  Useful also with platforms like Heroku (due to their ephemeral file system).
19
19
 
@@ -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.1.2'
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)
23
- digits = Math.log(query.maximum(:id), 10).to_i + 1
22
+ query = ActiveStorage::Blob.order(id: :desc).limit(100)
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
- digits = Math.log(blobs.first.id, 10).to_i + 1
56
- ::ActiveStorage::Tasks.print_blob_header(digits: digits)
55
+ digits = blobs.ids.inject(0) { |ret, id| size = id.to_s.size; [size, ret].max }
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.1.2
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: 2022-09-07 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
@@ -105,14 +105,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
105
  requirements:
106
106
  - - ">="
107
107
  - !ruby/object:Gem::Version
108
- version: 2.6.0
108
+ version: 2.7.0
109
109
  required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  requirements:
111
111
  - - ">="
112
112
  - !ruby/object:Gem::Version
113
113
  version: '0'
114
114
  requirements: []
115
- rubygems_version: 3.1.6
115
+ rubygems_version: 3.4.21
116
116
  signing_key:
117
117
  specification_version: 4
118
118
  summary: ActiveStorage DB Service