Commit Graph

1326 Commits

Author SHA1 Message Date
yui-knk
64d4571f6c [ci skip] Aline code examples in AR changelog 2015-10-27 20:12:31 +09:00
Sean Griffin
118232aef4 Merge pull request #19686 from tsun1215/index_errors
Errors can be indexed with nested attributes

Close #8638
2015-10-26 16:05:16 -06:00
Sean Griffin
04bc52f7dc Merge pull request #19924 from iamvery/db-tasks-exit-status
Explicitly exit with status "1" for create and drop failures
2015-10-26 12:27:58 -06:00
Jon McCartie
f0323288da Move default uuid generation to active_record 2015-10-23 21:34:00 -02:00
Rafael Sales
c2d33c4abf Fix generated projection fields in group by query
Closes #21922

Let `Book(id, author_id)`, `Photo(id, book_id, author_id)` and `Author(id)`

Running `Book.group(:author_id).joins(:photos).count` will produce:

* Rails 4.2 - conflicts `author_id` in both projection and group by:
```sql
SELECT COUNT(*) AS count_all, author_id AS author_id
  FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id"
 GROUP BY author_id
```

* Master (9d02a25) - conflicts `author_id` only in projection:
```sql
SELECT COUNT(*) AS count_all, author_id AS author_id
  FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id"
 GROUP BY "books"."author_id"
```

* With this fix:
```sql
SELECT COUNT(*) AS count_all, "books"."author_id" AS books_author_id
  FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id"
 GROUP BY "books"."author_id"
```
2015-10-22 04:22:57 -03:00
Jay Hayes
f33965f937 Update changelog with db rake task exit status fix 2015-10-20 19:18:45 -05:00
Sean Griffin
7df83b5d95 Merge pull request #21762 from jmccartie/jm/uuid
Set active_record config for always creating uuids in generators
2015-10-20 16:15:25 -06:00
Sean Griffin
0a6c4019df Merge pull request #20957 from akihiro17/find-by-issue
Fix find_by with association subquery issue
2015-10-20 15:25:29 -06:00
Jon McCartie
fb42c492a7 Set active_record config for always creating uuids in generators 2015-10-20 14:25:06 -07:00
Soutaro Matsumoto
a7628099de Qualify column names in calculation
Column names inserted via `group` have to be qualified with table name.
2015-10-20 14:47:55 -06:00
Sean Griffin
cbcdecd2c5 Do not cache prepared statements that are unlikely to have cache hits
Prior to this commit, Rails makes no differentiation between whether a
query uses bind parameters, and whether or not we cache that query as a
prepared statement. This leads to the cache populating extremely fast in
some cases, with the statements never being reused.

In particular, the two problematic cases are `where(foo: [1, 2, 3])` and
`where("foo = ?", 1)`. In both cases we'll end up quoting the values
rather than using a bind param, causing a cache entry for every value
ever used in that query.

It was noted that we can probably eventually change `where("foo = ?",
1)` to use a bind param, which would resolve that case. Additionally, on
PG we can change our generated query to be `WHERE foo = ANY($1)`, and
pass an array for the bind param. I hope to accomplish both in the
future.

For SQLite and MySQL, we still end up preparing the statements anyway,
we just don't cache it. The statement will be cleaned up after it is
executed. On postgres, we skip the prepare step entirely, as an API is
provided to execute with bind params without preparing the statement.

I'm not 100% happy on the way this ended up being structured. I was
hoping to use a decorator on the visitor, rather than mixing a module
into the object, but the way Arel has it's visitor pattern set up makes
it very difficult to extend without inheritance. I'd like to remove the
duplication from the various places that are extending it, but that'll
require a larger restructuring of that initialization logic. I'm going
to take another look at the structure of it soon.

This changes the signature of one of the adapter's internals, and will
require downstream changes from third party adapters. I'm not too
worried about this, as worst case they can simply add the parameter and
always ignore it, and just keep their previous behavior.

