refile-postgres 0.0.2 → 0.0.3

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: e67a83492371a4bbdac7474951277a515bc1f836
4
- data.tar.gz: 73ec8fb7a9d4ebdfd49522175bc7ccd2d075ad6b
3
+ metadata.gz: fb79dfe21ef11e8e168357f538f2a4c51a83095b
4
+ data.tar.gz: 66d562ee82b791458b122d3eb950d660a28144c8
5
5
  SHA512:
6
- metadata.gz: d2f9cdb78006f2fb7f8e3abf6ec066f77733a1d1a1107671837ff67fd9650762c00beaa49372b718b7e8c8f93a6f8f107897dd7bcac3f6988c30f1ceb5cb452e
7
- data.tar.gz: a73cfb197b8f2d2a6611a2abaa722741876cec5d91101bfae778bc952c2a3f239cac4a703b3371218e884bc67ec00bd4605017d206f43295c4cb8e3be770023d
6
+ metadata.gz: 450be3febecd24b2856bb9429166ba94f66aabff229b0056aeae0d5d5103a604295d23d95ced7ce381518fd31a20615d78551f4f30797866bbf7c0a9bb0ca41b
7
+ data.tar.gz: b5d83e96ccf67e3e046430215db09a513265ef5ae6c73227cb283b5a2c4d51a8015bf558b0859336592610f83513e07d90b6f886c8085f039bd628dbdf15261d
data/README.md CHANGED
@@ -1,6 +1,18 @@
1
1
  # Refile::Postgres
2
2
 
3
- TODO: Write a gem description
3
+ A PostgreSQL backend for [Refile](https://github.com/elabs/refile).
4
+
5
+ ## Why?
6
+
7
+ * You want to store all your data in one place to simplify backups and replication
8
+ * ACID
9
+
10
+ ## Take into account
11
+
12
+ * Gem is developed and tested using Postgresql 9.3, Ruby 2.1 and ActiveRecord 4.x. It might work with earlier versions.
13
+ * Performance hit storing files in database
14
+ * Higher memory requirements for database
15
+ * Backups can take significantly longer
4
16
 
5
17
  ## Installation
6
18
 
@@ -24,7 +36,7 @@ TODO: Write usage instructions here
24
36
 
25
37
  ## Contributing
26
38
 
27
- 1. Fork it ( https://github.com/[my-github-username]/refile-postgres/fork )
39
+ 1. Fork it ( https://github.com/krists/refile-postgres/fork )
28
40
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
41
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
42
  4. Push to the branch (`git push origin my-new-feature`)
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates initializer and sets Postgres as store backend
3
+
4
+ Example:
5
+ rails generate initializer
6
+
7
+ This will create:
8
+ config/initializers/refile.rb
@@ -0,0 +1,6 @@
1
+ class Refile::Postgres::InitializerGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+ def copy_initializer_file
4
+ copy_file "refile.rb", "config/initializers/refile.rb"
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ require "refile"
2
+ Refile.configure do |config|
3
+ config.store = Refile::Postgres::Backend.new(ActiveRecord::Base.connection.raw_connection)
4
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates database migration for table to story list of attachments
3
+
4
+ Example:
5
+ rails generate refile:postgres:migration
6
+
7
+ This will create:
8
+ db/migrate/<timestamp>_create_refile_attachments.rb
@@ -0,0 +1,15 @@
1
+ require "refile"
2
+ require 'rails/generators/active_record'
3
+ class Refile::Postgres::MigrationGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+ argument :table_name, type: :string, default: Refile::Postgres::Backend::DEFAULT_REGISTRY_TABLE
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def self.next_migration_number(path)
9
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
10
+ end
11
+
12
+ def copy_migration_file
13
+ migration_template "migration.rb.erb", "db/migrate/create_#{table_name}.rb"
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ class Create<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def up
3
+ create_table :<%= table_name %> do |t|
4
+ t.string :namespace, null: false
5
+ end
6
+ add_index :<%= table_name %>, :namespace
7
+ end
8
+
9
+ def drop
10
+ remove_index :<%= table_name %>, :namespace
11
+ drop_table :<%= table_name %>
12
+ end
13
+ end
14
+
@@ -2,7 +2,7 @@ module Refile
2
2
  module Postgres
3
3
  class Backend
4
4
  class Reader
5
- PQTRANS_INTRANS = 2 # (idle, within transaction block)
5
+ include SmartTransaction
6
6
 
7
7
  def initialize(connection, oid)
8
8
  @connection = connection
@@ -50,29 +50,6 @@ module Refile
50
50
  @closed = true
51
51
  end
52
52
 
53
- private
54
-
55
- def smart_transaction
56
- result = nil
57
- ensure_in_transaction do
58
- begin
59
- handle = connection.lo_open(oid)
60
- result = yield handle
61
- connection.lo_close(handle)
62
- end
63
- end
64
- result
65
- end
66
-
67
- def ensure_in_transaction
68
- if connection.transaction_status == PQTRANS_INTRANS
69
- yield
70
- else
71
- connection.transaction do
72
- yield
73
- end
74
- end
75
- end
76
53
  end
77
54
  end
78
55
  end
@@ -1,14 +1,28 @@
1
1
  module Refile
2
2
  module Postgres
3
3
  class Backend
4
+ include SmartTransaction
5
+ RegistryTableDoesNotExistError = Class.new(StandardError)
4
6
  DEFAULT_REGISTRY_TABLE = "refile_attachments"
5
7
  DEFAULT_NAMESPACE = "default"
6
8
  PG_LARGE_OBJECT_TABLE = "pg_largeobject"
9
+
7
10
  def initialize(connection, max_size: nil, namespace: DEFAULT_NAMESPACE, registry_table: DEFAULT_REGISTRY_TABLE)
11
+ unless connection.is_a?(PG::Connection)
12
+ raise ArgumentError.new("First argument should be an instance of PG::Connection. When using ActiveRecord its available in ActiveRecord::Base.connection.raw_connection")
13
+ end
8
14
  @connection = connection
9
15
  @namespace = namespace.to_s
10
16
  @registry_table = registry_table
11
17
  @max_size = max_size
18
+ connection.exec %{
19
+ SELECT count(*) from pg_catalog.pg_tables
20
+ WHERE tablename = '#{@registry_table}';
21
+ } do |result|
22
+ unless result[0]["count"].to_i > 0
23
+ raise RegistryTableDoesNotExistError.new(%{Please create a table "#{@registry_table}" where backend could store list of attachments})
24
+ end
25
+ end
12
26
  end
13
27
 
14
28
  attr_reader :connection, :namespace, :registry_table
@@ -16,7 +30,7 @@ module Refile
16
30
  def upload(uploadable)
17
31
  Refile.verify_uploadable(uploadable, @max_size)
18
32
  oid = connection.lo_creat
19
- connection.transaction do
33
+ ensure_in_transaction do
20
34
  begin
21
35
  handle = connection.lo_open(oid, PG::INV_WRITE)
22
36
  connection.lo_truncate(handle, 0)
@@ -72,7 +86,7 @@ module Refile
72
86
 
73
87
  def delete(id)
74
88
  if exists?(id)
75
- connection.transaction do
89
+ ensure_in_transaction do
76
90
  connection.lo_unlink(id.to_s.to_i)
77
91
  connection.exec_params("DELETE FROM #{registry_table} WHERE id = $1::integer;", [id])
78
92
  end
@@ -81,7 +95,7 @@ module Refile
81
95
 
82
96
  def clear!(confirm = nil)
83
97
  raise ArgumentError, "are you sure? this will remove all files in the backend, call as `clear!(:confirm)` if you're sure you want to do this" unless confirm == :confirm
84
- connection.transaction do
98
+ ensure_in_transaction do
85
99
  connection.exec_params(%{
86
100
  SELECT * FROM #{registry_table}
87
101
  INNER JOIN #{PG_LARGE_OBJECT_TABLE} ON #{registry_table}.id = #{PG_LARGE_OBJECT_TABLE}.loid
@@ -0,0 +1,31 @@
1
+ module Refile
2
+ module Postgres
3
+ module SmartTransaction
4
+
5
+ PQTRANS_INTRANS = 2 # (idle, within transaction block)
6
+
7
+ def smart_transaction
8
+ result = nil
9
+ ensure_in_transaction do
10
+ begin
11
+ handle = connection.lo_open(oid)
12
+ result = yield handle
13
+ connection.lo_close(handle)
14
+ end
15
+ end
16
+ result
17
+ end
18
+
19
+ def ensure_in_transaction
20
+ if connection.transaction_status == PQTRANS_INTRANS
21
+ yield
22
+ else
23
+ connection.transaction do
24
+ yield
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  module Refile
2
2
  module Postgres
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -1,4 +1,5 @@
1
1
  require "refile/postgres/version"
2
+ require "refile/postgres/smart_transaction"
2
3
  require "refile/postgres/backend"
3
4
  require "refile/postgres/backend/reader"
4
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refile-postgres
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krists Ozols
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-09 00:00:00.000000000 Z
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: refile
@@ -121,9 +121,16 @@ files:
121
121
  - LICENSE.txt
122
122
  - README.md
123
123
  - Rakefile
124
+ - lib/generators/refile/postgres/initializer/USAGE
125
+ - lib/generators/refile/postgres/initializer/initializer_generator.rb
126
+ - lib/generators/refile/postgres/initializer/templates/refile.rb
127
+ - lib/generators/refile/postgres/migration/USAGE
128
+ - lib/generators/refile/postgres/migration/migration_generator.rb
129
+ - lib/generators/refile/postgres/migration/templates/migration.rb.erb
124
130
  - lib/refile/postgres.rb
125
131
  - lib/refile/postgres/backend.rb
126
132
  - lib/refile/postgres/backend/reader.rb
133
+ - lib/refile/postgres/smart_transaction.rb
127
134
  - lib/refile/postgres/version.rb
128
135
  - refile-postgres.gemspec
129
136
  - spec/refile/postgres/backend_spec.rb