activerecord-oracle_enhanced-adapter 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. data/History.txt +34 -0
  2. data/README.rdoc +10 -5
  3. data/lib/active_record/connection_adapters/emulation/oracle_adapter.rb +1 -1
  4. data/lib/active_record/connection_adapters/oracle_enhanced.rake +4 -0
  5. data/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb +534 -170
  6. data/lib/active_record/connection_adapters/oracle_enhanced_connection.rb +53 -3
  7. data/lib/active_record/connection_adapters/oracle_enhanced_core_ext.rb +10 -10
  8. data/lib/active_record/connection_adapters/oracle_enhanced_cpk.rb +3 -3
  9. data/lib/active_record/connection_adapters/oracle_enhanced_dirty.rb +3 -3
  10. data/lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb +86 -58
  11. data/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb +105 -68
  12. data/lib/active_record/connection_adapters/oracle_enhanced_procedures.rb +27 -1
  13. data/lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb +164 -0
  14. data/lib/active_record/connection_adapters/oracle_enhanced_schema_dumper.rb +122 -0
  15. data/lib/active_record/connection_adapters/oracle_enhanced_schema_statements_ext.rb +224 -0
  16. data/lib/active_record/connection_adapters/oracle_enhanced_tasks.rb +2 -2
  17. data/lib/active_record/connection_adapters/oracle_enhanced_version.rb +1 -1
  18. data/spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb +230 -455
  19. data/spec/active_record/connection_adapters/oracle_enhanced_connection_spec.rb +37 -1
  20. data/spec/active_record/connection_adapters/oracle_enhanced_core_ext_spec.rb +1 -1
  21. data/spec/active_record/connection_adapters/oracle_enhanced_cpk_spec.rb +6 -2
  22. data/spec/active_record/connection_adapters/oracle_enhanced_data_types_spec.rb +21 -4
  23. data/spec/active_record/connection_adapters/oracle_enhanced_dbms_output_spec.rb +63 -0
  24. data/spec/active_record/connection_adapters/oracle_enhanced_dirty_spec.rb +1 -1
  25. data/spec/active_record/connection_adapters/oracle_enhanced_emulate_oracle_adapter_spec.rb +1 -3
  26. data/spec/active_record/connection_adapters/oracle_enhanced_procedures_spec.rb +1 -1
  27. data/spec/active_record/connection_adapters/oracle_enhanced_schema_dump_spec.rb +255 -0
  28. data/spec/active_record/connection_adapters/oracle_enhanced_schema_spec.rb +720 -0
  29. data/spec/spec_helper.rb +38 -7
  30. metadata +13 -15
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe "OracleEnhancedConnection create connection" do
4
4
 
@@ -38,6 +38,42 @@ describe "OracleEnhancedConnection create connection" do
38
38
 
39
39
  end
40
40
 
41
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
42
+
43
+ describe "OracleEnhancedConnection create JDBC connection" do
44
+ after(:each) do
45
+ @conn.logoff if @conn.active?
46
+ end
47
+
48
+ it "should create new connection using :url" do
49
+ params = CONNECTION_PARAMS.dup
50
+ params[:url] = "jdbc:oracle:thin:@#{DATABASE_HOST}:#{DATABASE_PORT}:#{DATABASE_NAME}"
51
+ params[:host] = nil
52
+ params[:database] = nil
53
+ @conn = ActiveRecord::ConnectionAdapters::OracleEnhancedConnection.create(params)
54
+ @conn.should be_active
55
+ end
56
+
57
+ it "should create new connection using :url and tnsnames alias" do
58
+ params = CONNECTION_PARAMS.dup
59
+ params[:url] = "jdbc:oracle:thin:@#{DATABASE_NAME}"
60
+ params[:host] = nil
61
+ params[:database] = nil
62
+ @conn = ActiveRecord::ConnectionAdapters::OracleEnhancedConnection.create(params)
63
+ @conn.should be_active
64
+ end
65
+
66
+ it "should create new connection using just tnsnames alias" do
67
+ params = CONNECTION_PARAMS.dup
68
+ params[:host] = nil
69
+ @conn = ActiveRecord::ConnectionAdapters::OracleEnhancedConnection.create(params)
70
+ @conn.should be_active
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
41
77
  describe "OracleEnhancedConnection SQL execution" do
42
78
 
43
79
  before(:all) do
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
4
4
 
5
5
  describe "OracleEnhancedAdapter to_d method" do
6
6
  it "BigDecimal#to_d returns the same decimal number" do
@@ -1,4 +1,6 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ unless defined?(NO_COMPOSITE_PRIMARY_KEYS)
2
4
 
3
5
  describe "OracleEnhancedAdapter composite_primary_keys support" do
4
6
 
@@ -100,4 +102,6 @@ describe "OracleEnhancedAdapter composite_primary_keys support" do
100
102
 
101
103
  # Other testing was done based on composite_primary_keys tests
102
104
 
103
- end
105
+ end
106
+
107
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe "OracleEnhancedAdapter date type detection based on column names" do
4
4
  before(:all) do
@@ -83,11 +83,9 @@ describe "OracleEnhancedAdapter date type detection based on column names" do
83
83
 
84
84
  describe "/ DATE values from ActiveRecord model" do
85
85
  before(:each) do
86
- ActiveRecord::Base.connection.clear_types_for_columns
87
86
  ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = false
88
87
  ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates = false
89
88
  class ::TestEmployee < ActiveRecord::Base
90
- set_table_name "hr.test_employees"
91
89
  set_primary_key :employee_id
92
90
  end
93
91
  end
@@ -107,6 +105,7 @@ describe "OracleEnhancedAdapter date type detection based on column names" do
107
105
  after(:each) do
108
106
  # @employee.destroy if @employee
109
107
  Object.send(:remove_const, "TestEmployee")
108
+ ActiveRecord::Base.connection.clear_types_for_columns
110
109
  end
111
110
 
112
111
  it "should return Time value from DATE column if emulate_dates_by_column_name is false" do
@@ -151,6 +150,16 @@ describe "OracleEnhancedAdapter date type detection based on column names" do
151
150
  @employee.hire_date.class.should == Date
152
151
  end
153
152
 
153
+ it "should see set_date_columns values in different connection" do
154
+ class ::TestEmployee < ActiveRecord::Base
155
+ set_date_columns :hire_date
156
+ end
157
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = false
158
+ # establish other connection
159
+ other_conn = ActiveRecord::Base.oracle_enhanced_connection(CONNECTION_PARAMS)
160
+ other_conn.get_type_for_column('test_employees', 'hire_date').should == :date
161
+ end
162
+
154
163
  it "should return Time value from DATE column if emulate_dates_by_column_name is true but column is defined as datetime" do
155
164
  class ::TestEmployee < ActiveRecord::Base
156
165
  set_datetime_columns :hire_date
@@ -837,6 +846,7 @@ describe "OracleEnhancedAdapter handling of CLOB columns" do
837
846
  :last_name => "Last"
838
847
  )
839
848
  @employee.should be_valid
849
+ TestEmployee.serialized_attributes.delete('comments')
840
850
  end
841
851
 
842
852
  it "should order by CLOB column" do
@@ -852,7 +862,14 @@ describe "OracleEnhancedAdapter handling of CLOB columns" do
852
862
  TestEmployee.find(:all, :order => :comments).should_not be_empty
853
863
  TestEmployee.find(:all, :order => " first_name DESC, last_name ASC ").should_not be_empty
854
864
  end
855
-
865
+
866
+ it "should accept Symbol value for CLOB column" do
867
+ @employee = TestEmployee.create!(
868
+ :comments => :test_comment
869
+ )
870
+ @employee.should be_valid
871
+ end
872
+
856
873
  end
857
874
 
