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,5 +1,5 @@
1
- # RSI: implementation idea taken from JDBC adapter
2
- if defined?(Rake.application) && Rake.application
1
+ # implementation idea taken from JDBC adapter
2
+ if defined?(Rake.application) && Rake.application && ActiveRecord::Base.configurations[RAILS_ENV]['adapter'] == 'oracle_enhanced'
3
3
  oracle_enhanced_rakefile = File.dirname(__FILE__) + "/oracle_enhanced.rake"
4
4
  if Rake.application.lookup("environment")
5
5
  # rails tasks already defined; load the override tasks now
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord #:nodoc:
2
2
  module ConnectionAdapters #:nodoc:
3
3
  module OracleEnhancedVersion #:nodoc:
4
- VERSION = '1.2.1'
4
+ VERSION = '1.2.2'
5
5
  end
6
6
  end
7
7
  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 establish connection" do
4
4
 
@@ -33,558 +33,333 @@ describe "OracleEnhancedAdapter establish connection" do
33
33
 
34
34
  end
35
35
 
36
- describe "OracleEnhancedAdapter schema dump" do
37
-
36
+ describe "OracleEnhancedAdapter" do
38
37
  before(:all) do
39
- if !defined?(RUBY_ENGINE)
40
- @old_conn = ActiveRecord::Base.oracle_connection(CONNECTION_PARAMS)
41
- @old_conn.class.should == ActiveRecord::ConnectionAdapters::OracleAdapter
42
- elsif RUBY_ENGINE == 'jruby'
43
- @old_conn = ActiveRecord::Base.jdbc_connection(JDBC_CONNECTION_PARAMS)
44
- @old_conn.class.should == ActiveRecord::ConnectionAdapters::JdbcAdapter
45
- end
46
-
47
- @new_conn = ActiveRecord::Base.oracle_enhanced_connection(CONNECTION_PARAMS)
48
- @new_conn.class.should == ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter
38
+ ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
39
+ @conn = ActiveRecord::Base.connection
49
40
  end
50
41
 
51
- after(:all) do
52
- # Workaround for undefining callback that was defined by JDBC adapter
53
- if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
54
- ActiveRecord::Base.class_eval do
55
- def after_save_with_oracle_lob
56
- nil
57
- end
42
+ describe "database session store" do
43
+ before(:all) do
44
+ @conn.execute <<-SQL
45
+ CREATE TABLE sessions (
46
+ id NUMBER(38,0) NOT NULL,
47
+ session_id VARCHAR2(255) DEFAULT NULL,
48
+ data CLOB DEFAULT NULL,
49
+ created_at DATE DEFAULT NULL,
50
+ updated_at DATE DEFAULT NULL,
51
+ PRIMARY KEY (ID)
52
+ )
53
+ SQL
54
+ @conn.execute <<-SQL
55
+ CREATE SEQUENCE sessions_seq MINVALUE 1 MAXVALUE 999999999999999999999999999
56
+ INCREMENT BY 1 START WITH 10040 CACHE 20 NOORDER NOCYCLE
57
+ SQL
58
+ if ENV['RAILS_GEM_VERSION'] >= '2.3'
59
+ @session_class = ActiveRecord::SessionStore::Session
60
+ else
61
+ @session_class = CGI::Session::ActiveRecordStore::Session
58
62
  end
59
63
  end
60
- end
61
64
 
62
- unless defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION =~ /^1\.9/
63
- it "should return the same tables list as original oracle adapter" do
64
- @new_conn.tables.sort.should == @old_conn.tables.sort
65
+ after(:all) do
66
+ @conn.execute "DROP TABLE sessions"
67
+ @conn.execute "DROP SEQUENCE sessions_seq"
65
68
  end
66
69
 
67
- it "should return the same index list as original oracle adapter" do
68
- @new_conn.indexes('employees').sort_by(&:name).should == @old_conn.indexes('employees').sort_by(&:name)
70
+ it "should create sessions table" do
71
+ ActiveRecord::Base.connection.tables.grep("sessions").should_not be_empty
69
72
  end
70
73
 
