1. 23 12月, 2014 12 次提交
    • D
      Fixing numeric attrs when set to same negative value · 2859341c
      Daniel Fox 提交于
      This bug occurs when an attribute of an ActiveRecord model is an
      ActiveRecord::Type::Integer type or a ActiveRecord::Type::Decimal type (or any
      other type that includes the ActiveRecord::Type::Numeric module. When the value
      of the attribute is negative and is set to the same negative value, it is marked
      as changed.
      
      Take the following example of a Person model with the integer attribute age:
      
          class Person < ActiveRecord::Base
            # age          :integer(4)
          end
      
      The following will produce the error:
      
          person = Person.new(age: -1)
          person.age = -1
          person.changes
          => { "age" => [-1, -1] }
          person.age_changed?
          => true
      
      The problematic line is here:
      
          module ActiveRecord
            module Type
              module Numeric
                ...
      
                def non_numeric_string?(value)
                  # 'wibble'.to_i will give zero, we want to make sure
                  # that we aren't marking int zero to string zero as
                  # changed.
                  value.to_s !~ /\A\d+\.?\d*\z/
                end
              end
            end
          end
      
      The regex match doesn't accept numbers with a leading '-'.
      2859341c
    • S
      Improve the performance of reading belongs_to associations · be2b98b4
      Sean Griffin 提交于
      `ActiveRecord::Base#[]` has overhead that was introduced in 4.2. The
      `foo["id"]` working with PKs other than ID isn't really a case that we
      want to support publicly, but deprecating was painful enough that we
      avoid it. `_read_attribute` was introduced as the faster alternative for
      use internally. By using that, we can save a lot of overhead. We also
      save some overhead by reading the attribute one fewer times in
      `stale_state`.
      
      Fixes #18151
      be2b98b4
    • S
      Don't perform statement caching for `find` when called from a scope · fb160f6e
      Sean Griffin 提交于
      If there is a method defined such as `find_and_do_stuff(id)`, which then
      gets called on an association, we will perform statement caching and the
      parent ID will not change on subsequent calls.
      
      Fixes #18117
      fb160f6e
    • S
      Don't calculate all in-place changes to determine if attribute_changed? · 18ae0656
      Sean Griffin 提交于
      Calling `changed_attributes` will ultimately check if every mutable
      attribute has changed in place. Since this gets called whenever an
      attribute is assigned, it's extremely slow. Instead, we can avoid this
      calculation until we actually need it.
      
      Fixes #18029
      18ae0656
    • S
      Don't wrap `create_table` in a transaction for tests which run on MySQL · 84927431
      Sean Griffin 提交于
      PG will warn without it, but mysql2 errors out.
      84927431
    • S
      Use the new `foreign_key` option on `references` in generators · a03ea684
      Sean Griffin 提交于
      Changes `rails g model Post user:references` from
      
          def change
            create_table :posts do |t|
              t.references :user, index: true
            end
      
            add_foreign_key :posts, :users
          end
      
      to
      
          def change
            create_table :posts do |t|
              t.references :user, index: true, foreign_key: true
            end
          end
      
      Changes `rails g migration add_user_to_posts user:references` from
      
          def change
            add_reference :posts, :users, index: true
            add_foreign_key :posts, :users
          end
      
      to
      
          def change
            add_reference :posts, :users, index: true, foreign_key: true
          end
      a03ea684
    • S
      Add `foreign_key` as an option to `references` for `change_table` · 82afeaf2
      Sean Griffin 提交于
      This has the same comments as 9af90ffa00ba35bdee888e3e1ab775ba0bdbe72c,
      however it affects the `add_reference` method, and `t.references` in the
      context of a `change_table` block.
      
      There is a lot of duplication of code between creating and updating
      tables. We should re-evaluate the structure of this code from a high
      level so changes like this don't need to be made in two places. (Note to
      self)
      82afeaf2
    • S
      Convert `add_references` to use kwargs · 68a6c8ec
      Sean Griffin 提交于
      While we still aren't accepting PRs that only make changes like this,
      it's fine when we're actively working on a method if it makes our lives
      easier.
      68a6c8ec
    • S
      Add a `foreign_key` option to `references` while creating the table · 99a6f9e6
      Sean Griffin 提交于
      Rather than having to do:
      
          create_table :posts do |t|
            t.references :user
          end
      
          add_foreign_key :posts, :users
      
      You can instead do:
      
          create_table :posts do |t|
            t.references :user, foreign_key: true
          end
      
      Similar to the `index` option, you can also pass a hash. This will be
      passed as the options to `add_foreign_key`. e.g.:
      
          create_table :posts do |t|
            t.references :user, foreign_key: { primary_key: :other_id }
          end
      
      is equivalent to
      
          create_table :posts do |t|
            t.references :user
          end
      
          add_foreign_key :posts, :users, primary_key: :other_id
      99a6f9e6
    • S
      Convert `references` to kwargs · a9c0c462
      Sean Griffin 提交于
      While we aren't taking PRs with these kinds of changes just yet, they
      are fine if we're actively working on the method and it makes things
      easier.
      a9c0c462
    • S
      Add `force: true` to table created in tests · 32f30d22
      Sean Griffin 提交于
      If the test is interrupted in a way that the teardown block fails to
      run, the tests will fail to run until the table is removed manually
      without this option.
      32f30d22
    • S
      Correctly handle limit on int4 and int8 types in PG · b0f2b94d
      Sean Griffin 提交于
      PG doesn't register it's types using the `int(4)` format that others do.
      As such, if we alias `int8` to the other integer types, the range
      information is lost. This is fixed by simply registering it separately.
      
      The other option (which I specifically chose to avoid) is to pass the
      information of the original type that was being aliased as an argument.
      I'd rather avoid that, since an alias should truly be treated the same.
      If we need different behavior for a different type, we should explicitly
      register it with that, and not have a conditional based on aliasing.
      
      Fixes #18144
      
      [Sean Griffin & ysbaddaden]
      b0f2b94d
  2. 22 12月, 2014 2 次提交
    • S
      Correctly handle Float -> BigDecimal with unspecified precision · 41f1323e
      Sean Griffin 提交于
      Fixes #18122
      41f1323e
    • S
      Clarify ActiveRecord testing guidelines [ci skip] · f5489f9a
      Sammy Larbi 提交于
      1. Specify that you need to create the test databases, and that no special
         Rails command needs to be run to do that.
      2. Although the underscore style of `rake test_mysql` works, make the
         documentation of running the tests in RUNNING_UNIT_TESTS.rdoc
         consistent with the "Contributing..." guide.
      3. Promote "Testing Active Record" to not be a subsection of
         "Running a Single Test," since it doesn't make sense as a subsection
         of that.
      f5489f9a
  3. 21 12月, 2014 1 次提交
  4. 20 12月, 2014 1 次提交
  5. 19 12月, 2014 2 次提交
  6. 18 12月, 2014 2 次提交
    • Y
      `db:structure:load` and `db:schema:load` no longer purge the database. · 36ce0c2c
      Yves Senn 提交于
      Closes #17945
      
      `db:test:prepare` still purges the database to always keep the test
      database in a consistent state.
      
      This patch introduces new problems with `db:schema:load`. Prior
      to the introduction of foreign-keys, we could run this file against
      a non-empty database. Since every `create_table` containted the
      `force: true` option, this would recreate tables when loading the schema.
      
      However with foreign-keys in place, `force: true` wont work anymore and
      the task will crash.
      
      /cc @schneems
      36ce0c2c
    • G
      Relax the UUID regex · c115a84c
      Godfrey Chan 提交于
      Apparently PG does not validate against RFC 4122. The intent of the original
      patch is just to protect against PG errors (which potentially breaks txns, etc)
      because of bad user input, so we shouldn't try any harder than PG itself.
      
      Closes #17931
      c115a84c
  7. 17 12月, 2014 2 次提交
    • R
      Remove unused line · 7da314bb
      Ryuta Kamizono 提交于
      7da314bb
    • S
      `update_column` take ruby-land input, not database-land input · dd8b5fb9
      Sean Griffin 提交于
      In the case of serialized columns, we would expect the unserialized
      value as input, not the serialized value. The original issue which made
      this distinction, #14163, introduced a bug. If you passed serialized
      input to the method, it would double serialize when it was sent to the
      database. You would see the wrong input upon reloading, or get an error
      if you had a specific type on the serialized column.
      
      To put it another way, `update_column` is a special case of
      `update_all`, which would take `['a']` and not `['a'].to_yaml`, but you
      would not pass data from `params` to it.
      
      Fixes #18037
      dd8b5fb9
  8. 13 12月, 2014 1 次提交
    • E
      Pass connection rather than alias_tracker · 80099618
      eileencodes 提交于
      Because we're only using the `connection` so passing the entire tracker
      isn't unnecessary.
      
      Eventually only the `connection` will be passed to `add_constraints`
      with later refactoring but curretly that's not possible because of
      `construct_tables` method.
      80099618
  9. 12 12月, 2014 2 次提交
  10. 11 12月, 2014 3 次提交
  11. 09 12月, 2014 8 次提交
    • A
      Unused csv fixture file · 8e529543
      Akira Matsuda 提交于
      8e529543
    • S
      Update test case for TZ aware attributes · 65588b74
      Sean Griffin 提交于
      The test added in 42418cfc wasn't
      actually testing anything, since the bug was with TZ aware attributes
      only.
      65588b74
    • S
      Allow custom handling of non-standard types in `time_zone_conversion` · 42418cfc
      Sean Griffin 提交于
      PostgreSQL for example, allows infinity as a valid value for date time
      columns. The PG type has explicit handling for that case. However, time
      zone conversion will end up trampling that handling. Unfortunately, we
      can't call super and then convert time zones.
      
      However, if we get back nil from `.in_time_zone`, it's something we
      didn't expect so we can let the superclass handle it.
      
      Fixes #17971
      42418cfc
    • C
      Remove unused "Developer" fixtures from tests · 3502e6ed
      claudiob 提交于
      The `RecursiveCallbackDeveloper` and `ImmutableMethodDeveloper` classes
      are not used anymore in tests, and neither is the `@Cancelled` variable.
      3502e6ed
    • U
      Add foreign_type option for polymorphic has_one and has_many. · 075c81fe
      Ulisses Almeida + Kassio Borges 提交于
      To be possible to use a custom column name to save/read the polymorphic
      associated type in a has_many or has_one polymorphic association, now users
      can use the option :foreign_type to inform in what column the associated object
      type will be saved.
      075c81fe
    • S
      Don't error when `attributes` is called on a frozen AR model · 7daeb98c
      Sean Griffin 提交于
      `freeze` will ultimately end up freezing the `AttributeSet`, which in
      turn freezes its `@attributes` hash. However, we actually insert a
      special object to lazily instantiate the values of the hash on demand.
      When it does need to actually instantiate all of them for iteration (the
      only case is `ActiveRecord::Base#attributes`, which calls
      `AttributeSet#to_h`), it will set an instance variable as a performance
      optimization
      
      Since it's just an optimization for subsequent calls, and that method
      being called at all is a very uncommon case, we can just leave the ivar
      alone if we're frozen, as opposed to coming up with some overly
      complicated mechanism for freezing which allows us to continue to modify
      ourselves.
      
      Fixes #17960
      7daeb98c
    • S
      Revert to 4.1 behavior for casting PG arrays · 4ed60af6
      Sean Griffin 提交于
      The user is able to pass PG string literals in 4.1, and have it
      converted to an array. This is also possible in 4.2, but it would remain
      in string form until saving and reloading, which breaks our
      `attr = save.reload.attr` contract. I think we should deprecate this in
      5.0, and only allow array input from user sources. However, this
      currently constitutes a breaking change to public API that did not go
      through a deprecation cycle.
      4ed60af6
    • Z
      04bf9fbe
  12. 08 12月, 2014 4 次提交