858
875
  describe "OracleEnhancedAdapter handling of BLOB columns" do
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe "OracleEnhancedAdapter logging dbms_output from plsql" do
4
+ include LoggerSpecHelper
5
+
6
+ before(:all) do
7
+ @buffer = StringIO.new
8
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
9
+ ActiveRecord::Base.connection.execute <<-SQL
10
+ CREATE or REPLACE
11
+ FUNCTION MORE_THAN_FIVE_CHARACTERS_LONG (some_text VARCHAR2) RETURN INTEGER
12
+ AS
13
+ longer_than_five INTEGER;
14
+ BEGIN
15
+ dbms_output.put_line('before the if -' || some_text || '-');
16
+ IF length(some_text) > 5 THEN
17
+ dbms_output.put_line('it is longer than 5');
18
+ longer_than_five := 1;
19
+ ELSE
20
+ dbms_output.put_line('it is 5 or shorter');
21
+ longer_than_five := 0;
22
+ END IF;
23
+ dbms_output.put_line('about to return: ' || longer_than_five);
24
+ RETURN longer_than_five;
25
+ END;
26
+ SQL
27
+ end
28
+
29
+ before(:each) do
30
+ @buffer = StringIO.new
31
+ log_to @buffer
32
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
33
+ @conn = ActiveRecord::Base.connection
34
+ end
35
+
36
+ it "should NOT log dbms output when dbms output is disabled" do
37
+ @conn.disable_dbms_output
38
+
39
+ @conn.select_all("select more_than_five_characters_long('hi there') is_it_long from dual").should == [{'is_it_long'=>1}]
40
+
41
+ @buffer.string.should_not match(/^DBMS_OUTPUT/)
42
+ end
43
+
44
+ it "should log dbms output lines to the rails log" do
45
+ @conn.enable_dbms_output
46
+
47
+ @conn.select_all("select more_than_five_characters_long('hi there') is_it_long from dual").should == [{'is_it_long'=>1}]
48
+
49
+ @buffer.string.should match(/^DBMS_OUTPUT: before the if -hi there-$/)
50
+ @buffer.string.should match(/^DBMS_OUTPUT: it is longer than 5$/)
51
+ @buffer.string.should match(/^DBMS_OUTPUT: about to return: 1$/)
52
+ end
53
+
54
+ it "should log dbms output lines to the rails log" do
55
+ @conn.enable_dbms_output
56
+
57
+ @conn.select_all("select more_than_five_characters_long('short') is_it_long from dual").should == [{'is_it_long'=>0}]
58
+
59
+ @buffer.string.should match(/^DBMS_OUTPUT: before the if -short-$/)
60
+ @buffer.string.should match(/^DBMS_OUTPUT: it is 5 or shorter$/)
61
+ @buffer.string.should match(/^DBMS_OUTPUT: about to return: 0$/)
62
+ end
63
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  if ActiveRecord::Base.instance_methods.include?('changed?')
4
4
 
@@ -1,6 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
-
3
-
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
4
2
 
5
3
  describe "OracleEnhancedAdapter emulate OracleAdapter" do
6
4
 
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe "OracleEnhancedAdapter custom methods for create, update and destroy" do
4
4
  include LoggerSpecHelper
