shrine-sql 2.0.0 → 2.1.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
  SHA1:
3
- metadata.gz: d5e95896227d7a62239867ee5c393272e57ec2db
4
- data.tar.gz: 9fc7dde76117e7334d0a8ea72c3c450d3456804a
3
+ metadata.gz: 52da0b029e5c8f837c30bdcf7970fb542b5af9f6
4
+ data.tar.gz: e0ffe4c625cbe7e0ead2479556b1e37ff3aa4c76
5
5
  SHA512:
6
- metadata.gz: 0be11d1b596031bac04bca0a557320975abbef0338b20a9ece8371bf37bbbd07c7d2986ce69295d984c704bf14a2353fd5c39727e4e3fdda0f49de2eaf324071
7
- data.tar.gz: 4f2d7b7c03e6d8490849066136ae3c137c53c2dd819e36c8c7eba757a4bbfeeb64dc238ced29326ea7dbf875b85775e13abfdf26ffd7905ef801c3cca1b85886
6
+ metadata.gz: afea505dc65f6ea8388118bb82a25ebd27ddaff8070ac1b71df4fd85f1d1f9c15ea1f256df845281076b4e7df034e019c459458325405c31d4de4de42d80ec87
7
+ data.tar.gz: 959729709cbdd3807b96b17cbb2c4327f35717122c1e265ff930c36793bf767c1d3b0f465bd6d694437012dd38013846c0f058e843de6d2f68f04ec74aeba230
data/README.md CHANGED
@@ -19,8 +19,9 @@ Sequel.migration do
19
19
  change do
20
20
  create_table :files do
21
21
  primary_key :id
22
- column :content, :blob # :bytea for PostgreSQL
23
- column :metadata, :text # :varchar
22
+ File :content
23
+ String :metadata, text: true
24
+ Time :created_at
24
25
  end
25
26
  end
26
27
  end
@@ -32,6 +33,7 @@ class CreateFiles < ActiveRecord::Migration
32
33
  create_table :files do |t|
33
34
  t.binary :content
34
35
  t.text :metadata
36
+ t.datetime :created_at
35
37
  end
36
38
  end
37
39
  end
@@ -45,7 +47,7 @@ require "shrine/storage/sql"
45
47
  require "sequel"
46
48
 
47
49
  DB = Sequel.connect("postgres:///my-database")
48
- Shrine.storages[:store] = Shrine::Storage::Sql.new(database: DB, table: :files)
50
+ Shrine::Storage::Sql.new(database: DB, table: :files)
49
51
  ```
50
52
 
51
53
  You can see [Connecting to a database] on how connect to any database with
@@ -62,7 +64,7 @@ Shrine.plugin :download_endpoint, storages: [:store]
62
64
  ```
63
65
  ```rb
64
66
  Rails.application.routes.draw do
65
- mount Shrine::DownloadEndpoint => "/attachments"
67
+ mount Shrine.download_endpoint => "/attachments"
66
68
  end
67
69
  ```
68
70
  ```rb
@@ -80,6 +82,22 @@ If you're using the SQL storage for both cache and store, moving from cache to
80
82
  store will copy the record using SQL instead "reuploading" it, which means the
81
83
  file contents won't be read into memory.
82
84
 
85
+ ## Clearing
86
+
87
+ You can delete all data from the storage via `Shrine::Storage::Sql#clear!`:
88
+
89
+ ```rb
90
+ sql = Shrine::Storage::Sql.new(database: DB, table: :files)
91
+ sql.clear!
92
+ ```
93
+
94
+ If you're using SQL as temporary storage, you can clear old files by passing
95
+ a block to `#clear!` and querying the `created_at` column:
96
+
97
+ ```rb
98
+ sql.clear! { |dataset| dataset.where{created_at < Time.now - 7*24*60*60} }
99
+ ```
100
+
83
101
  ## Contributing
84
102
 
85
103
  You can run the tests with Rake:
@@ -7,11 +7,11 @@ require "json"
7
7
  class Shrine
8
8
  module Storage
9
9
  class Sql
10
- attr_reader :database, :dataset
10
+ attr_reader :database, :table
11
11
 
12
12
  def initialize(database:, table:)
13
13
  @database = database
14
- @dataset = @database[table]
14
+ @table = table
15
15
  end
16
16
 
17
17
  def upload(io, id, **options)
@@ -39,9 +39,15 @@ class Shrine
39
39
  end
40
40
 
41
41
  def clear!
42
+ dataset = self.dataset
43
+ dataset = yield dataset if block_given?
42
44
  dataset.delete
43
45
  end
44
46
 
47
+ def dataset
48
+ database[table]
49
+ end
50
+
45
51
  protected
46
52
 
47
53
  def find(id_or_ids)
@@ -59,13 +65,20 @@ class Shrine
59
65
  end
60
66
  end
61
67
 
62
- def insert(io, id, shrine_metadata: {})
63
- dataset.insert(content: Sequel::SQL::Blob.new(io.read), metadata: shrine_metadata.to_json)
68
+ def insert(io, id, shrine_metadata: {}, **options)
69
+ record = {}
70
+ record[:content] = Sequel::SQL::Blob.new(io.read)
71
+ record[:metadata] = shrine_metadata.to_json
72
+ record[:created_at] = Sequel::CURRENT_TIMESTAMP if database.schema(table).assoc(:created_at)
73
+
74
+ dataset.insert(record)
64
75
  end
65
76
 
66
- def copy(io, id, shrine_metadata: {})
67
- record = io.storage.find(io.id).select(:content, :metadata)
68
- dataset.insert([:content, :metadata], record)
77
+ def copy(io, id, shrine_metadata: {}, **options)
78
+ record_dataset = io.storage.find(io.id).select(:content, :metadata)
79
+ record_dataset = record_dataset.select_append(Sequel::CURRENT_TIMESTAMP.as(:created_at)) if database.schema(table).assoc(:created_at)
80
+
81
+ dataset.insert(record_dataset.columns, record_dataset)
69
82
  end
70
83
 
71
84
  def copyable?(io, id)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-sql"
3
- gem.version = "2.0.0"
3
+ gem.version = "2.1.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.1"
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-21 00:00:00.000000000 Z
11
+ date: 2017-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine