Commit Graph

1366 Commits

Author SHA1 Message Date
Matthew Draper
6940dc860c Add migration versioning via Migration subclasses 2015-12-15 17:18:08 +10:30
Sean Griffin
574f255629 Use a bind param for LIMIT and OFFSET
We currently generate an unbounded number of prepared statements when
`limit` or `offset` are called with a dynamic argument. This changes
`LIMIT` and `OFFSET` to use bind params, eliminating the problem.

`Type::Value#hash` needed to be implemented, as it turns out we busted
the query cache if the type object used wasn't exactly the same object.

This drops support for passing an `Arel::Nodes::SqlLiteral` to `limit`.
Doing this relied on AR internals, and was never officially supported
usage.

Fixes #22250.
2015-12-14 08:40:02 -07:00
Genadi Samokovarov
5a300b2ed6 Introduce after_{create,update,delete}_commit callbacks
Those are actually shortcuts for `after_commit`.

Before:

    after_commit :add_to_index_later, on: :create
    after_commit :update_in_index_later, on: :update
    after_commit :remove_from_index_later, on: :destroy

After:

    after_create_commit  :add_to_index_later
    after_update_commit  :update_in_index_later
    after_destroy_commit :remove_from_index_later
2015-12-06 20:33:36 +02:00
claudiob
728e28ffd6 Remove old comment about AC::Parameters>subclasses
[ci skip]

Q: What happens if you initialize an AR model by passing Parameters that
have not been whitelisted with `permit`?

A: An `ActiveModel::ForbiddenAttributesError` is raised.

I think this behavior is correct, and it's better than what used to happen,
with unpermitted parameter being simply ignored.
2015-12-02 18:05:01 -08:00
Yves Senn
3da890f891 Merge pull request #17169 from kuldeepaggarwal/fix-STI-default-type
STI cast new instances to `default type` on initialize.
2015-12-02 13:39:30 +01:00
Kuldeep Aggarwal
6b18bdd00c STI cast new instances to default type on initialize.
fixes #17121
2015-12-02 16:11:24 +05:30
Yves Senn
f746534ca4 Merge pull request #22345 from GUI/fix-multi-schema-structure-dump
Fix rake db:structure:dump on Postgres when multiple schemas are used

Conflicts:
	activerecord/CHANGELOG.md

Closes #22346.
2015-11-30 16:06:57 +01:00
Ryuta Kamizono
d5007f183a Fix indentation in CHANGELOG.md [ci skip] 2015-11-30 20:44:56 +09:00
Sean Griffin
b5bbdbd3bc Revert "Add prepared statements support for Mysql2Adapter" 2015-11-26 11:53:10 -07:00
Ryuta Kamizono
38746d085b Add prepared statements support for Mysql2Adapter 2015-11-26 11:16:26 +09:00
Ryuta Kamizono
3991ebcb36 Add schema dumping support for PostgreSQL geometric data types 2015-11-24 06:16:17 +09:00
Nick Muerdter
d1dcdf2128 Fix rake db:structure:dump on Postgres when multiple schemas are used.
If postgresql is being used and there are multiple schemas listed on the
`schema_search_path`, then `structure.sql` dumps (triggered by `rake
db:structure:dump` or `config.active_record.schema_format = :sql`) began
failing in Rails 4.2.5.

This is due to the changes made in
https://github.com/rails/rails/pull/17885 The problem is that multiple
schemas were getting getting passed to `Kernel.system` as a single,
space delimited string argument (for example, "--schema=foo
--schema=bar"). However, with the updated array style of calling
`Kernel.system`, these need to be passed as separate arguments (for
example, "--schema=foo", "--schema=bar"). If they get passed as a single
string, then the underlying pg_dump program isn't sure how to interpret
that single argument and you'll get an error reporting: "pg_dump: No
matching schemas were found"
2015-11-19 11:42:04 -07:00
Sean Griffin
6b7861e834 Revert "Allow specifying the default table options for mysql adapters"
This reverts commit 8246b593bff71f2cebf274c133bb8917f1e094c8.

There was concern about this modifying the behavior of past migrations.
We're going to add an way to modify the migration generator instead.
2015-11-19 10:01:06 -07:00
Sean Griffin
8246b593bf Allow specifying the default table options for mysql adapters
It's often the case that you want to have an option that you cannot
specify at the database level, but want applied to *all* tables that you
create. For example, you might want to specify `ROW_FORMAT=DYNAMIC` to
not have to limit text columns to length 171 for indexing when using
utf8mb4. This allows an easy way to specify this in your database
configuration.

While this change affects both MySQL and MySQL2, the test only covers
MySQL2, as the legacy mysql adapter appears to always return ASCII
strings, and is tangential to what we're actually doing.
2015-11-19 09:38:27 -07:00
Andrew White
32a8cd961e Fix incorrect issue number [ci skip] 2015-11-17 15:46:31 +00:00
yui-knk
24b1c219e1 [ci skip] Add CHANGELOG for #22300 (817c1825c15013fd0180762ac5c05a2e024a640d) 2015-11-18 00:16:38 +09:00
yui-knk
7429633b82 Deprecate #table_exists?, #tables and passing arguments to #talbes
Reported on #21509, how views is treated by `#tables` are differ
by each adapters. To fix this different behavior, after Rails 5.0
is released, deprecate `#tables`.

And `#table_exists?` would check both tables and views.
To make their behavior consistent with `#tables`, after Rails 5.0
is released, deprecate `#table_exists?`.
2015-11-09 23:13:23 +09:00
Kassio Borges
c8bbe9aefa Improve support for non Active Record objects on validates_associated
Skipping `marked_for_destruction?` when the associated object does not responds
to it make easier to validate virtual associations built on top of Active Model
objects and/or serialized objects that implement a `valid?` instance method.
2015-11-08 10:58:39 -02:00
Kevin Buchanan
2fe4586974 Avoids mutating the original response in connection management middleware 2015-11-06 16:14:24 -06:00
Yves Senn
1e2f6bccc8 formatting pass over Active Record changelog. [ci skip] 2015-11-04 12:02:37 -05:00
Andrew White
2acf6ac13f Fix spelling error [ci skip] 2015-11-04 12:50:59 +00:00
yui-knk
a7beeb7faa Make db:migrate:status to render 1_some.rb format migrate files.
`1_valid_people_have_last_names.rb` and
`20150823202140_create_users.rb` are valid migration file name.
But `1_valid_people_have_last_names.rb` is rendered as
`********** NO FILE **********` when `rake db:migrate:status`.

Fix to this bug, this commit includes

* define some API private methdos and a Constant
  `match_to_migration_filename?`, `parse_migration_filename`, and
  `MigrationFilenameRegexp`
* use these methods in `db:migrate:status` task

Example:

These files are in `db/migrate`

* 1_valid_people_have_last_names.rb
* 20150819202140_irreversible_migration.rb
* 20150823202140_add_admin_flag_to_users.rb
* 20150823202141_migration_tests.rb
* 2_we_need_reminders.rb
* 3_innocent_jointable.rb

we can migrate all of them.

Before

```shell
$ bundle exec rake db:migrate:status

...

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     001             ********** NO FILE **********
   up     002             ********** NO FILE **********
   up     003             ********** NO FILE **********
   up     20150819202140  Irreversible migration
   up     20150823202140  Add admin flag to users
   up     20150823202141  Migration tests
```

After

```shell
$ bundle exec rake db:migrate:status

...

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     001             Valid people have last names
   up     002             We need reminders
   up     003             Innocent jointable
   up     20150819202140  Irreversible migration
   up     20150823202140  Add admin flag to users
   up     20150823202141  Migration tests
```
2015-11-02 23:06:57 +09:00
yui-knk
6011ab853c Define sanitize_sql_for_order for AR and use it inside preprocess_order_args
This commit follows up of 6a6dbb4c51fb0c58ba1a810eaa552774167b758a.
2015-11-02 21:18:18 +09:00
Ryuta Kamizono
1fa6c9e5fb Allow bigint with default nil for avoiding auto increment primary key
Such as #10404, #18206.
2015-11-02 12:12:20 +00:00
Ryuta Kamizono
322068fe85 Remove DEFAULT_CHARSET and DEFAULT_COLLATION in MySQLDatabaseTasks
This reverts commit f6ca7e4e75408bc42f515fc7206d6c6ff0dce7c6.

The default collation of utf8 in MySQL is the `utf8_general_ci`, and
this should not be changed. This is because, the better collation in the
all locales is not exists, optimal collation in own application is not
known other than themselves.

The `utf8_unicode_ci` is known as Japanese killer in Japan, there are
serious impacts in search of Japanese.

MySQL implements the `utf8_unicode_ci` according to the Unicode
Collation Algorithm (UCA) described at http://www.unicode.org/reports/tr10/,
but the `utf8_unicode_ci` have only partial support for the UCA, only
primary level key comparison implemented (also known as L1 (Base
characters) comparison).

Because L1 (Base characters) comparison does not distinguish between the
presence or absence of the accent, if distinction of the accent is
important there is a serious impact (e.g. Japanese).

Example:

```
> SHOW CREATE TABLE `dicts`\G
*************************** 1. row ***************************
       Table: dicts
Create Table: CREATE TABLE `dicts` (
  `word` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `meaning` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)

> INSERT INTO `dicts` VALUES ('ハハ', 'mother'), ('パパ', 'father');
Query OK, 2 rows affected (0.00 sec)

> SELECT * FROM `dicts` WHERE `word` = 'ハハ';
+--------+---------+
| word   | meaning |
+--------+---------+
| ハハ   | mother  |
| パパ   | father  |
+--------+---------+
2 rows in set (0.00 sec)

> CREATE UNIQUE INDEX `unique_index_word` ON `dicts`(`word`);
ERROR 1062 (23000): Duplicate entry 'ハハ' for key 'unique_index_word'
```

We should omit the collation entirely rather than providing a default.
Then the choice is the responsibility of the server and MySQL distribution.
2015-11-02 20:44:48 +09:00
Takashi Kokubun
2a90d60c94 Add CHANGELOG entry for #22125 [ci skip] 2015-11-02 11:08:00 +00:00
Sam Davies
2e43eefa94 Update changelog for #22122 2015-11-01 17:52:03 -03:00
yuuji.yaginuma
a9402862a1 minor formatting changes [ci skip]
* add newline for display the fenced code block
* add "#" in the comments section
2015-10-31 14:52:24 +09:00
Sean Griffin
e038975c29 Merge pull request #12071 from Crunch09/outer_joins
added ActiveRecord::Relation#outer_joins
2015-10-30 09:42:02 -06:00
Sean Griffin
7f41321cbd Add a changelog entry for #13008 2015-10-29 15:29:37 -06:00
Harry Marr
6a9323bca4 Don't disable errors when turning standard_conforming_strings on 2015-10-29 20:30:48 +00:00
Andrew White
c087cfc671 Add CHANGELOG entry for #22101 [ci skip] 2015-10-29 17:21:49 +00:00
Yves Senn
857a34a416 Revert "Revert "Merge pull request #22026 from akihiro17/fix-preload-association""
This reverts commit 5243946017d09afff4d70d273b0fcdfd41a4b22a.

This fixes an issue with the build where tests would fail on mysql and
postgresql due to different ordering.
2015-10-29 17:47:47 +01:00
Sean Griffin
5243946017 Revert "Merge pull request #22026 from akihiro17/fix-preload-association"
This reverts commit 6dc6a0b17cfaf7cb6aa2b1c163b6ca141b538a8e, reversing
changes made to ec94f00ba3cf250eb54fc5b7a5e3ed4b90164f34.

This pull request broke the build.
2015-10-29 10:16:17 -06:00
akihiro17
0fdc2dbe6f Set scope.reordering_value to true if :reordering values are specified
We should call `scope.order!` and set `scope.reordering_value` to `true` if :reordering values are specified

Fixes #21886
2015-10-30 00:13:11 +09:00
Sean Griffin
f7d0a3ba7e Merge pull request #18548 from sebjacobs/support-bidirectional-destroy-dependencies
Add support for bidirectional destroy dependencies
2015-10-28 12:24:07 -06:00
Rafael Mendonça França
0ef2256c4a Merge pull request #18383 from scambra/habtm-with-where-includes-16032-for-master
Includes HABTM returns correct size now
2015-10-27 22:07:41 -02:00
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