71
- it "should return the same pk_and_sequence_for as original oracle adapter" do
72
- if @old_conn.respond_to?(:pk_and_sequence_for)
73
- @new_conn.tables.each do |t|
74
- @new_conn.pk_and_sequence_for(t).should == @old_conn.pk_and_sequence_for(t)
75
- end
76
- end
74
+ it "should save session data" do
75
+ @session = @session_class.new :session_id => "111111", :data => "something" #, :updated_at => Time.now
76
+ @session.save!
77
+ @session = @session_class.find_by_session_id("111111")
78
+ @session.data.should == "something"
77
79
  end
78
80
 
79
- it "should return the same structure dump as original oracle adapter" do
80
- @new_conn.structure_dump.split(";\n\n").sort.should == @old_conn.structure_dump.split(";\n\n").sort
81
+ it "should change session data when partial updates enabled" do
82
+ return pending("Not in this ActiveRecord version") unless @session_class.respond_to?(:partial_updates=)
83
+ @session_class.partial_updates = true
84
+ @session = @session_class.new :session_id => "222222", :data => "something" #, :updated_at => Time.now
85
+ @session.save!
86
+ @session = @session_class.find_by_session_id("222222")
87
+ @session.data = "other thing"
88
+ @session.save!
89
+ # second save should call again blob writing callback
90
+ @session.save!
91
+ @session = @session_class.find_by_session_id("222222")
92
+ @session.data.should == "other thing"
81
93
  end
82
94
 
83
- it "should return the same structure drop as original oracle adapter" do
84
- @new_conn.structure_drop.split(";\n\n").sort.should == @old_conn.structure_drop.split(";\n\n").sort
95
+ it "should have one enhanced_write_lobs callback" do
96
+ return pending("Not in this ActiveRecord version") unless @session_class.respond_to?(:after_save_callback_chain)
97
+ @session_class.after_save_callback_chain.select{|cb| cb.method == :enhanced_write_lobs}.should have(1).record
85
98
  end
86
- end
87
99
 
88
- it "should return the character size of nvarchar fields" do
89
- @new_conn.execute <<-SQL
90
- CREATE TABLE nvarchartable (
91
- session_id NVARCHAR2(255) DEFAULT NULL
92
- )
93
- SQL
94
- if /.*session_id nvarchar2\((\d+)\).*/ =~ @new_conn.structure_dump
95
- "#$1".should == "255"
100
+ it "should not set sessions table session_id column type as integer if emulate_integers_by_column_name is true" do
101
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = true
102
+ columns = @conn.columns('sessions')
103
+ column = columns.detect{|c| c.name == "session_id"}
104
+ column.type.should == :string
96
105
  end
97
- @new_conn.execute "DROP TABLE nvarchartable"
98
- end
99
- end
100
-
101
- describe "OracleEnhancedAdapter database stucture dump extentions" do
102
- before(:all) do
103
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
104
- @conn = ActiveRecord::Base.connection
105
- @conn.execute <<-SQL
106
- CREATE TABLE nvarchartable (
107
- unq_nvarchar NVARCHAR2(255) DEFAULT NULL
108
- )
109
- SQL
110
- end
111
106
 
112
- after(:all) do
113
- @conn.execute "DROP TABLE nvarchartable"
114
107
  end
115
108
 
116
- it "should return the character size of nvarchar fields" do
117
- if /.*unq_nvarchar nvarchar2\((\d+)\).*/ =~ @conn.structure_dump
118
- "#$1".should == "255"
109
+ describe "ignore specified table columns" do
110
+ before(:all) do
111
+ @conn.execute <<-SQL
112
+ CREATE TABLE test_employees (
113
+ id NUMBER,
114
+ first_name VARCHAR2(20),
115
+ last_name VARCHAR2(25),
116
+ email VARCHAR2(25),
117
+ phone_number VARCHAR2(20),
118
+ hire_date DATE,
119
+ job_id NUMBER,
120
+ salary NUMBER,
121
+ commission_pct NUMBER(2,2),
122
+ manager_id NUMBER(6),
123
+ department_id NUMBER(4,0),
124
+ created_at DATE
125
+ )
126
+ SQL
127
+ @conn.execute <<-SQL
128
+ CREATE SEQUENCE test_employees_seq MINVALUE 1
129
+ INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE
130
+ SQL
119
131
  end
