pg-app-name 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg-app-name.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Vagmi Mudumbai
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Pg::App::Name
2
+
3
+ It sets the application name for postgres database connections. It is available in PostgreSQL server 9.x.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pg-app-name'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pg-app-name
18
+
19
+ ## Usage
20
+
21
+ It uses the environment variable `PG_APP_NAME` to set the application name. If you are using Rails, it automatically sets the rails app name as the application name for the database. Setting the environment variable will overwrite the Rails application name.
22
+
23
+ Start your app. Launch `psql` and query the `pg_stat_activity` table to watch the gem in action.
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ module Pg
2
+ module App
3
+ module Name
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,61 @@
1
+ require "pg-app-name/version"
2
+ require 'active_record'
3
+ module PG
4
+ module ApplicationName
5
+ def self.included(base)
6
+ unless base.respond_to? :establish_connection_with_application_name
7
+ base.extend ClassMethods
8
+ base.class_eval do
9
+ class << self
10
+ alias_method_chain :establish_connection, :application_name
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ def establish_connection_with_application_name(*args)
18
+ result = establish_connection_without_application_name(*args)
19
+ appname = ENV['PG_APP_NAME'] || "ruby"
20
+ appname = "#{appname}_without_pool"
21
+ ::ActiveRecord::Base.connection.execute("set application_name = '#{appname}';")
22
+ result
23
+ end
24
+ end
25
+ end
26
+
27
+ module PoolApplicationName
28
+ def self.included(base)
29
+ unless base.respond_to? :new_connection_with_application_name
30
+ base.class_eval do
31
+ include InstanceMethods
32
+ alias_method_chain :new_connection, :application_name
33
+ end
34
+ end
35
+ end
36
+
37
+ module InstanceMethods
38
+ def new_connection_with_application_name(*args)
39
+ result = new_connection_without_application_name(*args)
40
+ appname = ENV['PG_APP_NAME'] || "ruby"
41
+ @pool_number ||= 1
42
+ appname = "#{appname}_with_pool_#{@pool_number}"
43
+ result.execute("set application_name = '#{appname}';")
44
+ @pool_number = @pool_number + 1
45
+ result
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ class ActiveRecord::Base
52
+ include PG::ApplicationName
53
+ end
54
+
55
+ class ActiveRecord::ConnectionAdapters::ConnectionPool
56
+ include PG::PoolApplicationName
57
+ end
58
+
59
+ if defined?(RAILS)
60
+ ENV['PG_APP_NAME'] ||= Rails.application.class.name.split('::')[0].tableize rescue 'rails'
61
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/pg-app-name/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Vagmi Mudumbai"]
6
+ gem.email = ["vagmi.mudumbai@gmail.com"]
7
+ gem.description = %q{Sets the application name for a postgres connection}
8
+ gem.summary = %q{Sets the application name for a postgres connection}
9
+ gem.homepage = "https://github.com/vagmi/pg-app-name"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "pg-app-name"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Pg::App::Name::VERSION
17
+ gem.add_dependency 'activerecord', '~> 3'
18
+ gem.add_dependency 'pg', '~> 0'
19
+ gem.add_development_dependency 'rspec', '~> 2'
20
+ end
@@ -0,0 +1,35 @@
1
+ require 'pg-app-name'
2
+
3
+ connection_params = {adapter: 'postgresql', username: 'postgres', database: 'pg_app_name_test'}
4
+ connection_params['password'] = ENV['PG_PASS'] if ENV['PG_PASS']
5
+ ENV['PG_APP_NAME']="pgtest"
6
+
7
+ describe "Set PG App Name" do
8
+ it "should monkey patch ActiveRecord::Base" do
9
+ ActiveRecord::Base.respond_to?(:establish_connection_with_application_name).should be_true
10
+ end
11
+
12
+ it "should monkey patch ConnectionPool" do
13
+ cp = ActiveRecord::ConnectionAdapters::ConnectionPool.new(ActiveRecord::Base::ConnectionSpecification.new(connection_params,"postgresql_connection"))
14
+ cp.respond_to?(:new_connection_with_application_name).should be_true
15
+ end
16
+
17
+ it "should set the application name while establishing connection" do
18
+ ActiveRecord::Base.establish_connection connection_params
19
+ result = ActiveRecord::Base.connection.exec_query "select application_name from pg_stat_activity where datname='"+connection_params[:database]+"'"
20
+ result.rows.count.should == 1
21
+ result.rows[0][0].should=="pgtest_without_pool"
22
+ ActiveRecord::Base.connection.close
23
+ end
24
+
25
+ it "should set the application name while establishing connection with connection pool" do
26
+ cp = ActiveRecord::ConnectionAdapters::ConnectionPool.new(ActiveRecord::Base::ConnectionSpecification.new(connection_params,"postgresql_connection"))
27
+ conn = cp.checkout
28
+ result = conn.exec_query "select application_name from pg_stat_activity where datname='"+connection_params[:database]+"'"
29
+ result.rows.flatten!
30
+ result.rows.count.should == 2
31
+ result.rows.index("pgtest_with_pool_1").should_not be_nil
32
+ result.rows.index("pgtest_without_pool").should_not be_nil
33
+ cp.disconnect!
34
+ end
35
+ end
@@ -0,0 +1 @@
1
+ require 'pg-app-name'
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg-app-name
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vagmi Mudumbai
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: pg
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ description: Sets the application name for a postgres connection
63
+ email:
64
+ - vagmi.mudumbai@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - lib/pg-app-name.rb
75
+ - lib/pg-app-name/version.rb
76
+ - pg-app-name.gemspec
77
+ - spec/pg-app-name_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/vagmi/pg-app-name
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.21
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Sets the application name for a postgres connection
103
+ test_files:
104
+ - spec/pg-app-name_spec.rb
105
+ - spec/spec_helper.rb