aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract
Commit message (Collapse)AuthorAgeFilesLines
* Build a multi-statement query when inserting fixtures:Edouard CHIN2018-01-221-21/+55
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table - The previous code was bulk inserting fixtures for a single table, making X query for X fixture files - This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers): ```ruby # before %w(authors dogs computers).each do |table| sql = build_sql(table) connection.query(sql) end # after sql = build_sql(authors, dogs, computers) connection.query(sql) ``` - `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement - My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds - In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats: 1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)... - In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db - As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later 2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value - Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction 3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html) - Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets) - Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one - On mysql the `max_allowed_packet` is looked up: 1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised 2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
* Emulate JSON types for SQLite3 adapter (#29664)Ryuta Kamizono2017-12-031-0/+1
| | | | | Actually SQLite3 doesn't have JSON storage class (so it is stored as a TEXT like Date and Time). But emulating JSON types is convinient for making database agnostic migrations.
* Extract duplicated index column options normalization as ↵Ryuta Kamizono2017-12-031-9/+10
| | | | | | | `options_for_index_columns` And placed `add_options_for_index_columns` in `schema_statements.rb` consistently to ease to find related code.
* Refactor `length`, `order`, and `opclass` index options dumpingRyuta Kamizono2017-12-032-6/+15
|
* Merge pull request #31230 from dinahshi/postgresql_extract_sqlMatthew Draper2017-12-031-1/+15
|\ | | | | Extract sql fragment generators from PostgreSQL adapter
| * Extract sql fragment generators for alter table from PostgreSQL adapterDinah Shi2017-12-021-1/+15
| |
* | Add support for invalid foreign keys in PostgresTravis Hunter2017-12-012-0/+7
| | | | | | | | Add validate_constraint and update naming
* | Remove unpaired `}` [ci skip]Ryuta Kamizono2017-12-011-2/+1
| |
* | Add support for PostgreSQL operator classes to add_indexGreg Navis2017-11-302-3/+29
| | | | | | | | | | | | | | | | | | | | | | | | Add support for specifying non-default operator classes in PostgreSQL indexes. An example CREATE INDEX query that becomes possible is: CREATE INDEX users_name ON users USING gist (name gist_trgm_ops); Previously it was possible to specify the `gist` index but not the custom operator class. The `add_index` call for the above query is: add_index :users, :name, using: :gist, opclasses: {name: :gist_trgm_ops}
* | Merge pull request #31221 from matthewd/flush-idle-connectionsMatthew Draper2017-11-261-16/+58
|\ \ | |/ |/| Flush idle database connections
| * Flush idle database connectionsMatthew Draper2017-11-261-16/+58
| |
* | Merge pull request #31173 from matthewd/connection-fork-safetyMatthew Draper2017-11-251-0/+35
|\ \ | |/ |/| Improve AR connection fork safety
| * Improve AR connection fork safetyMatthew Draper2017-11-181-0/+35
| | | | | | | | | | | | Use whatever adapter-provided means we have available to ensure forked children don't send quit/shutdown/goodbye messages to the server on connections that belonged to their parent.
* | Merge pull request #28742 from quixoten/stack_conn_poolMatthew Draper2017-11-171-6/+3
|\ \ | |/ |/| Switch to LIFO for the connection pool
| * Fix typosDevin Christensen2017-04-131-1/+1
| |
| * Improve documentation and add testDevin Christensen2017-04-131-7/+4
| |
| * Switch to LIFO for the connection poolDevin Christensen2017-04-121-2/+2
| | | | | | | | | | | | | | | | Using a FIFO for the connection pool can lead to issues when there are upstream components (pgbouncer, haproxy, etc.) that terminate connections that are idle after a period of time. Switching to a LIFO reduces the probability that a thread will checkout a connection that is about to be closed by an idle timeout in an upstream component.
* | Properly check transaction in persistenceKeenan Brock2017-11-061-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ``` [NoMethodError]: undefined method `state' for nil:NilClass Method:[rescue in block in refresh] ``` In `within_new_transaction`, there is the possibility that `begin_transaction` returns a `nil`. (i.e.: so `transaction = nil`) So this method is checking `transaction` for nil in 2 spots. Unfortunately, there is one line that is not checking `transaction` for `nil` That line, `commit_transaction`, throws an exception for us in AR 5.0.0.1 The problem with the method is finally realized in the error checking itself. it calls `transaction.state` (i.e.: nil.state) and that is the final exception raised. The actual underlying (user) issue is hidden by this line. Solution is test transaction for nil.
* | [ci skip]Update the documentation about the primary key typesuginoy2017-10-291-15/+15
| | | | | | | | | | | | Replace the primary key type `integer` in docs with `bigint`. ref #26266
* | Remove deprecated argument `name` from `#indexes`Rafael Mendonça França2017-10-231-1/+1
| |
* | Remove deprecated methods `initialize_schema_migrations_table` and ↵Rafael Mendonça França2017-10-231-10/+0
| | | | | | | | `initialize_internal_metadata_table`
* | Remove deprecated argument `default` from `index_name_exists?`Rafael Mendonça França2017-10-231-6/+1
| |
* | Remove deprecated support to `quoted_id` when typecasting an Active Record ↵Rafael Mendonça França2017-10-231-17/+0
| | | | | | | | object
* | [Active Record] require => require_relativeAkira Matsuda2017-10-211-1/+1
| | | | | | | | This basically reverts 9d4f79d3d394edb74fa2192e5d9ad7b09ce50c6d
* | Express #change_column_comment as public apibogdanvlviv2017-10-041-1/+1
| | | | | | | | | | Implemented by #22911 Related to #30677
* | Preload digest/sha2 to avoid thread safe error.Francesco Rodriguez2017-09-251-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I got this error in production using Puma in multi-threaded mode: ``` RuntimeError: Digest::Base cannot be directly inherited in Ruby from active_support/security_utils.rb:23:in `variable_size_secure_compare' from active_support/security_utils.rb:23:in `hexdigest' from active_support/security_utils.rb:23:in `digest' ``` Looks like Digest uses const_missing to load Digest::SHA256 (https://github.com/ruby/ruby/blob/trunk/ext/digest/lib/digest.rb#L8) - https://bugs.ruby-lang.org/issues/9494 - https://github.com/ruby/ruby/commit/c02fa39463a0c6bf698b01bc610135604aca2ff4
* | Extract `integer_like_primary_key_type` to ease to handle it for adaptersRyuta Kamizono2017-09-251-0/+7
| |
* | Move integer-like primary key normalization to `new_column_definition`Ryuta Kamizono2017-09-231-0/+4
| | | | | | | | | | | | Currently the normalization only exists in `primary_key` shorthand. It should be moved to `new_column_definition` to also affect to `add_column` with primary key.
* | Add :comment option for add_column [ci skip]Yoshiyuki Kinjo2017-09-201-0/+2
| |
* | Should quote composite primary key namesRyuta Kamizono2017-09-041-1/+1
| | | | | | | | | | | | | | Otherwise using reserved words as composite primary key names will be failed as an invalid SQL. Fixes #30518.
* | `add_reference` should respect column position for both reference id and ↵Ryuta Kamizono2017-09-011-1/+1
| | | | | | | | | | | | type columns Fixes #30496.
* | Clarify that bulk option is supported only by MySQL [ci skip]Prathamesh Sonpatki2017-08-291-0/+2
| | | | | | | | - Closes #30441
* | Refactor `SchemaDumper` to make it possible to adapter specific customizationRyuta Kamizono2017-08-222-19/+22
| | | | | | | | | | | | | | Currently `SchemaDumper` is only customizable for column options. But 3rd party connection adapters (oracle-enhanced etc) need to customizable for table or index dumping also. To make it possible, I introduced adapter specific `SchemaDumper` classes for that.
* | Remove deprecated `#migration_keys`Ryuta Kamizono2017-08-221-6/+0
| |
* | Update links to use https instead of http [ci skip]Yoshiyuki Hirano2017-08-222-2/+2
| |
* | Require "active_support/core_ext/hash/compact" for `compact!`Ryuta Kamizono2017-08-211-0/+2
| |
* | Don't expose `prepare_column_options`Ryuta Kamizono2017-08-211-32/+13
| | | | | | | | | | This is only used for the internal `column_spec` and `column_spec_for_primary_key`.
* | Restore the ability that SQL with binds for `insert`, `update`, and `delete` ↵Ryuta Kamizono2017-08-181-6/+6
| | | | | | | | | | | | | | | | (#29944) Since 213796f, it was lost the ability that SQL with binds for `insert`, `update`, and `delete` (like `select_all`). This restores the ability because `insert`, `update`, and `delete` are public API, so it should not be removed without deprecation.
* | Restore `to_sql` to return only SQL (#29945)Ryuta Kamizono2017-08-182-5/+11
| | | | | | | | Because `to_sql` is public API. I introduced `to_sql_and_binds` internal API to return SQL and binds.
* | Fix RDoc formatting: `+` doesn't work with `@`ohbarye2017-08-111-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | refs: https://github.com/rails/rails/pull/30161 ``` $ echo "+@size+" | rdoc --pipe <p>+@size+</p> $ echo "<tt>@size</tt>" | rdoc --pipe <p><code>@size</code></p> ``` [ci skip]
* | Start `@reaper.run` after connection pool initializedRyuta Kamizono2017-08-111-2/+3
| | | | | | | | | | | | | | Otherwise `ConnectionPool#reap` may run before `@connections` has initialized. https://travis-ci.org/rails/rails/jobs/263037427#L888-L890
* | Merge pull request #30108 from yui-knk/require_concurrent_mapRafael França2017-08-081-0/+2
|\ \ | | | | | | Add missed `require`
| * | Add missed `require`yui-knk2017-08-071-0/+2
| | | | | | | | | | | | | | | `ActiveRecord::ConnectionAdapters::QueryCache::ConnectionPoolConfiguration` depends on `Concurrent::Map`.
* | | [ci skip] Postgres --> PostgreSQLRyuta Kamizono2017-08-081-1/+1
|/ /
* | Fix all rubocop violationsRafael Mendonça França2017-08-031-2/+2
| |
* | Change http postgresql.org links to https [ci skip]yuuji.yaginuma2017-07-301-1/+1
| | | | | | | | | | It seems that it accepts only HTTPS connections. Ref: https://github.com/postgres/postgres/commit/7f77cbd996855a06fb742ea11adbe55c42b48fe2
* | Clarify add_column limit documentationLisa Ugray2017-07-251-0/+1
| | | | | | | | | | The limit option is ignored by PostgreSQL and may be ignored by 3rd party backends. Make this clear in the docs. Fixes #29922.
* | Fix test failures when prepared statements are disabledSean Griffin2017-07-241-2/+25
| | | | | | | | | | | | | | | | | | This also reverts the change to enable prepared statements by default on MySQL (though I suspect we could enable them and it'd be great). This change brings back a collector closer to the old `Bind` collector in Arel. However, this one lives in AR, since this is an AR specific need. Additionally, we only use it for statement caching, since the new substitute collector in Arel is higher performance for most cases.
* | Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-243-22/+36
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A common source of bugs and code bloat within Active Record has been the need for us to maintain the list of bind values separately from the AST they're associated with. This makes any sort of AST manipulation incredibly difficult, as any time we want to potentially insert or remove an AST node, we need to traverse the entire tree to find where the associated bind parameters are. With this change, the bind parameters now live on the AST directly. Active Record does not need to know or care about them until the final AST traversal for SQL construction. Rather than returning just the SQL, the Arel collector will now return both the SQL and the bind parameters. At this point the connection adapter will have all the values that it had before. A bit of this code is janky and something I'd like to refactor later. In particular, I don't like how we're handling associations in the predicate builder, the special casing of `StatementCache::Substitute` in `QueryAttribute`, or generally how we're handling bind value replacement in the statement cache when prepared statements are disabled. This also mostly reverts #26378, as it moved all the code into a location that I wanted to delete. /cc @metaskills @yahonda, this change will affect the adapters Fixes #29766. Fixes #29804. Fixes #26541. Close #28539. Close #24769. Close #26468. Close #26202. There are probably other issues/PRs that can be closed because of this commit, but that's all I could find on the first few pages.
* | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-1911-0/+22
| |