120
- end
121
- end
122
132
 
123
- describe "OracleEnhancedAdapter database session store" do
124
- before(:all) do
125
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
126
- @conn = ActiveRecord::Base.connection
127
- @conn.execute <<-SQL
128
- CREATE TABLE sessions (
129
- id NUMBER(38,0) NOT NULL,
130
- session_id VARCHAR2(255) DEFAULT NULL,
131
- data CLOB DEFAULT NULL,
132
- created_at DATE DEFAULT NULL,
133
- updated_at DATE DEFAULT NULL,
134
- PRIMARY KEY (ID)
135
- )
136
- SQL
137
- @conn.execute <<-SQL
138
- CREATE SEQUENCE sessions_seq MINVALUE 1 MAXVALUE 999999999999999999999999999
139
- INCREMENT BY 1 START WITH 10040 CACHE 20 NOORDER NOCYCLE
140
- SQL
141
- if ENV['RAILS_GEM_VERSION'] >= '2.3'
142
- @session_class = ActiveRecord::SessionStore::Session
143
- else
144
- @session_class = CGI::Session::ActiveRecordStore::Session
133
+ after(:all) do
134
+ @conn.execute "DROP TABLE test_employees"
135
+ @conn.execute "DROP SEQUENCE test_employees_seq"
145
136
  end
146
- end
147
-
148
- after(:all) do
149
- @conn.execute "DROP TABLE sessions"
150
- @conn.execute "DROP SEQUENCE sessions_seq"
151
- end
152
-
153
- it "should create sessions table" do
154
- ActiveRecord::Base.connection.tables.grep("sessions").should_not be_empty
155
- end
156
-
157
- it "should save session data" do
158
- @session = @session_class.new :session_id => "111111", :data => "something" #, :updated_at => Time.now
159
- @session.save!
160
- @session = @session_class.find_by_session_id("111111")
161
- @session.data.should == "something"
162
- end
163
-
164
- it "should change session data when partial updates enabled" do
165
- return pending("Not in this ActiveRecord version") unless @session_class.respond_to?(:partial_updates=)
166
- @session_class.partial_updates = true
167
- @session = @session_class.new :session_id => "222222", :data => "something" #, :updated_at => Time.now
168
- @session.save!
169
- @session = @session_class.find_by_session_id("222222")
170
- @session.data = "other thing"
171
- @session.save!
172
- # second save should call again blob writing callback
173
- @session.save!
174
- @session = @session_class.find_by_session_id("222222")
175
- @session.data.should == "other thing"
176
- end
177
-
178
- it "should have one enhanced_write_lobs callback" do
179
- return pending("Not in this ActiveRecord version") unless @session_class.respond_to?(:after_save_callback_chain)
180
- @session_class.after_save_callback_chain.select{|cb| cb.method == :enhanced_write_lobs}.should have(1).record
181
- end
182
-
183
- it "should not set sessions table session_id column type as integer if emulate_integers_by_column_name is true" do
184
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = true
185
- columns = @conn.columns('sessions')
186
- column = columns.detect{|c| c.name == "session_id"}
187
- column.type.should == :string
188
- end
189
-
190
- end
191
-
192
- describe "OracleEnhancedAdapter ignore specified table columns" do
193
- before(:all) do
194
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
195
- @conn = ActiveRecord::Base.connection
196
- @conn.execute <<-SQL
197
- CREATE TABLE test_employees (
198
- id NUMBER,
199
- first_name VARCHAR2(20),
200
- last_name VARCHAR2(25),
201
- email VARCHAR2(25),
202
- phone_number VARCHAR2(20),
203
- hire_date DATE,
204
- job_id NUMBER,
205
- salary NUMBER,
206
- commission_pct NUMBER(2,2),
207
- manager_id NUMBER(6),
208
- department_id NUMBER(4,0),
209
- created_at DATE
210
- )
211
- SQL
212
- @conn.execute <<-SQL
213
- CREATE SEQUENCE test_employees_seq MINVALUE 1
214
- INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE
215
- SQL
216
- end
217
-
218
- after(:all) do
219
- @conn.execute "DROP TABLE test_employees"
220
- @conn.execute "DROP SEQUENCE test_employees_seq"
221
- end
222
137
 
