aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
Commit message (Collapse)AuthorAgeFilesLines
...
* Move UPDATE/DELETE with JOIN handling to the Arel sideRyuta Kamizono2018-10-032-43/+0
|
* Merge pull request #23593 from meinac/add_index_option_for_change_tableRyuta Kamizono2018-10-011-0/+2
|\ | | | | | | index option added for change_table migrations
| * Index option added for change_table migrationsMehmet Emin INAC2018-09-221-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In case if we want to add a column into the existing table with index on it, we have to add column and index in two seperate lines. With this feature we don't need to write an extra line to add index for column. We can just use `index` option. Old behaviour in action: ``` change_table(:languages) do |t| t.string :country_code t.index: :country_code end ``` New behaviour in action: ``` change_table(:languages) do |t| t.string :country_code, index: true end ``` Exactly same behaviour is already exist for `create_table` migrations.
* | Merge pull request #32031 from yahonda/remove_redundant_freezeRyuta Kamizono2018-10-019-24/+24
|\ \ | | | | | | Add `Style/RedundantFreeze` to remove redudant `.freeze`
| * | Add `Style/RedundantFreeze` to remove redudant `.freeze`Yasuo Honda2018-09-299-24/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since Rails 6.0 will support Ruby 2.4.1 or higher `# frozen_string_literal: true` magic comment is enough to make string object frozen. This magic comment is enabled by `Style/FrozenStringLiteralComment` cop. * Exclude these files not to auto correct false positive `Regexp#freeze` - 'actionpack/lib/action_dispatch/journey/router/utils.rb' - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb' It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333 Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed. * Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required - 'actionpack/test/controller/test_case_test.rb' - 'activemodel/test/cases/type/string_test.rb' - 'activesupport/lib/active_support/core_ext/string/strip.rb' - 'activesupport/test/core_ext/string_ext_test.rb' - 'railties/test/generators/actions_test.rb'
* | | Place `PartialQuery` and `PartialQueryCollector` in the same fileRyuta Kamizono2018-09-301-23/+1
|/ /
* / Enable `Performance/UnfreezeString` copyuuji.yaginuma2018-09-2310-17/+17
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In Ruby 2.3 or later, `String#+@` is available and `+@` is faster than `dup`. ```ruby # frozen_string_literal: true require "bundler/inline" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report('+@') { +"" } x.report('dup') { "".dup } x.compare! end ``` ``` $ ruby -v benchmark.rb ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux] Warming up -------------------------------------- +@ 282.289k i/100ms dup 187.638k i/100ms Calculating ------------------------------------- +@ 6.775M (± 3.6%) i/s - 33.875M in 5.006253s dup 3.320M (± 2.2%) i/s - 16.700M in 5.032125s Comparison: +@: 6775299.3 i/s dup: 3320400.7 i/s - 2.04x slower ```
* No private def in the codebaseRafael Mendonça França2018-09-211-3/+5
|
* Use utf8mb4 in all tests and examplesRyuta Kamizono2018-09-211-2/+2
| | | | | Since #33875, Rails dropped supporting MySQL 5.1 which does not support utf8mb4. We no longer need to use legacy utf8 (utf8mb3) conservatively.
* Deprecate ActiveRecord::Result#to_hash in favor of #to_aKevin Cheng2018-09-181-1/+1
| | | | | | | method returns an array of hashes, not a hash e.g. Hash.try_convert(result) calls #to_hash and raises a TypeError [Gannon McGibbon + Kevin Cheng]
* Merge pull request #33188 from larskanis/pg-1.1Yuji Yaginuma2018-09-172-2/+16
|\ | | | | PostgreSQL: prepare for pg-1.1
| * Return empty array when casting malformed array stringsLars Kanis2018-06-231-1/+7
| | | | | | | | | | | | | | | | Parsing of malformed array strings without raising an error is deprecated in pg-1.1. It's therefore necessary to catch parser errors starting with pg-2.0. See also pg commit: https://bitbucket.org/ged/ruby-pg/commits/1b081326b346368e70c9c03ee7080e28d6b3a3dc
| * PostgreSQL: Prepare for pg-1.1.0Lars Kanis2018-05-271-1/+9
| | | | | | | | | | | | | | | | | | | | | | | | Version 1.1.0 deprecates exec and async_exec with a params array due to distinct semantics between calls with and without params array. Instead exec_params or async_exec_params shall be used. Moreover in pg-1.1.0 exec_* and prepare methods are aliases for async_exec_* and async_prepare. async_* methods don't need to be called explicit - they are the default now, when calling exec_* and prepare. For pg versions before 1.1, keep using async_exec.
* | Merge pull request #33878 from kamipo/fallback_to_unprepared_statementRyuta Kamizono2018-09-152-0/+10
|\ \ | | | | | | Fallback to unprepared statement only when bind params limit is exceeded
| * | Fallback to unprepared statement only when bind params limit is exceededRyuta Kamizono2018-09-142-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a follow up and/or an alternative of #33844. Unlike #33844, this would attempt to construct unprepared statement only when bind params limit (mysql2 65535, pg 65535, sqlite3 249999) is exceeded. I only defined 65535 as the limit, not defined 249999 for sqlite3, since it is an edge case, I'm not excited to add less worth extra code.
* | | Merge pull request #33879 from yahonda/another_33876Ryuta Kamizono2018-09-141-1/+1
|\ \ \ | | | | | | | | Remove mysql2 gem version requirement "< 0.6.0"
| * | | Remove mysql2 gem version requirement "< 0.6.0"Yasuo Honda2018-09-141-1/+1
| | | | | | | | | | | | | | | | Suggested at https://github.com/rails/rails/pull/33876#issuecomment-421176221
* | | | SQLite3: Support multiple args function for expression indexesRyuta Kamizono2018-09-142-14/+18
|/ / / | | | | | | | | | | | | Follow up #33874. Related #23393.
* / / SQLite3 adapter supports expression indexesgkemmey2018-09-132-1/+9
|/ /
* | Drop MySQL 5.1 supportYasuo Honda2018-09-131-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | * MySQL 5.1 does not support `utf8mb4` character set * MySQL 5.1 had been already EOLed on Dec 2013 https://www.mysql.com/support/eol-notice.html > Per Oracle's Lifetime Support policy, as of December 31, 2013, MySQL 5.1 > is covered under Oracle Sustaining Support. * MySQL 5.5.8 is the first General Availability of MySQL 5.5 https://dev.mysql.com/doc/relnotes/mysql/5.5/en/news-5-5-8.html
* | Raise an exception if :charset is not specified and large prefixes / utf8mb4 ↵Yasuo Honda2018-09-131-1/+1
| | | | | | | | are not supported
* | Validate if `utf8mb4` character set and longer index key prefix is supportedYasuo Honda2018-09-131-1/+13
| | | | | | | | | | | | | | | | | | | | | | Once #33608 merged If users create a new database using MySQL 5.1.x, it will fail to create databases since MySQL 5.1 does not know `utf8mb4` character set. This pull request removes `encoding: utf8mb4` from `mysql.yml.tt` to let create_database method handles default character set by MySQL server version. `supports_longer_index_key_prefix?` method will need to validate if MySQL 5.5 and 5.6 server configured correctly to support longer index key prefix, but not yet.
* | Use utf8mb4 character set by default for MySQL database (#33608)Yasuo Honda2018-09-111-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Use utf8mb4 character set by default `utf8mb4` character set supports supplementary characters including emoji. `utf8` character set with 3-Byte encoding is not enough to support them. There was a downside of 4-Byte length character set with MySQL 5.5 and 5.6: "ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes" for Rails string data type which is mapped to varchar(255) type. MySQL 5.7 supports 3072 byte key prefix length by default. * Remove `DEFAULT COLLATE` from Active Record unit test databases There should be no "one size fits all" collation in MySQL 5.7. Let MySQL server choose the default collation for Active Record unit test databases. Users can choose their best collation for their databases by setting `options[:collation]` based on their requirements. * InnoDB FULLTEXT indexes support since MySQL 5.6 it does not have to use MyISAM storage engine whose maximum key length is 1000 bytes. Using MyISAM storag engine with utf8mb4 character set would cause "Specified key was too long; max key length is 1000 bytes" https://dev.mysql.com/doc/refman/5.6/en/innodb-fulltext-index.html * References "10.9.1 The utf8mb4 Character Set (4-Byte UTF-8 Unicode Encoding)" https://dev.mysql.com/doc/refman/5.7/en/charset-unicode-utf8mb4.html "10.9.2 The utf8mb3 Character Set (3-Byte UTF-8 Unicode Encoding)" https://dev.mysql.com/doc/refman/5.7/en/charset-unicode-utf8.html "14.8.1.7 Limits on InnoDB Tables" https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html > If innodb_large_prefix is enabled (the default), the index key prefix limit is 3072 bytes > for InnoDB tables that use DYNAMIC or COMPRESSED row format. * CI against MySQL 5.7 Followed this instruction and changed root password to empty string. https://docs.travis-ci.com/user/database-setup/#MySQL-57 * The recommended minimum version of MySQL is 5.7.9 to support utf8mb4 character set and `innodb_default_row_format` MySQL 5.7.9 introduces `innodb_default_row_format` to support 3072 byte length index by default. Users do not have to change MySQL database configuration to support Rails string type. https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_default_row_format https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html > If innodb_large_prefix is enabled (the default), > the index key prefix limit is 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format. * The recommended minimum version of MariaDB is 10.2.2 MariaDB 10.2.2 is the first version of MariaDB supporting `innodb_default_row_format` Also MariaDB says "MySQL 5.7 is compatible with MariaDB 10.2". - innodb_default_row_format https://mariadb.com/kb/en/library/xtradbinnodb-server-system-variables/#innodb_default_row_format - "MariaDB versus MySQL - Compatibility" https://mariadb.com/kb/en/library/mariadb-vs-mysql-compatibility/ > MySQL 5.7 is compatible with MariaDB 10.2 - "Supported Character Sets and Collations" https://mariadb.com/kb/en/library/supported-character-sets-and-collations/
* | Consistently use `visitor.compile`Ryuta Kamizono2018-09-091-3/+3
| |
* | Use `visitor.compile` instead of constructing by connection itselfRyuta Kamizono2018-09-091-5/+1
| |
* | `supports_xxx?` returns whether a feature is supported by the backendRyuta Kamizono2018-09-083-2/+6
| | | | | | | | Rather than a configuration on the connection.
* | Merge pull request #33809 from fidalgo/improve-remove-column-documentationRichard Schneeman2018-09-061-0/+1
|\ \ | | | | | | [ci skip] Improve remove_column documentation
| * | [ci skip] Improve remove_column documentationPaulo Fidalgo2018-09-061-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | Since when we remove one column it will also remove the associated indexes, we must ensure this behaviour is properly documented. In this commit we add a line to the documentation mentioning this behaviour.
* | | Deprecate most methods which were never used in `DatabaseLimits`Ryuta Kamizono2018-09-051-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | `DatabaseLimits` and those methods were introduced at 3809c80, but most methods were never used and never tested from the beginning (except `table_alias_length`, `index_name_length`, and `in_clause_length` (since 66c09372)). There is no reason to maintain unused those methods for about 8 years.
* | | Add config option for `replica`.Eileen Uchitelle2018-08-311-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This allows the user to add `replica: true` to the database config to signify the connection should be treated as readonly. This will be useful so we can ignore structure dumps or migrations (or creating / deleting etc) the readonly connection for the databases. These are paired with a write database which is where the create/drop/migrate should be run. This allows us to ask the connection if it's for a replica readonly db or a primary write db.
* | | Refactors Active Record connection managementEileen Uchitelle2018-08-301-38/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | While the three-tier config makes it easier to define databases for multiple database applications, it quickly became clear to offer full support for multiple databases we need to change the way the connections hash was handled. A three-tier config means that when Rails needed to choose a default configuration (in the case a user doesn't ask for a specific configuration) it wasn't clear to Rails which the default was. I [bandaid fixed this so the rake tasks could work](#32271) but that fix wasn't correct because it actually doubled up the configuration hashes. Instead of attemping to manipulate the hashes @tenderlove and I decided that it made more sense if we converted the hashes to objects so we can easily ask those object questions. In a three tier config like this: ``` development: primary: database: "my_primary_db" animals: database; "my_animals_db" ``` We end up with an object like this: ``` @configurations=[ #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",@spec_name="primary", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90 @env_name="development",@spec_name="animals", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}> ]> ``` The configurations setter takes the database configuration set by your application and turns them into an `ActiveRecord::DatabaseConfigurations` object that has one getter - `@configurations` which is an array of all the database objects. The configurations getter returns this object by default since it acts like a hash in most of the cases we need. For example if you need to access the default `development` database we can simply request it as we did before: ``` ActiveRecord::Base.configurations["development"] ``` This will return primary development database configuration hash: ``` { "database" => "my_primary_db" } ``` Internally all of Active Record has been converted to use the new objects. I've built this to be backwards compatible but allow for accessing the hash if needed for a deprecation period. To get the original hash instead of the object you can either add `to_h` on the configurations call or pass `legacy: true` to `configurations. ``` ActiveRecord::Base.configurations.to_h => { "development => { "database" => "my_primary_db" } } ActiveRecord::Base.configurations(legacy: true) => { "development => { "database" => "my_primary_db" } } ``` The new configurations object allows us to iterate over the Active Record configurations without losing the known environment or specification name for that configuration. You can also select all the configs for an env or env and spec. With this we can always ask any object what environment it belongs to: ``` db_configs = ActiveRecord::Base.configurations.configurations_for("development") => #<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800 @configurations=[ #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",@spec_name="primary", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90 @env_name="development",@spec_name="animals", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}> ]> db_config.env_name => "development" db_config.spec_name => "primary" db_config.config => { "adapter"=>"sqlite3", "database"=>"db/development.sqlite3" } ``` The configurations object is more flexible than the configurations hash and will allow us to build on top of the connection management in order to add support for primary/replica connections, sharding, and constructing queries for associations that live in multiple databases.
* | | Add documentation for `:collation` column option (#33733)Nate Pinsky2018-08-271-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Add documentation for `:collation` option The table definition supports a `:collation` option for string and text columns, but this is not documented anywhere that I could find. I'm not sure if the "If not specified" part is accurate. From [this PR](https://github.com/rails/rails/commit/1515c4d98da3f730ef971fa5a13cad828bd9bef4), it looks like it passes `nil` and lets the database handle the collation, but I'm happy to change it if I misread the code. [ci skip] * FIX remove whitespace [Nate Pinsky + Rafael Mendonça França]
* | | Merge pull request #31696 from BrentWheeldon/bmw-connection-pool-load-deadlockMatthew Draper2018-08-241-1/+3
|\ \ \ | | | | | | | | Prevent deadlocks when waiting for connection from pool.
| * | | Prevent deadlocks when waiting for connection from pool.Brent Wheeldon2018-03-231-1/+3
| | | | | | | | | | | | | | | | | | | | When a thread that had the load interlock but was blocked waiting to check a connection out of the connection pool but all of the threads using the available connections were blocked waiting to obtain the load interlock an `ActiveRecord::ConnectionTimeoutError` exception was be thrown by the thread waiting for the connection. When waiting for the connection to check out we should allow loading to proceed to avoid this deadlock.
* | | | Merge pull request #32647 from eugeneius/lazy_transactionsMatthew Draper2018-08-239-14/+103
|\ \ \ \ | | | | | | | | | | Omit BEGIN/COMMIT statements for empty transactions
| * | | | Omit BEGIN/COMMIT statements for empty transactionsEugene Kenny2018-08-139-14/+103
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If a transaction is opened and closed without any queries being run, we can safely omit the `BEGIN` and `COMMIT` statements, as they only exist to modify the connection's behaviour inside the transaction. This removes the overhead of those statements when saving a record with no changes, which makes workarounds like `save if changed?` unnecessary. This implementation buffers transactions inside the transaction manager and materializes them the next time the connection is used. For this to work, the adapter needs to guard all connection use with a call to `materialize_transactions`. Because of this, adapters must opt in to get this new behaviour by implementing `supports_lazy_transactions?`. If `raw_connection` is used to get a reference to the underlying database connection, the behaviour is disabled and transactions are opened eagerly, as we can't know how the connection will be used. However when the connection is checked back into the pool, we can assume that the application won't use the reference again and reenable lazy transactions. This prevents a single `raw_connection` call from disabling lazy transactions for the lifetime of the connection.
* | | | | Partly revert unused accessorRyuta Kamizono2018-08-231-8/+1
| | | | | | | | | | | | | | | | | | | | This was introduced at 24f6bf0d96b58f2b2ef6a886c93d35cf8ce4f293.
* | | | | Add database configuration to disable advisory locks.Guo Xiang Tan2018-08-223-3/+14
| | | | | | | | | | | | | | | | | | | | https://github.com/rails/rails/issues/31190
* | | | | SQLite3: Fix rename reference column not to lose foreign key constraintRyuta Kamizono2018-08-201-11/+15
| | | | | | | | | | | | | | | | | | | | Fixes #33520.
* | | | | Merge pull request #33585 from yahonda/diag33520Ryuta Kamizono2018-08-161-1/+9
|\ \ \ \ \ | | | | | | | | | | | | | | | | | | SQLite3 adapter `alter_table` method restores foreign keys
| * | | | | SQLite3 adapter `alter_table` method restores foreign keysYasuo Honda2018-08-111-1/+9
| |/ / / / | | | | | | | | | | | | | | | Related to #33520
* | | | | Fix bulk change table ignores comment option on PostgreSQL.Yoshiyuki Kinjo2018-08-151-1/+5
| | | | |
* | | | | Follow up #33530bogdanvlviv2018-08-151-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - Move changelog entry of #33530 up in order to preserve the chronology since we always add new entries on the top of a changelog file. - Clarify the changelog entry - Clarify the docs of remove_foreign_key - Ensure reversible of `remove_foreign_key` with `:primary_key` and `:to_table` options.
* | | | | Merge pull request #33530 from jychen7/33515-invert-remove-foreign-keyRichard Schneeman2018-08-141-1/+8
|\ \ \ \ \ | | | | | | | | | | | | 33515 invert remove foreign key support "to_table"
| * | | | | Allow `to_table` in `invert_remove_foreign_key`Rich2018-08-141-1/+8
| |/ / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | remove_foreign_key supports - remove_foreign_key :accounts, :branches - remove_foreign_key :accounts, to_table: :branches but the second one is not reversible. This branch is to fix and allow second one to be reversible. [Nikolay Epifanov, Rich Chen]
* / / / / Use `Array#extract!` where possiblebogdanvlviv2018-08-141-6/+8
|/ / / /
* | | | `retrieve_connection_pool` return a pool, not a connectionRyuta Kamizono2018-08-031-2/+2
| | | |
* | | | MySQL: Raise ActiveRecord::InvalidForeignKey for foreign-key constraint ↵George Claghorn2018-07-301-1/+2
| | | | | | | | | | | | | | | | violations on delete
* | | | Fix test failures due to Performance/RegexpMatch correctionBart de Water2018-07-281-1/+3
| | | | | | | | | | | | | | | | `5 =~ /\d/` returns nil, but Integer doesn't have a `match?` method.
* | | | Enable Start/EndWith and RegexpMatch copsBart de Water2018-07-281-1/+1
| | | | | | | | | | | | | | | | | | | | In cases where the MatchData object is not used, this provides a speed-up: https://github.com/JuanitoFatas/fast-ruby/#stringmatch-vs-stringmatch-vs-stringstart_withstringend_with-code-start-code-end