activerecord-jdbc-adapter 1.3.0.beta1 → 1.3.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -11
- data/.travis.yml +36 -7
- data/Appraisals +3 -3
- data/Gemfile +1 -1
- data/Gemfile.lock +13 -6
- data/History.txt +64 -0
- data/README.md +8 -1
- data/Rakefile +3 -1
- data/gemfiles/rails23.gemfile +1 -1
- data/gemfiles/rails23.gemfile.lock +6 -5
- data/gemfiles/rails30.gemfile +1 -1
- data/gemfiles/rails30.gemfile.lock +7 -6
- data/gemfiles/rails31.gemfile +1 -1
- data/gemfiles/rails31.gemfile.lock +6 -5
- data/gemfiles/rails32.gemfile +1 -1
- data/gemfiles/rails32.gemfile.lock +6 -5
- data/gemfiles/rails40.gemfile +2 -4
- data/gemfiles/rails40.gemfile.lock +37 -51
- data/lib/active_record/connection_adapters/as400_adapter.rb +2 -0
- data/lib/active_record/connection_adapters/db2_adapter.rb +1 -1
- data/lib/arel/visitors/db2.rb +5 -1
- data/lib/arel/visitors/hsqldb.rb +1 -0
- data/lib/arel/visitors/sql_server.rb +55 -13
- data/lib/arjdbc/db2/adapter.rb +197 -227
- data/lib/arjdbc/db2/as400.rb +124 -0
- data/lib/arjdbc/db2/connection_methods.rb +20 -1
- data/lib/arjdbc/derby/adapter.rb +17 -85
- data/lib/arjdbc/derby/connection_methods.rb +2 -1
- data/lib/arjdbc/discover.rb +55 -47
- data/lib/arjdbc/h2/adapter.rb +52 -18
- data/lib/arjdbc/h2/connection_methods.rb +10 -2
- data/lib/arjdbc/hsqldb/adapter.rb +33 -9
- data/lib/arjdbc/hsqldb/connection_methods.rb +10 -2
- data/lib/arjdbc/informix.rb +2 -1
- data/lib/arjdbc/jdbc.rb +5 -1
- data/lib/arjdbc/jdbc/adapter.rb +167 -89
- data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
- data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
- data/lib/arjdbc/jdbc/base_ext.rb +25 -3
- data/lib/arjdbc/jdbc/callbacks.rb +9 -8
- data/lib/arjdbc/jdbc/column.rb +8 -20
- data/lib/arjdbc/jdbc/connection.rb +69 -80
- data/lib/arjdbc/jdbc/extension.rb +6 -8
- data/lib/arjdbc/jdbc/jdbc.rake +3 -141
- data/lib/arjdbc/jdbc/rake_tasks.rb +3 -10
- data/lib/arjdbc/mssql/adapter.rb +108 -34
- data/lib/arjdbc/mssql/connection_methods.rb +3 -1
- data/lib/arjdbc/mssql/limit_helpers.rb +3 -2
- data/lib/arjdbc/mssql/lock_helpers.rb +5 -1
- data/lib/arjdbc/mysql/adapter.rb +127 -70
- data/lib/arjdbc/mysql/connection_methods.rb +5 -2
- data/lib/arjdbc/oracle/adapter.rb +124 -94
- data/lib/arjdbc/oracle/connection_methods.rb +2 -1
- data/lib/arjdbc/postgresql/adapter.rb +99 -67
- data/lib/arjdbc/postgresql/column_cast.rb +3 -5
- data/lib/arjdbc/postgresql/connection_methods.rb +6 -6
- data/lib/arjdbc/railtie.rb +3 -1
- data/lib/arjdbc/sqlite3/adapter.rb +60 -43
- data/lib/arjdbc/sqlite3/connection_methods.rb +9 -9
- data/lib/arjdbc/sybase.rb +1 -1
- data/lib/arjdbc/tasks.rb +13 -0
- data/lib/arjdbc/tasks/database_tasks.rb +50 -0
- data/lib/arjdbc/tasks/databases.rake +89 -0
- data/lib/arjdbc/tasks/databases3.rake +203 -0
- data/lib/arjdbc/tasks/databases4.rake +39 -0
- data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
- data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
- data/lib/arjdbc/tasks/h2_database_tasks.rb +29 -0
- data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
- data/lib/arjdbc/tasks/jdbc_database_tasks.rb +122 -0
- data/lib/arjdbc/tasks/mssql_database_tasks.rb +36 -0
- data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +297 -0
- data/lib/arjdbc/tasks/oracle_database_tasks.rb +62 -0
- data/lib/arjdbc/version.rb +1 -1
- data/pom.xml +11 -12
- data/rails_generators/jdbc_generator.rb +1 -1
- data/rails_generators/templates/config/initializers/jdbc.rb +8 -5
- data/rails_generators/templates/lib/tasks/jdbc.rake +7 -4
- data/rakelib/02-test.rake +42 -15
- data/rakelib/compile.rake +29 -2
- data/rakelib/db.rake +2 -1
- data/rakelib/rails.rake +23 -6
- data/src/java/arjdbc/ArJdbcModule.java +175 -0
- data/src/java/arjdbc/db2/DB2Module.java +2 -1
- data/src/java/arjdbc/derby/DerbyModule.java +5 -24
- data/src/java/arjdbc/hsqldb/HSQLDBModule.java +3 -2
- data/src/java/arjdbc/jdbc/AdapterJavaService.java +3 -46
- data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +1001 -259
- data/src/java/arjdbc/mssql/MSSQLModule.java +2 -1
- data/src/java/arjdbc/mysql/MySQLModule.java +4 -3
- data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +12 -7
- data/src/java/arjdbc/oracle/OracleModule.java +2 -1
- data/src/java/arjdbc/sqlite3/SQLite3Module.java +2 -1
- data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +12 -0
- data/test/db/db2.rb +14 -7
- data/test/db/db2/rake_test.rb +82 -0
- data/test/db/db2/rake_test_data.sql +35 -0
- data/test/db/db2/simple_test.rb +20 -0
- data/test/db/db2/unit_test.rb +3 -1
- data/test/db/derby.rb +7 -5
- data/test/db/derby/rake_test.rb +96 -0
- data/test/db/derby/simple_test.rb +10 -2
- data/test/db/h2.rb +6 -8
- data/test/db/h2/identity_column_test.rb +35 -0
- data/test/db/h2/offset_test.rb +49 -0
- data/test/db/h2/rake_test.rb +98 -0
- data/test/db/h2/schema_dump_test.rb +5 -1
- data/test/db/hsqldb.rb +6 -10
- data/test/db/hsqldb/rake_test.rb +101 -0
- data/test/db/hsqldb/schema_dump_test.rb +5 -1
- data/test/db/hsqldb/simple_test.rb +8 -0
- data/test/db/jndi_config.rb +1 -3
- data/test/db/jndi_pooled_config.rb +1 -3
- data/test/db/mssql/limit_offset_test.rb +23 -14
- data/test/db/mssql/rake_test.rb +143 -0
- data/test/db/mysql/_rails_test_mysql.32.out +1069 -1252
- data/test/db/mysql/nonstandard_primary_key_test.rb +21 -24
- data/test/db/mysql/rake_test.rb +97 -0
- data/test/db/mysql/schema_dump_test.rb +11 -11
- data/test/db/mysql/simple_test.rb +52 -3
- data/test/db/mysql/statement_escaping_test.rb +46 -0
- data/test/db/oracle/rake_test.rb +100 -0
- data/test/db/oracle/simple_test.rb +48 -0
- data/test/db/postgres/_rails_test_postgres.32.out +998 -1370
- data/test/db/postgres/active_schema_unit_test.rb +68 -0
- data/test/db/postgres/connection_test.rb +10 -2
- data/test/db/postgres/data_types_test.rb +2 -2
- data/test/db/postgres/ltree_test.rb +6 -5
- data/test/db/postgres/native_types_test.rb +1 -5
- data/test/db/postgres/rake_test.rb +117 -0
- data/test/db/postgres/schema_dump_test.rb +9 -2
- data/test/db/postgres/schema_test.rb +4 -2
- data/test/db/postgres/simple_test.rb +57 -16
- data/test/db/sqlite3.rb +3 -10
- data/test/db/sqlite3/_rails_test_sqlite3.32.out +1070 -1298
- data/test/db/sqlite3/rake_test.rb +71 -0
- data/test/db/sqlite3/simple_test.rb +9 -9
- data/test/has_many_through.rb +4 -1
- data/test/jdbc/db2.rb +14 -1
- data/test/jdbc_column_test.rb +23 -0
- data/test/{generic_jdbc_connection_test.rb → jdbc_connection_test.rb} +22 -17
- data/test/jndi_callbacks_test.rb +26 -28
- data/test/jndi_test.rb +7 -16
- data/test/models/data_types.rb +2 -1
- data/test/models/thing.rb +1 -0
- data/test/rails/mysql.rb +13 -0
- data/test/rails/sqlite3/version.rb +6 -0
- data/test/rails_stub.rb +31 -0
- data/test/rake_test_support.rb +298 -0
- data/test/serialize.rb +2 -4
- data/test/{helper.rb → shared_helper.rb} +0 -0
- data/test/simple.rb +167 -93
- data/test/test_helper.rb +52 -16
- metadata +388 -354
- data/lib/pg.rb +0 -26
- data/test/abstract_db_create.rb +0 -139
- data/test/activerecord/connection_adapters/type_conversion_test.rb +0 -36
- data/test/db/mssql/db_create_test.rb +0 -29
- data/test/db/mysql/db_create_test.rb +0 -33
- data/test/db/postgres/db_create_test.rb +0 -44
- data/test/db/postgres/db_drop_test.rb +0 -17
@@ -0,0 +1,298 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'pathname'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module RakeTestSupport
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
require 'rake'
|
9
|
+
base.module_eval { include Rake::DSL } # if defined?(Rake::DSL)
|
10
|
+
base.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def startup
|
16
|
+
super
|
17
|
+
load 'rails_stub.rb'
|
18
|
+
end
|
19
|
+
|
20
|
+
def shutdown
|
21
|
+
Object.send(:remove_const, :Rails)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def setup
|
28
|
+
@_prev_application = Rake.application
|
29
|
+
@_prev_configurations = ActiveRecord::Base.configurations
|
30
|
+
@_prev_connection_config = current_connection_config
|
31
|
+
db_config # if not re-defined initialize from current connection's config
|
32
|
+
|
33
|
+
@db_name = db_name unless @db_name ||= nil
|
34
|
+
@rails_env = rails_env unless @rails_env ||= nil
|
35
|
+
|
36
|
+
setup_rails
|
37
|
+
set_rails_env(@rails_env)
|
38
|
+
set_rails_root(".")
|
39
|
+
|
40
|
+
Rake.application = new_application
|
41
|
+
ActiveRecord::Base.connection.disconnect!
|
42
|
+
|
43
|
+
verbose(true)
|
44
|
+
|
45
|
+
load_tasks
|
46
|
+
|
47
|
+
do_setup
|
48
|
+
end
|
49
|
+
|
50
|
+
def do_setup
|
51
|
+
end
|
52
|
+
|
53
|
+
RAILS_4x = ActiveRecord::VERSION::MAJOR >= 4
|
54
|
+
|
55
|
+
def load_tasks
|
56
|
+
if ActiveRecord::VERSION::MAJOR >= 3
|
57
|
+
load "active_record/railties/databases.rake"
|
58
|
+
else # we still support AR-2.3
|
59
|
+
load "tasks/databases.rake" # from rails/railties
|
60
|
+
end
|
61
|
+
load 'arjdbc/tasks.rb' if defined?(JRUBY_VERSION)
|
62
|
+
|
63
|
+
namespace :db do
|
64
|
+
task :load_config do
|
65
|
+
# 4.0 :
|
66
|
+
# ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
|
67
|
+
# ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
68
|
+
# 3.2 :
|
69
|
+
# ActiveRecord::Base.configurations = Rails.application.config.database_configuration
|
70
|
+
# ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a
|
71
|
+
# 2.3 :
|
72
|
+
# ActiveRecord::Base.configurations = Rails::Configuration.new.database_configuration
|
73
|
+
ActiveRecord::Base.configurations = configurations
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
task :environment do
|
78
|
+
ActiveRecord::Base.configurations = configurations
|
79
|
+
ActiveRecord::Base.establish_connection @rails_env
|
80
|
+
@full_env_loaded = true
|
81
|
+
end
|
82
|
+
|
83
|
+
if RAILS_4x
|
84
|
+
ActiveRecord::Tasks::DatabaseTasks.env = @rails_env
|
85
|
+
ActiveRecord::Tasks::DatabaseTasks.db_dir = 'db'
|
86
|
+
else
|
87
|
+
task(:rails_env) { @rails_env_set = true }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def teardown
|
92
|
+
verify_and_restore_stdout
|
93
|
+
error = nil
|
94
|
+
begin
|
95
|
+
do_teardown
|
96
|
+
rescue => e
|
97
|
+
error = e
|
98
|
+
end
|
99
|
+
Rake.application = @_prev_application
|
100
|
+
restore_rails
|
101
|
+
ActiveRecord::Base.configurations = @_prev_configurations
|
102
|
+
ActiveRecord::Base.establish_connection @_prev_connection_config
|
103
|
+
@rails_env_set = nil
|
104
|
+
@full_env_loaded = nil
|
105
|
+
raise error if error
|
106
|
+
end
|
107
|
+
|
108
|
+
def do_teardown
|
109
|
+
end
|
110
|
+
|
111
|
+
def new_application
|
112
|
+
Rake::Application.new
|
113
|
+
end
|
114
|
+
|
115
|
+
# (Test) Helpers :
|
116
|
+
|
117
|
+
def create_schema_migrations_table(connection = ActiveRecord::Base.connection)
|
118
|
+
schema_migration = ActiveRecord::Migrator.schema_migrations_table_name
|
119
|
+
return if connection.table_exists?(schema_migration)
|
120
|
+
connection.create_table(schema_migration, :id => false) do |t|
|
121
|
+
t.column :version, :string, :null => false
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def create_rake_test_database
|
126
|
+
ActiveRecord::Base.establish_connection db_config
|
127
|
+
connection = ActiveRecord::Base.connection
|
128
|
+
connection.create_database(db_name, db_config) if connection.respond_to?(:create_database)
|
129
|
+
if block_given?
|
130
|
+
if db_name
|
131
|
+
config = db_config.merge :database => db_name
|
132
|
+
ActiveRecord::Base.establish_connection config
|
133
|
+
end
|
134
|
+
yield ActiveRecord::Base.connection
|
135
|
+
end
|
136
|
+
ActiveRecord::Base.connection.disconnect!
|
137
|
+
end
|
138
|
+
|
139
|
+
def drop_rake_test_database(silence = false)
|
140
|
+
ActiveRecord::Base.establish_connection db_config
|
141
|
+
begin
|
142
|
+
ActiveRecord::Base.connection.drop_database(db_name)
|
143
|
+
rescue => e
|
144
|
+
raise e unless silence
|
145
|
+
end
|
146
|
+
ActiveRecord::Base.connection.disconnect!
|
147
|
+
end
|
148
|
+
|
149
|
+
def structure_sql_filename
|
150
|
+
ar_version('3.2') ? 'structure.sql' : "#{@rails_env}_structure.sql"
|
151
|
+
end
|
152
|
+
|
153
|
+
def expect_rake_output(matcher)
|
154
|
+
@_stdout, @_stdout_matcher = $stdout, matcher
|
155
|
+
$stdout = StringIO.new
|
156
|
+
end
|
157
|
+
|
158
|
+
def verify_and_restore_stdout
|
159
|
+
if @_stdout ||= nil
|
160
|
+
_stdout = $stdout
|
161
|
+
$stdout = @_stdout
|
162
|
+
|
163
|
+
output = _stdout.string
|
164
|
+
if @_stdout_matcher.is_a?(String)
|
165
|
+
unless @_stdout_matcher.index("\n")
|
166
|
+
output = output.rstrip
|
167
|
+
end
|
168
|
+
assert_equal @_stdout_matcher, output
|
169
|
+
else
|
170
|
+
assert_match @_stdout_matcher, output
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
private :verify_and_restore_stdout
|
175
|
+
|
176
|
+
def rails_env
|
177
|
+
'unittest'
|
178
|
+
end
|
179
|
+
|
180
|
+
def db_name
|
181
|
+
'test_rake_db'
|
182
|
+
end
|
183
|
+
|
184
|
+
@@db_config = nil
|
185
|
+
|
186
|
+
def db_config
|
187
|
+
@@db_config ||= current_connection_config.reject { |key, val| val && key.to_s == 'url' }
|
188
|
+
end
|
189
|
+
|
190
|
+
def current_connection_config
|
191
|
+
if ActiveRecord::Base.respond_to?(:connection_config)
|
192
|
+
ActiveRecord::Base.connection_config
|
193
|
+
else
|
194
|
+
ActiveRecord::Base.connection_pool.spec.config
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
def configurations
|
199
|
+
@configurations ||= begin
|
200
|
+
db_config = self.db_config.dup
|
201
|
+
db_config.merge!(:database => @db_name) if @db_name ||= nil
|
202
|
+
db_config.stringify_keys!
|
203
|
+
raise "Rails.env not set" unless @rails_env ||= nil
|
204
|
+
configurations = { @rails_env => db_config }
|
205
|
+
configurations['test'] = db_config.dup
|
206
|
+
configurations
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
private
|
211
|
+
|
212
|
+
RAILS_2x = ActiveRecord::VERSION::MAJOR < 3
|
213
|
+
|
214
|
+
def setup_rails
|
215
|
+
RAILS_2x ? setup_rails2 : setup_rails3
|
216
|
+
end
|
217
|
+
|
218
|
+
def setup_rails2
|
219
|
+
test = self
|
220
|
+
Rails::Configuration.module_eval do
|
221
|
+
define_method(:database_configuration) { test.configurations }
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def setup_rails3
|
226
|
+
test = self
|
227
|
+
(class << Rails::Application.config; self ; end).instance_eval do
|
228
|
+
define_method(:database_configuration) { test.configurations }
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def set_rails_env(env); set_rails_constant("env", env); end
|
233
|
+
|
234
|
+
def set_rails_root(root = '.'); set_rails_constant("root", root); end
|
235
|
+
|
236
|
+
NO_VALUE = Java::JavaLang::Void rescue NilClass
|
237
|
+
|
238
|
+
def set_rails_constant(name, value)
|
239
|
+
name = name.to_s
|
240
|
+
|
241
|
+
@rails_constants ||= {}
|
242
|
+
begin
|
243
|
+
@rails_constants[name] = Object.const_get(rails_constant_name(name))
|
244
|
+
rescue NameError
|
245
|
+
@rails_constants[name] = NO_VALUE
|
246
|
+
end
|
247
|
+
silence_warnings { Object.const_set(rails_constant_name(name), value) } if RAILS_2x
|
248
|
+
|
249
|
+
case name
|
250
|
+
when 'env'
|
251
|
+
unless value.is_a?(ActiveSupport::StringInquirer)
|
252
|
+
value = ActiveSupport::StringInquirer.new(value)
|
253
|
+
end
|
254
|
+
when 'root'
|
255
|
+
unless value.is_a?(Pathname)
|
256
|
+
value = Pathname.new(value).realpath
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
Rails.instance_eval do
|
261
|
+
if methods(false).map(&:to_s).include?(name)
|
262
|
+
singleton_class = (class << self; self; end)
|
263
|
+
singleton_class.send(:alias_method, "orig_#{name}", name)
|
264
|
+
singleton_class.send(:define_method, name) { value }
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
def restore_rails
|
270
|
+
( @rails_constants ||= {} ).each do |name, value|
|
271
|
+
|
272
|
+
if value == NO_VALUE
|
273
|
+
Object.send(:remove_const, rails_constant_name(name)) if RAILS_2x
|
274
|
+
else
|
275
|
+
silence_warnings { Object.const_set(rails_constant_name(name), value) }
|
276
|
+
end
|
277
|
+
|
278
|
+
Rails.instance_eval do
|
279
|
+
if methods(false).map(&:to_s).include?(name)
|
280
|
+
singleton_class = (class << self; self; end)
|
281
|
+
singleton_class.send :remove_method, name
|
282
|
+
singleton_class.send :alias_method, name, "orig_#{name}"
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def rails_constant_name(name); "RAILS_#{name.upcase}"; end
|
290
|
+
|
291
|
+
def silence_warnings
|
292
|
+
prev, $VERBOSE = $VERBOSE, nil
|
293
|
+
yield
|
294
|
+
ensure
|
295
|
+
$VERBOSE = prev
|
296
|
+
end
|
297
|
+
|
298
|
+
end
|
data/test/serialize.rb
CHANGED
@@ -257,10 +257,8 @@ module SerializeTestMethods
|
|
257
257
|
myobj = MyObject.new('value1', 'value2')
|
258
258
|
topic = Topic.create(:content => myobj)
|
259
259
|
|
260
|
-
|
261
|
-
|
262
|
-
assert_raise(ActiveModel::MissingAttributeError) { Topic.select(:id).find(topic.id).content }
|
263
|
-
end
|
260
|
+
assert_equal myobj, Topic.select(:content).find(topic.id).content
|
261
|
+
assert_raise(ActiveModel::MissingAttributeError) { Topic.select(:id).find(topic.id).content }
|
264
262
|
ensure
|
265
263
|
ActiveRecord::Base.time_zone_aware_attributes = false
|
266
264
|
end if Test::Unit::TestCase.ar_version('3.2')
|
File without changes
|
data/test/simple.rb
CHANGED
@@ -66,10 +66,8 @@ module FixtureSetup
|
|
66
66
|
def setup
|
67
67
|
super
|
68
68
|
#
|
69
|
-
# just a random zone, unlikely to be local, and not
|
69
|
+
# just a random zone, unlikely to be local, and not UTC
|
70
70
|
Time.zone = 'Moscow' if Time.respond_to?(:zone)
|
71
|
-
#
|
72
|
-
DbType.create!
|
73
71
|
end
|
74
72
|
|
75
73
|
def teardown
|
@@ -112,13 +110,13 @@ module DirtyAttributeTests
|
|
112
110
|
user = User.create!(:login => 'cicina')
|
113
111
|
old_updated_at = 61.minutes.ago.in_time_zone
|
114
112
|
|
115
|
-
User
|
113
|
+
do_update_all(User, { :updated_at => old_updated_at }, :login => user.login)
|
116
114
|
|
117
115
|
with_partial_updates User, false do
|
118
116
|
assert_queries(1) { user.save! }
|
119
117
|
end
|
120
118
|
|
121
|
-
User
|
119
|
+
do_update_all(User, { :updated_at => old_updated_at }, :login => user.login)
|
122
120
|
|
123
121
|
with_partial_updates User, true do
|
124
122
|
assert_queries(0) { user.save! }
|
@@ -133,13 +131,13 @@ module DirtyAttributeTests
|
|
133
131
|
entry = Entry.create!(:title => 'foo')
|
134
132
|
old_updated_on = 25.hours.ago.beginning_of_day.in_time_zone
|
135
133
|
|
136
|
-
Entry
|
134
|
+
do_update_all(Entry, { :updated_on => old_updated_on }, :id => entry.id)
|
137
135
|
|
138
136
|
with_partial_updates Entry, false do
|
139
137
|
assert_queries(2) { 2.times { entry.save! } }
|
140
138
|
end
|
141
139
|
|
142
|
-
Entry
|
140
|
+
do_update_all(Entry, { :updated_on => old_updated_on }, :id => entry.id)
|
143
141
|
|
144
142
|
with_partial_updates Entry, true do
|
145
143
|
assert_queries(0) { 2.times { entry.save! } }
|
@@ -152,12 +150,34 @@ module DirtyAttributeTests
|
|
152
150
|
|
153
151
|
private
|
154
152
|
|
155
|
-
|
156
|
-
|
157
|
-
klass
|
158
|
-
|
159
|
-
|
160
|
-
|
153
|
+
if ActiveRecord::VERSION::MAJOR > 3
|
154
|
+
|
155
|
+
def with_partial_updates(klass, on = true)
|
156
|
+
old = klass.partial_writes?
|
157
|
+
klass.partial_writes = on
|
158
|
+
yield
|
159
|
+
ensure
|
160
|
+
klass.partial_writes = old
|
161
|
+
end
|
162
|
+
|
163
|
+
else
|
164
|
+
|
165
|
+
def with_partial_updates(klass, on = true)
|
166
|
+
old = klass.partial_updates?
|
167
|
+
klass.partial_updates = on
|
168
|
+
yield
|
169
|
+
ensure
|
170
|
+
klass.partial_updates = old
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
def do_update_all(model, values, conditions)
|
176
|
+
if ar_version('3.2')
|
177
|
+
model.where(conditions).update_all(values)
|
178
|
+
else # User.update_all values, conditions deprecated on 4.0
|
179
|
+
model.update_all(values, conditions)
|
180
|
+
end
|
161
181
|
end
|
162
182
|
|
163
183
|
end
|
@@ -292,24 +312,16 @@ module SimpleTestMethods
|
|
292
312
|
# precision will only be expected to the second :
|
293
313
|
original_time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
|
294
314
|
time = original_time.in_time_zone
|
295
|
-
e = DbType.
|
296
|
-
e.sample_datetime
|
297
|
-
e.save!
|
298
|
-
e = DbType.first
|
299
|
-
|
300
|
-
assert_equal time, e.sample_datetime
|
315
|
+
e = DbType.create! :sample_datetime => time
|
316
|
+
assert_equal time, e.reload.sample_datetime
|
301
317
|
end
|
302
318
|
|
303
319
|
def test_save_date_time
|
304
320
|
t = Time.now
|
305
321
|
# precision will only be expected to the second :
|
306
322
|
time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
|
307
|
-
|
308
|
-
|
309
|
-
e.sample_datetime = datetime
|
310
|
-
e.save!
|
311
|
-
e = DbType.first
|
312
|
-
assert_equal time, e.sample_datetime.localtime
|
323
|
+
e = DbType.create! :sample_datetime => time.to_datetime
|
324
|
+
assert_equal time, e.reload.sample_datetime.localtime
|
313
325
|
end
|
314
326
|
|
315
327
|
end
|
@@ -317,110 +329,89 @@ module SimpleTestMethods
|
|
317
329
|
def test_save_time
|
318
330
|
# Ruby doesn't have a plain Time class without a date.
|
319
331
|
time = Time.utc(2012, 12, 18, 21, 10, 15, 0)
|
320
|
-
e = DbType.
|
332
|
+
e = DbType.new
|
321
333
|
e.sample_time = time
|
322
334
|
e.save!
|
323
|
-
e = DbType.first
|
324
335
|
|
325
|
-
assert_time_equal time, e.sample_time
|
336
|
+
assert_time_equal time, e.reload.sample_time
|
326
337
|
end
|
327
338
|
|
328
339
|
def test_save_timestamp
|
329
340
|
timestamp = Time.utc(2012, 12, 18, 21, 10, 15, 0)
|
330
|
-
e = DbType.
|
341
|
+
e = DbType.create! :sample_datetime => Time.now
|
331
342
|
e.sample_timestamp = timestamp
|
332
343
|
e.save!
|
333
|
-
|
334
|
-
assert_timestamp_equal timestamp, e.sample_timestamp
|
344
|
+
assert_timestamp_equal timestamp, e.reload.sample_timestamp
|
335
345
|
end
|
336
346
|
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
# e = DbType.first
|
344
|
-
# assert_timestamp_equal timestamp, e.sample_timestamp
|
345
|
-
# end
|
347
|
+
def test_save_timestamp_with_usec
|
348
|
+
pend 'todo: support precision beyond seconds !?'
|
349
|
+
timestamp = Time.utc(1942, 11, 30, 01, 53, 59, 123_456)
|
350
|
+
e = DbType.create! :sample_timestamp => timestamp
|
351
|
+
assert_timestamp_equal timestamp, e.reload.sample_timestamp
|
352
|
+
end
|
346
353
|
|
347
354
|
def test_save_date
|
348
355
|
date = Date.new(2007)
|
349
|
-
e = DbType.
|
356
|
+
e = DbType.new
|
350
357
|
e.sample_date = date
|
351
|
-
e.save
|
352
|
-
e = DbType.first
|
358
|
+
e.save!; e.reload
|
353
359
|
assert_date_type e.sample_date
|
354
360
|
assert_date_equal date, e.sample_date
|
355
361
|
end
|
356
362
|
|
357
363
|
def test_save_float
|
358
|
-
e = DbType.
|
359
|
-
e.sample_float = 12.0
|
364
|
+
e = DbType.new :sample_float => 12.0
|
360
365
|
e.save!
|
361
|
-
|
362
|
-
e = DbType.first
|
363
|
-
assert_equal 12.0, e.sample_float
|
366
|
+
assert_equal 12.0, e.reload.sample_float
|
364
367
|
end
|
365
368
|
|
366
369
|
def test_boolean
|
367
|
-
e = DbType.
|
368
|
-
assert_nil e.sample_boolean # unset boolean should default to nil
|
370
|
+
e = DbType.create! :sample_float => 0
|
371
|
+
assert_nil e.reload.sample_boolean # unset boolean should default to nil
|
369
372
|
|
370
373
|
e.update_attributes :sample_boolean => false
|
371
|
-
|
372
|
-
e = DbType.first
|
373
|
-
assert_equal false, e.sample_boolean
|
374
|
+
assert_equal false, e.reload.sample_boolean
|
374
375
|
|
375
376
|
e.sample_boolean = true
|
376
377
|
e.save!
|
377
|
-
|
378
|
-
e = DbType.first
|
379
|
-
assert_equal true, e.sample_boolean
|
378
|
+
assert_equal true, e.reload.sample_boolean
|
380
379
|
end
|
381
380
|
|
382
381
|
def test_integer
|
383
|
-
e = DbType.
|
384
|
-
assert_nil e.sample_integer
|
382
|
+
e = DbType.create! :sample_boolean => false
|
383
|
+
assert_nil e.reload.sample_integer
|
385
384
|
|
386
385
|
e.sample_integer = 10
|
387
386
|
e.save!
|
388
|
-
|
389
|
-
e = DbType.first
|
390
|
-
assert_equal 10, e.sample_integer
|
387
|
+
assert_equal 10, e.reload.sample_integer
|
391
388
|
end
|
392
389
|
|
393
390
|
def test_text
|
394
|
-
e = DbType.
|
395
|
-
assert_null_text e.sample_text
|
391
|
+
e = DbType.create! :sample_boolean => false
|
392
|
+
assert_null_text e.reload.sample_text
|
396
393
|
|
397
394
|
e.sample_text = "ooop?"
|
398
395
|
e.save!
|
399
|
-
|
400
|
-
e = DbType.first
|
401
|
-
assert_equal "ooop?", e.sample_text
|
396
|
+
assert_equal "ooop?", e.reload.sample_text
|
402
397
|
end
|
403
398
|
|
404
399
|
def test_string
|
405
|
-
e = DbType.
|
406
|
-
assert_empty_string e.sample_string
|
400
|
+
e = DbType.create! :sample_boolean => false
|
401
|
+
assert_empty_string e.reload.sample_string
|
407
402
|
|
408
403
|
e.sample_string = "ooop?"
|
409
404
|
e.save!
|
410
|
-
|
411
|
-
e = DbType.first
|
412
|
-
assert_equal "ooop?", e.sample_string
|
405
|
+
assert_equal "ooop?", e.reload.sample_string
|
413
406
|
end
|
414
407
|
|
415
408
|
def test_save_binary
|
416
409
|
# string is 60_000 bytes
|
417
410
|
binary_string = "\000ABCDEFGHIJKLMNOPQRSTUVWXYZ'\001\003" * 1 # 2_000
|
418
|
-
e = DbType.
|
411
|
+
e = DbType.new
|
419
412
|
e.sample_binary = binary_string
|
420
413
|
e.save!
|
421
|
-
|
422
|
-
e = DbType.first
|
423
|
-
assert_equal binary_string, e.sample_binary
|
414
|
+
assert_equal binary_string, e.reload.sample_binary
|
424
415
|
end
|
425
416
|
|
426
417
|
def test_small_decimal
|
@@ -430,6 +421,14 @@ module SimpleTestMethods
|
|
430
421
|
db_type = DbType.find(db_type.id)
|
431
422
|
assert_kind_of BigDecimal, db_type.sample_small_decimal
|
432
423
|
assert_equal BigDecimal.new(test_value.to_s), db_type.sample_small_decimal
|
424
|
+
|
425
|
+
test_value = BigDecimal('1.23')
|
426
|
+
db_type = DbType.create!(:sample_small_decimal => test_value)
|
427
|
+
if ar_version('3.0')
|
428
|
+
assert_equal 1, DbType.where("sample_small_decimal < ?", 1.5).count
|
429
|
+
else
|
430
|
+
assert_equal 1, DbType.find(:all, :conditions => ["sample_small_decimal < ?", 1.5]).size
|
431
|
+
end
|
433
432
|
end
|
434
433
|
|
435
434
|
def test_decimal # _with_zero_scale
|
@@ -440,6 +439,12 @@ module SimpleTestMethods
|
|
440
439
|
assert_equal test_value.to_i, db_type.sample_decimal
|
441
440
|
end
|
442
441
|
|
442
|
+
def test_decimal_with_scale
|
443
|
+
test_value = BigDecimal("100023400056.795")
|
444
|
+
db_type = DbType.create!(:decimal_with_scale => test_value)
|
445
|
+
assert_equal test_value, db_type.reload.decimal_with_scale
|
446
|
+
end
|
447
|
+
|
443
448
|
def test_big_decimal
|
444
449
|
test_value = 9876543210_9876543210_9876543210.0
|
445
450
|
db_type = DbType.create!(:big_decimal => test_value)
|
@@ -448,6 +453,13 @@ module SimpleTestMethods
|
|
448
453
|
assert_equal test_value, db_type.big_decimal
|
449
454
|
end
|
450
455
|
|
456
|
+
# NOTE: relevant on 4.0 as it started using empty_insert_statement_value
|
457
|
+
def test_empty_insert_statement
|
458
|
+
DbType.create!
|
459
|
+
assert DbType.first
|
460
|
+
assert_not_nil DbType.first.id
|
461
|
+
end
|
462
|
+
|
451
463
|
def test_negative_default_value
|
452
464
|
assert_equal(-1, DbType.columns_hash['sample_integer_neg_default'].default)
|
453
465
|
assert_equal(-1, DbType.new.sample_integer_neg_default)
|
@@ -483,17 +495,17 @@ module SimpleTestMethods
|
|
483
495
|
end
|
484
496
|
|
485
497
|
def test_nil_values
|
486
|
-
|
487
|
-
assert_nil
|
498
|
+
e = DbType.create! :sample_integer => '', :sample_string => 'sample'
|
499
|
+
assert_nil e.reload.sample_integer
|
488
500
|
end
|
489
501
|
|
490
502
|
# These should make no difference, but might due to the wacky regexp SQL rewriting we do.
|
491
503
|
def test_save_value_containing_sql
|
492
|
-
e = DbType.
|
493
|
-
e.save
|
504
|
+
e = DbType.new :sample_string => 'sample'
|
505
|
+
e.save!
|
494
506
|
|
495
507
|
e.sample_string = e.sample_text = "\n\nselect from nothing where id = 'foo'"
|
496
|
-
e.save
|
508
|
+
e.save!
|
497
509
|
end
|
498
510
|
|
499
511
|
def test_invalid
|
@@ -508,6 +520,7 @@ module SimpleTestMethods
|
|
508
520
|
end
|
509
521
|
|
510
522
|
def test_reconnect
|
523
|
+
DbType.create! :sample_string => 'sample'
|
511
524
|
assert_equal 1, DbType.count
|
512
525
|
ActiveRecord::Base.connection.reconnect!
|
513
526
|
assert_equal 1, DbType.count
|
@@ -531,13 +544,16 @@ module SimpleTestMethods
|
|
531
544
|
class Animal < ActiveRecord::Base; end
|
532
545
|
|
533
546
|
def test_fetching_columns_for_nonexistent_table
|
534
|
-
|
535
|
-
|
547
|
+
disable_logger(Animal.connection) do
|
548
|
+
assert_raise(ActiveRecord::StatementInvalid, ActiveRecord::JDBCError) do
|
549
|
+
Animal.columns
|
550
|
+
end
|
536
551
|
end
|
537
552
|
end
|
538
553
|
end
|
539
554
|
|
540
555
|
def test_disconnect
|
556
|
+
DbType.create! :sample_string => 'sample'
|
541
557
|
assert_equal 1, DbType.count
|
542
558
|
ActiveRecord::Base.clear_active_connections!
|
543
559
|
ActiveRecord::Base.connection_pool.disconnect! if ActiveRecord::Base.respond_to?(:connection_pool)
|
@@ -702,7 +718,8 @@ module SimpleTestMethods
|
|
702
718
|
|
703
719
|
sql = "UPDATE entries SET title = ? WHERE id = #{entry.id}"
|
704
720
|
name = "UPDATE(raw_with_q_mark)"
|
705
|
-
|
721
|
+
title_column = Entry.columns.find { |n| n.to_s == 'title' }
|
722
|
+
connection.update sql, name, [ [ title_column, "bar?" ] ]
|
706
723
|
assert_equal 'bar?', entry.reload.title
|
707
724
|
|
708
725
|
sql = "UPDATE entries SET title = ? WHERE id = ?"
|
@@ -856,6 +873,27 @@ module SimpleTestMethods
|
|
856
873
|
assert_equal 'user1', result[0]['login']
|
857
874
|
assert_equal 'user2', result[1]['login']
|
858
875
|
end
|
876
|
+
|
877
|
+
def test_exec_query_raw_yields
|
878
|
+
User.create! :login => 'user3'
|
879
|
+
User.create! :login => 'user4'
|
880
|
+
|
881
|
+
arel = User.select('id, login, created_at').where("login = 'user3' or login = 'user4'")
|
882
|
+
yielded = 0
|
883
|
+
ActiveRecord::Base.connection.exec_query_raw(arel) do |*args| # id, login, created_at
|
884
|
+
assert_equal 3, args.size
|
885
|
+
yielded += 1
|
886
|
+
case yielded
|
887
|
+
when 1
|
888
|
+
assert_equal 'user3', args[1]
|
889
|
+
when 2
|
890
|
+
assert_equal 'user4', args[1]
|
891
|
+
else
|
892
|
+
fail "yielded 3 times"
|
893
|
+
end
|
894
|
+
end
|
895
|
+
assert yielded == 2
|
896
|
+
end if Test::Unit::TestCase.ar_version('3.0')
|
859
897
|
|
860
898
|
def test_select
|
861
899
|
Entry.delete_all
|
@@ -917,11 +955,40 @@ module SimpleTestMethods
|
|
917
955
|
end
|
918
956
|
end
|
919
957
|
|
958
|
+
def test_update
|
959
|
+
user = User.create! :login => 'update'
|
960
|
+
|
961
|
+
User.update(user.id, :login => 'UPDATEd')
|
962
|
+
assert_equal 'UPDATEd', user.reload.login
|
963
|
+
end
|
964
|
+
|
920
965
|
def test_connection_alive_sql
|
921
966
|
connection = ActiveRecord::Base.connection
|
922
|
-
alive_sql = connection.config[:connection_alive_sql]
|
923
|
-
|
924
|
-
|
967
|
+
if alive_sql = connection.config[:connection_alive_sql]
|
968
|
+
connection.execute alive_sql
|
969
|
+
end
|
970
|
+
# if no alive SQL than JDBC 4.0 driver's "alive" test will be used
|
971
|
+
end
|
972
|
+
|
973
|
+
def test_connection_valid
|
974
|
+
connection = ActiveRecord::Base.connection
|
975
|
+
assert connection.active? # JDBC connection.isValid (if alive_sql not set)
|
976
|
+
end
|
977
|
+
|
978
|
+
def test_query_cache
|
979
|
+
user_1 = User.create! :login => 'query_cache_1'
|
980
|
+
user_2 = User.create! :login => 'query_cache_2'
|
981
|
+
user_3 = User.create! :login => 'query_cache_3'
|
982
|
+
# NOTE: on 3.1 AR::Base.cache does not cache if AR not configured,
|
983
|
+
# due : `if ActiveRecord::Base.configurations.blank?; yield ...`
|
984
|
+
User.connection.cache do # instead of simply `User.cache`
|
985
|
+
id1 = user_1.id; id2 = user_2.id
|
986
|
+
assert_queries(2) { User.find(id1); User.find(id1); User.find(id2); User.find(id1) }
|
987
|
+
end
|
988
|
+
User.connection.uncached do
|
989
|
+
id1 = user_1.id; id3 = user_3.id
|
990
|
+
assert_queries(3) { User.find(id3); User.find(id1); User.find(id3) }
|
991
|
+
end
|
925
992
|
end
|
926
993
|
|
927
994
|
protected
|
@@ -1022,7 +1089,9 @@ module XmlColumnTests
|
|
1022
1089
|
module TestMethods
|
1023
1090
|
|
1024
1091
|
def test_create_xml_column
|
1025
|
-
create_xml_models!
|
1092
|
+
create_xml_models! do |t|
|
1093
|
+
skip('TableDefinition#xml not-implemented') unless t.respond_to?(:xml)
|
1094
|
+
end
|
1026
1095
|
|
1027
1096
|
xml_column = connection.columns(:xml_models).detect do |c|
|
1028
1097
|
c.name == "xml_col"
|
@@ -1047,7 +1116,7 @@ module XmlColumnTests
|
|
1047
1116
|
end
|
1048
1117
|
|
1049
1118
|
else
|
1050
|
-
|
1119
|
+
skip('TableDefinition#xml not-implemented')
|
1051
1120
|
end
|
1052
1121
|
ensure
|
1053
1122
|
drop_xml_models! if created
|
@@ -1066,11 +1135,16 @@ module XmlColumnTests
|
|
1066
1135
|
private
|
1067
1136
|
|
1068
1137
|
def create_xml_models!
|
1069
|
-
connection.create_table(:xml_models)
|
1138
|
+
connection.create_table(:xml_models) do |t|
|
1139
|
+
yield(t) if block_given?
|
1140
|
+
t.xml :xml_col
|
1141
|
+
end
|
1070
1142
|
end
|
1071
1143
|
|
1072
1144
|
def drop_xml_models!
|
1073
|
-
connection
|
1145
|
+
disable_logger(connection) do
|
1146
|
+
connection.drop_table(:xml_models)
|
1147
|
+
end
|
1074
1148
|
end
|
1075
1149
|
|
1076
1150
|
end
|
@@ -1088,9 +1162,9 @@ module ActiveRecord3TestMethods
|
|
1088
1162
|
def test_visitor_accessor
|
1089
1163
|
adapter = Entry.connection
|
1090
1164
|
adapter_spec = adapter.config[:adapter_spec]
|
1091
|
-
|
1165
|
+
visitor_type = adapter_spec.arel2_visitors(adapter.config).values.first
|
1092
1166
|
assert_not_nil adapter.visitor
|
1093
|
-
|
1167
|
+
assert_kind_of visitor_type, adapter.visitor
|
1094
1168
|
end if Test::Unit::TestCase.ar_version('3.2') # >= 3.2
|
1095
1169
|
|
1096
1170
|
def test_where
|
@@ -1118,7 +1192,7 @@ module ActiveRecord3TestMethods
|
|
1118
1192
|
assert_nothing_raised do
|
1119
1193
|
Thing.create! :name => "a thing"
|
1120
1194
|
end
|
1121
|
-
assert_equal 1, Thing.
|
1195
|
+
assert_equal 1, Thing.count
|
1122
1196
|
end
|
1123
1197
|
|
1124
1198
|
end
|