223
- after(:each) do
224
- Object.send(:remove_const, "TestEmployee")
225
- end
226
-
227
- it "should ignore specified table columns" do
228
- class ::TestEmployee < ActiveRecord::Base
229
- ignore_table_columns :phone_number, :hire_date
138
+ after(:each) do
139
+ Object.send(:remove_const, "TestEmployee")
140
+ ActiveRecord::Base.connection.clear_ignored_table_columns
230
141
  end
231
- TestEmployee.connection.columns('test_employees').select{|c| ['phone_number','hire_date'].include?(c.name) }.should be_empty
232
- end
233
142
 
234
- it "should ignore specified table columns specified in several lines" do
235
- class ::TestEmployee < ActiveRecord::Base
236
- ignore_table_columns :phone_number
237
- ignore_table_columns :hire_date
143
+ it "should ignore specified table columns" do
144
+ class ::TestEmployee < ActiveRecord::Base
145
+ ignore_table_columns :phone_number, :hire_date
146
+ end
147
+ TestEmployee.connection.columns('test_employees').select{|c| ['phone_number','hire_date'].include?(c.name) }.should be_empty
238
148
  end
239
- TestEmployee.connection.columns('test_employees').select{|c| ['phone_number','hire_date'].include?(c.name) }.should be_empty
240
- end
241
149
 
242
- it "should not ignore unspecified table columns" do
243
- class ::TestEmployee < ActiveRecord::Base
244
- ignore_table_columns :phone_number, :hire_date
150
+ it "should ignore specified table columns specified in several lines" do
151
+ class ::TestEmployee < ActiveRecord::Base
152
+ ignore_table_columns :phone_number
153
+ ignore_table_columns :hire_date
154
+ end
155
+ TestEmployee.connection.columns('test_employees').select{|c| ['phone_number','hire_date'].include?(c.name) }.should be_empty
245
156
  end
246
- TestEmployee.connection.columns('test_employees').select{|c| c.name == 'email' }.should_not be_empty
247
- end
248
-
249
157
 
250
- end
251
-
252
- describe "OracleEnhancedAdapter table and sequence creation with non-default primary key" do
253
-
254
- before(:all) do
255
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
256
- ActiveRecord::Schema.define do
257
- suppress_messages do
258
- create_table :keyboards, :force => true, :id => false do |t|
259
- t.primary_key :key_number
260
- t.string :name
261
- end
262
- create_table :id_keyboards, :force => true do |t|
263
- t.string :name
264
- end
158
+ it "should not ignore unspecified table columns" do
159
+ class ::TestEmployee < ActiveRecord::Base
160
+ ignore_table_columns :phone_number, :hire_date
265
161
  end
162
+ TestEmployee.connection.columns('test_employees').select{|c| c.name == 'email' }.should_not be_empty
266
163
  end
267
- class ::Keyboard < ActiveRecord::Base
268
- set_primary_key :key_number
269
- end
270
- class ::IdKeyboard < ActiveRecord::Base
271
- end
272
- end
273
164
 
274
- after(:all) do
275
- ActiveRecord::Schema.define do
276
- suppress_messages do
277
- drop_table :keyboards
278
- drop_table :id_keyboards
165
+ it "should ignore specified table columns in other connection" do
166
+ class ::TestEmployee < ActiveRecord::Base
167
+ ignore_table_columns :phone_number, :hire_date
279
168
  end
169
+ # establish other connection
170
+ other_conn = ActiveRecord::Base.oracle_enhanced_connection(CONNECTION_PARAMS)
171
+ other_conn.columns('test_employees').select{|c| ['phone_number','hire_date'].include?(c.name) }.should be_empty
280
172
  end
281
- Object.send(:remove_const, "Keyboard")
282
- Object.send(:remove_const, "IdKeyboard")
283
- end
284
173
 
285
- it "should create sequence for non-default primary key" do
286
- ActiveRecord::Base.connection.next_sequence_value(Keyboard.sequence_name).should_not be_nil
287
174
  end
