pg_application_name 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pg_application_name (0.8.0)
4
+ pg_application_name (1.1.0)
5
5
  pg
6
6
  rails (>= 3.0.0)
7
7
  rspec-rails
@@ -5,14 +5,16 @@ require 'active_record/connection_adapters/abstract_adapter'
5
5
  module ActiveRecord::ConnectionAdapters
6
6
  class PostgreSQLAdapter < AbstractAdapter
7
7
  def set_server_variable(var, value)
8
- var = var.to_s.gsub(/'/, "''")
9
- value = value.to_s.gsub(/'/, "''")
10
- execute("set #{var} = '#{value}'")
8
+ if var[0...63].to_s.split(' ').size == 1
9
+ value = value[0...63].to_s.gsub(/'/, "''")
10
+ execute("set #{var} = '#{value}'")
11
+ end
11
12
  end
12
13
 
13
14
  def get_user_variable(var)
14
- var = var.gsub(/'/, "''")
15
- return execute("show #{var}").values[0][0]
15
+ if var[0...63].split(' ').size == 1
16
+ return execute("show #{var}").values[0][0]
17
+ end
16
18
  end
17
19
 
18
20
  def set_server_application_name(value)
@@ -1,4 +1,4 @@
1
1
  module PgApplicationName
2
2
  # the current version of this gem
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.name = 'pg_application_name'
8
8
  s.version = PgApplicationName::VERSION
9
9
  s.license = 'New BSD License'
10
- s.date = '2012-09-14'
10
+ s.date = '2012-09-18'
11
11
  s.summary = "Stamp an ActiveRecord connection to a postgres database with an appropriate application_name."
12
12
  s.description = "This gem stamps an identification mark on the postgres database connection using the postgres connection variable 'application_name'. Such a stamp can be viewed in pg_stat_activity."
13
13
  s.authors = ["Keith Gabryelski"]
@@ -3,5 +3,59 @@ require 'spec_helper'
3
3
  module ActiveRecord::ConnectionAdapters
4
4
  describe ConnectionPool do
5
5
 
6
+ describe "#connection_application_name=" do
7
+ it "sets @connection_application_name" do
8
+ ActiveRecord::ConnectionAdapters::ConnectionPool.connection_application_name = "New name"
9
+ ActiveRecord::ConnectionAdapters::ConnectionPool.
10
+ instance_variable_get(:@connection_application_name).should == "New name"
11
+ end
12
+ end # #connection_application_name=
13
+
14
+ describe "#connection_application_name" do
15
+ context "when @connection_application_name is set" do
16
+ it "returns @connection_application_name value" do
17
+ ActiveRecord::ConnectionAdapters::ConnectionPool.connection_application_name.should == "New name"
18
+ end
19
+ end
20
+
21
+ context "when @connection_application_name isn't set" do
22
+ it "sets and returns @connection_application_name" do
23
+ ActiveRecord::ConnectionAdapters::ConnectionPool.connection_application_name = nil
24
+ ActiveRecord::ConnectionAdapters::ConnectionPool.connection_application_name.
25
+ should == "#{Rails.application.class.name}/#{Process.pid}"
26
+ end
27
+ end
28
+ end # #connection_application_name
29
+
30
+ describe "#initialize_connection_application_name" do
31
+ it "sets @connection_application_name and sets server_application_name" do
32
+ ActiveRecord::ConnectionAdapters::ConnectionPool.initialize_connection_application_name("New application name")
33
+ ActiveRecord::ConnectionAdapters::ConnectionPool.
34
+ instance_variable_get(:@connection_application_name).should == "New application name"
35
+ ActiveRecord::Base.connection.get_user_variable("application_name").should == "New application name"
36
+ end
37
+ end
38
+
39
+ describe "alias_method_chain :new_connection, :set_application_name" do
40
+ context "new_connection_without_set_application_name" do
41
+ it "calls new_connection method" do
42
+ ActiveRecord::Base.connection.set_server_variable(:application_name, "foo")
43
+ ActiveRecord::Base.connection_pool.send(:new_connection_without_set_application_name)
44
+ ActiveRecord::Base.connection.execute("select application_name from pg_stat_activity;").
45
+ values.flatten.select{|app_name| app_name == "foo"}.should have(1).item
46
+ end
47
+ end # new_connection_without_set_application_name
48
+
49
+ context "new_connection" do
50
+ it "calls new_connection_with_set_application_name method" do
51
+ ActiveRecord::Base.connection.set_server_variable(:application_name, "foo")
52
+ ActiveRecord::ConnectionAdapters::ConnectionPool.connection_application_name = "bar"
53
+ ActiveRecord::Base.connection_pool.send(:new_connection)
54
+ ActiveRecord::Base.connection.execute("select application_name from pg_stat_activity;").
55
+ values.flatten.select{|app_name| app_name == "bar"}.should have(1).item
56
+ end
57
+ end # new_connection
58
+ end # new_connection_without_set_application_name
59
+
6
60
  end # ConnectionPool
7
61
  end # ActiveRecord::ConnectionAdapters
@@ -3,5 +3,45 @@ require 'spec_helper'
3
3
  module ActiveRecord::ConnectionAdapters
4
4
  describe PostgreSQLAdapter do
5
5
 
6
+ describe "set_server_variable" do
7
+ context "when call the method with safe params" do
8
+ it "sets application name to the 'New name'" do
9
+ ActiveRecord::Base.connection.set_server_variable(:application_name, "New name")
10
+ ActiveRecord::Base.connection.execute("select application_name from pg_stat_activity;").
11
+ values.flatten.select{|app_name| app_name == "New name"}.should have(1).item
12
+ end
13
+ end # when call the method with safe params
14
+
15
+ context "when call the method without safe params" do
16
+ it "injection did not work" do
17
+ ActiveRecord::Base.connection.
18
+ set_server_variable(:application_name, "New name'; select * from pg_stat_activity where application_name = 'New name").
19
+ values.should be_blank
20
+ end
21
+ end # when call the method without safe params
22
+ end # set_server_variable
23
+
24
+ describe "get_user_variable" do
25
+ context "when call the method with safe param" do
26
+ it "returns user variable" do
27
+ ActiveRecord::Base.connection.get_user_variable("application_name").
28
+ should == "#{Rails.application.class.name}/#{Process.pid}"
29
+ end
30
+ end # when call the method with safe param
31
+
32
+ context "when parameter has blank space" do
33
+ it "returns nil" do
34
+ ActiveRecord::Base.connection.get_user_variable("application name'").should be_nil
35
+ end
36
+ end # when parameter has blank space
37
+ end # get_user_variable
38
+
39
+ describe "set_server_application_name" do
40
+ it "should receive set_server_variable method" do
41
+ ActiveRecord::Base.connection.should_receive(:set_server_variable).with(:application_name, "new name")
42
+ ActiveRecord::Base.connection.set_server_application_name("new name")
43
+ end
44
+ end # set_server_application_name
45
+
6
46
  end # PostgreSQLAdapter
7
47
  end # ActiveRecord::ConnectionAdapters
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_application_name
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-14 00:00:00.000000000 Z
12
+ date: 2012-09-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg