mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
parent
249e943a0d
commit
af9878d640
13 changed files with 90 additions and 101 deletions
|
@ -125,7 +125,7 @@ This would also define the following accessors: <tt>Product#name</tt> and
|
|||
)
|
||||
|
||||
{Learn more}[link:classes/ActiveRecord/Base.html] and read about the built-in support for
|
||||
MySQL[link:classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html],
|
||||
MySQL[link:classes/ActiveRecord/ConnectionAdapters/Mysql2Adapter.html],
|
||||
PostgreSQL[link:classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html], and
|
||||
SQLite3[link:classes/ActiveRecord/ConnectionAdapters/SQLite3Adapter.html].
|
||||
|
||||
|
|
|
@ -208,7 +208,7 @@ module ActiveRecord
|
|||
# * You are joining an existing open transaction
|
||||
# * You are creating a nested (savepoint) transaction
|
||||
#
|
||||
# The mysql, mysql2 and postgresql adapters support setting the transaction
|
||||
# The mysql2 and postgresql adapters support setting the transaction
|
||||
# isolation level. However, support is disabled for MySQL versions below 5,
|
||||
# because they are affected by a bug[http://bugs.mysql.com/bug.php?id=39170]
|
||||
# which means the isolation level gets persisted outside the transaction.
|
||||
|
|
|
@ -376,7 +376,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
# Provides access to the underlying database driver for this adapter. For
|
||||
# example, this method returns a Mysql object in case of MysqlAdapter,
|
||||
# example, this method returns a Mysql2::Client object in case of Mysql2Adapter,
|
||||
# and a PGconn object in case of PostgreSQLAdapter.
|
||||
#
|
||||
# This is useful for when you need to call a proprietary method such as
|
||||
|
|
|
@ -107,11 +107,10 @@ module ActiveRecord
|
|||
##
|
||||
# :singleton-method:
|
||||
# By default, the Mysql2Adapter will consider all columns of type <tt>tinyint(1)</tt>
|
||||
# as boolean. If you wish to disable this emulation (which was the default
|
||||
# behavior in versions 0.13.1 and earlier) you can add the following line
|
||||
# as boolean. If you wish to disable this emulation you can add the following line
|
||||
# to your application.rb file:
|
||||
#
|
||||
# ActiveRecord::ConnectionAdapters::Mysql[2]Adapter.emulate_booleans = false
|
||||
# ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans = false
|
||||
class_attribute :emulate_booleans
|
||||
self.emulate_booleans = true
|
||||
|
||||
|
@ -400,10 +399,10 @@ module ActiveRecord
|
|||
log(sql, name) { @connection.query(sql) }
|
||||
end
|
||||
|
||||
# Mysql2Adapter has to free a result after using it, so we use this method to write
|
||||
# stuff in an abstract way without concerning ourselves about whether it needs to be
|
||||
# explicitly freed or not.
|
||||
def execute_and_free(sql, name = nil) #:nodoc:
|
||||
# Mysql2Adapter doesn't have to free a result after using it, but we use this method
|
||||
# to write stuff in an abstract way without concerning ourselves about whether it
|
||||
# needs to be explicitly freed or not.
|
||||
def execute_and_free(sql, name = nil) # :nodoc:
|
||||
yield execute(sql, name)
|
||||
end
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ module ActiveRecord
|
|||
# example for regular databases (MySQL, PostgreSQL, etc):
|
||||
#
|
||||
# ActiveRecord::Base.establish_connection(
|
||||
# adapter: "mysql",
|
||||
# adapter: "mysql2",
|
||||
# host: "localhost",
|
||||
# username: "myuser",
|
||||
# password: "mypass",
|
||||
|
|
|
@ -272,7 +272,7 @@ module ActiveRecord
|
|||
# * You are joining an existing open transaction
|
||||
# * You are creating a nested (savepoint) transaction
|
||||
#
|
||||
# The mysql, mysql2 and postgresql adapters support setting the transaction isolation level.
|
||||
# The mysql2 and postgresql adapters support setting the transaction isolation level.
|
||||
class TransactionIsolationError < ActiveRecordError
|
||||
end
|
||||
end
|
||||
|
|
|
@ -68,9 +68,6 @@ class Mysql2ConnectionTest < ActiveRecord::Mysql2TestCase
|
|||
assert_equal 'utf8_general_ci', ARUnit2Model.connection.show_variable('collation_connection')
|
||||
end
|
||||
|
||||
# TODO: Below is a straight up copy/paste from mysql/connection_test.rb
|
||||
# I'm not sure what the correct way is to share these tests between
|
||||
# adapters in minitest.
|
||||
def test_mysql_default_in_strict_mode
|
||||
result = @connection.exec_query "SELECT @@SESSION.sql_mode"
|
||||
assert_equal [["STRICT_ALL_TABLES"]], result.rows
|
||||
|
|
|
@ -82,7 +82,6 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
classname = conn.class.name[/[^:]*$/]
|
||||
badchar = {
|
||||
'SQLite3Adapter' => '"',
|
||||
'MysqlAdapter' => '`',
|
||||
'Mysql2Adapter' => '`',
|
||||
'PostgreSQLAdapter' => '"',
|
||||
'OracleAdapter' => '"',
|
||||
|
|
|
@ -545,8 +545,8 @@ class CalculationsTest < ActiveRecord::TestCase
|
|||
assert_equal 7, Company.includes(:contracts).sum(:developer_id)
|
||||
end
|
||||
|
||||
def test_from_option_with_specified_index
|
||||
if Edge.connection.adapter_name == 'Mysql2'
|
||||
if current_adapter?(:Mysql2Adapter)
|
||||
def test_from_option_with_specified_index
|
||||
assert_equal Edge.count(:all), Edge.from('edges USE INDEX(unique_edge_index)').count(:all)
|
||||
assert_equal Edge.where('sink_id < 5').count(:all),
|
||||
Edge.from('edges USE INDEX(unique_edge_index)').where('sink_id < 5').count(:all)
|
||||
|
|
|
@ -63,8 +63,6 @@ module ActiveRecord
|
|||
# Do a manual insertion
|
||||
if current_adapter?(:OracleAdapter)
|
||||
connection.execute "insert into test_models (id, wealth) values (people_seq.nextval, 12345678901234567890.0123456789)"
|
||||
elsif current_adapter?(:MysqlAdapter) && Mysql.client_version < 50003 #before MySQL 5.0.3 decimals stored as strings
|
||||
connection.execute "insert into test_models (wealth) values ('12345678901234567890.0123456789')"
|
||||
elsif current_adapter?(:PostgreSQLAdapter)
|
||||
connection.execute "insert into test_models (wealth) values (12345678901234567890.0123456789)"
|
||||
else
|
||||
|
|
|
@ -12,7 +12,6 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
ADAPTERS_TASKS = {
|
||||
mysql: :mysql_tasks,
|
||||
mysql2: :mysql_tasks,
|
||||
postgresql: :postgresql_tasks,
|
||||
sqlite3: :sqlite_tasks
|
||||
|
|
|
@ -59,97 +59,94 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
class MysqlDBCreateAsRootTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
@connection = stub("Connection", create_database: true)
|
||||
@error = Mysql::Error.new "Invalid permissions"
|
||||
@configuration = {
|
||||
class MysqlDBCreateAsRootTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
@connection = stub("Connection", create_database: true)
|
||||
@error = Mysql2::Error.new("Invalid permissions")
|
||||
@configuration = {
|
||||
'adapter' => 'mysql2',
|
||||
'database' => 'my-app-db',
|
||||
'username' => 'pat',
|
||||
'password' => 'wossname'
|
||||
}
|
||||
|
||||
$stdin.stubs(:gets).returns("secret\n")
|
||||
$stdout.stubs(:print).returns(nil)
|
||||
@error.stubs(:errno).returns(1045)
|
||||
ActiveRecord::Base.stubs(:connection).returns(@connection)
|
||||
ActiveRecord::Base.stubs(:establish_connection).
|
||||
raises(@error).
|
||||
then.returns(true)
|
||||
end
|
||||
|
||||
def test_root_password_is_requested
|
||||
assert_permissions_granted_for("pat")
|
||||
$stdin.expects(:gets).returns("secret\n")
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_connection_established_as_root
|
||||
assert_permissions_granted_for("pat")
|
||||
ActiveRecord::Base.expects(:establish_connection).with(
|
||||
'adapter' => 'mysql2',
|
||||
'database' => nil,
|
||||
'username' => 'root',
|
||||
'password' => 'secret'
|
||||
)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_database_created_by_root
|
||||
assert_permissions_granted_for("pat")
|
||||
@connection.expects(:create_database).
|
||||
with('my-app-db', {})
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_grant_privileges_for_normal_user
|
||||
assert_permissions_granted_for("pat")
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_do_not_grant_privileges_for_root_user
|
||||
@configuration['username'] = 'root'
|
||||
@configuration['password'] = ''
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_connection_established_as_normal_user
|
||||
assert_permissions_granted_for("pat")
|
||||
ActiveRecord::Base.expects(:establish_connection).returns do
|
||||
ActiveRecord::Base.expects(:establish_connection).with(
|
||||
'adapter' => 'mysql2',
|
||||
'database' => 'my-app-db',
|
||||
'username' => 'pat',
|
||||
'password' => 'wossname'
|
||||
}
|
||||
|
||||
$stdin.stubs(:gets).returns("secret\n")
|
||||
$stdout.stubs(:print).returns(nil)
|
||||
@error.stubs(:errno).returns(1045)
|
||||
ActiveRecord::Base.stubs(:connection).returns(@connection)
|
||||
ActiveRecord::Base.stubs(:establish_connection).
|
||||
raises(@error).
|
||||
then.returns(true)
|
||||
end
|
||||
|
||||
if defined?(::Mysql)
|
||||
def test_root_password_is_requested
|
||||
assert_permissions_granted_for "pat"
|
||||
$stdin.expects(:gets).returns("secret\n")
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
end
|
||||
|
||||
def test_connection_established_as_root
|
||||
assert_permissions_granted_for "pat"
|
||||
ActiveRecord::Base.expects(:establish_connection).with(
|
||||
'adapter' => 'mysql2',
|
||||
'database' => nil,
|
||||
'username' => 'root',
|
||||
'password' => 'secret'
|
||||
)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
raise @error
|
||||
end
|
||||
|
||||
def test_database_created_by_root
|
||||
assert_permissions_granted_for "pat"
|
||||
@connection.expects(:create_database).
|
||||
with('my-app-db', {})
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
def test_sends_output_to_stderr_when_other_errors
|
||||
@error.stubs(:errno).returns(42)
|
||||
|
||||
def test_grant_privileges_for_normal_user
|
||||
assert_permissions_granted_for "pat"
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
$stderr.expects(:puts).at_least_once.returns(nil)
|
||||
|
||||
def test_do_not_grant_privileges_for_root_user
|
||||
@configuration['username'] = 'root'
|
||||
@configuration['password'] = ''
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_connection_established_as_normal_user
|
||||
assert_permissions_granted_for "pat"
|
||||
ActiveRecord::Base.expects(:establish_connection).returns do
|
||||
ActiveRecord::Base.expects(:establish_connection).with(
|
||||
'adapter' => 'mysql2',
|
||||
'database' => 'my-app-db',
|
||||
'username' => 'pat',
|
||||
'password' => 'secret'
|
||||
)
|
||||
private
|
||||
|
||||
raise @error
|
||||
end
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_sends_output_to_stderr_when_other_errors
|
||||
@error.stubs(:errno).returns(42)
|
||||
|
||||
$stderr.expects(:puts).at_least_once.returns(nil)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
private
|
||||
def assert_permissions_granted_for(db_user)
|
||||
db_name = @configuration['database']
|
||||
db_password = @configuration['password']
|
||||
@connection.expects(:execute).with("GRANT ALL PRIVILEGES ON #{db_name}.* TO '#{db_user}'@'localhost' IDENTIFIED BY '#{db_password}' WITH GRANT OPTION;")
|
||||
end
|
||||
def assert_permissions_granted_for(db_user)
|
||||
db_name = @configuration['database']
|
||||
db_password = @configuration['password']
|
||||
@connection.expects(:execute).with("GRANT ALL PRIVILEGES ON #{db_name}.* TO '#{db_user}'@'localhost' IDENTIFIED BY '#{db_password}' WITH GRANT OPTION;")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -319,7 +319,7 @@ All these configuration options are delegated to the `I18n` library.
|
|||
|
||||
The MySQL adapter adds one additional configuration option:
|
||||
|
||||
* `ActiveRecord::ConnectionAdapters::Mysql[2]Adapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns in a MySQL database to be booleans and is true by default.
|
||||
* `ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns in a MySQL database to be booleans and is true by default.
|
||||
|
||||
The schema dumper adds one additional configuration option:
|
||||
|
||||
|
|
Loading…
Reference in a new issue