2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2013-01-30 07:55:01 -05:00
|
|
|
require "models/book"
|
2014-01-29 21:17:10 -05:00
|
|
|
require "models/post"
|
|
|
|
require "models/author"
|
2016-01-30 09:25:11 -05:00
|
|
|
require "models/event"
|
2005-09-23 09:29:33 -04:00
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
module ActiveRecord
|
|
|
|
class AdapterTest < ActiveRecord::TestCase
|
|
|
|
def setup
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
end
|
2008-05-06 19:08:23 -04:00
|
|
|
|
2013-01-30 07:55:01 -05:00
|
|
|
##
|
|
|
|
# PostgreSQL does not support null bytes in strings
|
2016-02-28 22:22:08 -05:00
|
|
|
unless current_adapter?(:PostgreSQLAdapter) ||
|
|
|
|
(current_adapter?(:SQLite3Adapter) && !ActiveRecord::Base.connection.prepared_statements)
|
2013-01-30 07:55:01 -05:00
|
|
|
def test_update_prepared_statement
|
|
|
|
b = Book.create(name: "my \x00 book")
|
|
|
|
b.reload
|
|
|
|
assert_equal "my \x00 book", b.name
|
2018-02-14 21:33:02 -05:00
|
|
|
b.update(name: "my other \x00 book")
|
2013-01-30 07:55:01 -05:00
|
|
|
b.reload
|
|
|
|
assert_equal "my other \x00 book", b.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-29 16:54:15 -04:00
|
|
|
def test_create_record_with_pk_as_zero
|
|
|
|
Book.create(id: 0)
|
|
|
|
assert_equal 0, Book.find(0).id
|
|
|
|
assert_nothing_raised { Book.destroy(0) }
|
|
|
|
end
|
|
|
|
|
2017-02-28 05:42:02 -05:00
|
|
|
def test_valid_column
|
|
|
|
@connection.native_database_types.each_key do |type|
|
|
|
|
assert @connection.valid_type?(type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_column
|
|
|
|
assert_not @connection.valid_type?(:foobar)
|
|
|
|
end
|
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_tables
|
2016-12-29 03:33:15 -05:00
|
|
|
tables = @connection.tables
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes tables, "accounts"
|
|
|
|
assert_includes tables, "authors"
|
|
|
|
assert_includes tables, "tasks"
|
|
|
|
assert_includes tables, "topics"
|
2011-11-29 16:05:57 -05:00
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_table_exists?
|
2016-12-29 03:33:15 -05:00
|
|
|
assert @connection.table_exists?("accounts")
|
|
|
|
assert @connection.table_exists?(:accounts)
|
|
|
|
assert_not @connection.table_exists?("nonexistingtable")
|
|
|
|
assert_not @connection.table_exists?("'")
|
|
|
|
assert_not @connection.table_exists?(nil)
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
|
|
|
|
2015-09-22 09:58:18 -04:00
|
|
|
def test_data_sources
|
|
|
|
data_sources = @connection.data_sources
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes data_sources, "accounts"
|
|
|
|
assert_includes data_sources, "authors"
|
|
|
|
assert_includes data_sources, "tasks"
|
|
|
|
assert_includes data_sources, "topics"
|
2015-09-22 09:58:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_data_source_exists?
|
|
|
|
assert @connection.data_source_exists?("accounts")
|
|
|
|
assert @connection.data_source_exists?(:accounts)
|
|
|
|
assert_not @connection.data_source_exists?("nonexistingtable")
|
2016-10-13 15:50:38 -04:00
|
|
|
assert_not @connection.data_source_exists?("'")
|
2015-09-22 09:58:18 -04:00
|
|
|
assert_not @connection.data_source_exists?(nil)
|
|
|
|
end
|
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_indexes
|
|
|
|
idx_name = "accounts_idx"
|
|
|
|
|
2016-10-02 17:49:27 -04:00
|
|
|
indexes = @connection.indexes("accounts")
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty indexes
|
2016-10-02 17:49:27 -04:00
|
|
|
|
|
|
|
@connection.add_index :accounts, :firm_id, name: idx_name
|
|
|
|
indexes = @connection.indexes("accounts")
|
|
|
|
assert_equal "accounts", indexes.first.table
|
|
|
|
assert_equal idx_name, indexes.first.name
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not indexes.first.unique
|
2016-10-02 17:49:27 -04:00
|
|
|
assert_equal ["firm_id"], indexes.first.columns
|
2011-11-29 16:05:57 -05:00
|
|
|
ensure
|
2016-08-06 13:37:57 -04:00
|
|
|
@connection.remove_index(:accounts, name: idx_name) rescue nil
|
2006-03-17 22:54:04 -05:00
|
|
|
end
|
2005-10-16 00:05:15 -04:00
|
|
|
|
2016-04-11 13:57:19 -04:00
|
|
|
def test_remove_index_when_name_and_wrong_column_name_specified
|
|
|
|
index_name = "accounts_idx"
|
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
@connection.add_index :accounts, :firm_id, name: index_name
|
2016-04-11 13:57:19 -04:00
|
|
|
assert_raises ArgumentError do
|
2016-08-06 13:37:57 -04:00
|
|
|
@connection.remove_index :accounts, name: index_name, column: :wrong_column_name
|
2016-04-11 13:57:19 -04:00
|
|
|
end
|
|
|
|
ensure
|
2016-08-06 13:37:57 -04:00
|
|
|
@connection.remove_index(:accounts, name: index_name)
|
2016-04-11 13:57:19 -04:00
|
|
|
end
|
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_current_database
|
|
|
|
if @connection.respond_to?(:current_database)
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal ARTest.connection_config["arunit"]["database"], @connection.current_database
|
2011-11-29 16:05:57 -05:00
|
|
|
end
|
2007-05-25 20:20:37 -04:00
|
|
|
end
|
|
|
|
|
2015-12-15 17:01:30 -05:00
|
|
|
if current_adapter?(:Mysql2Adapter)
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_charset
|
|
|
|
assert_not_nil @connection.charset
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_not_equal "character_set_database", @connection.charset
|
|
|
|
assert_equal @connection.show_variable("character_set_database"), @connection.charset
|
2011-11-29 16:05:57 -05:00
|
|
|
end
|
2007-06-20 14:30:35 -04:00
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_collation
|
|
|
|
assert_not_nil @connection.collation
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_not_equal "collation_database", @connection.collation
|
|
|
|
assert_equal @connection.show_variable("collation_database"), @connection.collation
|
2011-11-29 16:05:57 -05:00
|
|
|
end
|
2009-08-09 16:22:53 -04:00
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_show_nonexistent_variable_returns_nil
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_nil @connection.show_variable("foo_bar_baz")
|
2011-11-29 16:05:57 -05:00
|
|
|
end
|
2011-06-05 06:28:02 -04:00
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_not_specifying_database_name_for_cross_database_selects
|
|
|
|
begin
|
|
|
|
assert_nothing_raised do
|
2016-08-06 12:26:20 -04:00
|
|
|
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["arunit"].except(:database))
|
2011-11-29 16:05:57 -05:00
|
|
|
|
|
|
|
config = ARTest.connection_config
|
2012-10-26 10:51:02 -04:00
|
|
|
ActiveRecord::Base.connection.execute(
|
2011-11-29 16:05:57 -05:00
|
|
|
"SELECT #{config['arunit']['database']}.pirates.*, #{config['arunit2']['database']}.courses.* " \
|
|
|
|
"FROM #{config['arunit']['database']}.pirates, #{config['arunit2']['database']}.courses"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
ensure
|
2013-12-25 04:54:25 -05:00
|
|
|
ActiveRecord::Base.establish_connection :arunit
|
2010-05-25 09:11:23 -04:00
|
|
|
end
|
2009-08-09 16:22:53 -04:00
|
|
|
end
|
|
|
|
end
|
2007-05-25 20:20:37 -04:00
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_table_alias
|
|
|
|
def @connection.test_table_alias_length() 10; end
|
|
|
|
class << @connection
|
|
|
|
alias_method :old_table_alias_length, :table_alias_length
|
|
|
|
alias_method :table_alias_length, :test_table_alias_length
|
|
|
|
end
|
2007-05-25 20:20:37 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "posts", @connection.table_alias_for("posts")
|
|
|
|
assert_equal "posts_comm", @connection.table_alias_for("posts_comments")
|
|
|
|
assert_equal "dbo_posts", @connection.table_alias_for("dbo.posts")
|
2007-05-25 20:20:37 -04:00
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
class << @connection
|
|
|
|
remove_method :table_alias_length
|
|
|
|
alias_method :table_alias_length, :old_table_alias_length
|
|
|
|
end
|
2006-03-18 02:31:01 -05:00
|
|
|
end
|
2006-03-18 00:43:35 -05:00
|
|
|
|
2011-11-29 16:05:57 -05:00
|
|
|
def test_uniqueness_violations_are_translated_to_specific_exception
|
2009-04-04 19:19:29 -04:00
|
|
|
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
2015-11-03 09:54:34 -05:00
|
|
|
error = assert_raises(ActiveRecord::RecordNotUnique) do
|
2011-11-29 16:05:57 -05:00
|
|
|
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
|
|
|
end
|
2015-11-03 09:54:34 -05:00
|
|
|
|
|
|
|
assert_not_nil error.cause
|
2009-04-04 19:19:29 -04:00
|
|
|
end
|
2014-05-16 14:05:45 -04:00
|
|
|
|
2016-06-20 11:38:48 -04:00
|
|
|
def test_not_null_violations_are_translated_to_specific_exception
|
|
|
|
error = assert_raises(ActiveRecord::NotNullViolation) do
|
|
|
|
Post.create
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_nil error.cause
|
|
|
|
end
|
|
|
|
|
2014-04-02 06:49:57 -04:00
|
|
|
unless current_adapter?(:SQLite3Adapter)
|
2016-01-30 09:25:11 -05:00
|
|
|
def test_value_limit_violations_are_translated_to_specific_exception
|
|
|
|
error = assert_raises(ActiveRecord::ValueTooLong) do
|
2016-08-06 12:26:20 -04:00
|
|
|
Event.create(title: "abcdefgh")
|
2016-01-30 09:25:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_nil error.cause
|
|
|
|
end
|
2016-05-31 15:00:05 -04:00
|
|
|
|
|
|
|
def test_numeric_value_out_of_ranges_are_translated_to_specific_exception
|
|
|
|
error = assert_raises(ActiveRecord::RangeError) do
|
2016-12-11 17:42:46 -05:00
|
|
|
Book.connection.create("INSERT INTO books(author_id) VALUES (9223372036854775808)")
|
2016-05-31 15:00:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_nil error.cause
|
|
|
|
end
|
2014-03-26 21:23:42 -04:00
|
|
|
end
|
|
|
|
|
2017-07-05 18:21:25 -04:00
|
|
|
def test_exceptions_from_notifications_are_not_translated
|
2017-07-06 13:33:36 -04:00
|
|
|
original_error = StandardError.new("This StandardError shouldn't get translated")
|
2017-07-05 18:21:25 -04:00
|
|
|
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") { raise original_error }
|
2017-07-06 13:33:36 -04:00
|
|
|
actual_error = assert_raises(StandardError) do
|
2017-07-05 18:21:25 -04:00
|
|
|
@connection.execute("SELECT * FROM posts")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal original_error, actual_error
|
|
|
|
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
|
|
|
|
end
|
|
|
|
|
2017-07-07 14:28:17 -04:00
|
|
|
def test_database_related_exceptions_are_translated_to_statement_invalid
|
2017-07-05 18:21:25 -04:00
|
|
|
error = assert_raises(ActiveRecord::StatementInvalid) do
|
|
|
|
@connection.execute("This is a syntax error")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_of ActiveRecord::StatementInvalid, error
|
2017-07-07 14:28:47 -04:00
|
|
|
assert_kind_of Exception, error.cause
|
2017-07-05 18:21:25 -04:00
|
|
|
end
|
|
|
|
|
2013-07-16 10:48:18 -04:00
|
|
|
def test_select_all_always_return_activerecord_result
|
|
|
|
result = @connection.select_all "SELECT * FROM posts"
|
|
|
|
assert result.is_a?(ActiveRecord::Result)
|
|
|
|
end
|
2013-12-03 11:54:25 -05:00
|
|
|
|
2017-02-07 18:52:21 -05:00
|
|
|
if ActiveRecord::Base.connection.prepared_statements
|
|
|
|
def test_select_all_with_legacy_binds
|
|
|
|
post = Post.create!(title: "foo", body: "bar")
|
|
|
|
expected = @connection.select_all("SELECT * FROM posts WHERE id = #{post.id}")
|
Refactor Active Record to let Arel manage bind params
A common source of bugs and code bloat within Active Record has been the
need for us to maintain the list of bind values separately from the AST
they're associated with. This makes any sort of AST manipulation
incredibly difficult, as any time we want to potentially insert or
remove an AST node, we need to traverse the entire tree to find where
the associated bind parameters are.
With this change, the bind parameters now live on the AST directly.
Active Record does not need to know or care about them until the final
AST traversal for SQL construction. Rather than returning just the SQL,
the Arel collector will now return both the SQL and the bind parameters.
At this point the connection adapter will have all the values that it
had before.
A bit of this code is janky and something I'd like to refactor later. In
particular, I don't like how we're handling associations in the
predicate builder, the special casing of `StatementCache::Substitute` in
`QueryAttribute`, or generally how we're handling bind value replacement
in the statement cache when prepared statements are disabled.
This also mostly reverts #26378, as it moved all the code into a
location that I wanted to delete.
/cc @metaskills @yahonda, this change will affect the adapters
Fixes #29766.
Fixes #29804.
Fixes #26541.
Close #28539.
Close #24769.
Close #26468.
Close #26202.
There are probably other issues/PRs that can be closed because of this
commit, but that's all I could find on the first few pages.
2017-07-24 08:19:35 -04:00
|
|
|
result = @connection.select_all("SELECT * FROM posts WHERE id = #{Arel::Nodes::BindParam.new(nil).to_sql}", nil, [[nil, post.id]])
|
2017-02-07 18:52:21 -05:00
|
|
|
assert_equal expected.to_hash, result.to_hash
|
|
|
|
end
|
2017-08-18 02:17:07 -04:00
|
|
|
|
|
|
|
def test_insert_update_delete_with_legacy_binds
|
|
|
|
binds = [[nil, 1]]
|
|
|
|
bind_param = Arel::Nodes::BindParam.new(nil)
|
|
|
|
|
|
|
|
id = @connection.insert("INSERT INTO events(id) VALUES (#{bind_param.to_sql})", nil, nil, nil, nil, binds)
|
|
|
|
assert_equal 1, id
|
|
|
|
|
|
|
|
@connection.update("UPDATE events SET title = 'foo' WHERE id = #{bind_param.to_sql}", nil, binds)
|
|
|
|
result = @connection.select_all("SELECT * FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
|
|
|
|
assert_equal({ "id" => 1, "title" => "foo" }, result.first)
|
|
|
|
|
|
|
|
@connection.delete("DELETE FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
|
|
|
|
result = @connection.select_all("SELECT * FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
|
|
|
|
assert_nil result.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_insert_update_delete_with_binds
|
|
|
|
binds = [Relation::QueryAttribute.new("id", 1, Type.default_value)]
|
|
|
|
bind_param = Arel::Nodes::BindParam.new(nil)
|
|
|
|
|
|
|
|
id = @connection.insert("INSERT INTO events(id) VALUES (#{bind_param.to_sql})", nil, nil, nil, nil, binds)
|
|
|
|
assert_equal 1, id
|
|
|
|
|
|
|
|
@connection.update("UPDATE events SET title = 'foo' WHERE id = #{bind_param.to_sql}", nil, binds)
|
|
|
|
result = @connection.select_all("SELECT * FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
|
|
|
|
assert_equal({ "id" => 1, "title" => "foo" }, result.first)
|
|
|
|
|
|
|
|
@connection.delete("DELETE FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
|
|
|
|
result = @connection.select_all("SELECT * FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
|
|
|
|
assert_nil result.first
|
|
|
|
end
|
2017-02-07 18:52:21 -05:00
|
|
|
end
|
|
|
|
|
2014-01-29 21:17:10 -05:00
|
|
|
def test_select_methods_passing_a_association_relation
|
2016-08-06 12:26:20 -04:00
|
|
|
author = Author.create!(name: "john")
|
|
|
|
Post.create!(author: author, title: "foo", body: "bar")
|
|
|
|
query = author.posts.where(title: "foo").select(:title)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "title" => "foo" }, @connection.select_one(query))
|
2014-01-29 21:17:10 -05:00
|
|
|
assert @connection.select_all(query).is_a?(ActiveRecord::Result)
|
|
|
|
assert_equal "foo", @connection.select_value(query)
|
|
|
|
assert_equal ["foo"], @connection.select_values(query)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_select_methods_passing_a_relation
|
2016-08-06 12:26:20 -04:00
|
|
|
Post.create!(title: "foo", body: "bar")
|
|
|
|
query = Post.where(title: "foo").select(:title)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "title" => "foo" }, @connection.select_one(query))
|
2014-01-29 21:17:10 -05:00
|
|
|
assert @connection.select_all(query).is_a?(ActiveRecord::Result)
|
|
|
|
assert_equal "foo", @connection.select_value(query)
|
|
|
|
assert_equal ["foo"], @connection.select_values(query)
|
|
|
|
end
|
|
|
|
|
2013-12-03 11:54:25 -05:00
|
|
|
test "type_to_sql returns a String for unmapped types" do
|
|
|
|
assert_equal "special_db_type", @connection.type_to_sql(:special_db_type)
|
|
|
|
end
|
2015-01-02 14:15:18 -05:00
|
|
|
|
2015-01-02 16:58:46 -05:00
|
|
|
unless current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_log_invalid_encoding
|
2016-12-12 16:51:39 -05:00
|
|
|
error = assert_raises RuntimeError do
|
2015-01-02 16:58:46 -05:00
|
|
|
@connection.send :log, "SELECT 'ы' FROM DUAL" do
|
2017-08-19 08:11:32 -04:00
|
|
|
raise "ы".dup.force_encoding(Encoding::ASCII_8BIT)
|
2015-01-02 16:58:46 -05:00
|
|
|
end
|
2015-01-02 14:15:18 -05:00
|
|
|
end
|
2015-11-03 09:54:34 -05:00
|
|
|
|
2017-08-19 08:11:32 -04:00
|
|
|
assert_equal "ы", error.message
|
2015-01-02 14:15:18 -05:00
|
|
|
end
|
|
|
|
end
|
2011-06-30 19:29:53 -04:00
|
|
|
end
|
2012-09-14 18:59:35 -04:00
|
|
|
|
2016-06-20 19:39:05 -04:00
|
|
|
class AdapterForeignKeyTest < ActiveRecord::TestCase
|
|
|
|
self.use_transactional_tests = false
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_foreign_key_violations_are_translated_to_specific_exception_with_validate_false
|
|
|
|
klass_has_fk = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "fk_test_has_fk"
|
|
|
|
end
|
|
|
|
|
|
|
|
error = assert_raises(ActiveRecord::InvalidForeignKey) do
|
|
|
|
has_fk = klass_has_fk.new
|
|
|
|
has_fk.fk_id = 1231231231
|
|
|
|
has_fk.save(validate: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_nil error.cause
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_foreign_key_violations_are_translated_to_specific_exception
|
|
|
|
error = assert_raises(ActiveRecord::InvalidForeignKey) do
|
|
|
|
insert_into_fk_test_has_fk
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_nil error.cause
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_disable_referential_integrity
|
|
|
|
assert_nothing_raised do
|
|
|
|
@connection.disable_referential_integrity do
|
|
|
|
insert_into_fk_test_has_fk
|
|
|
|
# should delete created record as otherwise disable_referential_integrity will try to enable constraints
|
|
|
|
# after executed block and will fail (at least on Oracle)
|
|
|
|
@connection.execute "DELETE FROM fk_test_has_fk"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def insert_into_fk_test_has_fk
|
|
|
|
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
|
|
|
|
if @connection.prefetch_primary_key?
|
|
|
|
id_value = @connection.next_sequence_value(@connection.default_sequence_name("fk_test_has_fk", "id"))
|
|
|
|
@connection.execute "INSERT INTO fk_test_has_fk (id,fk_id) VALUES (#{id_value},0)"
|
|
|
|
else
|
|
|
|
@connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-14 18:59:35 -04:00
|
|
|
class AdapterTestWithoutTransaction < ActiveRecord::TestCase
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2012-09-14 18:59:35 -04:00
|
|
|
|
2013-01-19 20:33:14 -05:00
|
|
|
class Klass < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
2012-09-14 18:59:35 -04:00
|
|
|
def setup
|
2013-12-24 04:18:54 -05:00
|
|
|
Klass.establish_connection :arunit
|
2013-01-19 20:33:14 -05:00
|
|
|
@connection = Klass.connection
|
2012-09-14 18:59:35 -04:00
|
|
|
end
|
|
|
|
|
2014-03-14 00:35:58 -04:00
|
|
|
teardown do
|
2013-01-19 20:33:14 -05:00
|
|
|
Klass.remove_connection
|
2012-09-14 18:59:35 -04:00
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
unless in_memory_db?
|
|
|
|
test "transaction state is reset after a reconnect" do
|
|
|
|
@connection.begin_transaction
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @connection, :transaction_open?
|
2013-11-08 10:57:51 -05:00
|
|
|
@connection.reconnect!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @connection, :transaction_open?
|
2013-11-08 10:57:51 -05:00
|
|
|
end
|
2012-09-14 18:59:35 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
test "transaction state is reset after a disconnect" do
|
|
|
|
@connection.begin_transaction
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @connection, :transaction_open?
|
2013-11-08 10:57:51 -05:00
|
|
|
@connection.disconnect!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @connection, :transaction_open?
|
2013-11-08 10:57:51 -05:00
|
|
|
end
|
2012-09-14 18:59:35 -04:00
|
|
|
end
|
2017-07-17 02:06:38 -04:00
|
|
|
|
|
|
|
# test resetting sequences in odd tables in PostgreSQL
|
|
|
|
if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
|
|
|
|
require "models/movie"
|
|
|
|
require "models/subscriber"
|
|
|
|
|
|
|
|
def test_reset_empty_table_with_custom_pk
|
|
|
|
Movie.delete_all
|
|
|
|
Movie.connection.reset_pk_sequence! "movies"
|
|
|
|
assert_equal 1, Movie.create(name: "fight club").id
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reset_table_with_non_integer_pk
|
|
|
|
Subscriber.delete_all
|
|
|
|
Subscriber.connection.reset_pk_sequence! "subscribers"
|
|
|
|
sub = Subscriber.new(name: "robert drake")
|
|
|
|
sub.id = "bob drake"
|
|
|
|
assert_nothing_raised { sub.save! }
|
|
|
|
end
|
|
|
|
end
|
2012-09-14 18:59:35 -04:00
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|