aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb4
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb18
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb6
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/transaction.rb50
5 files changed, 48 insertions, 32 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
index 42ad285340..42c794c828 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -136,7 +136,7 @@ module ActiveRecord
#
# In order to get around this problem, #transaction will emulate the effect
# of nested transactions, by using savepoints:
- # http://dev.mysql.com/doc/refman/5.0/en/savepoint.html
+ # http://dev.mysql.com/doc/refman/5.6/en/savepoint.html
# Savepoints are supported by MySQL and PostgreSQL. SQLite3 version >= '3.6.8'
# supports savepoints.
#
@@ -189,7 +189,7 @@ module ActiveRecord
# semantics of these different levels:
#
# * http://www.postgresql.org/docs/9.1/static/transaction-iso.html
- # * https://dev.mysql.com/doc/refman/5.0/en/set-transaction.html
+ # * https://dev.mysql.com/doc/refman/5.6/en/set-transaction.html
#
# An <tt>ActiveRecord::TransactionIsolationError</tt> will be raised if:
#
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index 0acc815d51..a768ee2d70 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -401,7 +401,7 @@ module ActiveRecord
# Adds a reference. Optionally adds a +type+ column, if the
# +:polymorphic+ option is provided. +references+ and +belongs_to+
- # are acceptable. The reference column will be an +integer+ by default,
+ # are interchangeable. The reference column will be an +integer+ by default,
# the +:type+ option can be used to specify a different type. A foreign
# key will be created if the +:foreign_key+ option is passed.
#
@@ -535,6 +535,8 @@ module ActiveRecord
# Checks to see if a column exists.
#
+ # t.string(:name) unless t.column_exists?(:name, :string)
+ #
# See SchemaStatements#column_exists?
def column_exists?(column_name, type = nil, options = {})
@base.column_exists?(name, column_name, type, options)
@@ -554,6 +556,10 @@ module ActiveRecord
# Checks to see if an index exists.
#
+ # unless t.index_exists?(:branch_id)
+ # t.index(:branch_id)
+ # end
+ #
# See SchemaStatements#index_exists?
def index_exists?(column_name, options = {})
@base.index_exists?(name, column_name, options)
@@ -667,10 +673,20 @@ module ActiveRecord
end
alias :remove_belongs_to :remove_references
+ # Adds a foreign key.
+ #
+ # t.foreign_key(:authors)
+ #
+ # See SchemaStatements#add_foreign_key
def foreign_key(*args) # :nodoc:
@base.add_foreign_key(name, *args)
end
+ # Checks to see if a foreign key exists.
+ #
+ # t.foreign_key(:authors) unless t.foreign_key_exists?(:authors)
+ #
+ # See SchemaStatements#foreign_key_exists?
def foreign_key_exists?(*args) # :nodoc:
@base.foreign_key_exists?(name, *args)
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
index af7ef7cbaa..999cb0ec5a 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
@@ -24,7 +24,7 @@ module ActiveRecord
def prepare_column_options(column)
spec = {}
spec[:name] = column.name.inspect
- spec[:type] = column.type.to_s
+ spec[:type] = schema_type(column)
spec[:null] = 'false' unless column.null
limit = column.limit || native_database_types[column.type][:limit]
@@ -45,6 +45,10 @@ module ActiveRecord
private
+ def schema_type(column)
+ column.type.to_s
+ end
+
def schema_default(column)
type = lookup_cast_type_from_column(column)
default = type.deserialize(column.default)
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index d42f9a894b..72e019066e 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -652,7 +652,7 @@ module ActiveRecord
alias :add_belongs_to :add_reference
# Removes the reference(s). Also removes a +type+ column if one exists.
- # <tt>remove_reference</tt>, <tt>remove_references</tt> and <tt>remove_belongs_to</tt> are acceptable.
+ # <tt>remove_reference</tt> and <tt>remove_belongs_to</tt> are acceptable.
#
# ====== Remove the reference
#
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
index 3a1e4a4a88..295a7bed87 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
@@ -1,13 +1,10 @@
module ActiveRecord
module ConnectionAdapters
class TransactionState
- attr_reader :parent
-
VALID_STATES = Set.new([:committed, :rolledback, nil])
def initialize(state = nil)
@state = state
- @parent = nil
end
def finalized?
@@ -27,7 +24,7 @@ module ActiveRecord
end
def set_state(state)
- if !VALID_STATES.include?(state)
+ unless VALID_STATES.include?(state)
raise ArgumentError, "Invalid transaction state: #{state}"
end
@state = state
@@ -47,11 +44,12 @@ module ActiveRecord
attr_reader :connection, :state, :records, :savepoint_name
attr_writer :joinable
- def initialize(connection, options)
+ def initialize(connection, options, run_commit_callbacks: false)
@connection = connection
@state = TransactionState.new
@records = []
@joinable = options.fetch(:joinable, true)
+ @run_commit_callbacks = run_commit_callbacks
end
def add_record(record)
@@ -78,18 +76,21 @@ module ActiveRecord
end
def before_commit_records
- records.uniq.each(&:before_committed!)
+ records.uniq.each(&:before_committed!) if @run_commit_callbacks
end
def commit_records
ite = records.uniq
while record = ite.shift
- record.committed!
+ if @run_commit_callbacks
+ record.committed!
+ else
+ # if not running callbacks, only adds the record to the parent transaction
+ record.add_to_transaction
+ end
end
ensure
- ite.each do |i|
- i.committed!(should_run_callbacks: false)
- end
+ ite.each { |i| i.committed!(should_run_callbacks: false) }
end
def full_rollback?; true; end
@@ -100,8 +101,8 @@ module ActiveRecord
class SavepointTransaction < Transaction
- def initialize(connection, savepoint_name, options)
- super(connection, options)
+ def initialize(connection, savepoint_name, options, *args)
+ super(connection, options, *args)
if options[:isolation]
raise ActiveRecord::TransactionIsolationError, "cannot set transaction isolation in a nested transaction"
end
@@ -123,7 +124,7 @@ module ActiveRecord
class RealTransaction < Transaction
- def initialize(connection, options)
+ def initialize(connection, options, *args)
super
if options[:isolation]
connection.begin_isolated_db_transaction(options[:isolation])
@@ -150,11 +151,13 @@ module ActiveRecord
end
def begin_transaction(options = {})
+ run_commit_callbacks = !current_transaction.joinable?
transaction =
if @stack.empty?
- RealTransaction.new(@connection, options)
+ RealTransaction.new(@connection, options, run_commit_callbacks: run_commit_callbacks)
else
- SavepointTransaction.new(@connection, "active_record_#{@stack.size}", options)
+ SavepointTransaction.new(@connection, "active_record_#{@stack.size}", options,
+ run_commit_callbacks: run_commit_callbacks)
end
@stack.push(transaction)
@@ -162,18 +165,11 @@ module ActiveRecord
end
def commit_transaction
- inner_transaction = @stack.pop
-
- if current_transaction.joinable?
- inner_transaction.commit
- inner_transaction.records.each do |r|
- r.add_to_transaction
- end
- else
- inner_transaction.before_commit_records
- inner_transaction.commit
- inner_transaction.commit_records
- end
+ transaction = @stack.last
+ transaction.before_commit_records
+ @stack.pop
+ transaction.commit
+ transaction.commit_records
end
def rollback_transaction(transaction = nil)