1. 22 3月, 2018 2 次提交
    • E
      Refactor configs_for and friends · 4e663c1e
      eileencodes 提交于
      Moves the configs_for and DatabaseConfig struct into it's own file. I
      was considering doing this in a future refactoring but our set up forced
      me to move it now. You see there are `mattr_accessor`'s on the Core
      module that have default settings. For example the `schema_format`
      defaults to Ruby. So if I call `configs_for` or any methods in the Core
      module it will reset the `schema_format` to `:ruby`. By moving it to
      it's own class we can keep the logic contained and avoid this
      unfortunate issue.
      
      The second change here does a double loop over the yaml files. Bear with
      me...
      
      Our tests dictate that we need to load an environment before our rake
      tasks because we could have something in an environment that the
      database.yml depends on. There are side-effects to this and I think
      there's a deeper bug that needs to be fixed but that's for another
      issue. The gist of the problem is when I was creating the dynamic rake
      tasks if the yaml that that rake task is calling evaluates code (like
      erb) that calls the environment configs the code will blow up because
      the environment is not loaded yet.
      
      To avoid this issue we added a new method that simply loads the yaml and
      does not evaluate the erb or anything in it. We then use that yaml to
      create the task name. Inside the task name we can then call
      `load_config` and load the real config to actually call the code
      internal to the task. I admit, this is gross, but refactoring can't all
      be pretty all the time and I'm working hard with `@tenderlove` to
      refactor much more of this code to get to a better place re connection
      management and rake tasks.
      4e663c1e
    • E
      Add DatabaseConfig Struct and associated methods · 1756094b
      eileencodes 提交于
      Passing around and parsing hashes is easy if you know that it's a two
      tier config and each key will be named after the environment and each
      value will be the config for that environment key.
      
      This falls apart pretty quickly with three-tier configs. We have no idea
      what the second tier will be named (we know the first is primary but we
      don't know the second), we have no easy way of figuring out
      how deep a hash we have without iterating over it, and we'd have to do
      this a lot throughout the code since it breaks all of Active Record's
      assumptions regarding configurations.
      
      These methods allow us to pass around objects instead. This will allow
      us to more easily parse the configs for the rake tasks. Evenually I'd
      like to replace the Active Record connection management that passes
      around config hashes to use these methods as well but that's much
      farther down the road.
      
      `walk_configs` takes an environment, specification name, and a config
      and turns them into DatabaseConfig struct objects so we can ask the
      configs questions like:
      
      ```
      db_config.spec_name
      => animals
      
      db_config.env_name
      => development
      
      db_config.config
      { :adapter => mysql etc }
      ```
      
      `db_configs` loops through all given configurations and returns an array
      of DatabaseConfig structs for each config in the yaml file.
      
      and lastly `configs_for` takes an environment and either returns the
      spec name and config if a block is given or returns an array of
      DatabaseConfig structs just for the given environment.
      1756094b
  2. 25 1月, 2018 2 次提交
  3. 14 12月, 2017 1 次提交
    • O
      Log call site for all queries · 3876defd
      Olivier Lacan 提交于
      This new ActiveRecord configuration option allows you to easily
      pinpoint what line of application code is triggering SQL queries in the
      development log by appending below each SQL statement log the line of
      Ruby code that triggered it.
      
      It’s useful with N+1 issues, and to locate stray queries.
      
      By default this new option ignores Rails and Ruby code in order to
      surface only callers from your application Ruby code or your gems.
      
      It is enabled on newly generated Rails 5.2 applications and can be
      enabled on existing Rails applications:
      
      ```ruby
      Rails.application.configure do
        # ...
        config.active_record.verbose_query_logs = true
      end
      ```
      
      The `rails app:upgrade` task will also add it to
      `config/development.rb`.
      
      This feature purposely avoids coupling with
      ActiveSupport::BacktraceCleaner since ActiveRecord can be used without
      ActiveRecord. This decision can be reverted in the future to allow more
      configurable backtraces (the exclusion of gem callers for example).
      3876defd
  4. 20 11月, 2017 1 次提交
    • R
      Prevent extra `spawn` to make `klass.all` faster (#29009) · eeaf9cf6
      Ryuta Kamizono 提交于
      These extra `spawn` are called via `klass.all` and `klass.all` is called
      everywhere in the internal. Avoiding the extra `spawn` makes` klass.all`
      30% faster for STI classes.
      
      https://gist.github.com/kamipo/684d03817a8115848cec8e8b079560b7
      
      ```
      Warming up --------------------------------------
             fast relation     4.410k i/100ms
             slow relation     3.334k i/100ms
      Calculating -------------------------------------
             fast relation     47.373k (± 5.2%) i/s -    238.140k in   5.041836s
             slow relation     35.757k (±15.9%) i/s -    176.702k in   5.104625s
      
      Comparison:
             fast relation:    47373.2 i/s
             slow relation:    35756.7 i/s - 1.32x  slower
      ```
      eeaf9cf6
  5. 09 11月, 2017 2 次提交
  6. 24 10月, 2017 1 次提交
  7. 22 8月, 2017 1 次提交
  8. 12 8月, 2017 1 次提交
  9. 04 8月, 2017 2 次提交
  10. 20 7月, 2017 1 次提交
  11. 19 7月, 2017 1 次提交
  12. 17 7月, 2017 1 次提交
  13. 16 7月, 2017 1 次提交
  14. 02 7月, 2017 1 次提交
  15. 01 7月, 2017 1 次提交
  16. 03 6月, 2017 1 次提交
  17. 15 3月, 2017 1 次提交
  18. 15 1月, 2017 1 次提交
  19. 05 1月, 2017 1 次提交
  20. 04 1月, 2017 2 次提交
  21. 31 12月, 2016 1 次提交
  22. 24 12月, 2016 1 次提交
  23. 06 12月, 2016 1 次提交
  24. 04 11月, 2016 1 次提交
    • S
      Don't assign default attributes until after loading schema · 98faa2a6
      Sean Griffin 提交于
      If the call to `.define_attribute_methods` actually ends up loading the
      schema (*very* hard to do, as it requires the object being created
      without `allocate` having been called, but it can be done by manually
      calling `initialize` from inside `marshal_load` if you're crazy), the
      value of `_default_attributes` will change from that call.
      98faa2a6
  25. 31 10月, 2016 1 次提交
  26. 29 10月, 2016 1 次提交
  27. 26 9月, 2016 1 次提交
    • S
      Use xor to avoid allocations in `AR::Core#hash` · a6da7938
      Sean Griffin 提交于
      This is not as good a solution as actually hashing both values, but Ruby
      doesn't expose that capability other than allocating the array. Unless we were
      to do something silly like have a thread local array that is re-used, I don't
      see any other way to do this without allocation. This solution may not be
      perfect, but it should reasonably avoid collisions to the extent that we need.
      a6da7938
  28. 02 9月, 2016 1 次提交
  29. 01 9月, 2016 1 次提交
  30. 31 8月, 2016 1 次提交
    • S
      Ensure that inverse associations are set before running callbacks · caa178c1
      Sean Griffin 提交于
      If a parent association was accessed in an `after_find` or
      `after_initialize` callback, it would always end up loading the
      association, and then immediately overwriting the association we just
      loaded. If this occurred in a way that the parent's `current_scope` was
      set to eager load the child, this would result in an infinite loop and
      eventually overflow the stack.
      
      For records that are created with `.new`, we have a mechanism to
      perform an action before the callbacks are run. I've introduced the same
      code path for records created with `instantiate`, and updated all code
      which sets inverse instances on newly loaded associations to use this
      block instead.
      
      Fixes #26320.
      caa178c1
  31. 28 8月, 2016 1 次提交
  32. 09 8月, 2016 1 次提交
  33. 08 8月, 2016 2 次提交
  34. 07 8月, 2016 1 次提交