refile-postgres 1.1.1 → 1.1.2

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: 41c4e736cddecfc7074a09729f45beb73a33491b
4
- data.tar.gz: cc27187f7e397a82da021f29a1115d73946aaa0c
3
+ metadata.gz: 8b097fc4b87bee20b25c98ada162adf99147f2c4
4
+ data.tar.gz: f285a72849806fe2d37ce3bcd928ac42c79b7195
5
5
  SHA512:
6
- metadata.gz: aee232288904b017f79b68ef199a3d8a4226fa4f21ea9c5dafff39ace386cfa8acdccafb0871d77a88e6cb89e758b15bb42426cc71c853765543c4ac4a41c5b9
7
- data.tar.gz: 3aecf0a65e5fc3817d568db0f3dd96caa6f9dafedd5914f3575a0a7774e3b1bdabf52a184750bc66f6c2349903bc99eb6c556807a2667e54918719913ba2eb8a
6
+ metadata.gz: 9a8a599de65bc5295d7710c55f1f0e4ff7b39e203b209cdea2e57b0745d687f0152cc1db7d910fb287adef0249cf75c1763658a1a8335c73d24da1dfc844905f
7
+ data.tar.gz: 7c6ece224d86451f0cba3ebb7f3570764f5fcf7c3ed7c64599c732d47edf8fbabce8d12d91ea1c3f6bb54a10bd95e561728824f07de4fe42b10a65032ff04f8d
data/.gitignore CHANGED
@@ -12,3 +12,7 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ .ruby-version
16
+ .ruby-gemset
17
+ .vagrant
18
+ berks-cookbooks
data/Berksfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://supermarket.chef.io"
2
+
3
+ cookbook 'apt', '~> 2.7.0'
4
+ cookbook 'locale', '~> 1.0.2'
data/Berksfile.lock ADDED
@@ -0,0 +1,7 @@
1
+ DEPENDENCIES
2
+ apt (~> 2.7.0)
3
+ locale (~> 1.0.2)
4
+
5
+ GRAPH
6
+ apt (2.7.0)
7
+ locale (1.0.2)
data/Rakefile CHANGED
@@ -1,2 +1 @@
1
1
  require "bundler/gem_tasks"
