activerecord-oracle_enhanced-adapter 1.4.3 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +1162 -2
  3. data/README.md +567 -155
  4. data/VERSION +1 -1
  5. data/lib/active_record/connection_adapters/emulation/oracle_adapter.rb +3 -1
  6. data/lib/active_record/connection_adapters/oracle_enhanced/column.rb +19 -0
  7. data/lib/active_record/connection_adapters/oracle_enhanced/connection.rb +132 -0
  8. data/lib/active_record/connection_adapters/oracle_enhanced/context_index.rb +345 -0
  9. data/lib/active_record/connection_adapters/oracle_enhanced/database_limits.rb +52 -0
  10. data/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb +280 -0
  11. data/lib/active_record/connection_adapters/oracle_enhanced/database_tasks.rb +64 -0
  12. data/lib/active_record/connection_adapters/oracle_enhanced/dbms_output.rb +59 -0
  13. data/lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb +538 -0
  14. data/lib/active_record/connection_adapters/oracle_enhanced/jdbc_quoting.rb +38 -0
  15. data/lib/active_record/connection_adapters/oracle_enhanced/lob.rb +46 -0
  16. data/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb +435 -0
  17. data/lib/active_record/connection_adapters/oracle_enhanced/oci_quoting.rb +44 -0
  18. data/lib/active_record/connection_adapters/oracle_enhanced/procedures.rb +196 -0
  19. data/lib/active_record/connection_adapters/oracle_enhanced/quoting.rb +164 -0
  20. data/lib/active_record/connection_adapters/oracle_enhanced/schema_creation.rb +95 -0
  21. data/lib/active_record/connection_adapters/oracle_enhanced/schema_definitions.rb +79 -0
  22. data/lib/active_record/connection_adapters/oracle_enhanced/schema_dumper.rb +194 -0
  23. data/lib/active_record/connection_adapters/oracle_enhanced/schema_statements.rb +709 -0
  24. data/lib/active_record/connection_adapters/oracle_enhanced/schema_statements_ext.rb +28 -0
  25. data/lib/active_record/connection_adapters/oracle_enhanced/structure_dump.rb +353 -0
  26. data/lib/active_record/connection_adapters/oracle_enhanced/type_metadata.rb +33 -0
  27. data/lib/active_record/connection_adapters/oracle_enhanced/version.rb +3 -0
  28. data/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb +385 -1083
  29. data/lib/active_record/type/oracle_enhanced/boolean.rb +20 -0
  30. data/lib/active_record/type/oracle_enhanced/integer.rb +15 -0
  31. data/lib/active_record/type/oracle_enhanced/json.rb +10 -0
  32. data/lib/active_record/type/oracle_enhanced/national_character_string.rb +26 -0
  33. data/lib/active_record/type/oracle_enhanced/national_character_text.rb +36 -0
  34. data/lib/active_record/type/oracle_enhanced/raw.rb +25 -0
  35. data/lib/active_record/type/oracle_enhanced/string.rb +29 -0
  36. data/lib/active_record/type/oracle_enhanced/text.rb +32 -0
  37. data/lib/active_record/type/oracle_enhanced/timestampltz.rb +25 -0
  38. data/lib/active_record/type/oracle_enhanced/timestamptz.rb +25 -0
  39. data/lib/activerecord-oracle_enhanced-adapter.rb +5 -13
  40. data/spec/active_record/connection_adapters/{oracle_enhanced_emulate_oracle_adapter_spec.rb → emulation/oracle_adapter_spec.rb} +5 -4
  41. data/spec/active_record/connection_adapters/oracle_enhanced/connection_spec.rb +469 -0
  42. data/spec/active_record/connection_adapters/{oracle_enhanced_context_index_spec.rb → oracle_enhanced/context_index_spec.rb} +140 -128
  43. data/spec/active_record/connection_adapters/oracle_enhanced/database_tasks_spec.rb +112 -0
  44. data/spec/active_record/connection_adapters/{oracle_enhanced_dbms_output_spec.rb → oracle_enhanced/dbms_output_spec.rb} +13 -13
  45. data/spec/active_record/connection_adapters/oracle_enhanced/procedures_spec.rb +365 -0
  46. data/spec/active_record/connection_adapters/oracle_enhanced/quoting_spec.rb +196 -0
  47. data/spec/active_record/connection_adapters/oracle_enhanced/schema_dumper_spec.rb +492 -0
  48. data/spec/active_record/connection_adapters/oracle_enhanced/schema_statements_spec.rb +1433 -0
  49. data/spec/active_record/connection_adapters/oracle_enhanced/structure_dump_spec.rb +478 -0
  50. data/spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb +385 -550
  51. data/spec/active_record/connection_adapters/oracle_enhanced_data_types_spec.rb +92 -1249
  52. data/spec/active_record/oracle_enhanced/type/binary_spec.rb +119 -0
  53. data/spec/active_record/oracle_enhanced/type/boolean_spec.rb +208 -0
  54. data/spec/active_record/oracle_enhanced/type/dirty_spec.rb +139 -0
  55. data/spec/active_record/oracle_enhanced/type/float_spec.rb +48 -0
  56. data/spec/active_record/oracle_enhanced/type/integer_spec.rb +91 -0
  57. data/spec/active_record/oracle_enhanced/type/json_spec.rb +57 -0
  58. data/spec/active_record/oracle_enhanced/type/national_character_string_spec.rb +55 -0
  59. data/spec/active_record/oracle_enhanced/type/national_character_text_spec.rb +230 -0
  60. data/spec/active_record/oracle_enhanced/type/raw_spec.rb +122 -0
  61. data/spec/active_record/oracle_enhanced/type/text_spec.rb +229 -0
  62. data/spec/active_record/oracle_enhanced/type/timestamp_spec.rb +75 -0
  63. data/spec/spec_config.yaml.template +11 -0
  64. data/spec/spec_helper.rb +100 -93
  65. data/spec/support/alter_system_set_open_cursors.sql +1 -0
  66. data/spec/support/alter_system_user_password.sql +2 -0
  67. data/spec/support/create_oracle_enhanced_users.sql +31 -0
  68. metadata +105 -152
  69. data/.rspec +0 -2
  70. data/Gemfile +0 -52
  71. data/RUNNING_TESTS.md +0 -45
  72. data/Rakefile +0 -59
  73. data/activerecord-oracle_enhanced-adapter.gemspec +0 -130
  74. data/lib/active_record/connection_adapters/oracle_enhanced.rake +0 -105
  75. data/lib/active_record/connection_adapters/oracle_enhanced_activerecord_patches.rb +0 -41
  76. data/lib/active_record/connection_adapters/oracle_enhanced_base_ext.rb +0 -121
  77. data/lib/active_record/connection_adapters/oracle_enhanced_column.rb +0 -151
  78. data/lib/active_record/connection_adapters/oracle_enhanced_connection.rb +0 -119
  79. data/lib/active_record/connection_adapters/oracle_enhanced_context_index.rb +0 -359
  80. data/lib/active_record/connection_adapters/oracle_enhanced_core_ext.rb +0 -25
  81. data/lib/active_record/connection_adapters/oracle_enhanced_cpk.rb +0 -21
  82. data/lib/active_record/connection_adapters/oracle_enhanced_dirty.rb +0 -46
  83. data/lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb +0 -572
  84. data/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb +0 -497
  85. data/lib/active_record/connection_adapters/oracle_enhanced_procedures.rb +0 -260
  86. data/lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb +0 -227
  87. data/lib/active_record/connection_adapters/oracle_enhanced_schema_dumper.rb +0 -260
  88. data/lib/active_record/connection_adapters/oracle_enhanced_schema_statements.rb +0 -428
  89. data/lib/active_record/connection_adapters/oracle_enhanced_schema_statements_ext.rb +0 -258
  90. data/lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb +0 -294
  91. data/lib/active_record/connection_adapters/oracle_enhanced_tasks.rb +0 -17
  92. data/lib/active_record/connection_adapters/oracle_enhanced_version.rb +0 -1
  93. data/spec/active_record/connection_adapters/oracle_enhanced_connection_spec.rb +0 -334
  94. data/spec/active_record/connection_adapters/oracle_enhanced_core_ext_spec.rb +0 -19
  95. data/spec/active_record/connection_adapters/oracle_enhanced_cpk_spec.rb +0 -113
  96. data/spec/active_record/connection_adapters/oracle_enhanced_dirty_spec.rb +0 -141
  97. data/spec/active_record/connection_adapters/oracle_enhanced_procedures_spec.rb +0 -378
  98. data/spec/active_record/connection_adapters/oracle_enhanced_schema_dump_spec.rb +0 -440
  99. data/spec/active_record/connection_adapters/oracle_enhanced_schema_statements_spec.rb +0 -1400
  100. data/spec/active_record/connection_adapters/oracle_enhanced_structure_dump_spec.rb +0 -339
