diff options
Diffstat (limited to 'activerecord/lib/active_record')
14 files changed, 80 insertions, 50 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index c3d4eab562..891b50d160 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -56,7 +56,7 @@ module ActiveRecord def ids_writer(ids) primary_key = reflection.association_primary_key pk_type = klass.type_for_attribute(primary_key) - ids = Array(ids).reject(&:blank?) + ids = Array(ids).compact_blank ids.map! { |i| pk_type.cast(i) } records = klass.where(primary_key => ids).index_by do |r| diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 94d8134b55..734ebb45ae 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -302,7 +302,7 @@ module ActiveRecord def validate_single_association(reflection) association = association_instance_get(reflection.name) record = association && association.reader - association_valid?(reflection, record) if record + association_valid?(reflection, record) if record && record.changed_for_autosave? end # Validate the associated records if <tt>:validate</tt> or 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 13f94a4722..88367c79a1 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -100,7 +100,7 @@ module ActiveRecord def index_exists?(table_name, column_name, options = {}) column_names = Array(column_name).map(&:to_s) checks = [] - checks << lambda { |i| i.columns == column_names } + checks << lambda { |i| Array(i.columns) == column_names } checks << lambda { |i| i.unique } if options[:unique] checks << lambda { |i| i.name == options[:name].to_s } if options[:name] 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 405fecb603..ef1eef6b69 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -476,12 +476,12 @@ module ActiveRecord # distinct queries, and requires that the ORDER BY include the distinct column. # See https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html def columns_for_distinct(columns, orders) # :nodoc: - order_columns = orders.reject(&:blank?).map { |s| + order_columns = orders.compact_blank.map { |s| # Convert Arel node to string s = s.to_sql unless s.is_a?(String) # Remove any ASC/DESC modifiers s.gsub(/\s+(?:ASC|DESC)\b/i, "") - }.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" } + }.compact_blank.map.with_index { |column, i| "#{column} AS alias_#{i}" } (order_columns << super).join(", ") end diff --git a/activerecord/lib/active_record/connection_adapters/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/connection_specification.rb index df26f67c6e..0732b69f81 100644 --- a/activerecord/lib/active_record/connection_adapters/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/connection_specification.rb @@ -50,7 +50,7 @@ module ActiveRecord # Converts the given URL to a full connection hash. def to_hash - config = raw_config.reject { |_, value| value.blank? } + config = raw_config.compact_blank config.map { |key, value| config[key] = uri_parser.unescape(value) if value.is_a? String } config end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb index 6434377b57..357493dfc0 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb @@ -26,9 +26,9 @@ module ActiveRecord value = value.sub(/^\((.+)\)$/, '-\1') # (4) case value - when /^-?\D+[\d,]+\.\d{2}$/ # (1) + when /^-?\D*[\d,]+\.\d{2}$/ # (1) value.gsub!(/[^-\d.]/, "") - when /^-?\D+[\d.]+,\d{2}$/ # (2) + when /^-?\D*[\d.]+,\d{2}$/ # (2) value.gsub!(/[^-\d,]/, "").sub!(/,/, ".") end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 0062952667..628a609521 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -555,13 +555,13 @@ module ActiveRecord # PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and # requires that the ORDER BY include the distinct column. def columns_for_distinct(columns, orders) #:nodoc: - order_columns = orders.reject(&:blank?).map { |s| + order_columns = orders.compact_blank.map { |s| # Convert Arel node to string s = s.to_sql unless s.is_a?(String) # Remove any ASC/DESC modifiers s.gsub(/\s+(?:ASC|DESC)\b/i, "") .gsub(/\s+NULLS\s+(?:FIRST|LAST)\b/i, "") - }.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" } + }.compact_blank.map.with_index { |column, i| "#{column} AS alias_#{i}" } (order_columns << super).join(", ") end diff --git a/activerecord/lib/active_record/connection_adapters/schema_cache.rb b/activerecord/lib/active_record/connection_adapters/schema_cache.rb index 7d54fcf9a0..5e30304864 100644 --- a/activerecord/lib/active_record/connection_adapters/schema_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/schema_cache.rb @@ -46,7 +46,11 @@ module ActiveRecord end def primary_keys(table_name) - @primary_keys[table_name] ||= data_source_exists?(table_name) ? connection.primary_key(table_name) : nil + @primary_keys.fetch(table_name) do + if data_source_exists?(table_name) + @primary_keys[deep_deduplicate(table_name)] = deep_deduplicate(connection.primary_key(table_name)) + end + end end # A cached lookup for table existence. @@ -54,7 +58,7 @@ module ActiveRecord prepare_data_sources if @data_sources.empty? return @data_sources[name] if @data_sources.key? name - @data_sources[name] = connection.data_source_exists?(name) + @data_sources[deep_deduplicate(name)] = connection.data_source_exists?(name) end # Add internal cache for table with +table_name+. @@ -73,13 +77,17 @@ module ActiveRecord # Get the columns for a table def columns(table_name) - @columns[table_name] ||= connection.columns(table_name) + @columns.fetch(table_name) do + @columns[deep_deduplicate(table_name)] = deep_deduplicate(connection.columns(table_name)) + end end # Get the columns for a table as a hash, key is the column name # value is the column object. def columns_hash(table_name) - @columns_hash[table_name] ||= columns(table_name).index_by(&:name) + @columns_hash.fetch(table_name) do + @columns_hash[deep_deduplicate(table_name)] = columns(table_name).index_by(&:name) + end end # Checks whether the columns hash is already cached for a table. @@ -88,7 +96,9 @@ module ActiveRecord end def indexes(table_name) - @indexes[table_name] ||= connection.indexes(table_name) + @indexes.fetch(table_name) do + @indexes[deep_deduplicate(table_name)] = deep_deduplicate(connection.indexes(table_name)) + end end def database_version # :nodoc: diff --git a/activerecord/lib/active_record/database_configurations.rb b/activerecord/lib/active_record/database_configurations.rb index bf31bb7c22..8baa0f5af6 100644 --- a/activerecord/lib/active_record/database_configurations.rb +++ b/activerecord/lib/active_record/database_configurations.rb @@ -104,18 +104,30 @@ module ActiveRecord return configs.configurations if configs.is_a?(DatabaseConfigurations) return configs if configs.is_a?(Array) - build_db_config = configs.each_pair.flat_map do |env_name, config| - walk_configs(env_name.to_s, "primary", config) - end.flatten.compact + db_configs = configs.flat_map do |env_name, config| + if config.is_a?(Hash) && config.all? { |_, v| v.is_a?(Hash) } + walk_configs(env_name.to_s, config) + else + build_db_config_from_raw_config(env_name.to_s, "primary", config) + end + end - if url = ENV["DATABASE_URL"] - build_url_config(url, build_db_config) - else - build_db_config + current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_s + + unless db_configs.find(&:for_current_env?) + db_configs << environment_url_config(current_env, "primary", {}) + end + + merge_db_environment_variables(current_env, db_configs.compact) + end + + def walk_configs(env_name, config) + config.map do |spec_name, sub_config| + build_db_config_from_raw_config(env_name, spec_name.to_s, sub_config) end end - def walk_configs(env_name, spec_name, config) + def build_db_config_from_raw_config(env_name, spec_name, config) case config when String build_db_config_from_string(env_name, spec_name, config) @@ -141,31 +153,27 @@ module ActiveRecord config_without_url.delete "url" ActiveRecord::DatabaseConfigurations::UrlConfig.new(env_name, spec_name, url, config_without_url) - elsif config["database"] || config["adapter"] || ENV["DATABASE_URL"] - ActiveRecord::DatabaseConfigurations::HashConfig.new(env_name, spec_name, config) else - config.each_pair.map do |sub_spec_name, sub_config| - walk_configs(env_name, sub_spec_name, sub_config) - end + ActiveRecord::DatabaseConfigurations::HashConfig.new(env_name, spec_name, config) end end - def build_url_config(url, configs) - env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_s + def merge_db_environment_variables(current_env, configs) + configs.map do |config| + next config if config.url_config? || config.env_name != current_env - if configs.find(&:for_current_env?) - configs.map do |config| - if config.url_config? - config - else - ActiveRecord::DatabaseConfigurations::UrlConfig.new(config.env_name, config.spec_name, url, config.config) - end - end - else - configs + [ActiveRecord::DatabaseConfigurations::UrlConfig.new(env, "primary", url)] + url_config = environment_url_config(current_env, config.spec_name, config.config) + url_config || config end end + def environment_url_config(env, spec_name, config) + url = ENV["DATABASE_URL"] + return unless url + + ActiveRecord::DatabaseConfigurations::UrlConfig.new(env, spec_name, url, config) + end + def method_missing(method, *args, &blk) case method when :each, :first diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 8077630aeb..fc49f752aa 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -200,6 +200,8 @@ module ActiveRecord # scope :active, -> { where(status: 0) } # scope :not_active, -> { where.not(status: 0) } if enum_scopes != false + klass.send(:detect_negative_condition!, value_method_name) + klass.send(:detect_enum_conflict!, name, value_method_name, true) klass.scope value_method_name, -> { where(attr => value) } @@ -261,5 +263,12 @@ module ActiveRecord source: source } end + + def detect_negative_condition!(method_name) + if method_name.start_with?("not_") && logger + logger.warn "An enum element in #{self.name} uses the prefix 'not_'." \ + " This will cause a conflict with auto generated negative scopes." + end + end end end diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 98f57549a5..4d9acc911b 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -297,10 +297,11 @@ db_namespace = namespace :db do ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config| ActiveRecord::Base.establish_connection(db_config.config) - ActiveRecord::Tasks::DatabaseTasks.migrate - # Skipped when no database - ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, ActiveRecord::Base.schema_format, db_config.spec_name) + ActiveRecord::Tasks::DatabaseTasks.migrate + if ActiveRecord::Base.dump_schema_after_migration + ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, ActiveRecord::Base.schema_format, db_config.spec_name) + end rescue ActiveRecord::NoDatabaseError ActiveRecord::Tasks::DatabaseTasks.create_current(db_config.env_name, db_config.spec_name) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 6a181882ae..6957ba052b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -138,7 +138,7 @@ module ActiveRecord end def includes!(*args) # :nodoc: - args.reject!(&:blank?) + args.compact_blank! args.flatten! self.includes_values |= args @@ -265,7 +265,7 @@ module ActiveRecord end def _select!(*fields) # :nodoc: - fields.reject!(&:blank?) + fields.compact_blank! fields.flatten! self.select_values += fields self @@ -965,7 +965,7 @@ module ActiveRecord def reverse_order! # :nodoc: orders = order_values.uniq - orders.reject!(&:blank?) + orders.compact_blank! self.order_values = reverse_sql_order(orders) self end @@ -1053,7 +1053,7 @@ module ActiveRecord ) arel.skip(Arel::Nodes::BindParam.new(offset_attribute)) end - arel.group(*arel_columns(group_values.uniq.reject(&:blank?))) unless group_values.empty? + arel.group(*arel_columns(group_values.uniq.compact_blank)) unless group_values.empty? build_order(arel) @@ -1129,7 +1129,7 @@ module ActiveRecord association_joins = buckets[:association_join] stashed_joins = buckets[:stashed_join] join_nodes = buckets[:join_node].tap(&:uniq!) - string_joins = buckets[:string_join].delete_if(&:blank?).map!(&:strip).tap(&:uniq!) + string_joins = buckets[:string_join].compact_blank!.map!(&:strip).tap(&:uniq!) string_joins.map! { |join| table.create_string_join(Arel.sql(join)) } @@ -1227,7 +1227,7 @@ module ActiveRecord def build_order(arel) orders = order_values.uniq - orders.reject!(&:blank?) + orders.compact_blank! arel.order(*orders) unless orders.empty? end diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index a78bebf764..5d1ce19829 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -154,6 +154,8 @@ module ActiveRecord end def for_each(databases) + return {} unless defined?(Rails) + database_configs = ActiveRecord::DatabaseConfigurations.new(databases).configs_for(env_name: Rails.env) # if this is a single database application we don't want tasks for each primary database diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index a7e04007a9..e3efeb75b5 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -16,7 +16,7 @@ module ActiveRecord connection.create_database configuration["database"], creation_options establish_connection configuration rescue ActiveRecord::StatementInvalid => error - if error.cause.error_number == ER_DB_CREATE_EXISTS + if connection.error_number(error.cause) == ER_DB_CREATE_EXISTS raise DatabaseAlreadyExists else raise |