288
175
 
289
- it "should create sequence for default primary key" do
290
- ActiveRecord::Base.connection.next_sequence_value(IdKeyboard.sequence_name).should_not be_nil
291
- end
292
- end
176
+ describe "without composite_primary_keys" do
293
177
 
294
- describe "OracleEnhancedAdapter without composite_primary_keys" do
178
+ before(:all) do
179
+ Object.send(:remove_const, 'CompositePrimaryKeys') if defined?(CompositePrimaryKeys)
180
+ class ::Employee < ActiveRecord::Base
181
+ set_primary_key :employee_id
182
+ end
183
+ end
295
184
 
296
- before(:all) do
297
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
298
- Object.send(:remove_const, 'CompositePrimaryKeys') if defined?(CompositePrimaryKeys)
299
- class ::Employee < ActiveRecord::Base
300
- set_primary_key :employee_id
185
+ it "should tell ActiveRecord that count distinct is supported" do
186
+ ActiveRecord::Base.connection.supports_count_distinct?.should be_true
301
187
  end
302
- end
303
188
 
304
- it "should tell ActiveRecord that count distinct is supported" do
305
- ActiveRecord::Base.connection.supports_count_distinct?.should be_true
306
- end
189
+ it "should execute correct SQL COUNT DISTINCT statement" do
190
+ lambda { Employee.count(:employee_id, :distinct => true) }.should_not raise_error
191
+ end
307
192
 
308
- it "should execute correct SQL COUNT DISTINCT statement" do
309
- lambda { Employee.count(:employee_id, :distinct => true) }.should_not raise_error
310
193
  end
311
194
 
312
- end
313
-
314
- describe "OracleEnhancedAdapter sequence creation parameters" do
315
195
 
316
- before(:all) do
317
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
318
- end
196
+ describe "column quoting" do
319
197
 
320
- def create_test_employees_table(sequence_start_value = nil)
321
- ActiveRecord::Schema.define do
322
- suppress_messages do
323
- create_table :test_employees, sequence_start_value ? {:sequence_start_value => sequence_start_value} : {} do |t|
324
- t.string :first_name
325
- t.string :last_name
198
+ def create_test_reserved_words_table
199
+ ActiveRecord::Schema.define do
200
+ suppress_messages do
201
+ create_table :test_reserved_words do |t|
202
+ t.string :varchar2
203
+ t.integer :integer
204
+ end
326
205
  end
327
206
  end
328
207
  end
329
- end
330
-
331
- def save_default_sequence_start_value
332
- @saved_sequence_start_value = ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value
333
- end
334
-
335
- def restore_default_sequence_start_value
336
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value = @saved_sequence_start_value
337
- end
338
208
 
339
- before(:each) do
340
- save_default_sequence_start_value
341
- end
342
- after(:each) do
343
- restore_default_sequence_start_value
344
- ActiveRecord::Schema.define do
345
- suppress_messages do
346
- drop_table :test_employees
209
+ after(:each) do
210
+ ActiveRecord::Schema.define do
211
+ suppress_messages do
212
+ drop_table :test_reserved_words
213
+ end
347
214
  end
215
+ Object.send(:remove_const, "TestReservedWord")
216
+ ActiveRecord::Base.table_name_prefix = nil
348
217
  end
349
- Object.send(:remove_const, "TestEmployee")
350
- end
351
218
 
352
- it "should use default sequence start value 10000" do
353
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value.should == 10000
219
+ it "should allow creation of a table with oracle reserved words as column names" do
220
+ create_test_reserved_words_table
221
+ class ::TestReservedWord < ActiveRecord::Base; end
354
222
 
355
- create_test_employees_table
356
- class ::TestEmployee < ActiveRecord::Base; end
223
+ [:varchar2, :integer].each do |attr|
224
+ TestReservedWord.columns_hash[attr.to_s].name.should == attr.to_s
225
+ end
226
+ end
357
227
 
358
- employee = TestEmployee.create!
359
- employee.id.should == 10000
360
228
  end
361
229
 
362
- it "should use specified default sequence start value" do
363
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value = 1
230
+ describe "valid table names" do
231
+ before(:all) do
232
+ @adapter = ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter
233
+ end
364
234
 
365
- create_test_employees_table
366
- class ::TestEmployee < ActiveRecord::Base; end
235
+ it "should be valid with letters and digits" do
236
+ @adapter.valid_table_name?("abc_123").should be_true
237
+ end
367
238
 
368
- employee = TestEmployee.create!
369
- employee.id.should == 1
370
- end
239
+ it "should be valid with schema name" do
240
+ @adapter.valid_table_name?("abc_123.def_456").should be_true
241
+ end
371
242
 
372
- it "should use sequence start value from table definition" do
373
- create_test_employees_table(10)
374
- class ::TestEmployee < ActiveRecord::Base; end
243
+ it "should be valid with $ in name" do
244
+ @adapter.valid_table_name?("sys.v$session").should be_true
245
+ end
375
246
 
376
- employee = TestEmployee.create!
377
- employee.id.should == 10
378
- end
247
+ it "should not be valid with two dots in name" do
248
+ @adapter.valid_table_name?("abc_123.def_456.ghi_789").should be_false
249
+ end
379
250
 
380
- it "should use sequence start value and other options from table definition" do
381
- create_test_employees_table("100 NOCACHE INCREMENT BY 10")
382
- class ::TestEmployee < ActiveRecord::Base; end
251
+ it "should not be valid with invalid characters" do
252
+ @adapter.valid_table_name?("warehouse-things").should be_false
253
+ end
383
254
 
384
- employee = TestEmployee.create!
385
- employee.id.should == 100
386
- employee = TestEmployee.create!
387
- employee.id.should == 110
388
255
  end
389
256
 
390
- end
391
-
392
- describe "OracleEnhancedAdapter table and column comments" do
393
-
394
- before(:all) do
395
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
396
- @conn = ActiveRecord::Base.connection
397
- end
257
+ describe "table quoting" do
398
258
 
399
- def create_test_employees_table(table_comment=nil, column_comments={})
400
- ActiveRecord::Schema.define do
401
- suppress_messages do
402
- create_table :test_employees, :comment => table_comment do |t|
403
- t.string :first_name, :comment => column_comments[:first_name]
404
- t.string :last_name, :comment => column_comments[:last_name]
259
+ def create_warehouse_things_table
260
+ ActiveRecord::Schema.define do
261
+ suppress_messages do
262
+ create_table "warehouse-things" do |t|
263
+ t.string :name
264
+ t.integer :foo
265
+ end
405
266
  end
406
267
  end
407
268
  end
408
- end
409
269
 
410
- after(:each) do
411
- ActiveRecord::Schema.define do
412
- suppress_messages do
413
- drop_table :test_employees
270
+ def create_camel_case_table
271
+ ActiveRecord::Schema.define do
272
+ suppress_messages do
273
+ create_table "CamelCase" do |t|
274
+ t.string :name
275
+ t.integer :foo
276
+ end
277
+ end
414
278
  end
415
279
  end
416
- Object.send(:remove_const, "TestEmployee")
417
- ActiveRecord::Base.table_name_prefix = nil
418
- end
419
-
420
- it "should create table with table comment" do
421
- table_comment = "Test Employees"
422
- create_test_employees_table(table_comment)
423
- class ::TestEmployee < ActiveRecord::Base; end
424
-
425
- @conn.table_comment("test_employees").should == table_comment
426
- TestEmployee.table_comment.should == table_comment
427
- end
428
-
429
- it "should create table with columns comment" do
430
- column_comments = {:first_name => "Given Name", :last_name => "Surname"}
431
- create_test_employees_table(nil, column_comments)
432
- class ::TestEmployee < ActiveRecord::Base; end
433
-
434
- [:first_name, :last_name].each do |attr|
435
- @conn.column_comment("test_employees", attr.to_s).should == column_comments[attr]
436
- end
437
- [:first_name, :last_name].each do |attr|
438
- TestEmployee.columns_hash[attr.to_s].comment.should == column_comments[attr]
439
- end
440
- end
441
-
442
- it "should create table with table and columns comment and custom table name prefix" do
443
- ActiveRecord::Base.table_name_prefix = "xxx_"
444
- table_comment = "Test Employees"
445
- column_comments = {:first_name => "Given Name", :last_name => "Surname"}
446
- create_test_employees_table(table_comment, column_comments)
447
- class ::TestEmployee < ActiveRecord::Base; end
448
-
449
- @conn.table_comment(TestEmployee.table_name).should == table_comment
450
- TestEmployee.table_comment.should == table_comment
451
- [:first_name, :last_name].each do |attr|
452
- @conn.column_comment(TestEmployee.table_name, attr.to_s).should == column_comments[attr]
453
- end
454
- [:first_name, :last_name].each do |attr|
455
- TestEmployee.columns_hash[attr.to_s].comment.should == column_comments[attr]
456
- end
457
- end
458
280
 