2
-
data/Vagrantfile ADDED
@@ -0,0 +1,24 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant.configure(2) do |config|
5
+ config.vm.box = "chef/ubuntu-14.04"
6
+
7
+ config.ssh.forward_agent = true
8
+ config.vm.synced_folder ".", "/home/vagrant/project"
9
+
10
+ config.vm.provider "virtualbox" do |vb|
11
+ vb.memory = "2048"
12
+ vb.cpus = 2
13
+ end
14
+
15
+ config.berkshelf.enabled = false
16
+
17
+ config.vm.provision "chef_solo" do |chef|
18
+ chef.cookbooks_path = ["cookbooks", "berks-cookbooks"]
19
+ chef.log_level = :info
20
+ chef.add_recipe "dev-essential"
21
+ chef.add_recipe "ruby"
22
+ chef.add_recipe "postgresql"
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ name 'dev-essential'
2
+ maintainer 'Krists Ozols'
3
+ maintainer_email 'krists.ozols@gmail.com'
4
+ license 'MIT'
5
+ description 'Installs/Configures some essentials for development'
6
+ version '0.1.0'
7
+
8
+ depends 'locale', '~> 1.0.2'
@@ -0,0 +1,9 @@
1
+ include_recipe "locale"
2
+
3
+ %w{
4
+ git-core curl vim tmux
5
+ }.each do |p|
6
+ package p do
7
+ action :install
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ name 'postgresql'
2
+ maintainer 'Krists Ozols'
3
+ maintainer_email 'krists.ozols@gmail.com'
4
+ license 'MIT'
5
+ description 'Installs/Configures PostgreSQL 9.4'
6
+ version '0.1.0'
7
+
8
+ depends 'apt', '~> 2.7.0'
@@ -0,0 +1,29 @@
1
+ apt_repository 'postgresql' do
2
+ uri 'http://apt.postgresql.org/pub/repos/apt/'
3
+ distribution 'trusty-pgdg'
4
+ components ['main']
5
+ key 'https://www.postgresql.org/media/keys/ACCC4CF8.asc'
6
+ action :add
7
+ end
8
+
9
+ %w{
10
+ postgresql-9.4
11
+ postgresql-client-9.4
12
+ postgresql-contrib-9.4
13
+ postgresql-server-dev-9.4
14
+ }.each do |p|
15
+ package p do
16
+ action :install
17
+ end
18
+ end
19
+
20
+ execute 'Create vagrant postgresql user' do
21
+ guard = <<-EOH
22
+ psql -U postgres -c "select * from pg_user where
23
+ usename='vagrant'" |
24
+ grep -c vagrant
25
+ EOH
26
+ user 'postgres'
27
+ command %{psql -U postgres -c "CREATE ROLE vagrant PASSWORD 'vagrant' SUPERUSER CREATEDB LOGIN;"}
28
+ not_if guard, user: 'postgres'
29
+ end
@@ -0,0 +1,2 @@
1
+ default[:ruby_install][:version] = '0.5.0'
2
+ default[:chruby][:version] = '0.3.9'
@@ -0,0 +1,6 @@
1
+ name 'ruby'
2
+ maintainer 'Krists Ozols'
3
+ maintainer_email 'krists.ozols@gmail.com'
4
+ license 'MIT'
5
+ description 'Installs/Configures ruby-installer and chruby'
6
+ version '0.1.0'
@@ -0,0 +1,52 @@
1
+
2
+ remote_file "/tmp/ruby-install-#{node[:ruby_install][:version]}.tar.gz" do
3
+ source "https://github.com/postmodern/ruby-install/archive/v#{node[:ruby_install][:version]}.tar.gz"
4
+ action :create
5
+ end
6
+
7
+ bash "Extract ruby-install source code" do
8
+ code <<-EOH
9
+ cd /tmp
10
+ tar -zxvf ruby-install-#{node[:ruby_install][:version]}.tar.gz
11
+ EOH
12
+ not_if { ::File.exists?("/tmp/ruby-install-#{node[:ruby_install][:version]}") }
13
+ end
14
+
15
+ execute 'Install ruby-install' do
16
+ cwd "/tmp/ruby-install-#{node[:ruby_install][:version]}"
17
+ command 'make install'
18
+ action :run
19
+ not_if { ::File.exists?("/usr/local/bin/ruby-install") }
20
+ end
21
+
22
+ remote_file "/tmp/chruby-#{node[:chruby][:version]}.tar.gz" do
23
+ source "https://github.com/postmodern/chruby/archive/v#{node[:chruby][:version]}.tar.gz"
24
+ action :create
25
+ end
26
+
27
+ bash "Extract chruby source code" do
28
+ code <<-EOH
29
+ cd /tmp
30
+ tar -zxvf chruby-#{node[:chruby][:version]}.tar.gz
31
+ EOH
32
+ not_if { ::File.exists?("/tmp/chruby-#{node[:chruby][:version]}") }
33
+ end
34
+
35
+ execute 'Install chruby' do
36
+ cwd "/tmp/chruby-#{node[:chruby][:version]}"
37
+ command 'make install'
38
+ action :run
39
+ not_if { ::File.exists?("/usr/local/bin/chruby") }
40
+ end
41
+
42
+ file "/etc/profile.d/chruby.sh" do
43
+ owner 'root'
44
+ group 'root'
45
+ mode '0644'
46
+ content <<-EOC
47
+ if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then
48
+ source /usr/local/share/chruby/chruby.sh
49
+ source /usr/local/share/chruby/auto.sh
50
+ fi
51
+ EOC
52
+ end
@@ -1,4 +1,4 @@
1
1
  require "refile"
2
2
  Refile.configure do |config|
3
- config.store = Refile::Postgres::Backend.new(ActiveRecord::Base.connection.raw_connection)
3
+ config.store = Refile::Postgres::Backend.new(proc { ActiveRecord::Base.connection.raw_connection } )
4
4
  end
@@ -8,19 +8,17 @@ module Refile
8
8
  DEFAULT_NAMESPACE = "default"
9
9
  PG_LARGE_OBJECT_TABLE = "pg_largeobject"
10
10
  READ_CHUNK_SIZE = 3000
11
+ INIT_CONNECTION_ARG_ERROR_MSG = "When initializing new Refile::Postgres::Backend first argument should be an instance of PG::Connection or a lambda/proc that returns it. When using ActiveRecord it is available as ActiveRecord::Base.connection.raw_connection"
11
12
 
12
- def initialize(connection, max_size: nil, namespace: DEFAULT_NAMESPACE, registry_table: DEFAULT_REGISTRY_TABLE)
13
- unless connection.is_a?(PG::Connection)
14
- raise ArgumentError.new("First argument should be an instance of PG::Connection. When using ActiveRecord it is available as ActiveRecord::Base.connection.raw_connection")
15
- end
16
- @connection = connection
13
+ def initialize(connection_or_proc, max_size: nil, namespace: DEFAULT_NAMESPACE, registry_table: DEFAULT_REGISTRY_TABLE)
14
+ @connection_or_proc = connection_or_proc
17
15
  @namespace = namespace.to_s
18
16
  @registry_table = registry_table
19
17
  @registry_table_validated = false
20
18
  @max_size = max_size
21
19
  end
22
20
 
23
- attr_reader :connection, :namespace, :max_size
21
+ attr_reader :namespace, :max_size
24
22
 
25
23
  def registry_table
26
24
  unless @registry_table_validated
@@ -37,6 +35,14 @@ module Refile
37
35
  @registry_table
38
36
  end
39
37
 
38
+ def connection
39
+ if has_active_connection?
40
+ @connection
41
+ else
42
+ obtain_new_connection
43
+ end
44
+ end
45
+
40
46
  verify_uploadable def upload(uploadable)
41
47
  oid = connection.lo_creat
