diff options
Diffstat (limited to 'activerecord')
6 files changed, 42 insertions, 25 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 65578c1dc9..c50229e779 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -65,6 +65,19 @@ *Aaron Christy* +## Rails 3.1.3 (unreleased) ## + +* Perf fix: If we're deleting all records in an association, don't add a IN(..) clause + to the query. *GH 3672* + + *Jon Leighton* + +* Fix bug with referencing other mysql databases in set_table_name. *GH 3690* + +* Fix performance bug with mysql databases on a server with lots of other databses. *GH 3678* + + *Christos Zisopoulos and Kenny J* + ## Rails 3.1.2 (unreleased) ## * Fix bug with PostgreSQLAdapter#indexes. When the search path has multiple schemas, spaces diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 50ee60284c..3353cdf1ef 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -89,8 +89,12 @@ module ActiveRecord records.each { |r| r.destroy } update_counter(-records.length) unless inverse_updates_counter_cache? else - keys = records.map { |r| r[reflection.association_primary_key] } - scope = scoped.where(reflection.association_primary_key => keys) + scope = scoped + + unless records == load_target + keys = records.map { |r| r[reflection.association_primary_key] } + scope = scoped.where(reflection.association_primary_key => keys) + end if method == :delete_all update_counter(-scope.delete_all) diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index e26b10fa97..f72d2974c8 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -576,17 +576,8 @@ module ActiveRecord # Returns a table's primary key and belonging sequence. def pk_and_sequence_for(table) - sql = <<-SQL - SELECT t.constraint_type, k.column_name - FROM information_schema.table_constraints t - JOIN information_schema.key_column_usage k - USING (constraint_name, table_schema, table_name) - WHERE t.table_schema = DATABASE() - AND t.table_name = '#{table}' - SQL - - execute_and_free(sql, 'SCHEMA') do |result| - keys = each_hash(result).select { |row| row[:constraint_type] == 'PRIMARY KEY' }.map { |row| row[:column_name] } + execute_and_free("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA') do |result| + keys = each_hash(result).map { |row| row[:Column_name] } keys.length == 1 ? [keys.first, nil] : nil end end diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 4ffcf6dbc1..abd71793fd 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -295,10 +295,10 @@ db_namespace = namespace :db do end desc 'Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)' - task :setup => :seed + task :setup => ['db:schema:load_if_ruby', 'db:structure:load_if_sql', :seed] desc 'Load the seed data from db/seeds.rb' - task :seed => ['db:schema:load_if_ruby', 'db:structure:load_if_sql'] do + task :seed do db_namespace['abort_if_pending_migrations'].invoke Rails.application.load_seed end @@ -377,10 +377,7 @@ db_namespace = namespace :db do ActiveRecord::Base.establish_connection(abcs[Rails.env]) File.open("#{Rails.root}/db/structure.sql", "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump } when /postgresql/ - ENV['PGHOST'] = abcs[Rails.env]['host'] if abcs[Rails.env]['host'] - ENV['PGPORT'] = abcs[Rails.env]['port'].to_s if abcs[Rails.env]['port'] - ENV['PGPASSWORD'] = abcs[Rails.env]['password'].to_s if abcs[Rails.env]['password'] - ENV['PGUSER'] = abcs[Rails.env]['username'].to_s if abcs[Rails.env]['username'] + set_psql_env(abcs[Rails.env]) search_path = abcs[Rails.env]['schema_search_path'] unless search_path.blank? search_path = search_path.split(",").map{|search_path_part| "--schema=#{search_path_part.strip}" }.join(" ") @@ -418,10 +415,7 @@ db_namespace = namespace :db do ActiveRecord::Base.connection.execute(table) end when /postgresql/ - ENV['PGHOST'] = abcs[env]['host'] if abcs[env]['host'] - ENV['PGPORT'] = abcs[env]['port'].to_s if abcs[env]['port'] - ENV['PGPASSWORD'] = abcs[env]['password'].to_s if abcs[env]['password'] - ENV['PGUSER'] = abcs[env]['username'].to_s if abcs[env]['username'] + set_psql_env(abcs[env]) `psql -f "#{Rails.root}/db/structure.sql" #{abcs[env]['database']} #{abcs[env]['template']}` when /sqlite/ dbfile = abcs[env]['database'] @@ -599,3 +593,10 @@ end def firebird_db_string(config) FireRuby::Database.db_string_for(config.symbolize_keys) end + +def set_psql_env(config) + ENV['PGHOST'] = config['host'] if config['host'] + ENV['PGPORT'] = config['port'].to_s if config['port'] + ENV['PGPASSWORD'] = config['password'].to_s if config['password'] + ENV['PGUSER'] = config['username'].to_s if config['username'] +end diff --git a/activerecord/test/cases/adapters/mysql/schema_test.rb b/activerecord/test/cases/adapters/mysql/schema_test.rb index a2155d1dd1..1aa034ed53 100644 --- a/activerecord/test/cases/adapters/mysql/schema_test.rb +++ b/activerecord/test/cases/adapters/mysql/schema_test.rb @@ -13,7 +13,7 @@ module ActiveRecord table = Post.table_name @db_name = db - @omgpost = Class.new(Post) do + @omgpost = Class.new(ActiveRecord::Base) do set_table_name "#{db}.#{table}" def self.name; 'Post'; end end @@ -23,6 +23,10 @@ module ActiveRecord assert @omgpost.find(:first) end + def test_primary_key + assert_equal 'id', @omgpost.primary_key + end + def test_table_exists? name = @omgpost.table_name assert @connection.table_exists?(name), "#{name} table should exist" diff --git a/activerecord/test/cases/adapters/mysql2/schema_test.rb b/activerecord/test/cases/adapters/mysql2/schema_test.rb index 858d1da2dd..49514e1539 100644 --- a/activerecord/test/cases/adapters/mysql2/schema_test.rb +++ b/activerecord/test/cases/adapters/mysql2/schema_test.rb @@ -13,7 +13,7 @@ module ActiveRecord table = Post.table_name @db_name = db - @omgpost = Class.new(Post) do + @omgpost = Class.new(ActiveRecord::Base) do set_table_name "#{db}.#{table}" def self.name; 'Post'; end end @@ -23,6 +23,10 @@ module ActiveRecord assert @omgpost.find(:first) end + def test_primary_key + assert_equal 'id', @omgpost.primary_key + end + def test_table_exists? name = @omgpost.table_name assert @connection.table_exists?(name), "#{name} table should exist" |