459
- end
460
-
461
- describe "OracleEnhancedAdapter column quoting" do
462
-
463
- before(:all) do
464
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
465
- @conn = ActiveRecord::Base.connection
466
- end
467
-
468
- def create_test_reserved_words_table
469
- ActiveRecord::Schema.define do
470
- suppress_messages do
471
- create_table :test_reserved_words do |t|
472
- t.string :varchar2
473
- t.integer :integer
281
+ after(:each) do
282
+ ActiveRecord::Schema.define do
283
+ suppress_messages do
284
+ drop_table "warehouse-things" rescue nil
285
+ drop_table "CamelCase" rescue nil
474
286
  end
475
287
  end
288
+ Object.send(:remove_const, "WarehouseThing") rescue nil
289
+ Object.send(:remove_const, "CamelCase") rescue nil
476
290
  end
477
- end
478
291
 
479
- after(:each) do
480
- ActiveRecord::Schema.define do
481
- suppress_messages do
482
- drop_table :test_reserved_words
292
+ it "should allow creation of a table with non alphanumeric characters" do
293
+ create_warehouse_things_table
294
+ class ::WarehouseThing < ActiveRecord::Base
295
+ set_table_name "warehouse-things"
483
296
  end
484
- end
485
- Object.send(:remove_const, "TestReservedWord")
486
- ActiveRecord::Base.table_name_prefix = nil
487
- end
488
297
 
489
- it "should allow creation of a table with oracle reserved words as column names" do
490
- create_test_reserved_words_table
491
- class ::TestReservedWord < ActiveRecord::Base; end
298
+ wh = WarehouseThing.create!(:name => "Foo", :foo => 2)
299
+ wh.id.should_not be_nil
492
300
 
493
- [:varchar2, :integer].each do |attr|
494
- TestReservedWord.columns_hash[attr.to_s].name.should == attr.to_s
301
+ @conn.tables.should include("warehouse-things")
495
302
  end
496
- end
497
-
498
- end
499
-
500
- describe "OracleEnhancedAdapter valid table names" do
501
- before(:all) do
502
- @adapter = ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter
503
- end
504
- it "should be valid with letters and digits" do
505
- @adapter.valid_table_name?("abc_123").should be_true
506
- end
507
-
508
- it "should be valid with schema name" do
509
- @adapter.valid_table_name?("abc_123.def_456").should be_true
510
- end
511
-
512
- it "should be valid with $ in name" do
513
- @adapter.valid_table_name?("sys.v$session").should be_true
514
- end
515
-
516
- it "should not be valid with two dots in name" do
517
- @adapter.valid_table_name?("abc_123.def_456.ghi_789").should be_false
518
- end
519
-
520
- it "should not be valid with invalid characters" do
521
- @adapter.valid_table_name?("warehouse-things").should be_false
522
- end
523
303
 
524
- end
304
+ it "should allow creation of a table with CamelCase name" do
305
+ create_camel_case_table
306
+ class ::CamelCase < ActiveRecord::Base
307
+ set_table_name "CamelCase"
308
+ end
525
309
 
526
- describe "OracleEnhancedAdapter table quoting" do
310
+ cc = CamelCase.create!(:name => "Foo", :foo => 2)
311
+ cc.id.should_not be_nil
312
+
313
+ @conn.tables.should include("CamelCase")
314
+ end
527
315
 
528
- before(:all) do
529
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
530
- @conn = ActiveRecord::Base.connection
531
316
  end