42
48
  ensure_in_transaction do
@@ -122,7 +128,20 @@ module Refile
122
128
  connection.exec_params("DELETE FROM #{registry_table} WHERE namespace = $1::varchar;", [namespace])
123
129
  end
124
130
  end
131
+
132
+ private
133
+
134
+ def has_active_connection?
135
+ @connection && !@connection.finished?
136
+ end
137
+
138
+ def obtain_new_connection
139
+ candidate = @connection_or_proc.is_a?(Proc) ? @connection_or_proc.call : @connection_or_proc
140
+ unless candidate.is_a?(PG::Connection)
141
+ raise ArgumentError.new(INIT_CONNECTION_ARG_ERROR_MSG)
142
+ end
143
+ @connection = candidate
144
+ end
125
145
  end
126
146
  end
127
147
  end
128
-
@@ -1,5 +1,5 @@
1
1
  module Refile
2
2
  module Postgres
3
- VERSION = "1.1.1"
3
+ VERSION = "1.1.2"
4
4
  end
5
5
  end
@@ -1,8 +1,47 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Refile::Postgres::Backend do
4
- let(:db_connection) { PG.connect(dbname: 'refile_test') }
5
- let(:backend) { Refile::Postgres::Backend.new(db_connection, max_size: 100) }
4
+ let(:connection_or_proc) { PG.connect(dbname: 'refile_test') }
5
+ let(:backend) { Refile::Postgres::Backend.new(connection_or_proc, max_size: 100) }
6
6
  it_behaves_like :backend
7
- end
8
7
 
8
+ context "Connection tests" do
9
+ def connection
10
+ PG.connect(dbname: 'refile_test')
11
+ end
12
+
13
+ context "when using proc" do
14
+ def connection_or_proc
15
+ proc { connection }
16
+ end
17
+
18
+ it "reuses the same PG::Connection if connection is ok" do
19
+ expect(backend.connection).to eq(backend.connection)
20
+ end
21
+
22
+ it "executes proc and obtains new connection if old one is closed" do
23
+ old = backend.connection
24
+ old.close
25
+ expect(backend.connection).not_to eq(old)
26
+ expect(backend.connection.finished?).to be_falsey
27
+ end
28
+ end
29
+
30
+ context "when not using procs and providing PG::Connection directly" do
31
+ def connection_or_proc
32
+ connection
33
+ end
34
+
35
+ it "reuses the same PG::Connection" do
36
+ expect(backend.connection).to eq(backend.connection)
37
+ end
38
+
39
+ it "continues to use old connection if the old one is closed" do
40
+ old = backend.connection
41
+ old.close
42
+ expect(backend.connection).to eq(old)
43
+ expect(backend.connection.finished?).to be_truthy
44
+ end
45
+ end
46
+ end
47
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,12 +2,12 @@ $LOAD_PATH.unshift(File.join(Gem::Specification.find_by_name("refile").gem_dir,
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require "refile/spec_helper"
4
4
  require "pg"
5
+ require "pry"
5
6
  require "refile/postgres"
6
7
 
7
8
  RSpec.configure do |config|
8
9
  config.before(:all) do
9
- DB_NAME = 'refile_test'
10
- connection = PG.connect(dbname: DB_NAME)
10
+ connection = PG.connect(dbname: 'refile_test')
11
11
  connection.exec %{ DROP TABLE IF EXISTS #{Refile::Postgres::Backend::DEFAULT_REGISTRY_TABLE} CASCADE; }
12
12
  connection.exec %{
13
13
  CREATE TABLE IF NOT EXISTS #{Refile::Postgres::Backend::DEFAULT_REGISTRY_TABLE}
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: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krists Ozols
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-27 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: refile
@@ -145,10 +145,20 @@ extensions: []
145
145
  extra_rdoc_files: []
146
146
  files:
147
147
  - ".gitignore"
148
+ - Berksfile
149
+ - Berksfile.lock
148
150
  - Gemfile
149
151
  - LICENSE.txt
150
152
  - README.md
151
153
  - Rakefile
154
+ - Vagrantfile
155
+ - cookbooks/dev-essential/metadata.rb
156
+ - cookbooks/dev-essential/recipes/default.rb
157
+ - cookbooks/postgresql/metadata.rb
158
+ - cookbooks/postgresql/recipes/default.rb
159
+ - cookbooks/ruby/attributes/default.rb
160
+ - cookbooks/ruby/metadata.rb
161
+ - cookbooks/ruby/recipes/default.rb
152
162
  - lib/generators/refile/postgres/initializer/USAGE
153
163
  - lib/generators/refile/postgres/initializer/initializer_generator.rb
154
164
  - lib/generators/refile/postgres/initializer/templates/refile.rb
@@ -183,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
193
  version: '0'
184
194
  requirements: []
185
195
  rubyforge_project:
186
- rubygems_version: 2.4.6
196
+ rubygems_version: 2.4.5
187
197
  signing_key:
188
198
  specification_version: 4
189
199
  summary: Postgres database as a backend for Refile