aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
* Use selected_migrations if block_given?oz2018-01-181-1/+1
| | | | This slipped in as part of a2827ec9811b5012e8e366011fd44c8eb53fc714.
* Refactor migration to move migrations paths to connectioneileencodes2018-01-189-140/+168
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Rails has some support for multiple databases but it can be hard to handle migrations with those. The easiest way to implement multiple databases is to contain migrations into their own folder ("db/migrate" for the primary db and "db/seconddb_migrate" for the second db). Without this you would need to write code that allowed you to switch connections in migrations. I can tell you from experience that is not a fun way to implement multiple databases. This refactoring is a pre-requisite for implementing other features related to parallel testing and improved handling for multiple databases. The refactoring here moves the class methods from the `Migrator` class into it's own new class `MigrationContext`. The goal was to move the `migrations_paths` method off of the `Migrator` class and onto the connection. This allows users to do the following in their `database.yml`: ``` development: adapter: mysql2 username: root password: development_seconddb: adapter: mysql2 username: root password: migrations_paths: "db/second_db_migrate" ``` Migrations for the `seconddb` can now be store in the `db/second_db_migrate` directory. Migrations for the primary database are stored in `db/migrate`". The refactoring here drastically reduces the internal API for migrations since we don't need to pass `migrations_paths` around to every single method. Additionally this change does not require any Rails applications to make changes unless they want to use the new public API. All of the class methods from the `Migrator` class were `nodoc`'d except for the `migrations_paths` and `migrations_path` getter/setters respectively.
* Fix relation merger issue with `left_outer_joins`Mehmet Emin INAC2018-01-152-1/+27
|
* Don't allow destroyed object mutation after `save` or `save!` is calledRyuta Kamizono2018-01-151-0/+1
| | | | | | | | | | Currently `object.save` will unfreeze the object, due to `changes_applied` replaces frozen `@attributes` to new `@attributes`. Since originally destroyed objects are not allowed to be mutated, `save` and `save!` should not return success in that case. Fixes #28563.
* `create_database` should not add default charset when `collation` is givenRyuta Kamizono2018-01-151-1/+1
| | | | | | | | | | | | | If `collation` is given without `charset`, it may generate invalid SQL. For example `create_database(:matt_aimonetti, collation: "utf8mb4_bin")`: ``` > CREATE DATABASE `matt_aimonetti` DEFAULT CHARACTER SET `utf8` COLLATE `utf8mb4_bin`; ERROR 1253 (42000): COLLATION 'utf8mb4_bin' is not valid for CHARACTER SET 'utf8' ``` In MySQL, charset is used to find the default collation. If `collation` is given explicitly, it is not necessary to give extra charset.
* Merge pull request #28313 from sandrew/masterRyuta Kamizono2018-01-151-2/+3
|\ | | | | | | Allow unscoping of left_outer_joins
| * Allow unscoping of left_outer_joinsAndrei Shaidurov2017-03-061-1/+1
| |
* | Don't need to pass `manager` to `convert_join_strings_to_ast`Ryuta Kamizono2018-01-141-2/+2
| | | | | | | | Because `Relation` already have Arel `table`.
* | Don't pass garbage args to alias trackerRyuta Kamizono2018-01-142-11/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | This is a complete fix to #30995. Originally alias tracker will only track table aliases on `Arel::Nodes::Join`, other args are ignored. Since c5ab6e5, parent aliases hash will be passed then it caused the regression #30995. It is enough to pass list of `Arel::Nodes::Join` simply, not need to pass garbage args which will be ignored.
* | Merge pull request #31671 from larskanis/pg-1.0Rafael Mendonça França2018-01-111-1/+1
|\ \ | | | | | | | | | PostgreSQL: Allow pg-1.0 gem to be used with ActiveRecord
| * | PostgreSQL: Allow pg-1.0 gem to be used with ActiveRecordLars Kanis2018-01-101-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | pg-1.0.0 is just released and most Gemfiles don't restrict it's version. But the version is checked when connecting to the database, which leads to the following error: Gem::LoadError: can't activate pg (~> 0.18), already activated pg-1.0.0 See also this pg issue: https://bitbucket.org/ged/ruby-pg/issues/270/pg-100-x64-mingw32-rails-server-not-start Preparation for pg-1.0 was done in commit f28a331023fab, but the pg version constraint was not yet relaxed.
* | | Merge pull request #23146 from piotrj/issue_18424Ryuta Kamizono2018-01-111-0/+1
|\ \ \ | | | | | | | | | | | | When deleting through records, take into account association conditions
| * | | When deleting through records, take into account association conditionsPiotr Jakubowski2016-05-041-8/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #18424. When deleting through records, it didn't take into account the conditions that may have been affecting join model table, but was defined in association definition.
* | | | Remove the deprecated `:conditions` option in ↵Ryuta Kamizono2018-01-111-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | `INVALID_AUTOMATIC_INVERSE_OPTIONS` This option was moved into active_record_deprecated_finders in ac4d101.
* | | | Use `apply_join_dependency` instead of meaningless named ↵Ryuta Kamizono2018-01-112-26/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `find_with_associations` `find_with_associations` is meaningless name in this point since it just contain `construct_join_dependency` and `apply_join_dependency`, does not contain finding anything. If `apply_join_dependency` returns `relation` and `join_dependency` then `find_with_associations` is no longer needed.
* | | | Make `relation.exists?` more performant when using eager loadingRyuta Kamizono2018-01-111-3/+4
| |/ / |/| | | | | | | | | | | | | | `relation.exists?` just wants to know if there is a result or not, does not need the exact records matched. Therefore, an intermediate SELECT query for eager loading is not necessary.
* | | [ci skip] setting :inverse_of works with :as, :through and :polymorphic ↵Bart de Water2018-01-101-12/+18
| | | | | | | | | | | | | | | | | | | | | | | | options, and is needed for bi-directionality with a scope [ci skip] Remove :conditions opion from association basics guide This got replaced by scopes.
* | | Fix `stale_state` for nested `has_many :through` associationsRyuta Kamizono2018-01-102-10/+13
| | | | | | | | | | | | Need reloading when through record has replaced.
* | | Merge pull request #16314 from ↵Ryuta Kamizono2018-01-102-1/+9
|\ \ \ | | | | | | | | | | | | | | | | | | | | zoltankiss/allow-nested-has-many-associations-on-unpersisted-parent-instances fix nested `has many :through` associations on unpersisted parent instances
| * | | Fix nested `has many :through` associations on unpersisted instancesZoltan Kiss2015-03-262-1/+5
| | | | | | | | | | | | | | | | Fixes: #16313
* | | | Bring back passing single record support for `Preloader`Ryuta Kamizono2018-01-101-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I removed redundant `Array.wrap(records)` since `Preloader` is nodoc class and Active Record always pass `records` as an array to `Preloader`. But if users relies on that behavior, it is not worth dropping its behavior. Fixes #31661.
* | | | resolve inconsistencies between first and to_a.first with limitBrian Christian2018-01-091-1/+5
| | | |
* | | | Fix deleting through records when using has_many through with `source_type`Ryuta Kamizono2018-01-081-8/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently deleting through records doesn't respect `source_type`. It should not be ignored in that case. Related #23209. Fixes #24116.
* | | | Simply use `scope.delete_all` instead of constructing delete managerRyuta Kamizono2018-01-071-8/+1
| | | |
* | | | Make `find_nth_from_last` more performant when using reversible orderRyuta Kamizono2018-01-071-6/+5
| | | | | | | | | | | | | | | | | | | | We can use `relation.last(index)[-index]` instead of loading all records when using reversible order because `last` will call `reverse_order`.
* | | | Fix `last` with `offset` to behave consistently with loaded relationRyuta Kamizono2018-01-071-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | Currently `last` with `offset` behaves incorrectly because `offset` can not be reversed like `limit`. Therefore, `offset` should also be handled like `limit`.
* | | | Fix `pluck` with eager loading to respect `offset`Ryuta Kamizono2018-01-071-1/+1
| | | |
* | | | Revert commit 4ec5b0d6b4d8a57e034b1014942356e95caf47aa in favor of #28379Ryuta Kamizono2018-01-071-6/+1
| | | | | | | | | | | | | | | | | | | | Commit 4ec5b0d was for fixing the regression #18787, but #28379 fixes #18787 as well. So 4ec5b0d is no longer necessary.
* | | | Remove passing argument to singular and collection association readersRyuta Kamizono2018-01-051-2/+2
| | | | | | | | | | | | | | | | Follow up of 09cac8c67afdc4b2a1c6ae07931ddc082629b277.
* | | | Partial revert the changing default value of `readonly_value`Ryuta Kamizono2018-01-051-13/+2
| | | | | | | | | | | | | | | | | | | | This is a partial revert of #26182. There is no reason to change the default value.
* | | | Fix newly added reflection order when redefining associationRyuta Kamizono2018-01-041-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently reflections keeps the order when first added even if when redefining association. As a result of the order, redefining through association which use newly added association will raise `HasManyThroughOrderError`. We need to redefine reflection order as well when redefining association. Fixes #31068.
* | | | Deprecate `valid_alter_table_type?` in sqlite3 adapterRyuta Kamizono2018-01-041-7/+12
| | | | | | | | | | | | | | | | | | | | This method which is used only in the internal was introduced in ac384820 and was renamed in #17579. It does not need to be exposed.
* | | | Correctly handle infinity value in PostgreSQL range typeyuuji.yaginuma2018-01-042-2/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | An empty string is an invalid value in Ruby's range class. So need to handle `Float::INFINITY` as it is and cast it in `encode_range`. Fixes #31612
* | | | Merge pull request #31331 from dinahshi/postgresql_bulk_updateMatthew Draper2018-01-032-5/+40
|\ \ \ \ | | | | | | | | | | Add bulk alter support for PostgreSQL
| * | | | Add bulk alter support for PostgreSQLDinah Shi2017-12-062-5/+40
| | | | |
* | | | | Merge pull request #25456 from ojab/masterRyuta Kamizono2018-01-031-3/+1
|\ \ \ \ \ | | | | | | | | | | | | | | | | | | Remove dormant check
| * | | | | Remove dormant checkojab2016-06-211-3/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This check was introduced in 6edaa26 and moved through multiple refactorings. No test are broken after removal and AFAICS there is no way to trigger it.
* | | | | | Merge pull request #27561 from fishbrain/count-all-in-has-many-associationRyuta Kamizono2018-01-031-1/+1
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | Use `count(:all)` in HasManyAssociation#count_records
| * | | | | | Use `count(:all)` in HasManyAssociation#count_recordsKlas Eskilson2017-02-071-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Problem: Calling `count` on an association can cause invalid SQL queries to be created where the `SELECT COUNT(a, b, c)` function receives multiple columns. This will cause a `StatementInvalid` exception later on. Solution: Use `count(:all)`, which generates a `SELECT COUNT(*)...` query independently of the association. This also includes a test case that, before the fix, broke.
* | | | | | | Active Record: Bump license years for 2018 [ci skip]Ryuta Kamizono2018-01-011-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Follow up of #31606.
* | | | | | | Remove `association_primary_key_type` from `AssociationReflection` and ↵Ryuta Kamizono2018-01-012-10/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `ThroughReflection` This method was introduced in #26718, which is internally used only in `CollectionAssociation`. There is no need to be in the reflection classes.
* | | | | | | Refactor delegating `join_primary_key` instead of `join_keys` and ↵Ryuta Kamizono2018-01-014-6/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `association_primary_key` in `TableMetadata` Because `join_primary_key` is called by `join_keys` and it is to abstract calling `association_primary_key`.
* | | | | | | Merge pull request #31575 from bogdan/bugfix-has-many-reattachmentRyuta Kamizono2018-01-012-13/+8
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Bugfix foreign key replacement in inverse association
| * | | | | | | Bugfix foreign key replacement in inverse associationBogdan Gusiev2017-12-272-13/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | when model is added to collection association
* | | | | | | | Fix recreating partial indexes after alter table for sqlitefatkodima2017-12-311-0/+1
| | | | | | | |
* | | | | | | | Fix `cache_key` with a relation having distinct and orderRyuta Kamizono2017-12-301-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We can't replace existing SELECT list as long as having DISTINCT, it will cause incorrect result. And also, PostgreSQL has a limitation that ORDER BY expressions must appear in select list for SELECT DISTINCT. Therefore, we should not replace existing SELECT list when using DISTINCT. Fixes #29779.
* | | | | | | | Fix `cache_key` with a relation having custom select and orderRyuta Kamizono2017-12-291-2/+1
|/ / / / / / / | | | | | | | | | | | | | | | | | | | | | We can't replace existing select list as long as referenced by ORDER BY.
* | | | | | | Make `sql_type` of primary key in SQLite to comparable with an integer (#28008)Ryuta Kamizono2017-12-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Originally I tried to add `assert_equal pk.sql_type, ref.sql_type`. But the assert failed even though the same type due to `sql_type` of primary key in SQLite is upper case. Prefer lower case like other types.
* | | | | | | Fix `add_column` with :primary_key type compatibility for SQLitefatkodima2017-12-251-1/+1
| | | | | | |
* | | | | | | save attributes changed by callbacks after update_attributeMike Busch2017-12-221-5/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | update_attribute previously stopped execution, before saving and before running callbacks, if the record's attributes hadn't changed. [The documentation](http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute) says that "Callbacks are invoked", which was not happening if the persisted attributes hadn't changed.