Skip to content

Commit 8c4ff29

Browse files
committed
Merge pull request #94 from govdelivery/current_schema
use current_schema in PLSQL::Schema#schema_name
2 parents e406170 + d48b0cb commit 8c4ff29

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

lib/plsql/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def logoff
9090
# Current Oracle schema name
9191
def schema_name
9292
return nil unless connection
93-
@schema_name ||= select_first("SELECT SYS_CONTEXT('userenv','session_user') FROM dual")[0]
93+
@schema_name ||= select_first("SELECT SYS_CONTEXT('userenv','current_schema') FROM dual")[0]
9494
end
9595

9696
# Default timezone to which database values will be converted - :utc or :local

spec/plsql/package_spec.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
END test_procedure;
2121
END;
2222
SQL
23-
2423
end
2524

2625
after(:all) do
@@ -67,6 +66,44 @@
6766
end
6867
end
6968

69+
context "with a user with execute privilege who is not the package owner" do
70+
before(:all) do
71+
plsql.execute("grant execute on TEST_PACKAGE to #{DATABASE_USERS_AND_PASSWORDS[1][0]}")
72+
@original_connection = plsql.connection
73+
@conn = get_connection(1)
74+
end
75+
76+
before(:each) do
77+
# resetting connection clears cached package objects and schema name
78+
plsql.connection = @conn
79+
end
80+
81+
after(:all) do
82+
plsql.logoff
83+
plsql.connection = @original_connection
84+
end
85+
86+
it "should not find existing package" do
87+
expect(PLSQL::Package.find(plsql, :test_package)).to be_nil
88+
end
89+
90+
context "who sets current_schema to match the package owner" do
91+
before(:all) do
92+
plsql.execute "ALTER SESSION set current_schema=#{DATABASE_USERS_AND_PASSWORDS[0][0]}"
93+
end
94+
95+
it "should find existing package" do
96+
expect(PLSQL::Package.find(plsql, :test_package)).not_to be_nil
97+
end
98+
99+
it "should report an existing procedure as existing" do
100+
expect(plsql.test_package.procedure_defined?(:test_procedure)).to be_truthy
101+
end
102+
103+
end
104+
105+
end
106+
70107
describe "variables" do
71108
it "should set and get package variable value" do
72109
plsql.test_package.test_variable = 1

spec/plsql/schema_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
expect(plsql.schema_name).to eq(DATABASE_USERS_AND_PASSWORDS[0][0].upcase)
3838
end
3939

40+
it 'should match altered current_schema in database session' do
41+
plsql.connection = @conn
42+
expected_current_schema = DATABASE_USERS_AND_PASSWORDS[1][0]
43+
plsql.execute "ALTER SESSION set current_schema=#{expected_current_schema}"
44+
expect(plsql.schema_name).to eq(expected_current_schema.upcase)
45+
end
46+
4047
it "should return new schema name after reconnection" do
4148
plsql.connection = @conn
4249
expect(plsql.schema_name).to eq(DATABASE_USERS_AND_PASSWORDS[0][0].upcase)

0 commit comments

Comments
 (0)