Fixes #21992.
2015-10-20 14:16:22 -06:00
Rafael Mendonça França
209f3b30af Add CHANGELOG entry for fix of #21955 [ci skip] 2015-10-20 15:30:09 -02:00
Jake Worth
7663376f07 where raises ArgumentError on unsupported types.
[#20473]
2015-10-16 09:23:15 -05:00
Sean Griffin
34321e4a43 Add an immutable string type to opt out of string duping
This type adds an escape hatch to apps for which string duping causes
unacceptable memory growth. The reason we are duping them is in order to
detect mutation, which was a feature added to 4.2 in #15674. The string
type was modified to support this behavior in #15788.

Memory growth is really only a concern for string types, as it's the
only mutable type where the act of coersion does not create a new object
regardless (as we're usually returning an object of a different class).

I do feel strongly that if we are going to support detecting mutation,
we should do it universally for any type which is mutable. While it is
less common and ideomatic to mutate strings than arrays or hashes, there
shouldn't be rules or gotchas to understanding our behavior.

However, I also appreciate that for apps which are using a lot of string
columns, this would increase the number of allocations by a large
factor. To ensure that we keep our contract, if you'd like to opt out of
mutation detection on strings, you'll also be option out of mutation of
those strings.

I'm not completely married to the thought that strings coming out of
this actually need to be frozen -- and I think the name is correct
either way, as the purpose of this is to provide a string type which
does not detect mutation.

In the new implementation, I'm only overriding `cast_value`. I did not
port over the duping in `serialize`. I cannot think of a reason we'd
need to dup the string there, and the tests pass without it.
Unfortunately that line was introduced at a time where I was not nearly
as good about writing my commit messages, so I have no context as to
why I added it. Thanks past Sean. You are a jerk.
2015-10-15 09:50:37 -07:00
Ted Johansson
b901a49473 Add deprecation warning to ActiveRecord::Relation#update
When passing an instance of `ActiveRecord::Base` to `#update`, it would
internally call `#find`, resulting in a misleading deprecation warning.

This change gives this deprecated use of `#update` its own, meaningful
warning.
2015-10-15 17:00:28 +08:00
Yves Senn
5a14349baf :to_table when adding a fk through add_reference.
Closes #21563.

The `name` argument of `add_references` was both used to generate the
column name `<name>_id` and as the target table for the foreign key
`name.pluralize`.

It's primary purpose is to define the column name. In cases where the
`to_table` of the foreign key is different than the column name we
should be able to specify it individually.
2015-10-13 11:05:19 +02:00
Yves Senn
9f4cefd28f Merge pull request #21931 from paul/bugfix/remove-deprecated-pg_dump-flag
Remove deprecated pg_dump -i flag
2015-10-12 16:34:26 +02:00
Jeremy Daer
f50d953ff6 Merge pull request #11410 from bogdan/increment-concurency
Make AR#increment! and #decrement! concurrency-safe
2015-10-10 13:24:54 -07:00
Matthew Draper
1b6fcae948 Avoid leaking the first relation we call #first on
With the previous implementation, the block passed to
define_singleton_method, which will live forever as the method body,
captures the parameters (args and block) in its enclosure.

For the current_scope registry, that can include an AR::Relation.
2015-10-09 07:09:57 +10:30
Ryuta Kamizono
fd37486e07 Remove unused pk_and_sequence_for in AbstractMysqlAdapter
`pk_and_sequence_for` is implemented for PG and MySQL adapters (not
implemented for Sqlite3 adapter). But MySQL adapters are not using
`pk_and_sequence_for` already.
2015-10-08 03:17:02 +09:00
akihiro17
f798cbd2f3 Don't cache arguments in #find_by if they are an ActiveRecord::Relation
In this commit, find_by doesn't cache arguments
so that find_by with association subquery works correctly.

Fixes #20817
2015-10-06 02:36:46 +09:00
Bogdan Gusiev
85a1c02508 Make #increment! and #decrement! methods concurency safe 2015-10-05 15:13:04 +03:00
Roque Pinel
e2b3ccd1aa Refactor AS::Callbacks halt config and fix the documentation
Move from `AS::Callbacks::CallbackChain.halt_and_display_warning_on_return_false`
to `AS::Callbacks.halt_and_display_warning_on_return_false` base on
[this
discussion](https://github.com/rails/rails/pull/21218#discussion_r39354580)

Fix the documentation broken by 0a120a818d413c64ff9867125f0b03788fc306f8
2015-10-01 13:04:20 -04:00
Yves Senn
591a0bb87f Merge pull request #20574 from repinel/fix-db-fixtures-load
Allow fixtures YAML files to set the model class in the file itself

Conflicts:
	activerecord/CHANGELOG.md
2015-09-30 11:06:41 +02:00
Sean Griffin
37661bfc81 validates_acceptance_of shouldn't require a database connection
The implementation of `attribute_method?` on Active Record requires
establishing a database connection and querying the schema. As a general
rule, we don't want to require database connections for any class macro,
as the class should be able to be loaded without a database (e.g. for
things like compiling assets).

Instead of eagerly defining these methods, we do it lazily the first
time they are accessed via `method_missing`. This should not cause any
performance hits, as it will only hit `method_missing` once for the
entire class.
2015-09-25 07:54:38 -06:00
Jean Boussier
4ccdd4122d Implement ActiveRecord::Base.ignored_columns 2015-09-24 11:04:44 -04:00
Sean Griffin
4a375a83de Merge pull request #21550 from didacte/unscope-associations
ActiveRecord: use association's `unscope` when preloading
2015-09-24 08:50:22 -06:00
Sean Griffin
66337b62ad Merge pull request #20317
AR: take precision into count when assigning a value to timestamp
attribute
2015-09-23 09:01:38 -06:00
Bogdan Gusiev
d03f519665 Fixed taking precision into count when assigning a value to timestamp attribute
Timestamp column can have less precision than ruby timestamp
In result in how big a fraction of a second can be stored in the
database.

  m = Model.create!
  m.created_at.usec == m.reload.created_at.usec
    # => false
    # due to different seconds precision in Time.now and database column

If the precision is low enough, (mysql default is 0, so it is always low
enough by default) the value changes when model is reloaded from the
database. This patch fixes that issue ensuring that any timestamp
assigned as an attribute is converted to column precision under the
attribute.
2015-09-23 13:29:08 +03:00
Yves Senn
152b85f06c introduce conn.data_source_exists? and conn.data_sources.
These new methods are used from the Active Record model layer to
determine which relations are viable to back a model. These new methods
allow us to change `conn.tables` in the future to only return tables and
no views. Same for `conn.table_exists?`.

The goal is to provide the following introspection methods on the
connection:

* `tables`
* `table_exists?`
* `views`
* `view_exists?`
* `data_sources` (views + tables)
* `data_source_exists?` (views + tables)
2015-09-22 19:48:44 +02:00
Yves Senn
3931cec9cd Merge pull request #20569 from theSteveMitchell/master
Check mysql structure_load for errors
2015-09-22 11:10:58 +02:00
Sean Griffin
9cc324a3f1 Ensure aliased attributes passed to select are quoted if using from
Fixes #21488

[Sean Griffin & johanlunds]
2015-09-21 11:14:32 -06:00
Jeremy Daer
26aa1b58b0 Merge pull request #17696 from kamipo/unsigned_integer_support
Add `unsigned` support for numeric data types in MySQL
2015-09-19 17:44:57 -07:00
Jeremy Daer
7286677050 Merge pull request #21609 from kamipo/do_not_dump_view_as_table
Do not dump a view as a table in sqlite3, mysql and mysql2 adapters
2015-09-19 15:56:01 -07:00
Ryuta Kamizono
ab12859947 Correctly dump composite primary key
Example:

    create_table :barcodes, primary_key: ["region", "code"] do |t|
      t.string :region
      t.integer :code
    end
2015-09-20 06:43:00 +09:00
Ryuta Kamizono
dfeb3ee78a Add unsigned types for numeric data types in MySQL
In the case of using `unsigned` as the type:

    create_table :foos do |t|
      t.unsigned_integer :unsigned_integer
      t.unsigned_bigint  :unsigned_bigint
      t.unsigned_float   :unsigned_float
      t.unsigned_decimal :unsigned_decimal, precision: 10, scale: 2
    end
2015-09-18 20:22:32 +09:00
Ryuta Kamizono
f3772f729c Add unsigned support for numeric data types in MySQL
Example:

    create_table :foos do |t|
      t.integer :unsigned_integer, unsigned: true
      t.bigint  :unsigned_bigint,  unsigned: true
      t.float   :unsigned_float,   unsigned: true
      t.decimal :unsigned_decimal, unsigned: true, precision: 10, scale: 2
    end
2015-09-18 20:22:32 +09:00
Yves Senn
9feda8f211 Merge pull request #21581 from ronakjangir47/restrict_with_error
`restrict_with_error` message will now respect owner’s human name
2015-09-17 16:30:10 +02:00
Ryuta Kamizono
dcd39949f8 Add #views and #view_exists? methods on connection adapters 2015-09-13 21:02:43 +09:00
Roque Pinel
2acec46577 Allow fixtures YAML files to set the model class in the file itself
Currently, `set_fixture_class` is only available using the
`TestFixtures` concern and it is ignored for `rake db:fixtures:load`.
Using the correct model class, it is possible for the fixture load
to also load the associations from the YAML files (e.g., `:belongs_to`
and `:has_many`).
2015-09-11 14:29:06 -04:00
Matthew Draper
8c34d106ca Correct query for PostgreSQL 8.2
Generic cast-to-text was only added in 8.3.
2015-09-08 04:32:38 +09:30
Yves Senn
8e155b0a9a Merge pull request #21527 from rngtng/fix-migrator-path-setup
Use global migrations_path configuration in Migrator
2015-09-07 16:23:42 +02:00
Tobias Bielohlawek
73726340c3 Allow global migrations_path configuration with using value from database_tasks instead of Migrator 2015-09-07 16:05:21 +02:00
Yves Senn
488afd5cc3 changelog, minor formatting changes. 2015-09-07 15:52:02 +02:00
Matthew Draper
0962527953 Merge pull request #21317 from greysteil/support-postgres-drop-index-concurrently
Support dropping indexes concurrently in Postgres
2015-09-07 00:15:11 +09:30
Wojciech Wnętrzak
c82c5f8ffd Deprecate passing conditions to AR::Relation destroy_all and delete_all methods 2015-09-06 16:14:16 +02:00
Grey Baker
ce17e232a1 Support dropping indexes concurrently in Postgres
See http://www.postgresql.org/docs/9.4/static/sql-dropindex.html
for more details.
2015-09-05 17:04:03 +01:00
Yves Senn
35df925b47 pg, create_schema, drop_schema and rename_table quote schema name.
Closes #21418.

Previously schema names were not quoted. This leads to issues when a
schema names contains a ".". Methods in `schema_statements.rb` should
quote user input.
2015-08-28 15:59:37 +02:00
Yves Senn
dd118dcc45 PostgreSQL, add :if_exists to #drop_schema. 2015-08-28 11:35:12 +02:00
Agis-
19b168e611 Only nullify persisted has_one target associations
Since after 87d1aba3c `dependent: :destroy` callbacks on has_one
assocations run *after* destroy, it is possible that a nullification is
attempted on an already destroyed target:

    class Car < ActiveRecord::Base
      has_one :engine, dependent: :nullify
    end

    class Engine < ActiveRecord::Base
      belongs_to :car, dependent: :destroy
    end

    > car = Car.create!
    > engine = Engine.create!(car: car)
    > engine.destroy! # => ActiveRecord::ActiveRecordError: cannot update a
    >   destroyed record

In the above case, `engine.destroy!` deletes `engine` and *then* triggers the
deletion of `car`, which in turn triggers a nullification of `engine.car_id`.
However, `engine` is already destroyed at that point.

Fixes #21223.
2015-08-24 11:49:43 +03:00