aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
Commit message (Collapse)AuthorAgeFilesLines
* Extract `sanitize_as_sql_comment` from SQL visitor into connectionRyuta Kamizono2019-03-191-0/+4
| | | | Probably that is useful for any other feature as well.
* Don't expose internal `type_casted_binds` methodRyuta Kamizono2019-03-191-7/+7
| | | | Internal usage for the method as public has removed at #29623.
* Tweak `truncate_tables`Ryuta Kamizono2019-03-181-17/+17
| | | | | | * Remove redundant `table_names.empty?` * Early return in `truncate_tables` since it is already deeply nested * Move `truncate_tables` out from between `exec_delete` and `exec_update`
* SQLite3: Make fixture loading to bulk statementsRyuta Kamizono2019-03-174-47/+38
|
* Fix undefined local variable or method `discard_remaining_results'Ryuta Kamizono2019-03-171-1/+1
| | | | https://buildkite.com/rails/rails/builds/59632#fe3d2551-569a-46c8-94f3-7abe835d4d7a/122-153
* Ensure `execute_batch` discards remaining resultsRyuta Kamizono2019-03-172-14/+5
|
* Make `truncate_tables` to bulk statementsRyuta Kamizono2019-03-176-52/+118
| | | | | | | | | | | | | | | | | | Before: ``` (16.4ms) TRUNCATE TABLE `author_addresses` (20.5ms) TRUNCATE TABLE `authors` (19.4ms) TRUNCATE TABLE `posts` ``` After: ``` Truncate Tables (19.5ms) TRUNCATE TABLE `author_addresses`; TRUNCATE TABLE `authors`; TRUNCATE TABLE `posts` ```
* Extract `truncate` and `truncate_tables` into database statementsRyuta Kamizono2019-03-175-13/+15
| | | | This is to easier make `truncate_tables` to bulk statements.
* Support Optimizer HintsRyuta Kamizono2019-03-163-2/+22
| | | | | | | | | | | | | | | | | | We as Arm Treasure Data are using Optimizer Hints with a monkey patch (https://gist.github.com/kamipo/4c8539f0ce4acf85075cf5a6b0d9712e), especially in order to use `MAX_EXECUTION_TIME` (refer #31129). Example: ```ruby class Job < ApplicationRecord default_scope { optimizer_hints("MAX_EXECUTION_TIME(50000) NO_INDEX_MERGE(jobs)") } end ``` Optimizer Hints is supported not only for MySQL but also for most databases (PostgreSQL on RDS, Oracle, SQL Server, etc), it is really helpful to turn heavy queries for large scale applications.
* Revert "Remove Marshal support from SchemaCache"Rafael Mendonça França2019-03-131-0/+11
| | | | This reverts commit 65f2eeaaf5774f0891fff700f4defb0b90a05789.
* Schema Cache: cache table indexesKasper Timm Hansen2019-03-131-5/+15
| | | | | | Useful to not query for indexes when an application uses schema cache. Ref https://github.com/rails/rails/pull/35546
* Merge branch 'test-remove-marshal-support-from-schema-cache'Kasper Timm Hansen2019-03-121-10/+0
|\
| * Remove Marshal support from SchemaCacheKasper Timm Hansen2019-03-121-10/+0
| | | | | | | | | | YAML has been used to serialize the schema cache ever since 2016 with Rails 5.1: 4c00c6ed
* | Modernize size calculation in Schema CacheKasper Timm Hansen2019-03-121-1/+1
|/ | | | | Not looking for other contributions like this, but I took the liberty since I was already working on this.
* Initialize `@default_timezone` and `@timestamp_decoder` in `add_pg_decoders`Ryuta Kamizono2019-03-131-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Staled `@default_timezone` would cause an error on `reconnect!` after `disconnect!`. https://buildkite.com/rails/rails/builds/59495#23be8079-3a4f-4375-9991-0a6f874554f2 Steps to reproduce: ``` % ARCONN=postgresql bin/test test/cases/adapter_test.rb test/cases/base_test.rb -n "/(?:test_attributes_on_dummy_time|test_reconnect_after_a_disconnect)$/" --seed 15849 Using postgresql Run options: -n "/(?:test_attributes_on_dummy_time|test_reconnect_after_a_disconnect)$/" --seed 15849 # Running: . E Error: ActiveRecord::AdapterTestWithoutTransaction#test_reconnect_after_a_disconnect: NoMethodError: undefined method `add_coder' for #<PG::TypeMapAllStrings:0x00007f85ab9dd5b8> /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:866:in `update_typemap_for_default_timezone' /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:652:in `exec_no_cache' /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:636:in `execute_and_clear' /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:894:in `add_pg_decoders' /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:744:in `connect' /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:285:in `rescue in block in reconnect!' /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:281:in `block in reconnect!' /Users/kamipo/.rbenv/versions/2.6.1/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize' /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:280:in `reconnect!' /Users/kamipo/src/github.com/rails/rails/activerecord/test/cases/adapter_test.rb:465:in `block in <class:AdapterTestWithoutTransaction>' ```
* Squish the deprecation messages across the codebasePrathamesh Sonpatki2019-03-111-1/+1
| | | | | | | | | | | | | | | | Sample example -> Before: prathamesh@Prathameshs-MacBook-Pro-2 blog *$ rails server thin DEPRECATION WARNING: Passing the Rack server name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -u option instead. After: prathamesh@Prathameshs-MacBook-Pro-2 squish_app *$ rails server thin DEPRECATION WARNING: Passing the Rack server name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -u option instead.
* PostgreSQL: Add `add_pg_encoders` and `add_pg_decoders` in `connect`Ryuta Kamizono2019-03-101-0/+2
| | | | | | It is to work that on `reconnect!` after `disconnect!`. https://buildkite.com/rails/rails/builds/59378#1efea538-cfca-4d43-8b7e-ae78e97227c8
* SQLite3: Set `busy_timeout` in `configure_connection`Ryuta Kamizono2019-03-101-2/+2
| | | | It is to work that on `reconnect!` after `disconnect!`
* Fix `reconnect!` to work after `disconnect!`Ryuta Kamizono2019-03-102-4/+16
|
* Move all Arel constructions from uniqueness validator into connection adapterRyuta Kamizono2019-03-072-4/+5
|
* Allow `remove_foreign_key` with both `to_table` and `options`Ryuta Kamizono2019-03-064-19/+15
| | | | | | | Foreign keys could be created to the same table. So `remove_foreign_key :from_table, :to_table` is sometimes ambiguous. This allows `remove_foreign_key` to remove the select one on the same table with giving both `to_table` and `options`.
* Ensure `clear_cache!` clears the prepared statements cacheRyuta Kamizono2019-03-064-26/+21
| | | | | | | | | | | Since #23461, all adapters supports prepared statements, so that clears the prepared statements cache is no longer database specific. Actually, I struggled to identify the cause of random CI failure in #23461, that was missing `@statements.clear` in `clear_cache!`. This extracts `clear_cache!` to ensure the common concerns in the abstract adapter.
* Add insert_all to ActiveRecord models (#35077)Bob Lail2019-03-055-11/+107
| | | | | Adds a method to ActiveRecord allowing records to be inserted in bulk without instantiating ActiveRecord models. This method supports options for handling uniqueness violations by skipping duplicate records or overwriting them in an UPSERT operation. ActiveRecord already supports bulk-update and bulk-destroy actions that execute SQL UPDATE and DELETE commands directly. It also supports bulk-read actions through `pluck`. It makes sense for it also to support bulk-creation.
* Allow `truncate` for SQLite3 adapter and add `rails db:seed:replant` (#34779)Bogdan2019-03-041-0/+4
| | | | | | | | | | | | | * Add `ActiveRecord::Base.connection.truncate` for SQLite3 adapter. SQLite doesn't support `TRUNCATE TABLE`, but SQLite3 adapter can support `ActiveRecord::Base.connection.truncate` by using `DELETE FROM`. `DELETE` without `WHERE` uses "The Truncate Optimization", see https://www.sqlite.org/lang_delete.html. * Add `rails db:seed:replant` that truncates database tables and loads the seeds Closes #34765
* Deprecate mismatched collation comparison for uniquness validatorRyuta Kamizono2019-03-042-1/+15
| | | | | | | | | | | | | | | | | | | | | In MySQL, the default collation is case insensitive. Since the uniqueness validator enforces case sensitive comparison by default, it frequently causes mismatched collation issues (performance, weird behavior, etc) to MySQL users. https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/ https://github.com/rails/rails/issues/1399 https://github.com/rails/rails/pull/13465 https://github.com/gitlabhq/gitlabhq/commit/c1dddf8c7d947691729f6d64a8ea768b5c915855 https://github.com/huginn/huginn/pull/1330#discussion_r55152573 I'd like to deprecate the implicit default enforcing since I frequently experienced the problems in code reviews. Note that this change has no effect to sqlite3, postgresql, and oracle-enhanced adapters which are implemented as case sensitive by default, only affect to mysql2 adapter (I can take a work if sqlserver adapter will support Rails 6.0).
* Merge pull request #35402 from alimi/update-ar-read-regexAaron Patterson2019-02-251-1/+1
|\ | | | | Support read queries with leading characters while preventing writes
| * Update READ_QUERY regexAli Ibrahim2019-02-251-1/+1
| | | | | | | | | | | | | | * The READ_QUERY regex would consider reads to be writes if they started with spaces or parens. For example, a UNION query might have parens around each SELECT - (SELECT ...) UNION (SELECT ...). * It will now correctly treat these queries as reads.
* | Refactor `type_to_sql` to handle converting `limit` to `size` in itselfRyuta Kamizono2019-02-264-67/+60
| | | | | | | | | | Also, improving an argument error message for `limit`, extracting around `type_to_sql` code into schema statements, and more exercise tests.
* | Fix prepared statements caching to be enabled even when query caching is enabledRyuta Kamizono2019-02-263-13/+20
|/ | | | | | | | | | | | Related cbcdecd, 2a56b2d. This is a regression caused by cbcdecd. If query caching is enabled, prepared statement handles are never re-used, since we missed that a query is preprocessed when query caching is enabled, but doesn't keep the `preparable` flag. We should care about that case.
* minor grammar fix [ci skip]Shivam Jain2019-02-241-1/+1
|
* Address "warning: in `column': the last argument was passed as a single Hash"Ryuta Kamizono2019-02-212-4/+4
|
* Extract `default_uniqueness_comparison` to ease to handle mismatched ↵Ryuta Kamizono2019-02-211-0/+4
| | | | | | | | | | | | | | | | | | collation issues In MySQL, the default collation is case insensitive. Since the uniqueness validator enforces case sensitive comparison by default, it frequently causes mismatched collation issues (performance, weird behavior, etc) to MySQL users. https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/ https://github.com/rails/rails/issues/1399 https://github.com/rails/rails/pull/13465 https://github.com/gitlabhq/gitlabhq/commit/c1dddf8c7d947691729f6d64a8ea768b5c915855 https://github.com/huginn/huginn/pull/1330#discussion_r55152573 This extracts `default_uniqueness_comparison` to ease to handle the mismatched collation issues on the connection.
* Merge pull request #35263 from hatch-carl/reduce-postgres-uuid-allocationsRyuta Kamizono2019-02-211-3/+6
|\ | | | | Reduce unused allocations when casting UUIDs for Postgres
| * Reduce unused allocations when casting UUIDs for PostgresCarl Thuringer2019-02-201-3/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using the subscript method `#[]` on a string has several overloads and rather complex implementation. One of the overloads is the capability to accept a regular expression and then run a match, then return the receiver (if it matched) or one of the groups from the MatchData. The function of the `UUID#cast` method is to cast a UUID to a type and format acceptable by postgres. Naturally UUIDs are supposed to be string and of a certain format, but it had been determined that it was not ideal for the framework to send just any old string to Postgres and allow the engine to complain when "foobar" or "" was sent, being obviously of the wrong format for a valid UUID. Therefore this code was written to facilitate the checking, and if it were not of the correct format, a `nil` would be returned as is conventional in Rails. Now, the subscript method will allocate one or more strings on a match and return one of them, based on the index parameter. However, there is no need for a new string, as a UUID of the correct format is already such, and so long as the format was verified then the string supplied is adequate for consumption by the database. The subscript method also creates a MatchData object which will never be used, and so must eventually be garbage collected. Garbage collection indeed. This innocuous method tends to be called quite a lot, for example if the primary key of a table is a uuid, then this method will be called. If the foreign key of a relation is a UUID, once again this method is called. If that foreign key is belonging to a has_many relationship with dozens of objects, then again dozens of UUIDs shall be cast to a dup of themselves, and spawn dozens of MatchData objects, and so on. So, for users that: * Use UUIDs as primary keys * Use Postgres * Operate on collections of objects This accomplishes a significant savings in total allocations, and may save many garbage collections.
* | PostgreSQL: Support endless range values for range typesRyuta Kamizono2019-02-202-2/+2
|/
* Fix the regex that extract mismatched foreign key informationRyuta Kamizono2019-02-171-8/+18
| | | | | | | | | | | | | The CI failure for `test_errors_for_bigint_fks_on_integer_pk_table` is due to the poor regex that extract all ``` `(\w+)` ``` like parts from the message (`:foreign_key` should be `"old_car_id"`, but `"engines"`): https://travis-ci.org/rails/rails/jobs/494123455#L1703 I've improved the regex more strictly and have more exercised mismatched foreign key tests. Fixes #35294
* Merge pull request #35297 from yhara/fix-ar-connection-handler-leakRyuta Kamizono2019-02-161-7/+11
|\ | | | | | | Fix possible memory leak of ConnectionHandler
| * Fix possible memory leak of ConnectionHandlerYutaka HARA2019-02-161-7/+11
| | | | | | | | refs #35296
* | Refactor `remove_foreign_key` to delete the foreign key before `alter_table`Ryuta Kamizono2019-02-161-4/+2
|/
* Properly handle cached queries with too many bind parametersMatthew Dunbar2019-02-141-0/+6
|
* Add `remove_foreign_key` for `change_table`Ryuta Kamizono2019-02-111-0/+11
|
* SQLite3: Implement `add_foreign_key` and `remove_foreign_key`Ryuta Kamizono2019-02-115-6/+34
| | | | | | | | | | | | I implemented Foreign key create in `create_table` for SQLite3 at #24743. This follows #24743 to implement `add_foreign_key` and `remove_foreign_key`. Unfortunately SQLite3 has one limitation that `PRAGMA foreign_key_list(table-name)` doesn't have constraint name. So we couldn't implement find/remove foreign key by name for now. Fixes #35207. Closes #31343.
* More exercise table name prefix and suffix testsRyuta Kamizono2019-02-114-8/+16
|
* Merge pull request #35203 from chiastolite/add_column_without_column_namesRyuta Kamizono2019-02-101-0/+1
|\ | | | | | | Do not allow to add column without column name
| * Do not allow to add column without column nameHiroyuki Morita2019-02-101-0/+1
|/
* Refactor to extract defining column methods as `define_column_methods`Ryuta Kamizono2019-02-093-142/+161
| | | | | It makes to ease to handle all short-hand methods (e.g. validates arguments etc).
* Fix elapsed time calculationsbogdanvlviv2019-02-082-6/+6
| | | | | | | | | | | | | | | | | | | I've found a few places in Rails code base where I think it makes sense to calculate elapsed time more precisely by using `Concurrent.monotonic_time`: - Fix calculation of elapsed time in `ActiveSupport::Cache::MemoryStore#prune` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#wait_poll` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::ConnectionPool#attempt_to_checkout_all_existing_connections` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::Mysql2Adapter#explain` See https://docs.ruby-lang.org/en/2.5.0/Process.html#method-c-clock_gettime https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way Related to 7c4542146f0dde962205e5a90839349631ae60fb
* Relax sqlite3 version dependencySergey Ponomarev2019-02-041-1/+1
|
* Merge pull request #35089 from ↵Eileen M. Uchitelle2019-02-041-1/+1
|\ | | | | | | | | eileencodes/fix-query-cache-for-database-switching Invalidate all query caches for current thread
| * Invalidate query cache for all connections in the current threadEileen Uchitelle2019-02-011-1/+1
| | | | | | | | | | | | This change ensures that all query cahces are cleared across all connections per handler for the current thread so if you write on one connection the read will have the query cache cleared.