@@ -1,440 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "OracleEnhancedAdapter schema dump" do
4
- include SchemaSpecHelper
5
-
6
- before(:all) do
7
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
8
- @conn = ActiveRecord::Base.connection
9
- @oracle11g = !! @conn.select_value("SELECT * FROM v$version WHERE banner LIKE 'Oracle%11g%'")
10
- end
11
-
12
- def standard_dump(options = {})
13
- stream = StringIO.new
14
- ActiveRecord::SchemaDumper.ignore_tables = options[:ignore_tables]||[]
15
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
16
- stream.string
17
- end
18
-
19
- def create_test_posts_table(options = {})
20
- options.merge! :force => true
21
- schema_define do
22
- create_table :test_posts, options do |t|
23
- t.string :title
24
- t.timestamps
25
- end
26
- add_index :test_posts, :title
27
- end
28
- end
29
-
30
- def drop_test_posts_table
31
- schema_define do
32
- drop_table :test_posts
33
- end
34
- rescue
35
- nil
36
- end
37
-
38
- describe "tables" do
39
- after(:each) do
40
- drop_test_posts_table
41
- end
42
-
43
- it "should not include ignored table names in schema dump" do
44
- create_test_posts_table
45
- standard_dump(:ignore_tables => %w(test_posts)).should_not =~ /create_table "test_posts"/
46
- end
47
-
48
- it "should not include ignored table regexes in schema dump" do
49
- create_test_posts_table
50
- standard_dump(:ignore_tables => [ /test_posts/i ]).should_not =~ /create_table "test_posts"/
51
- end
52
-
53
- end
54
-
55
- describe "dumping default values" do
56
- before :each do
57
- schema_define do
58
- create_table "test_defaults", :force => true do |t|
59
- t.string "regular", :default => "c"
60
- t.string "special_c", :default => "\n"
61
- end
62
- end
63
- end
64
-
65
- after(:each) do
66
- schema_define do
67
- drop_table "test_defaults"
68
- end
69
- end
70
-
71
- it "should be able to dump default values using special characters" do
72
- standard_dump.should =~ /t.string \"special_c\", :default => "\\n"/
73
- end
74
- end
75
- describe "table prefixes and suffixes" do
76
- after(:each) do
77
- drop_test_posts_table
78
- @conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if @conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
79
- ActiveRecord::Base.table_name_prefix = ''
80
- ActiveRecord::Base.table_name_suffix = ''
81
- end
82
-
83
- it "should remove table prefix in schema dump" do
84
- ActiveRecord::Base.table_name_prefix = 'xxx_'
85
- create_test_posts_table
86
- standard_dump.should =~ /create_table "test_posts".*add_index "test_posts"/m
87
- end
88
-
89
- it "should remove table prefix with $ sign in schema dump" do
90
- ActiveRecord::Base.table_name_prefix = 'xxx$'
91
- create_test_posts_table
92
- standard_dump.should =~ /create_table "test_posts".*add_index "test_posts"/m
93
- end
94
-
95
- it "should remove table suffix in schema dump" do
96
- ActiveRecord::Base.table_name_suffix = '_xxx'
97
- create_test_posts_table
98
- standard_dump.should =~ /create_table "test_posts".*add_index "test_posts"/m
99
- end
100
-
101
- it "should remove table suffix with $ sign in schema dump" do
102
- ActiveRecord::Base.table_name_suffix = '$xxx'
103
- create_test_posts_table
104
- standard_dump.should =~ /create_table "test_posts".*add_index "test_posts"/m
105
- end
106
-
107
- it "should not include schema_migrations table with prefix in schema dump" do
108
- ActiveRecord::Base.table_name_prefix = 'xxx_'
109
- @conn.initialize_schema_migrations_table
110
- standard_dump.should_not =~ /schema_migrations/
111
- end
112
-
113
- it "should not include schema_migrations table with suffix in schema dump" do
114
- ActiveRecord::Base.table_name_suffix = '_xxx'
115
- @conn.initialize_schema_migrations_table
116
- standard_dump.should_not =~ /schema_migrations/
117
- end
118
-
119
- end
120
-
121
- describe "table with non-default primary key" do
122
- after(:each) do
123
- drop_test_posts_table
124
- end
125
-
126
- it "should include non-default primary key in schema dump" do
127
- create_test_posts_table(:primary_key => 'post_id')
128
- standard_dump.should =~ /create_table "test_posts", :primary_key => "post_id"/
129
- end
130
-
131
- end
132
-
133
- describe "table with primary key trigger" do
134
-
135
- after(:each) do
136
- drop_test_posts_table
137
- @conn.clear_prefetch_primary_key
138
- end
139
-
140
- it "should include primary key trigger in schema dump" do
141
- create_test_posts_table(:primary_key_trigger => true)
142
- standard_dump.should =~ /create_table "test_posts".*add_primary_key_trigger "test_posts"/m
143
- end
144
-
145
- it "should include primary key trigger with non-default primary key in schema dump" do
146
- create_test_posts_table(:primary_key_trigger => true, :primary_key => 'post_id')
147
- standard_dump.should =~ /create_table "test_posts", :primary_key => "post_id".*add_primary_key_trigger "test_posts", :primary_key => "post_id"/m
148
- end
149
-
150
- end
151
-
152
- describe "foreign key constraints" do
153
- before(:all) do
154
- schema_define do
155
- create_table :test_posts, :force => true do |t|
156
- t.string :title
157
- end
158
- create_table :test_comments, :force => true do |t|
159
- t.string :body, :limit => 4000
160
- t.references :test_post
161
- end
162
- end
163
- end
164
-
165
- after(:each) do
166
- schema_define do
167
- remove_foreign_key :test_comments, :test_posts rescue nil
168
- remove_foreign_key :test_comments, :name => 'comments_posts_baz_fooz_fk' rescue nil
169
- end
170
- end
171
- after(:all) do
172
- schema_define do
173
- drop_table :test_comments rescue nil
174
- drop_table :test_posts rescue nil
175
- end
176
- end
177
-
178
- it "should include foreign key in schema dump" do
179
- schema_define do
180
- add_foreign_key :test_comments, :test_posts
181
- end
182
- standard_dump.should =~ /add_foreign_key "test_comments", "test_posts", :name => "test_comments_test_post_id_fk"/
183
- end
184
-
185
- it "should include foreign key with delete dependency in schema dump" do
186
- schema_define do
187
- add_foreign_key :test_comments, :test_posts, :dependent => :delete
188
- end
189
- standard_dump.should =~ /add_foreign_key "test_comments", "test_posts", :name => "test_comments_test_post_id_fk", :dependent => :delete/
190
- end
191
-
192
- it "should include foreign key with nullify dependency in schema dump" do
193
- schema_define do
194
- add_foreign_key :test_comments, :test_posts, :dependent => :nullify
195
- end
196
- standard_dump.should =~ /add_foreign_key "test_comments", "test_posts", :name => "test_comments_test_post_id_fk", :dependent => :nullify/
197
- end
198
-
199
- it "should not include foreign keys on ignored table names in schema dump" do
200
- schema_define do
201
- add_foreign_key :test_comments, :test_posts
202
- end
203
- standard_dump(:ignore_tables => %w(test_comments)).should_not =~ /add_foreign_key "test_comments"/
204
- end
205
-
206
- it "should not include foreign keys on ignored table regexes in schema dump" do
207
- schema_define do
208
- add_foreign_key :test_comments, :test_posts
209
- end
210
- standard_dump(:ignore_tables => [ /test_comments/i ]).should_not =~ /add_foreign_key "test_comments"/
211
- end
212
-
213
- it "should include foreign keys referencing ignored table names in schema dump" do
214
- schema_define do
215
- add_foreign_key :test_comments, :test_posts
216
- end
217
- standard_dump(:ignore_tables => %w(test_posts)).should =~ /add_foreign_key "test_comments"/
218
- end
219
-
220
- it "should include foreign keys referencing ignored table regexes in schema dump" do
221
- schema_define do
222
- add_foreign_key :test_comments, :test_posts
223
- end
224
- standard_dump(:ignore_tables => [ /test_posts/i ]).should =~ /add_foreign_key "test_comments"/
225
- end
226
-
227
- it "should include composite foreign keys" do
228
- schema_define do
229
- add_column :test_posts, :baz_id, :integer
230
- add_column :test_posts, :fooz_id, :integer
231
-
232
- execute <<-SQL
233
- ALTER TABLE TEST_POSTS
234
- ADD CONSTRAINT UK_FOOZ_BAZ UNIQUE (BAZ_ID,FOOZ_ID)
235
- SQL
236
-
237
- add_column :test_comments, :baz_id, :integer
238
- add_column :test_comments, :fooz_id, :integer
239
-
240
- add_foreign_key :test_comments, :test_posts, :columns => ["baz_id", "fooz_id"], :name => 'comments_posts_baz_fooz_fk'
241
- end
242
- standard_dump.should =~ /add_foreign_key "test_comments", "test_posts", :columns => \["baz_id", "fooz_id"\], :name => "comments_posts_baz_fooz_fk"/
243
- end
244
- it "should include foreign keys following all tables" do
245
- # if foreign keys preceed declaration of all tables
246
- # it can cause problems when using db:test rake tasks
247
- schema_define do
248
- add_foreign_key :test_comments, :test_posts
249
- end
250
- dump = standard_dump
251
- dump.rindex("create_table").should < dump.index("add_foreign_key")
252
- end
253
- end
254
-
255
- describe "synonyms" do
256
- after(:each) do
257
- schema_define do
258
- remove_synonym :test_synonym
259
- end
260
- end
261
-
262
- it "should include synonym to other schema table in schema dump" do
263
- schema_define do
264
- add_synonym :test_synonym, "schema_name.table_name", :force => true
265
- end
266
- standard_dump.should =~ /add_synonym "test_synonym", "schema_name.table_name", :force => true/
267
- end
268
-
269
- it "should include synonym to other database table in schema dump" do
270
- schema_define do
271
- add_synonym :test_synonym, "table_name@link_name", :force => true
272
- end
273
- standard_dump.should =~ /add_synonym "test_synonym", "table_name@link_name(\.[-A-Za-z0-9_]+)*", :force => true/
274
- end
275
-
276
- it "should not include ignored table names in schema dump" do
277
- schema_define do
278
- add_synonym :test_synonym, "schema_name.table_name", :force => true
279
- end
280
- standard_dump(:ignore_tables => %w(test_synonym)).should_not =~ /add_synonym "test_synonym"/
281
- end
282
-
283
- it "should not include ignored table regexes in schema dump" do
284
- schema_define do
285
- add_synonym :test_synonym, "schema_name.table_name", :force => true
286
- end
287
- standard_dump(:ignore_tables => [ /test_synonym/i ]).should_not =~ /add_synonym "test_synonym"/
288
- end
289
-
290
- it "should include synonyms to ignored table regexes in schema dump" do
291
- schema_define do
292
- add_synonym :test_synonym, "schema_name.table_name", :force => true
293
- end
294
- standard_dump(:ignore_tables => [ /table_name/i ]).should =~ /add_synonym "test_synonym"/
295
- end
296
-
297
- end
298
-
299
- describe "temporary tables" do
300
- after(:each) do
301
- drop_test_posts_table
302
- end
303
-
304
- it "should include temporary options" do
305
- create_test_posts_table(:temporary => true)
306
- standard_dump.should =~ /create_table "test_posts", :temporary => true/
307
- end
308
- end
309
-
310
- describe "indexes" do
311
- after(:each) do
312
- drop_test_posts_table
313
- end
314
-
315
- it "should not specify default tablespace in add index" do
316
- create_test_posts_table
317
- standard_dump.should =~ /add_index "test_posts", \["title"\], :name => "index_test_posts_on_title"$/
318
- end
319
-
320
- it "should specify non-default tablespace in add index" do
321
- tablespace_name = @conn.default_tablespace
322
- @conn.stub!(:default_tablespace).and_return('dummy')
323
- create_test_posts_table
324
- standard_dump.should =~ /add_index "test_posts", \["title"\], :name => "index_test_posts_on_title", :tablespace => "#{tablespace_name}"$/
325
- end
326
-
327
- it "should create and dump function-based indexes" do
328
- create_test_posts_table
329
- @conn.add_index :test_posts, "NVL(created_at, updated_at)", :name => "index_test_posts_cr_upd_at"
330
- standard_dump.should =~ /add_index "test_posts", \["NVL\(\\"CREATED_AT\\",\\"UPDATED_AT\\"\)"\], :name => "index_test_posts_cr_upd_at"$/
331
- end
332
-
333
- end
334
-
335
- describe "materialized views" do
336
- after(:each) do
337
- @conn.execute "DROP MATERIALIZED VIEW test_posts_mv" rescue nil
338
- drop_test_posts_table
339
- end
340
-
341
- it "should not include materialized views in schema dump" do
342
- create_test_posts_table
343
- @conn.execute "CREATE MATERIALIZED VIEW test_posts_mv AS SELECT * FROM test_posts"
344
- standard_dump.should_not =~ /create_table "test_posts_mv"/
345
- end
346
- end
347
-
348
- describe 'virtual columns' do
349
- before(:all) do
350
- if @oracle11g
351
- schema_define do
352
- create_table :test_names, :force => true do |t|
353
- t.string :first_name
354
- t.string :last_name
355
- t.virtual :full_name, :as => "first_name || ', ' || last_name"
356
- t.virtual :short_name, :as => "COALESCE(first_name, last_name)", :type => :string, :limit => 300
357
- t.virtual :abbrev_name, :as => "SUBSTR(first_name,1,50) || ' ' || SUBSTR(last_name,1,1) || '.'", :type => "VARCHAR(100)"
358
- t.virtual :name_ratio, :as=>'(LENGTH(first_name)*10/LENGTH(last_name)*10)'
359
- t.column :full_name_length, :virtual, :as => "length(first_name || ', ' || last_name)", :type => :integer
360
- t.virtual :field_with_leading_space, :as => "' ' || first_name || ' '", :limit => 300, :type => :string
361
- end
362
- end
363
- else
364
- pending "Not supported in this database version"
365
- end
366
- end
367
-
368
- before(:each) do
369
- if @oracle11g
370
- class ::TestName < ActiveRecord::Base
371
- if self.respond_to?(:table_name=)
372
- self.table_name = "test_names"
373
- else
374
- set_table_name "test_names"
375
- end
376
- end
377
- end
378
- end
379
-
380
- after(:all) do
381
- if @oracle11g
382
- schema_define do
383
- drop_table :test_names
384
- end
385
- end
386
- end
387
-
388
- it 'should dump correctly' do
389
- standard_dump.should =~ /t\.virtual "full_name",(\s*):limit => 512,(\s*):as => "\\"FIRST_NAME\\"\|\|', '\|\|\\"LAST_NAME\\"",(\s*):type => :string/
390
- standard_dump.should =~ /t\.virtual "short_name",(\s*):limit => 300,(\s*):as =>(.*),(\s*):type => :string/
391
- standard_dump.should =~ /t\.virtual "full_name_length",(\s*):precision => 38,(\s*):scale => 0,(\s*):as =>(.*),(\s*):type => :integer/
392
- standard_dump.should =~ /t\.virtual "name_ratio",(\s*):as =>(.*)\"$/ # no :type
393
- standard_dump.should =~ /t\.virtual "abbrev_name",(\s*):limit => 100,(\s*):as =>(.*),(\s*):type => :string/
394
- standard_dump.should =~ /t\.virtual "field_with_leading_space",(\s*):limit => 300,(\s*):as => "' '\|\|\\"FIRST_NAME\\"\|\|' '",(\s*):type => :string/
395
- end
396
-
397
- context 'with column cache' do
398
- before(:all) do
399
- @old_cache = ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.cache_columns
400
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.cache_columns = true
401
- end
402
- after(:all) do
403
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.cache_columns = @old_cache
404
- end
405
- it 'should not change column defaults after several dumps' do
406
- col = TestName.columns.detect{|c| c.name == 'full_name'}
407
- col.should_not be_nil
408
- col.virtual_column_data_default.should_not =~ /:as/
409
-
410
- standard_dump
411
- col.virtual_column_data_default.should_not =~ /:as/
412
-
413
- standard_dump
414
- col.virtual_column_data_default.should_not =~ /:as/
415
- end
416
- end
417
-
418
- context "with index on virtual column" do
419
- before(:all) do
420
- if @oracle11g
421
- schema_define do
422
- add_index 'test_names', 'field_with_leading_space', :name => "index_on_virtual_col"
423
- end
424
- end
425
- end
426
- after(:all) do
427
- if @oracle11g
428
- schema_define do
429
- remove_index 'test_names', :name => 'index_on_virtual_col'
430
- end
431
- end
432
- end
433
- it 'should dump correctly' do
434
- standard_dump.should_not =~ /add_index "test_names".+FIRST_NAME.+$/
435
- standard_dump.should =~ /add_index "test_names".+field_with_leading_space.+$/
436
- end
437
- end
438
- end
439
-
440
- end
@@ -1,1400 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "OracleEnhancedAdapter schema definition" do
4
- include SchemaSpecHelper
5
- include LoggerSpecHelper
6
-
7
- before(:all) do
8
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
9
- @oracle11g = !! ActiveRecord::Base.connection.select_value("SELECT * FROM v$version WHERE banner LIKE 'Oracle%11g%'")
10
- end
11
-
12
- describe "table and sequence creation with non-default primary key" do
13
-
14
- before(:all) do
15
- @conn = ActiveRecord::Base.connection
16
- schema_define do
17
- create_table :keyboards, :force => true, :id => false do |t|
18
- t.primary_key :key_number
19
- t.string :name
20
- end
21
- create_table :id_keyboards, :force => true do |t|
22
- t.string :name
23
- end
24
- end
25
- class ::Keyboard < ActiveRecord::Base
26
- if self.respond_to?(:primary_key=)
27
- self.primary_key = :key_number
28
- else
29
- set_primary_key :key_number
30
- end
31
- end
32
- class ::IdKeyboard < ActiveRecord::Base
33
- end
34
- end
35
-
36
- after(:all) do
37
- schema_define do
38
- drop_table :keyboards
39
- drop_table :id_keyboards
40
- end
41
- Object.send(:remove_const, "Keyboard")
42
- Object.send(:remove_const, "IdKeyboard")
43
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
44
- end
45
-
46
- it "should create sequence for non-default primary key" do
47
- ActiveRecord::Base.connection.next_sequence_value(Keyboard.sequence_name).should_not be_nil
48
- end
49
-
50
- it "should create sequence for default primary key" do
51
- ActiveRecord::Base.connection.next_sequence_value(IdKeyboard.sequence_name).should_not be_nil
52
- end
53
- end
54
-
55
- describe "sequence creation parameters" do
56
-
57
- def create_test_employees_table(sequence_start_value = nil)
58
- schema_define do
59
- create_table :test_employees, sequence_start_value ? {:sequence_start_value => sequence_start_value} : {} do |t|
60
- t.string :first_name
61
- t.string :last_name
62
- end
63
- end
64
- end
65
-
66
- def save_default_sequence_start_value
67
- @saved_sequence_start_value = ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value
68
- end
69
-
70
- def restore_default_sequence_start_value
71
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value = @saved_sequence_start_value
72
- end
73
-
74
- before(:all) do
75
- @conn = ActiveRecord::Base.connection
76
- end
77
-
78
- before(:each) do
79
- save_default_sequence_start_value
80
- end
81
-
82
- after(:each) do
83
- restore_default_sequence_start_value
84
- schema_define do
85
- drop_table :test_employees
86
- end
87
- Object.send(:remove_const, "TestEmployee")
88
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
89
- end
90
-
91
- it "should use default sequence start value 10000" do
92
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value.should == 10000
93
-
94
- create_test_employees_table
95
- class ::TestEmployee < ActiveRecord::Base; end
96
-
97
- employee = TestEmployee.create!
98
- employee.id.should == 10000
99
- end
100
-
101
- it "should use specified default sequence start value" do
102
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value = 1
103
-
104
- create_test_employees_table
105
- class ::TestEmployee < ActiveRecord::Base; end
106
-
107
- employee = TestEmployee.create!
108
- employee.id.should == 1
109
- end
110
-
111
- it "should use sequence start value from table definition" do
112
- create_test_employees_table(10)
113
- class ::TestEmployee < ActiveRecord::Base; end
114
-
115
- employee = TestEmployee.create!
116
- employee.id.should == 10
117
- end
118
-
119
- it "should use sequence start value and other options from table definition" do
120
- create_test_employees_table("100 NOCACHE INCREMENT BY 10")
121
- class ::TestEmployee < ActiveRecord::Base; end
122
-
123
- employee = TestEmployee.create!
124
- employee.id.should == 100
125
- employee = TestEmployee.create!
126
- employee.id.should == 110
127
- end
128
-
129
- end
130
-
131
- describe "create table with primary key trigger" do
132
- def create_table_with_trigger(options = {})
133
- options.merge! :primary_key_trigger => true, :force => true
134
- schema_define do
135
- create_table :test_employees, options do |t|
136
- t.string :first_name
137
- t.string :last_name
138
- end
139
- end
140
- end
141
-
142
- def create_table_and_separately_trigger(options = {})
143
- options.merge! :force => true
144
- schema_define do
145
- create_table :test_employees, options do |t|
146
- t.string :first_name
147
- t.string :last_name
148
- end
149
- add_primary_key_trigger :test_employees, options
150
- end
151
- end
152
-
153
- def drop_table_with_trigger(options = {})
154
- seq_name = options[:sequence_name]
155
- schema_define do
156
- drop_table :test_employees, (seq_name ? {:sequence_name => seq_name} : {})
157
- end
158
- Object.send(:remove_const, "TestEmployee")
159
- @conn.clear_prefetch_primary_key
160
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
161
- end
162
-
163
- describe "with default primary key" do
164
- before(:all) do
165
- @conn = ActiveRecord::Base.connection
166
- create_table_with_trigger
167
- class ::TestEmployee < ActiveRecord::Base
168
- end
169
- end
170
-
171
- after(:all) do
172
- drop_table_with_trigger
173
- end
174
-
175
- it "should populate primary key using trigger" do
176
- lambda do
177
- @conn.execute "INSERT INTO test_employees (first_name) VALUES ('Raimonds')"
178
- end.should_not raise_error
179
- end
180
-
181
- it "should return new key value using connection insert method" do
182
- insert_id = @conn.insert("INSERT INTO test_employees (first_name) VALUES ('Raimonds')", nil, "id")
183
- @conn.select_value("SELECT test_employees_seq.currval FROM dual").should == insert_id
184
- end
185
-
186
- it "should create new record for model" do
187
- e = TestEmployee.create!(:first_name => 'Raimonds')
188
- @conn.select_value("SELECT test_employees_seq.currval FROM dual").should == e.id
189
- end
190
-
191
- it "should not generate NoMethodError for :returning_id:Symbol" do
192
- set_logger
193
- @conn.reconnect! unless @conn.active?
194
- insert_id = @conn.insert("INSERT INTO test_employees (first_name) VALUES ('Yasuo')", nil, "id")
195
- @logger.output(:error).should_not match(/^Could not log "sql.active_record" event. NoMethodError: undefined method `name' for :returning_id:Symbol/)
196
- clear_logger
197
- end
198
-
199
- end
200
-
201
- describe "with separate creation of primary key trigger" do
202
- before(:all) do
203
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
204
- @conn = ActiveRecord::Base.connection
205
- create_table_and_separately_trigger
206
- class ::TestEmployee < ActiveRecord::Base
207
- end
208
- end
209
-
210
- after(:all) do
211
- drop_table_with_trigger
212
- end
213
-
214
- it "should populate primary key using trigger" do
215
- lambda do
216
- @conn.execute "INSERT INTO test_employees (first_name) VALUES ('Raimonds')"
217
- end.should_not raise_error
218
- end
219
-
220
- it "should return new key value using connection insert method" do
221
- insert_id = @conn.insert("INSERT INTO test_employees (first_name) VALUES ('Raimonds')", nil, "id")
222
- @conn.select_value("SELECT test_employees_seq.currval FROM dual").should == insert_id
223
- end
224
-
225
- it "should create new record for model" do
226
- e = TestEmployee.create!(:first_name => 'Raimonds')
227
- @conn.select_value("SELECT test_employees_seq.currval FROM dual").should == e.id
228
- end
229
- end
230
-
231
- describe "with non-default primary key and non-default sequence name" do
232
- before(:all) do
233
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
234
- @conn = ActiveRecord::Base.connection
235
- @primary_key = "employee_id"
236
- @sequence_name = "test_employees_s"
237
- create_table_with_trigger(:primary_key => @primary_key, :sequence_name => @sequence_name)
238
- class ::TestEmployee < ActiveRecord::Base
239
- if self.respond_to?(:primary_key=)
240
- self.primary_key = "employee_id"
241
- else
242
- set_primary_key "employee_id"
243
- end
244
- end
245
- end
246
-
247
- after(:all) do
248
- drop_table_with_trigger(:sequence_name => @sequence_name)
249
- end
250
-
251
- it "should populate primary key using trigger" do
252
- lambda do
253
- @conn.execute "INSERT INTO test_employees (first_name) VALUES ('Raimonds')"
254
- end.should_not raise_error
255
- end
256
-
257
- it "should return new key value using connection insert method" do
258
- insert_id = @conn.insert("INSERT INTO test_employees (first_name) VALUES ('Raimonds')", nil, @primary_key)
259
- @conn.select_value("SELECT #{@sequence_name}.currval FROM dual").should == insert_id
260
- end
261
-
262
- it "should create new record for model with autogenerated sequence option" do
263
- e = TestEmployee.create!(:first_name => 'Raimonds')
264
- @conn.select_value("SELECT #{@sequence_name}.currval FROM dual").should == e.id
265
- end
266
- end
267
-
268
- describe "with non-default sequence name and non-default trigger name" do
269
- before(:all) do
270
- ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
271
- @conn = ActiveRecord::Base.connection
272
- @sequence_name = "test_employees_s"
273
- create_table_with_trigger(:sequence_name => @sequence_name, :trigger_name => "test_employees_t1")
274
- class ::TestEmployee < ActiveRecord::Base
275
- if self.respond_to?(:sequence_name=)
276
- self.sequence_name = :autogenerated
277
- else
278
- set_sequence_name :autogenerated
279
- end
280
- end
281
- end
282
-
283
- after(:all) do
284
- drop_table_with_trigger(:sequence_name => @sequence_name)
285
- end
286
-
287
- it "should populate primary key using trigger" do
288
- lambda do
289
- @conn.execute "INSERT INTO test_employees (first_name) VALUES ('Raimonds')"
290
- end.should_not raise_error
291
- end
292
-
293
- it "should return new key value using connection insert method" do
294
- insert_id = @conn.insert("INSERT INTO test_employees (first_name) VALUES ('Raimonds')", nil, "id")
295
- @conn.select_value("SELECT #{@sequence_name}.currval FROM dual").should == insert_id
296
- end
297
-
298
- it "should create new record for model with autogenerated sequence option" do
299
- e = TestEmployee.create!(:first_name => 'Raimonds')
300
- @conn.select_value("SELECT #{@sequence_name}.currval FROM dual").should == e.id
301
- end
302
- end
303
-
304
- end
305
-
306
- describe "table and column comments" do
307
-
308
- def create_test_employees_table(table_comment=nil, column_comments={})
309
- schema_define do
310
- create_table :test_employees, :comment => table_comment do |t|
311
- t.string :first_name, :comment => column_comments[:first_name]
312
- t.string :last_name, :comment => column_comments[:last_name]
313
- end
314
- end
315
- end
316
-
317
- before(:all) do
318
- @conn = ActiveRecord::Base.connection
319
- end
320
-
321
- after(:each) do
322
- schema_define do
323
- drop_table :test_employees
324
- end
325
- Object.send(:remove_const, "TestEmployee")
326
- ActiveRecord::Base.table_name_prefix = ''
327
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
328
- end
329
-
330
- it "should create table with table comment" do
331
- table_comment = "Test Employees"
332
- create_test_employees_table(table_comment)
333
- class ::TestEmployee < ActiveRecord::Base; end
334
-
335
- @conn.table_comment("test_employees").should == table_comment
336
- TestEmployee.table_comment.should == table_comment
337
- end
338
-
339
- it "should create table with columns comment" do
340
- column_comments = {:first_name => "Given Name", :last_name => "Surname"}
341
- create_test_employees_table(nil, column_comments)
342
- class ::TestEmployee < ActiveRecord::Base; end
343
-
344
- [:first_name, :last_name].each do |attr|
345
- @conn.column_comment("test_employees", attr.to_s).should == column_comments[attr]
346
- end
347
- [:first_name, :last_name].each do |attr|
348
- TestEmployee.columns_hash[attr.to_s].comment.should == column_comments[attr]
349
- end
350
- end
351
-
352
- it "should create table with table and columns comment and custom table name prefix" do
353
- ActiveRecord::Base.table_name_prefix = "xxx_"
354
- table_comment = "Test Employees"
355
- column_comments = {:first_name => "Given Name", :last_name => "Surname"}
356
- create_test_employees_table(table_comment, column_comments)
357
- class ::TestEmployee < ActiveRecord::Base; end
358
-
359
- @conn.table_comment(TestEmployee.table_name).should == table_comment
360
- TestEmployee.table_comment.should == table_comment
361
- [:first_name, :last_name].each do |attr|
362
- @conn.column_comment(TestEmployee.table_name, attr.to_s).should == column_comments[attr]
363
- end
364
- [:first_name, :last_name].each do |attr|
365
- TestEmployee.columns_hash[attr.to_s].comment.should == column_comments[attr]
366
- end
367
- end
368
-
369
- end
370
-
371
- describe "rename tables and sequences" do
372
- before(:each) do
373
- @conn = ActiveRecord::Base.connection
374
- schema_define do
375
- drop_table :test_employees rescue nil
376
- drop_table :new_test_employees rescue nil
377
- drop_table :test_employees_no_primary_key rescue nil
378
-
379
- create_table :test_employees do |t|
380
- t.string :first_name
381
- t.string :last_name
382
- end
383
-
384
- create_table :test_employees_no_pkey, :id => false do |t|
385
- t.string :first_name
386
- t.string :last_name
387
- end
388
- end
389
- end
390
-
391
- after(:each) do
392
- schema_define do
393
- drop_table :test_employees rescue nil
394
- drop_table :new_test_employees rescue nil
395
- drop_table :test_employees_no_pkey rescue nil
396
- drop_table :new_test_employees_no_pkey rescue nil
397
- end
398
- end
399
-
400
- it "should rename table name with new one" do
401
- lambda do
402
- @conn.rename_table("test_employees","new_test_employees")
403
- end.should_not raise_error
404
- end
405
-
406
- it "should raise error when new table name length is too long" do
407
- lambda do
408
- @conn.rename_table("test_employees","a"*31)
409
- end.should raise_error
410
- end
411
-
412
- it "should raise error when new sequence name length is too long" do
413
- lambda do
414
- @conn.rename_table("test_employees","a"*27)
415
- end.should raise_error
416
- end
417
-
418
- it "should rename table when table has no primary key and sequence" do
419
- lambda do
420
- @conn.rename_table("test_employees_no_pkey","new_test_employees_no_pkey")
421
- end.should_not raise_error
422
- end
423
-
424
- end
425
-
426
- describe "create triggers" do
427
-
428
- before(:all) do
429
- @conn = ActiveRecord::Base.connection
430
- schema_define do
431
- create_table :test_employees do |t|
432
- t.string :first_name
433
- t.string :last_name
434
- end
435
- end
436
- class ::TestEmployee < ActiveRecord::Base; end
437
- end
438
-
439
- after(:all) do
440
- schema_define do
441
- drop_table :test_employees
442
- end
443
- Object.send(:remove_const, "TestEmployee")
444
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
445
- end
446
-
447
- it "should create table trigger with :new reference" do
448
- lambda do
449
- @conn.execute <<-SQL
450
- CREATE OR REPLACE TRIGGER test_employees_pkt
451
- BEFORE INSERT ON test_employees FOR EACH ROW
452
- BEGIN
453
- IF inserting THEN
454
- IF :new.id IS NULL THEN
455
- SELECT test_employees_seq.NEXTVAL INTO :new.id FROM dual;
456
- END IF;
457
- END IF;
458
- END;
459
- SQL
460
- end.should_not raise_error
461
- end
462
- end
463
-
464
- describe "add index" do
465
- before(:all) do
466
- @conn = ActiveRecord::Base.connection
467
- end
468
-
469
- it "should return default index name if it is not larger than 30 characters" do
470
- @conn.index_name("employees", :column => "first_name").should == "index_employees_on_first_name"
471
- end
472
-
473
- it "should return shortened index name by removing 'index', 'on' and 'and' keywords" do
474
- @conn.index_name("employees", :column => ["first_name", "email"]).should == "i_employees_first_name_email"
475
- end
476
-
477
- it "should return shortened index name by shortening table and column names" do
478
- @conn.index_name("employees", :column => ["first_name", "last_name"]).should == "i_emp_fir_nam_las_nam"
479
- end
480
-
481
- it "should raise error if too large index name cannot be shortened" do
482
- @conn.index_name("test_employees", :column => ["first_name", "middle_name", "last_name"]).should ==
483
- 'i'+Digest::SHA1.hexdigest("index_test_employees_on_first_name_and_middle_name_and_last_name")[0,29]
484
- end
485
-
486
- end
487
-
488
- describe "rename index" do
489
- before(:each) do
490
- @conn = ActiveRecord::Base.connection
491
- schema_define do
492
- create_table :test_employees do |t|
493
- t.string :first_name
494
- t.string :last_name
495
- end
496
- add_index :test_employees, :first_name
497
- end
498
- class ::TestEmployee < ActiveRecord::Base; end
499
- end
500
-
501
- after(:each) do
502
- schema_define do
503
- drop_table :test_employees
504
- end
505
- Object.send(:remove_const, "TestEmployee")
506
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
507
- end
508
-
509
- it "should raise error when current index name and new index name are identical" do
510
- lambda do
511
- @conn.rename_index("test_employees","i_test_employees_first_name","i_test_employees_first_name")
512
- end.should raise_error
513
- end
514
-
515
- it "should raise error when new index name length is too long" do
516
- lambda do
517
- @conn.rename_index("test_employees","i_test_employees_first_name","a"*31)
518
- end.should raise_error
519
- end
520
-
521
- it "should raise error when current index name does not exist" do
522
- lambda do
523
- @conn.rename_index("test_employees","nonexist_index_name","new_index_name")
524
- end.should raise_error
525
- end
526
-
527
- it "should rename index name with new one" do
528
- lambda do
529
- @conn.rename_index("test_employees","i_test_employees_first_name","new_index_name")
530
- end.should_not raise_error
531
- end
532
- end
533
-
534
- describe "ignore options for LOB columns" do
535
- after(:each) do
536
- schema_define do
537
- drop_table :test_posts
538
- end
539
- end
540
-
541
- it "should ignore :limit option for :text column" do
542
- lambda do
543
- schema_define do
544
- create_table :test_posts, :force => true do |t|
545
- t.text :body, :limit => 10000
546
- end
547
- end
548
- end.should_not raise_error
549
- end
550
-
551
- it "should ignore :limit option for :binary column" do
552
- lambda do
553
- schema_define do
554
- create_table :test_posts, :force => true do |t|
555
- t.binary :picture, :limit => 10000
556
- end
557
- end
558
- end.should_not raise_error
559
- end
560
-
561
- end
562
-
563
- describe "foreign key constraints" do
564
- let(:table_name_prefix) { '' }
565
- let(:table_name_suffix) { '' }
566
-
567
- before(:each) do
568
- ActiveRecord::Base.table_name_prefix = table_name_prefix
569
- ActiveRecord::Base.table_name_suffix = table_name_suffix
570
- schema_define do
571
- create_table :test_posts, :force => true do |t|
572
- t.string :title
573
- end
574
- create_table :test_comments, :force => true do |t|
575
- t.string :body, :limit => 4000
576
- t.references :test_post
577
- t.integer :post_id
578
- end
579
- end
580
- class ::TestPost < ActiveRecord::Base
581
- has_many :test_comments
582
- end
583
- class ::TestComment < ActiveRecord::Base
584
- belongs_to :test_post
585
- end
586
- end
587
-
588
- after(:each) do
589
- Object.send(:remove_const, "TestPost")
590
- Object.send(:remove_const, "TestComment")
591
- schema_define do
592
- drop_table :test_comments rescue nil
593
- drop_table :test_posts rescue nil
594
- end
595
- ActiveRecord::Base.table_name_prefix = ''
596
- ActiveRecord::Base.table_name_suffix = ''
597
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
598
- end
599
-
600
- it "should add foreign key" do
601
- schema_define do
602
- add_foreign_key :test_comments, :test_posts
603
- end
604
- lambda do
605
- TestComment.create(:body => "test", :test_post_id => 1)
606
- end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.TEST_COMMENTS_TEST_POST_ID_FK/}
607
- end
608
-
609
- context "with table_name_prefix" do
610
- let(:table_name_prefix) { 'xxx_' }
611
-
612
- it "should use table_name_prefix for foreign table" do
613
- schema_define do
614
- add_foreign_key :test_comments, :test_posts
615
- end
616
-
617
- lambda do
618
- TestComment.create(:body => "test", :test_post_id => 1)
619
- end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.XXX_TES_COM_TES_POS_ID_FK/}
620
- end
621
- end
622
-
623
- context "with table_name_suffix" do
624
- let(:table_name_suffix) { '_xxx' }
625
-
626
- it "should use table_name_suffix for foreign table" do
627
- schema_define do
628
- add_foreign_key :test_comments, :test_posts
629
- end
630
-
631
- lambda do
632
- TestComment.create(:body => "test", :test_post_id => 1)
633
- end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.TES_COM_XXX_TES_POS_ID_FK/}
634
- end
635
- end
636
-
637
- it "should add foreign key with name" do
638
- schema_define do
639
- add_foreign_key :test_comments, :test_posts, :name => "comments_posts_fk"
640
- end
641
- lambda do
642
- TestComment.create(:body => "test", :test_post_id => 1)
643
- end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.COMMENTS_POSTS_FK/}
644
- end
645
-
646
- it "should add foreign key with long name which is shortened" do
647
- schema_define do
648
- add_foreign_key :test_comments, :test_posts, :name => "test_comments_test_post_id_foreign_key"
649
- end
650
- lambda do
651
- TestComment.create(:body => "test", :test_post_id => 1)
652
- end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.TES_COM_TES_POS_ID_FOR_KEY/}
653
- end
654
-
655
- it "should add foreign key with very long name which is shortened" do
656
- schema_define do
657
- add_foreign_key :test_comments, :test_posts, :name => "long_prefix_test_comments_test_post_id_foreign_key"
658
- end
659
- lambda do
660
- TestComment.create(:body => "test", :test_post_id => 1)
661
- end.should raise_error() {|e| e.message.should =~
662
- /ORA-02291.*\.C#{Digest::SHA1.hexdigest("long_prefix_test_comments_test_post_id_foreign_key")[0,29].upcase}/}
663
- end
664
-
665
- it "should add foreign key with column" do
666
- schema_define do
667
- add_foreign_key :test_comments, :test_posts, :column => "post_id"
668
- end
669
- lambda do
670
- TestComment.create(:body => "test", :post_id => 1)
671
- end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.TEST_COMMENTS_POST_ID_FK/}
672
- end
673
-
674
- it "should add foreign key with delete dependency" do
675
- schema_define do
676
- add_foreign_key :test_comments, :test_posts, :dependent => :delete
677
- end
678
- p = TestPost.create(:title => "test")
679
- c = TestComment.create(:body => "test", :test_post => p)
680
- TestPost.delete(p.id)
681
- TestComment.find_by_id(c.id).should be_nil
682
- end
683
-
684
- it "should add foreign key with nullify dependency" do
685
- schema_define do
686
- add_foreign_key :test_comments, :test_posts, :dependent => :nullify
687
- end
688
- p = TestPost.create(:title => "test")
689
- c = TestComment.create(:body => "test", :test_post => p)
690
- TestPost.delete(p.id)
691
- TestComment.find_by_id(c.id).test_post_id.should be_nil
692
- end
693
-
694
- it "should add a composite foreign key" do
695
- schema_define do
696
- add_column :test_posts, :baz_id, :integer
697
- add_column :test_posts, :fooz_id, :integer
698
-
699
- execute <<-SQL
700
- ALTER TABLE TEST_POSTS
701
- ADD CONSTRAINT UK_FOOZ_BAZ UNIQUE (BAZ_ID,FOOZ_ID)
702
- SQL
703
-
704
- add_column :test_comments, :baz_id, :integer
705
- add_column :test_comments, :fooz_id, :integer
706
-
707
- add_foreign_key :test_comments, :test_posts, :columns => ["baz_id", "fooz_id"]
708
- end
709
-
710
- lambda do
711
- TestComment.create(:body => "test", :fooz_id => 1, :baz_id => 1)
712
- end.should raise_error() {|e| e.message.should =~
713
- /ORA-02291.*\.TES_COM_BAZ_ID_FOO_ID_FK/}
714
- end
715
-
716
- it "should add a composite foreign key with name" do
717
- schema_define do
718
- add_column :test_posts, :baz_id, :integer
719
- add_column :test_posts, :fooz_id, :integer
720
-
721
- execute <<-SQL
722
- ALTER TABLE TEST_POSTS
723
- ADD CONSTRAINT UK_FOOZ_BAZ UNIQUE (BAZ_ID,FOOZ_ID)
724
- SQL
725
-
726
- add_column :test_comments, :baz_id, :integer
727
- add_column :test_comments, :fooz_id, :integer
728
-
729
- add_foreign_key :test_comments, :test_posts, :columns => ["baz_id", "fooz_id"], :name => 'comments_posts_baz_fooz_fk'
730
- end
731
-
732
- lambda do
733
- TestComment.create(:body => "test", :baz_id => 1, :fooz_id => 1)
734
- end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.COMMENTS_POSTS_BAZ_FOOZ_FK/}
735
- end
736
-
737
- it "should remove foreign key by table name" do
738
- schema_define do
739
- add_foreign_key :test_comments, :test_posts
740
- remove_foreign_key :test_comments, :test_posts
741
- end
742
- lambda do
743
- TestComment.create(:body => "test", :test_post_id => 1)
744
- end.should_not raise_error
745
- end
746
-
747
- it "should remove foreign key by constraint name" do
748
- schema_define do
749
- add_foreign_key :test_comments, :test_posts, :name => "comments_posts_fk"
750
- remove_foreign_key :test_comments, :name => "comments_posts_fk"
751
- end
752
- lambda do
753
- TestComment.create(:body => "test", :test_post_id => 1)
754
- end.should_not raise_error
755
- end
756
-
757
- it "should remove foreign key by column name" do
758
- schema_define do
759
- add_foreign_key :test_comments, :test_posts
760
- remove_foreign_key :test_comments, :column => "test_post_id"
761
- end
762
- lambda do
763
- TestComment.create(:body => "test", :test_post_id => 1)
764
- end.should_not raise_error
765
- end
766
-
767
- end
768
-
769
- describe "foreign key in table definition" do
770
- before(:each) do
771
- schema_define do
772
- create_table :test_posts, :force => true do |t|
773
- t.string :title
774
- end
775
- end
776
- class ::TestPost < ActiveRecord::Base
777
- has_many :test_comments
778
- end
779
- class ::TestComment < ActiveRecord::Base
780
- belongs_to :test_post
781
- end
782
- end
783
-
784
- after(:each) do
785
- Object.send(:remove_const, "TestPost")
786
- Object.send(:remove_const, "TestComment")
787
- schema_define do
788
- drop_table :test_comments rescue nil
789
- drop_table :test_posts rescue nil
790
- end
791
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
792
- end
793
-
794
- it "should add foreign key in create_table" do
795
- schema_define do
796
- create_table :test_comments, :force => true do |t|
797
- t.string :body, :limit => 4000
798
- t.references :test_post
799
- t.foreign_key :test_posts
800
- end
801
- end
802
- lambda do
803
- TestComment.create(:body => "test", :test_post_id => 1)
804
- end.should raise_error() {|e| e.message.should =~ /ORA-02291/}
805
- end
806
-
807
- it "should add foreign key in create_table references" do
808
- schema_define do
809
- create_table :test_comments, :force => true do |t|
810
- t.string :body, :limit => 4000
811
- t.references :test_post, :foreign_key => true
812
- end
813
- end
814
- lambda do
815
- TestComment.create(:body => "test", :test_post_id => 1)
816
- end.should raise_error() {|e| e.message.should =~ /ORA-02291/}
817
- end
818
-
819
- it "should add foreign key in change_table" do
820
- return pending("Not in this ActiveRecord version") unless ENV['RAILS_GEM_VERSION'] >= '2.1'
821
- schema_define do
822
- create_table :test_comments, :force => true do |t|
823
- t.string :body, :limit => 4000
824
- t.references :test_post
825
- end
826
- change_table :test_comments do |t|
827
- t.foreign_key :test_posts
828
- end
829
- end
830
- lambda do
831
- TestComment.create(:body => "test", :test_post_id => 1)
832
- end.should raise_error() {|e| e.message.should =~ /ORA-02291/}
833
- end
834
-
835
- it "should add foreign key in change_table references" do
836
- return pending("Not in this ActiveRecord version") unless ENV['RAILS_GEM_VERSION'] >= '2.1'
837
- schema_define do
838
- create_table :test_comments, :force => true do |t|
839
- t.string :body, :limit => 4000
840
- end
841
- change_table :test_comments do |t|
842
- t.references :test_post, :foreign_key => true
843
- end
844
- end
845
- lambda do
846
- TestComment.create(:body => "test", :test_post_id => 1)
847
- end.should raise_error() {|e| e.message.should =~ /ORA-02291/}
848
- end
849
-
850
- it "should remove foreign key by table name" do
851
- return pending("Not in this ActiveRecord version") unless ENV['RAILS_GEM_VERSION'] >= '2.1'
852
- schema_define do
853
- create_table :test_comments, :force => true do |t|
854
- t.string :body, :limit => 4000
855
- t.references :test_post
856
- end
857
- change_table :test_comments do |t|
858
- t.foreign_key :test_posts
859
- end
860
- change_table :test_comments do |t|
861
- t.remove_foreign_key :test_posts
862
- end
863
- end
864
- lambda do
865
- TestComment.create(:body => "test", :test_post_id => 1)
866
- end.should_not raise_error
867
- end
868
-
869
- end
870
-
871
- describe "disable referential integrity" do
872
- before(:all) do
873
- @conn = ActiveRecord::Base.connection
874
- end
875
-
876
- before(:each) do
877
- schema_define do
878
- create_table :test_posts, :force => true do |t|
879
- t.string :title
880
- end
881
- create_table :test_comments, :force => true do |t|
882
- t.string :body, :limit => 4000
883
- t.references :test_post, :foreign_key => true
884
- end
885
- end
886
- end
887
-
888
- after(:each) do
889
- schema_define do
890
- drop_table :test_comments rescue nil
891
- drop_table :test_posts rescue nil
892
- end
893
- end
894
-
895
- it "should disable all foreign keys" do
896
- lambda do
897
- @conn.execute "INSERT INTO test_comments (id, body, test_post_id) VALUES (1, 'test', 1)"
898
- end.should raise_error
899
- @conn.disable_referential_integrity do
900
- lambda do
901
- @conn.execute "INSERT INTO test_comments (id, body, test_post_id) VALUES (2, 'test', 2)"
902
- @conn.execute "INSERT INTO test_posts (id, title) VALUES (2, 'test')"
903
- end.should_not raise_error
904
- end
905
- lambda do
906
- @conn.execute "INSERT INTO test_comments (id, body, test_post_id) VALUES (3, 'test', 3)"
907
- end.should raise_error
908
- end
909
-
910
- end
911
-
912
- describe "synonyms" do
913
- before(:all) do
914
- @conn = ActiveRecord::Base.connection
915
- @db_link = "db_link"
916
- @username = @db_link_username = CONNECTION_PARAMS[:username]
917
- @db_link_password = CONNECTION_PARAMS[:password]
918
- @db_link_database = CONNECTION_PARAMS[:database]
919
- @conn.execute "DROP DATABASE LINK #{@db_link}" rescue nil
920
- @conn.execute "CREATE DATABASE LINK #{@db_link} CONNECT TO #{@db_link_username} IDENTIFIED BY \"#{@db_link_password}\" USING '#{@db_link_database}'"
921
- schema_define do
922
- create_table :test_posts, :force => true do |t|
923
- t.string :title
924
- end
925
- end
926
- end
927
-
928
- after(:all) do
929
- schema_define do
930
- drop_table :test_posts
931
- end
932
- @conn.execute "DROP DATABASE LINK #{@db_link}" rescue nil
933
- end
934
-
935
- before(:each) do
936
- class ::TestPost < ActiveRecord::Base
937
- if self.respond_to?(:table_name=)
938
- self.table_name = "synonym_to_posts"
939
- else
940
- set_table_name "synonym_to_posts"
941
- end
942
- end
943
- end
944
-
945
- after(:each) do
946
- Object.send(:remove_const, "TestPost")
947
- schema_define do
948
- remove_synonym :synonym_to_posts
949
- remove_synonym :synonym_to_posts_seq
950
- end
951
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
952
- end
953
-
954
- it "should create synonym to table and sequence" do
955
- schema_name = @username
956
- schema_define do
957
- add_synonym :synonym_to_posts, "#{schema_name}.test_posts", :force => true
958
- add_synonym :synonym_to_posts_seq, "#{schema_name}.test_posts_seq", :force => true
959
- end
960
- lambda do
961
- TestPost.create(:title => "test")
962
- end.should_not raise_error
963
- end
964
-
965
- it "should create synonym to table over database link" do
966
- db_link = @db_link
967
- schema_define do
968
- add_synonym :synonym_to_posts, "test_posts@#{db_link}", :force => true
969
- add_synonym :synonym_to_posts_seq, "test_posts_seq@#{db_link}", :force => true
970
- end
971
- lambda do
972
- TestPost.create(:title => "test")
973
- end.should_not raise_error
974
- end
975
-
976
- end
977
-
978
- describe "alter columns with column cache" do
979
- include LoggerSpecHelper
980
-
981
- before(:all) do
982
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.cache_columns = true
983
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces.delete(:clob)
984
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces.delete(:blob)
985
- end
986
-
987
- after(:all) do
988
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.cache_columns = nil
989
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces.delete(:clob)
990
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces.delete(:blob)
991
- end
992
-
993
- before(:each) do
994
- schema_define do
995
- create_table :test_posts, :force => true do |t|
996
- t.string :title, :null => false
997
- t.string :content
998
- end
999
- end
1000
- class ::TestPost < ActiveRecord::Base; end
1001
- TestPost.columns_hash['title'].null.should be_false
1002
- end
1003
-
1004
- after(:each) do
1005
- Object.send(:remove_const, "TestPost")
1006
- schema_define { drop_table :test_posts }
1007
- ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
1008
- end
1009
-
1010
- it "should change column to nullable" do
1011
- schema_define do
1012
- change_column :test_posts, :title, :string, :null => true
1013
- end
1014
- TestPost.reset_column_information
1015
- TestPost.columns_hash['title'].null.should be_true
1016
- end
1017
-
1018
- it "should add column" do
1019
- schema_define do
1020
- add_column :test_posts, :body, :string
1021
- end
1022
- TestPost.reset_column_information
1023
- TestPost.columns_hash['body'].should_not be_nil
1024
- end
1025
-
1026
- it "should add lob column with non_default tablespace" do
1027
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces[:clob] = DATABASE_NON_DEFAULT_TABLESPACE
1028
- schema_define do
1029
- add_column :test_posts, :body, :text
1030
- end
1031
- TestPost.connection.select_value("SELECT tablespace_name FROM user_lobs WHERE table_name='TEST_POSTS' and column_name = 'BODY'").should == DATABASE_NON_DEFAULT_TABLESPACE
1032
- end
1033
-
1034
- it "should add blob column with non_default tablespace" do
1035
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces[:blob] = DATABASE_NON_DEFAULT_TABLESPACE
1036
- schema_define do
1037
- add_column :test_posts, :attachment, :binary
1038
- end
1039
- TestPost.connection.select_value("SELECT tablespace_name FROM user_lobs WHERE table_name='TEST_POSTS' and column_name = 'ATTACHMENT'").should == DATABASE_NON_DEFAULT_TABLESPACE
1040
- end
1041
-
1042
- it "should rename column" do
1043
- schema_define do
1044
- rename_column :test_posts, :title, :subject
1045
- end
1046
- TestPost.reset_column_information
1047
- TestPost.columns_hash['subject'].should_not be_nil
1048
- TestPost.columns_hash['title'].should be_nil
1049
- end
1050
-
1051
- it "should remove column" do
1052
- schema_define do
1053
- remove_column :test_posts, :title
1054
- end
1055
- TestPost.reset_column_information
1056
- TestPost.columns_hash['title'].should be_nil
1057
- end
1058
-
1059
- it "should remove column when using change_table" do
1060
- schema_define do
1061
- change_table :test_posts do |t|
1062
- t.remove :title
1063
- end
1064
- end
1065
- TestPost.reset_column_information
1066
- TestPost.columns_hash['title'].should be_nil
1067
- end
1068
-
1069
- it "should remove multiple columns when using change_table" do
1070
- schema_define do
1071
- change_table :test_posts do |t|
1072
- t.remove :title, :content
1073
- end
1074
- end
1075
- TestPost.reset_column_information
1076
- TestPost.columns_hash['title'].should be_nil
1077
- TestPost.columns_hash['content'].should be_nil
1078
- end
1079
- end
1080
-
1081
- describe 'virtual columns in create_table' do
1082
- before(:each) do
1083
- pending "Not supported in this database version" unless @oracle11g
1084
- end
1085
-
1086
- it 'should create virtual column with old syntax' do
1087
- schema_define do
1088
- create_table :test_fractions, :force => true do |t|
1089
- t.integer :field1
1090
- t.virtual :field2, :default => 'field1 + 1'
1091
- end
1092
- end
1093
- class ::TestFraction < ActiveRecord::Base
1094
- if self.respond_to?(:table_name=)
1095
- self.table_name = "test_fractions"
1096
- else
1097
- set_table_name "test_fractions"
1098
- end
1099
- end
1100
-
1101
- TestFraction.reset_column_information
1102
- tf = TestFraction.columns.detect { |c| c.virtual? }
1103
- tf.should_not be nil
1104
- tf.name.should == "field2"
1105
- tf.virtual?.should be true
1106
- lambda do
1107
- tf = TestFraction.new(:field1=>10)
1108
- tf.field2.should be nil # not whatever is in DATA_DEFAULT column
1109
- tf.save!
1110
- tf.reload
1111
- end.should_not raise_error
1112
- tf.field2.to_i.should == 11
1113
-
1114
- schema_define do
1115
- drop_table :test_fractions
1116
- end
1117
- end
1118
-
1119
- it 'should raise error if column expression is not provided' do
1120
- lambda {
1121
- schema_define do
1122
- create_table :test_fractions do |t|
1123
- t.integer :field1
1124
- t.virtual :field2
1125
- end
1126
- end
1127
- }.should raise_error
1128
- end
1129
- end
1130
-
1131
- describe 'virtual columns' do
1132
- before(:each) do
1133
- pending "Not supported in this database version" unless @oracle11g
1134
- expr = "( numerator/NULLIF(denominator,0) )*100"
1135
- schema_define do
1136
- create_table :test_fractions, :force => true do |t|
1137
- t.integer :numerator, :default=>0
1138
- t.integer :denominator, :default=>0
1139
- t.virtual :percent, :as => expr
1140
- end
1141
- end
1142
- class ::TestFraction < ActiveRecord::Base
1143
- if self.respond_to?(:table_name=)
1144
- self.table_name = "test_fractions"
1145
- else
1146
- set_table_name "test_fractions"
1147
- end
1148
- end
1149
- TestFraction.reset_column_information
1150
- end
1151
-
1152
- after(:each) do
1153
- if @oracle11g
1154
- schema_define do
1155
- drop_table :test_fractions
1156
- end
1157
- end
1158
- end
1159
-
1160
- it 'should include virtual columns and not try to update them' do
1161
- tf = TestFraction.columns.detect { |c| c.virtual? }
1162
- tf.should_not be nil
1163
- tf.name.should == "percent"
1164
- tf.virtual?.should be true
1165
- lambda do
1166
- tf = TestFraction.new(:numerator=>20, :denominator=>100)
1167
- tf.percent.should be nil # not whatever is in DATA_DEFAULT column
1168
- tf.save!
1169
- tf.reload
1170
- end.should_not raise_error
1171
- tf.percent.to_i.should == 20
1172
- end
1173
-
1174
- it 'should add virtual column' do
1175
- schema_define do
1176
- add_column :test_fractions, :rem, :virtual, :as => 'remainder(numerator, NULLIF(denominator,0))'
1177
- end
1178
- TestFraction.reset_column_information
1179
- tf = TestFraction.columns.detect { |c| c.name == 'rem' }
1180
- tf.should_not be nil
1181
- tf.virtual?.should be true
1182
- lambda do
1183
- tf = TestFraction.new(:numerator=>7, :denominator=>5)
1184
- tf.rem.should be nil
1185
- tf.save!
1186
- tf.reload
1187
- end.should_not raise_error
1188
- tf.rem.to_i.should == 2
1189
- end
1190
-
1191
- it 'should add virtual column with explicit type' do
1192
- schema_define do
1193
- add_column :test_fractions, :expression, :virtual, :as => "TO_CHAR(numerator) || '/' || TO_CHAR(denominator)", :type => :string, :limit => 100
1194
- end
1195
- TestFraction.reset_column_information
1196
- tf = TestFraction.columns.detect { |c| c.name == 'expression' }
1197
- tf.should_not be nil
1198
- tf.virtual?.should be true
1199
- tf.type.should be :string
1200
- tf.limit.should be 100
1201
- lambda do
1202
- tf = TestFraction.new(:numerator=>7, :denominator=>5)
1203
- tf.expression.should be nil
1204
- tf.save!
1205
- tf.reload
1206
- end.should_not raise_error
1207
- tf.expression.should == '7/5'
1208
- end
1209
-
1210
- it 'should change virtual column definition' do
1211
- schema_define do
1212
- change_column :test_fractions, :percent, :virtual,
1213
- :as => "ROUND((numerator/NULLIF(denominator,0))*100, 2)", :type => :decimal, :precision => 15, :scale => 2
1214
- end
1215
- TestFraction.reset_column_information
1216
- tf = TestFraction.columns.detect { |c| c.name == 'percent' }
1217
- tf.should_not be nil
1218
- tf.virtual?.should be true
1219
- tf.type.should be :decimal
1220
- tf.precision.should be 15
1221
- tf.scale.should be 2
1222
- lambda do
1223
- tf = TestFraction.new(:numerator=>11, :denominator=>17)
1224
- tf.percent.should be nil
1225
- tf.save!
1226
- tf.reload
1227
- end.should_not raise_error
1228
- tf.percent.should == '64.71'.to_d
1229
- end
1230
-
1231
- it 'should change virtual column type' do
1232
- schema_define do
1233
- change_column :test_fractions, :percent, :virtual, :type => :decimal, :precision => 12, :scale => 5
1234
- end
1235
- TestFraction.reset_column_information
1236
- tf = TestFraction.columns.detect { |c| c.name == 'percent' }
1237
- tf.should_not be nil
1238
- tf.virtual?.should be true
1239
- tf.type.should be :decimal
1240
- tf.precision.should be 12
1241
- tf.scale.should be 5
1242
- lambda do
1243
- tf = TestFraction.new(:numerator=>11, :denominator=>17)
1244
- tf.percent.should be nil
1245
- tf.save!
1246
- tf.reload
1247
- end.should_not raise_error
1248
- tf.percent.should == '64.70588'.to_d
1249
- end
1250
- end
1251
-
1252
- describe "miscellaneous options" do
1253
- before(:all) do
1254
- @conn = ActiveRecord::Base.connection
1255
- end
1256
-
1257
- before(:each) do
1258
- @conn.instance_variable_set :@would_execute_sql, @would_execute_sql=''
1259
- class <<@conn
1260
- def execute(sql,name=nil); @would_execute_sql << sql << ";\n"; end
1261
- def index_name_exists?(table_name, index_name, default); default; end
1262
- end
1263
- end
1264
-
1265
- after(:each) do
1266
- class <<@conn
1267
- remove_method :execute
1268
- end
1269
- @conn.instance_eval{ remove_instance_variable :@would_execute_sql }
1270
- end
1271
-
1272
- it "should support the :options option to create_table" do
1273
- schema_define do
1274
- create_table :test_posts, :options=>'NOLOGGING', :force => true do |t|
1275
- t.string :title, :null => false
1276
- end
1277
- end
1278
- @would_execute_sql.should =~ /CREATE +TABLE .* \(.*\) NOLOGGING/
1279
- end
1280
-
1281
- it "should support the :tablespace option to create_table" do
1282
- schema_define do
1283
- create_table :test_posts, :tablespace=>'bogus', :force => true do |t|
1284
- t.string :title, :null => false
1285
- end
1286
- end
1287
- @would_execute_sql.should =~ /CREATE +TABLE .* \(.*\) TABLESPACE bogus/
1288
- end
1289
-
1290
- describe "creating a table with a tablespace defaults set" do
1291
- after(:each) do
1292
- @conn.drop_table :tablespace_tests rescue nil
1293
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces.delete(:table)
1294
- end
1295
- it "should use correct tablespace" do
1296
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces[:table] = DATABASE_NON_DEFAULT_TABLESPACE
1297
- @conn.create_table :tablespace_tests do |t|
1298
- t.integer :id
1299
- end
1300
- @would_execute_sql.should =~ /CREATE +TABLE .* \(.*\) TABLESPACE #{DATABASE_NON_DEFAULT_TABLESPACE}/
1301
- end
1302
- end
1303
-
1304
- describe "creating an index-organized table" do
1305
- after(:each) do
1306
- @conn.drop_table :tablespace_tests rescue nil
1307
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces.delete(:table)
1308
- end
1309
- it "should use correct tablespace" do
1310
- @conn.create_table :tablespace_tests, :id=>false, :organization=>'INDEX INITRANS 4 COMPRESS 1', :tablespace=>'bogus' do |t|
1311
- t.integer :id
1312
- end
1313
- @would_execute_sql.should =~ /CREATE +TABLE .*\(.*\)\s+ORGANIZATION INDEX INITRANS 4 COMPRESS 1 TABLESPACE bogus/
1314
- end
1315
- end
1316
-
1317
- it "should support the :options option to add_index" do
1318
- schema_define do
1319
- add_index :keyboards, :name, :options=>'NOLOGGING'
1320
- end
1321
- @would_execute_sql.should =~ /CREATE +INDEX .* ON .* \(.*\) NOLOGGING/
1322
- end
1323
-
1324
- it "should support the :tablespace option to add_index" do
1325
- schema_define do
1326
- add_index :keyboards, :name, :tablespace=>'bogus'
1327
- end
1328
- @would_execute_sql.should =~ /CREATE +INDEX .* ON .* \(.*\) TABLESPACE bogus/
1329
- end
1330
-
1331
- it "should use default_tablespaces in add_index" do
1332
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces[:index] = DATABASE_NON_DEFAULT_TABLESPACE
1333
- schema_define do
1334
- add_index :keyboards, :name
1335
- end
1336
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces.delete(:index)
1337
- @would_execute_sql.should =~ /CREATE +INDEX .* ON .* \(.*\) TABLESPACE #{DATABASE_NON_DEFAULT_TABLESPACE}/
1338
- end
1339
-
1340
- describe "#initialize_schema_migrations_table" do
1341
- # In Rails 2.3 to 3.2.x the index name for the migrations
1342
- # table is hard-coded. We can modify the index name here
1343
- # so we can support prefixes/suffixes that would
1344
- # cause the index to be too long.
1345
- #
1346
- # Rails 4 can use this solution as well.
1347
- after(:each) do
1348
- ActiveRecord::Base.table_name_prefix = ''
1349
- ActiveRecord::Base.table_name_suffix = ''
1350
- end
1351
-
1352
- def add_schema_migrations_index
1353
- schema_define do
1354
- initialize_schema_migrations_table
1355
- end
1356
- end
1357
-
1358
- context "without prefix or suffix" do
1359
- it "should not truncate the index name" do
1360
- add_schema_migrations_index
1361
-
1362
- @would_execute_sql.should include('CREATE UNIQUE INDEX "UNIQUE_SCHEMA_MIGRATIONS" ON "SCHEMA_MIGRATIONS" ("VERSION")')
1363
- end
1364
- end
1365
-
1366
- context "with prefix" do
1367
- before { ActiveRecord::Base.table_name_prefix = 'toolong_' }
1368
-
1369
- it "should truncate the 'unique_schema_migrations' portion of the index name to fit the prefix within the limit" do
1370
- add_schema_migrations_index
1371
-
1372
- @would_execute_sql.should include('CREATE UNIQUE INDEX "TOOLONG_UNIQUE_SCHEMA_MIGRATIO" ON "TOOLONG_SCHEMA_MIGRATIONS" ("VERSION")')
1373
- end
1374
- end
1375
-
1376
- context "with suffix" do
1377
- before { ActiveRecord::Base.table_name_suffix = '_toolong' }
1378
-
1379
- it "should truncate the 'unique_schema_migrations' portion of the index name to fit the suffix within the limit" do
1380
- add_schema_migrations_index
1381
-
1382
- @would_execute_sql.should include('CREATE UNIQUE INDEX "UNIQUE_SCHEMA_MIGRATIO_TOOLONG" ON "SCHEMA_MIGRATIONS_TOOLONG" ("VERSION")')
1383
- end
1384
- end
1385
-
1386
- context "with prefix and suffix" do
1387
- before do
1388
- ActiveRecord::Base.table_name_prefix = 'begin_'
1389
- ActiveRecord::Base.table_name_suffix = '_end'
1390
- end
1391
-
1392
- it "should truncate the 'unique_schema_migrations' portion of the index name to fit the suffix within the limit" do
1393
- add_schema_migrations_index
1394
-
1395
- @would_execute_sql.should include('CREATE UNIQUE INDEX "BEGIN_UNIQUE_SCHEMA_MIGRAT_END" ON "BEGIN_SCHEMA_MIGRATIONS_END" ("VERSION")')
1396
- end
1397
- end
1398
- end
1399
- end
1400
- end