@@ -0,0 +1,255 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe "OracleEnhancedAdapter original schema dump" do
4
+
5
+ before(:all) do
6
+ if !defined?(RUBY_ENGINE)
7
+ if ActiveRecord::Base.respond_to?(:oracle_connection)
8
+ @old_conn = ActiveRecord::Base.oracle_connection(CONNECTION_PARAMS)
9
+ @old_conn.class.should == ActiveRecord::ConnectionAdapters::OracleAdapter
10
+ end
11
+ elsif RUBY_ENGINE == 'jruby'
12
+ @old_conn = ActiveRecord::Base.jdbc_connection(JDBC_CONNECTION_PARAMS)
13
+ @old_conn.class.should == ActiveRecord::ConnectionAdapters::JdbcAdapter
14
+ end
15
+
16
+ @new_conn = ActiveRecord::Base.oracle_enhanced_connection(CONNECTION_PARAMS)
17
+ @new_conn.class.should == ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter
18
+ end
19
+
20
+ after(:all) do
21
+ # Workaround for undefining callback that was defined by JDBC adapter
22
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
23
+ ActiveRecord::Base.class_eval do
24
+ def after_save_with_oracle_lob
25
+ nil
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ if !defined?(RUBY_ENGINE) && ActiveRecord::Base.respond_to?(:oracle_connection) || defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
32
+ it "should return the same tables list as original oracle adapter" do
33
+ @new_conn.tables.sort.should == @old_conn.tables.sort
34
+ end
35
+
36
+ it "should return the same index list as original oracle adapter" do
37
+ @new_conn.indexes('employees').sort_by(&:name).should == @old_conn.indexes('employees').sort_by(&:name)
38
+ end
39
+
40
+ it "should return the same pk_and_sequence_for as original oracle adapter" do
41
+ if @old_conn.respond_to?(:pk_and_sequence_for)
42
+ @new_conn.tables.each do |t|
43
+ @new_conn.pk_and_sequence_for(t).should == @old_conn.pk_and_sequence_for(t)
44
+ end
45
+ end
46
+ end
47
+
48
+ it "should return the same structure dump as original oracle adapter" do
49
+ @new_conn.structure_dump.split(";\n\n").sort.should == @old_conn.structure_dump.split(";\n\n").sort
50
+ end
51
+
52
+ it "should return the same structure drop as original oracle adapter" do
53
+ @new_conn.structure_drop.split(";\n\n").sort.should == @old_conn.structure_drop.split(";\n\n").sort
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+ describe "OracleEnhancedAdapter schema dump" do
60
+ include SchemaSpecHelper
61
+
62
+ before(:all) do
63
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
64
+ @conn = ActiveRecord::Base.connection
65
+ end
66
+
67
+ def standard_dump
68
+ stream = StringIO.new
69
+ ActiveRecord::SchemaDumper.ignore_tables = []
70
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
71
+ stream.string
72
+ end
73
+
74
+ def create_test_posts_table(options = {})
75
+ options.merge! :force => true
76
+ schema_define do
77
+ create_table :test_posts, options do |t|
78
+ t.string :title
79
+ end
80
+ add_index :test_posts, :title
81
+ end
82
+ end
83
+
84
+ def drop_test_posts_table
85
+ schema_define do
86
+ drop_table :test_posts
87
+ end
88
+ rescue
89
+ nil
90
+ end
91
+
92
+ describe "table prefixes and suffixes" do
93
+ after(:each) do
94
+ drop_test_posts_table
95
+ @conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if @conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
96
+ ActiveRecord::Base.table_name_prefix = ''
97
+ ActiveRecord::Base.table_name_suffix = ''
98
+ end
99
+
100
+ it "should remove table prefix in schema dump" do
101
+ ActiveRecord::Base.table_name_prefix = 'xxx_'
102
+ create_test_posts_table
103
+ standard_dump.should =~ /create_table "test_posts".*add_index "test_posts"/m
104
+ end
105
+
106
+ it "should remove table suffix in schema dump" do
107
+ ActiveRecord::Base.table_name_suffix = '_xxx'
108
+ create_test_posts_table
109
+ standard_dump.should =~ /create_table "test_posts".*add_index "test_posts"/m
110
+ end
111
+
112
+ it "should not include schema_migrations table with prefix in schema dump" do
113
+ ActiveRecord::Base.table_name_prefix = 'xxx_'
114
+ @conn.initialize_schema_migrations_table
115
+ standard_dump.should_not =~ /schema_migrations/
116
+ end
117
+
118
+ it "should not include schema_migrations table with suffix in schema dump" do
119
+ ActiveRecord::Base.table_name_suffix = '_xxx'
120
+ @conn.initialize_schema_migrations_table
121
+ standard_dump.should_not =~ /schema_migrations/
122
+ end
123
+
124
+ end
125
+
126
+ describe "table with non-default primary key" do
127
+ after(:each) do
128
+ drop_test_posts_table
129
+ end
130
+
131
+ it "should include non-default primary key in schema dump" do
132
+ create_test_posts_table(:primary_key => 'post_id')
133
+ standard_dump.should =~ /create_table "test_posts", :primary_key => "post_id"/
134
+ end
135
+
136
+ end
137
+
138
+ describe "table with primary key trigger" do
139
+
140
+ after(:each) do
141
+ drop_test_posts_table
142
+ end
143
+
144
+ it "should include primary key trigger in schema dump" do
145
+ create_test_posts_table(:primary_key_trigger => true)
146
+ standard_dump.should =~ /create_table "test_posts".*add_primary_key_trigger "test_posts"/m
147
+ end
148
+
149
+ it "should include primary key trigger with non-default primary key in schema dump" do
150
+ create_test_posts_table(:primary_key_trigger => true, :primary_key => 'post_id')
151
+ standard_dump.should =~ /create_table "test_posts", :primary_key => "post_id".*add_primary_key_trigger "test_posts", :primary_key => "post_id"/m
152
+ end
153
+
154
+ end
155
+
156
+ describe "foreign key constraints" do
157
+ before(:all) do
158
+ schema_define do
159
+ create_table :test_posts, :force => true do |t|
160
+ t.string :title
161
+ end
162
+ create_table :test_comments, :force => true do |t|
163
+ t.string :body, :limit => 4000
164
+ t.references :test_post
165
+ end
166
+ end
167
+ end
168
+
169
+ after(:each) do
170
+ schema_define do
171
+ remove_foreign_key :test_comments, :test_posts
172
+ end
173
+ end
174
+ after(:all) do
175
+ schema_define do
176
+ drop_table :test_comments rescue nil
177
+ drop_table :test_posts rescue nil
178
+ end
179
+ end
180
+
181
+ it "should include foreign key in schema dump" do
182
+ schema_define do
183
+ add_foreign_key :test_comments, :test_posts
184
+ end
185
+ standard_dump.should =~ /add_foreign_key "test_comments", "test_posts", :name => "test_comments_test_post_id_fk"/
186
+ end
187
+
188
+ it "should include foreign key with delete dependency in schema dump" do
189
+ schema_define do
190
+ add_foreign_key :test_comments, :test_posts, :dependent => :delete
191
+ end
192
+ standard_dump.should =~ /add_foreign_key "test_comments", "test_posts", :name => "test_comments_test_post_id_fk", :dependent => :delete/
193
+ end
194
+
195
+ it "should include foreign key with nullify dependency in schema dump" do
196
+ schema_define do
197
+ add_foreign_key :test_comments, :test_posts, :dependent => :nullify
198
+ end
199
+ standard_dump.should =~ /add_foreign_key "test_comments", "test_posts", :name => "test_comments_test_post_id_fk", :dependent => :nullify/
200
+ end
201
+
202
+ end
203
+
204
+ describe "synonyms" do
205
+ after(:each) do
206
+ schema_define do
207
+ remove_synonym :test_synonym
208
+ end
209
+ end
210
+
211
+ it "should include synonym to other schema table in schema dump" do
212
+ schema_define do
213
+ add_synonym :test_synonym, "schema_name.table_name", :force => true
214
+ end
215
+ standard_dump.should =~ /add_synonym "test_synonym", "schema_name.table_name", :force => true/
216
+ end
217
+
218
+ it "should include synonym to other database table in schema dump" do
219
+ schema_define do
220
+ add_synonym :test_synonym, "table_name@link_name", :force => true
221
+ end
222
+ standard_dump.should =~ /add_synonym "test_synonym", "table_name@link_name", :force => true/
223
+ end
224
+
225
+ end
226
+
227
+ end
228
+
229
+ describe "OracleEnhancedAdapter structure dump" do
230
+ before(:all) do
231
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
232
+ @conn = ActiveRecord::Base.connection
233
+ end
234
+
235
+ describe "database stucture dump extentions" do
236
+ before(:all) do
237
+ @conn.execute <<-SQL
238
+ CREATE TABLE nvarchartable (
239
+ unq_nvarchar NVARCHAR2(255) DEFAULT NULL
240
+ )
241
+ SQL
242
+ end
243
+
244
+ after(:all) do
245
+ @conn.execute "DROP TABLE nvarchartable"
246
+ end
247
+
248
+ it "should return the character size of nvarchar fields" do
249
+ if /.*unq_nvarchar nvarchar2\((\d+)\).*/ =~ @conn.structure_dump
250
+ "#$1".should == "255"
251
+ end
252
+ end
253
+ end
254
+
255
+ end