carrierwave-postgresql-table 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '0873cdce630b258b27a7d62a870287ad2628e302'
4
- data.tar.gz: 7e9ea222ace5c49b3f7392578b79e9c80209b2ab
3
+ metadata.gz: 5ef806ccfb8914f80bcbe80cdd3ebd8faa89cd7c
4
+ data.tar.gz: 0ab0e9e7757bb7c0767aa375ea55dceda7b2ac03
5
5
  SHA512:
6
- metadata.gz: ad6c264a57086c050e01211d70e5f40e883a1881ace02bf5c694fe3fd1a6cebcedaddaaa0c6e60314b0a877b0301e707d0423872952347facb71ed45a49fcdba
7
- data.tar.gz: c5a0f07a08f07ef5ae5770c752a589233e80a0e3fd9dc0197e537dbf127cd0bd89752f6d130a66006cdacf9611adb5bccc16fc72fa8e4ae7df69db0922118d73
6
+ metadata.gz: cca0e9d3e00674e05e7c224154d225706653393160210c3ae1bc1920ede30840316aa032aef256cf532b47abd89b9770bb25869baf6073300d1845696fda7c7c
7
+ data.tar.gz: 1d74a9ee0a82fdda54485fffd8cee3c2aa06f84197861ff2941119216fa5c3fa2395aae706253067f8e80c586d9f263b16d8cba0a76e0f72abb8f56548d13e1c
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /test/tmp/
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module PostgresqlTable
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -119,11 +119,12 @@ module CarrierWave
119
119
 
120
120
  def store(new_file)
121
121
  CarrierWaveFile.transaction do
122
+ connection = CarrierWaveFile.connection
123
+ raw_connection = connection.raw_connection
122
124
  oid = nil
123
125
  if(new_file.kind_of?(CarrierWave::Storage::PostgresqlTable::File))
124
126
  oid = new_file.copy_to(@path)
125
127
  else
126
- raw_connection = CarrierWaveFile.connection.raw_connection
127
128
  file = new_file.to_file
128
129
 
129
130
  begin
@@ -142,10 +143,20 @@ module CarrierWave
142
143
  end
143
144
 
144
145
  begin
146
+ old_oid = @record.pg_largeobject_oid
145
147
  @record.pg_largeobject_oid = oid
146
148
  @record.size = new_file.size
147
149
  @record.content_type = new_file.content_type
148
150
  @record.save
151
+
152
+ # Cleanup old, unused largeobject OIDs if we're updating the
153
+ # record with a new OID reference.
154
+ if(old_oid && old_oid != oid)
155
+ old_references = connection.select_value("SELECT COUNT(*) FROM #{CarrierWaveFile.table_name} WHERE pg_largeobject_oid = #{CarrierWaveFile.sanitize(old_oid)}").to_i
156
+ if(old_references == 0)
157
+ raw_connection.lo_unlink(old_oid)
158
+ end
159
+ end
149
160
  rescue ::ActiveRecord::RecordNotUnique
150
161
  @record = CarrierWaveFile.find_or_initialize_by(:path => @path)
151
162
  retry
@@ -174,7 +185,13 @@ module CarrierWave
174
185
  end
175
186
 
176
187
  def move_to(new_path)
177
- @record.update_attribute(:path, new_path)
188
+ CarrierWaveFile.transaction do
189
+ # Remove any existing files at the current path.
190
+ CarrierWaveFile.delete_all_files("path = #{CarrierWaveFile.sanitize(new_path)} AND id != #{CarrierWaveFile.sanitize(@record.id)}")
191
+
192
+ # Change the current record's path to the new path.
193
+ @record.update_attribute(:path, new_path)
194
+ end
178
195
  end
179
196
 
180
197
  def delete
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-postgresql-table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Muerdter