532
317
 
533
- def create_warehouse_things_table
534
- ActiveRecord::Schema.define do
535
- suppress_messages do
536
- create_table "warehouse-things" do |t|
537
- t.string :name
538
- t.integer :foo
539
- end
318
+ describe "access table over database link" do
319
+ before(:all) do
320
+ @db_link = "db_link"
321
+ @sys_conn = ActiveRecord::Base.oracle_enhanced_connection(SYSTEM_CONNECTION_PARAMS)
322
+ @sys_conn.drop_table :test_posts rescue nil
323
+ @sys_conn.create_table :test_posts do |t|
324
+ t.string :title
325
+ # cannot update LOBs over database link
326
+ t.string :body
327
+ t.timestamps
540
328
  end
541
- end
542
- end
543
-
544
- def create_camel_case_table
545
- ActiveRecord::Schema.define do
546
- suppress_messages do
547
- create_table "CamelCase" do |t|
548
- t.string :name
549
- t.integer :foo
550
- end
329
+ @db_link_username = SYSTEM_CONNECTION_PARAMS[:username]
330
+ @db_link_password = SYSTEM_CONNECTION_PARAMS[:password]
331
+ @db_link_database = SYSTEM_CONNECTION_PARAMS[:database]
332
+ @conn.execute "DROP DATABASE LINK #{@db_link}" rescue nil
333
+ @conn.execute "CREATE DATABASE LINK #{@db_link} CONNECT TO #{@db_link_username} IDENTIFIED BY #{@db_link_password} USING '#{@db_link_database}'"
334
+ @conn.execute "CREATE OR REPLACE SYNONYM test_posts FOR test_posts@#{@db_link}"
335
+ @conn.execute "CREATE OR REPLACE SYNONYM test_posts_seq FOR test_posts_seq@#{@db_link}"
336
+ class ::TestPost < ActiveRecord::Base
551
337
  end
338
+ TestPost.set_table_name "test_posts"
552
339
  end
553
- end
554
340
 
555
- after(:each) do
556
- ActiveRecord::Schema.define do
557
- suppress_messages do
558
- drop_table "warehouse-things" rescue nil
559
- drop_table "CamelCase" rescue nil
560
- end
341
+ after(:all) do
342
+ @conn.execute "DROP SYNONYM test_posts"
343
+ @conn.execute "DROP SYNONYM test_posts_seq"
344
+ @conn.execute "DROP DATABASE LINK #{@db_link}" rescue nil
345
+ @sys_conn.drop_table :test_posts rescue nil
346
+ Object.send(:remove_const, "TestPost") rescue nil
561
347
  end
562
- Object.send(:remove_const, "WarehouseThing") rescue nil
563
- Object.send(:remove_const, "CamelCase") rescue nil
564
- end
565
348
 
566
- it "should allow creation of a table with non alphanumeric characters" do
567
- create_warehouse_things_table
568
- class ::WarehouseThing < ActiveRecord::Base
569
- set_table_name "warehouse-things"
349
+ it "should verify database link" do
350
+ @conn.select_value("select * from dual@#{@db_link}") == 'X'
570
351
  end
571
352
 
572
- wh = WarehouseThing.create!(:name => "Foo", :foo => 2)
573
- wh.id.should_not be_nil
574
-
575
- @conn.tables.should include("warehouse-things")
576
- end
353
+ it "should get column names" do
354
+ TestPost.column_names.should == ["id", "title", "body", "created_at", "updated_at"]
355
+ end
577
356
 
578
- it "should allow creation of a table with CamelCase name" do
579
- create_camel_case_table
580
- class ::CamelCase < ActiveRecord::Base
581
- set_table_name "CamelCase"
357
+ it "should create record" do
358
+ p = TestPost.create(:title => "Title", :body => "Body")
359
+ p.id.should_not be_nil
360
+ TestPost.find(p.id).should_not be_nil
582
361
  end
583
362
 
584
- cc = CamelCase.create!(:name => "Foo", :foo => 2)
585
- cc.id.should_not be_nil
586
-
587
- @conn.tables.should include("CamelCase")
588
363
  end
589
364
 
590
365
  end