aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
Commit message (Collapse)AuthorAgeFilesLines
* Address "warning: shadowing outer local variable - parts"Ryuta Kamizono2018-12-034-7/+8
| | | | And hide the `READ_QUERY` internal constant.
* Add ability to prevent writes to a databaseEileen Uchitelle2018-11-305-1/+64
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This PR adds the ability to prevent writes to a database even if the database user is able to write (ie the database is a primary and not a replica). This is useful for a few reasons: 1) when converting your database from a single db to a primary/replica setup - you can fix all the writes on reads early on, 2) when we implement automatic database switching or when an app is manually switching connections this feature can be used to ensure reads are reading and writes are writing. We want to make sure we raise if we ever try to write in read mode, regardless of database type and 3) for local development if you don't want to set up multiple databases but do want to support rw/ro queries. This should be used in conjunction with `connected_to` in write mode. For example: ``` ActiveRecord::Base.connected_to(role: :writing) do Dog.connection.while_preventing_writes do Dog.create! # will raise because we're preventing writes end end ActiveRecord::Base.connected_to(role: :reading) do Dog.connection.while_preventing_writes do Dog.first # will not raise because we're not writing end end ```
* Allow spaces in postgres table namesGannon McGibbon2018-11-281-1/+1
| | | | | Fixes issue where "user post" is misinterpreted as "\"user\".\"post\"" when quoting table names with the postgres adapter.
* Merge pull request #34557 from sergioisidoro/sergio-patch-load-errorRafael França2018-11-281-2/+2
|\ | | | | Patch load error in case GemSpecError
| * Patch load error in case GemSpecErrorsergioisidoro2018-11-281-2/+2
| |
* | `Mutable` helper is in `ActiveModel`Ryuta Kamizono2018-11-274-4/+4
| | | | | | | | It should be referenced by full qualified name from Active Record.
* | Merge pull request #34520 from yahonda/bump_pg93Rafael França2018-11-262-19/+17
|\ \ | | | | | | Bump the minimum version of PostgreSQL to 9.3
| * | Bump the minimum version of PostgreSQL to 9.3Yasuo Honda2018-11-252-19/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | https://www.postgresql.org/support/versioning/ - 9.1 EOLed on September 2016. - 9.2 EOLed on September 2017. 9.3 is also not supported since Nov 8, 2018. https://www.postgresql.org/about/news/1905/ I think it may be a little bit early to drop PostgreSQL 9.3 yet. * Deprecated `supports_ranges?` since no other databases support range data type * Add `supports_materialized_views?` to abstract adapter Materialized views itself is supported by other databases, other connection adapters may support them * Remove `with_manual_interventions` It was only necessary for PostgreSQL 9.1 or earlier * Drop CI against PostgreSQL 9.2
* | | When running exec_query MySQL always returns ActiveRecord::ResultAlireza Bashiri2018-11-251-2/+10
|/ / | | | | | | | | When running `exec_query` with `INSERT` (or other write commands), MySQL returns `ActiveRecord::Result`.
* | Merge pull request #34468 from gmcgibbon/redact_sql_in_errorsRafael França2018-11-234-41/+41
|\ \ | | | | | | Redact SQL in errors
| * | Redact SQL in errorsGannon McGibbon2018-11-224-41/+41
| | | | | | | | | | | | | | | Move `ActiveRecord::StatementInvalid` SQL to error property. Also add bindings as an error property.
* | | Use squiggly heredoc to strip odd indentation in the executed SQLRyuta Kamizono2018-11-225-17/+17
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before: ``` LOG: execute <unnamed>: SELECT t.oid, t.typname FROM pg_type as t WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'bool') LOG: execute <unnamed>: SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype FROM pg_type as t LEFT JOIN pg_range as r ON oid = rngtypid WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'text', 'varchar', 'char', 'name', 'bpchar', 'bool', 'bit', 'varbit', 'timestamptz', 'date', 'money', 'bytea', 'point', 'hstore', 'json', 'jsonb', 'cidr', 'inet', 'uuid', 'xml', 'tsvector', 'macaddr', 'citext', 'ltree', 'interval', 'path', 'line', 'polygon', 'circle', 'lseg', 'box', 'time', 'timestamp', 'numeric') OR t.typtype IN ('r', 'e', 'd') OR t.typinput::varchar = 'array_in' OR t.typelem != 0 LOG: statement: SHOW TIME ZONE LOG: statement: SELECT 1 LOG: execute <unnamed>: SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view AND c.relname = 'accounts' AND n.nspname = ANY (current_schemas(false)) ``` After: ``` LOG: execute <unnamed>: SELECT t.oid, t.typname FROM pg_type as t WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'bool') LOG: execute <unnamed>: SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype FROM pg_type as t LEFT JOIN pg_range as r ON oid = rngtypid WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'text', 'varchar', 'char', 'name', 'bpchar', 'bool', 'bit', 'varbit', 'timestamptz', 'date', 'money', 'bytea', 'point', 'hstore', 'json', 'jsonb', 'cidr', 'inet', 'uuid', 'xml', 'tsvector', 'macaddr', 'citext', 'ltree', 'interval', 'path', 'line', 'polygon', 'circle', 'lseg', 'box', 'time', 'timestamp', 'numeric') OR t.typtype IN ('r', 'e', 'd') OR t.typinput::varchar = 'array_in' OR t.typelem != 0 LOG: statement: SHOW TIME ZONE LOG: statement: SELECT 1 LOG: execute <unnamed>: SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view AND c.relname = 'accounts' AND n.nspname = ANY (current_schemas(false)) ```
* | Fixing an issue when parsing an opclass by allowing indexed columnThomas Bianchini2018-11-211-1/+1
| | | | | | | | | | | | | | | | in indexdef to be wrapped up by double quotes Fixes #34493. *Thomas Bianchini*
* | Fix typo and clarify documentationJacob Evelyn2018-11-161-3/+4
| | | | | | | | | | | | This commit fixes a small typo in documentation of the "UNLOGGED" table option for PostgreSQL databases, and clarifies the documentation slightly.
* | Merge pull request #34436 from gmcgibbon/fix_default_max_bind_length_sqliteRafael França2018-11-131-0/+6
|\ \ | | | | | | Adjust bind length of SQLite to default (999)
| * | Adjust bind length of SQLite to default (999)Gannon McGibbon2018-11-131-0/+6
| | | | | | | | | | | | | | | Change `#bind_params_length` in SQLite adapter to return the default maximum amount (999). See https://www.sqlite.org/limits.html
* | | Add support for UNLOGGED Postgresql tablesJacob Evelyn2018-11-134-1/+36
|/ / | | | | | | | | | | | | | | | | | | | | This commit adds support for the `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables` setting, which turns `CREATE TABLE` SQL statements into `CREATE UNLOGGED TABLE` statements. This can improve PostgreSQL performance but at the cost of data durability, and thus it is highly recommended that you *DO NOT* enable this in a production environment.
* | Deprecate `t.indexes = [...]` which is not by designRyuta Kamizono2018-11-091-2/+5
| | | | | | | | Use `t.index ...` instead.
* | Refactor to initialize `TableDefinition` by kwargsRyuta Kamizono2018-11-092-4/+14
| |
* | PostgreSQL: Properly quote all `Infinity` and `NaN`Ryuta Kamizono2018-11-091-4/+4
| | | | | | | | Since quoted `Infinity` and `NaN` are valid data for PostgreSQL.
* | Add an :if_not_exists option to create_tablefatkodima2018-11-083-4/+10
| | | | | | | | [fatkodima & Stefan Kanev]
* | Always add records to parent of nested transactionEugene Kenny2018-11-071-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When a record with transactional callbacks is saved, it's attached to the current transaction so that the callbacks can be run when the transaction is committed. Records can also be added manually with `add_transaction_record`, even if they have no transactional callbacks. When a nested transaction is committed, its records are transferred to the parent transaction, as transactional callbacks should only be run when the outermost transaction is committed (the "real" transaction). However, this currently only happens when the record has transactional callbacks, and not when added manually with `add_transaction_record`. If a record is added to a nested transaction, we should always attach it to the parent transaction when the nested transaction is committed, regardless of whether it has any transactional callbacks. [Eugene Kenny & Ryuta Kamizono]
* | Checking boundable not only `IN` clause but also `NOT IN` clauseRyuta Kamizono2018-11-031-8/+4
| |
* | Support default expression for MySQLRyuta Kamizono2018-10-251-4/+7
| | | | | | | | | | | | | | MySQL 8.0.13 and higher supports default value to be a function or expression. https://dev.mysql.com/doc/refman/8.0/en/create-table.html
* | Support expression indexes for MySQLRyuta Kamizono2018-10-252-4/+34
| | | | | | | | | | | | | | MySQL 8.0.13 and higher supports functional key parts that index expression values rather than column or column prefix values. https://dev.mysql.com/doc/refman/8.0/en/create-index.html
* | Merge pull request #34303 from kamipo/lazy_checking_boundableRafael França2018-10-241-1/+10
|\ \ | |/ |/| Lazy checking whether or not values in IN clause are boundable
| * Lazy checking whether or not values in IN clause are boundableRyuta Kamizono2018-10-241-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | Since #33844, eager loading/preloading with too many and/or too large ids won't be broken by pre-checking whether the value is constructable or not. But the pre-checking caused the type to be evaluated at relation build time instead of at the query execution time, that is breaking an expectation for some apps. I've made the pre-cheking lazy as much as possible, that is no longer happend at relation build time.
* | Hide PG::Connection from API docs [ci skip]Francesco Rodríguez2018-10-231-1/+1
| |
* | MySQL 8.0.13 raises `ER_NO_REFERENCED_ROW` and `ER_ROW_IS_REFERENCED`Yasuo Honda2018-10-231-1/+3
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | when user has no parent table access privileges Refer https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-13.html#mysqld-8-0-13-errors >> * Previously, the ER_NO_REFERENCED_ROW_2 and ER_ROW_IS_REFERENCED_2 error messages for foreign key operations were displayed and revealed information about parent tables, even when the user had no parent table access privileges. Error handling for this situation has been revised: * If the user does have table-level privileges for all parent tables, ER_NO_REFERENCED_ROW_2 and ER_ROW_IS_REFERENCED_2 are displayed, the same as before. * If the user does not have table-level privileges for all parent tables, more generic error messages are displayed instead (ER_NO_REFERENCED_ROW and ER_ROW_IS_REFERENCED). << This pull request addresses these 3 failures: ```ruby $ ARCONN=mysql2 bundle exec ruby -w -Itest test/cases/adapter_test.rb -n /foreign/ Using mysql2 Run options: -n /foreign/ --seed 14251 F Failure: ActiveRecord::AdapterForeignKeyTest#test_foreign_key_violations_are_translated_to_specific_exception_with_validate_false [test/cases/adapter_test.rb:348]: [ActiveRecord::InvalidForeignKey] exception expected, not Class: <ActiveRecord::StatementInvalid> Message: <"Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails: INSERT INTO `fk_test_has_fk` (`fk_id`) VALUES (1231231231)"> ... snip ... rails test test/cases/adapter_test.rb:343 F Failure: ActiveRecord::AdapterForeignKeyTest#test_foreign_key_violations_on_delete_are_translated_to_specific_exception [test/cases/adapter_test.rb:368]: [ActiveRecord::InvalidForeignKey] exception expected, not Class: <ActiveRecord::StatementInvalid> Message: <"Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails: DELETE FROM fk_test_has_pk WHERE pk_id = 1"> ... snip ... rails test test/cases/adapter_test.rb:365 F Failure: ActiveRecord::AdapterForeignKeyTest#test_foreign_key_violations_on_insert_are_translated_to_specific_exception [test/cases/adapter_test.rb:358]: [ActiveRecord::InvalidForeignKey] exception expected, not Class: <ActiveRecord::StatementInvalid> Message: <"Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails: INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"> ... snip ... rails test test/cases/adapter_test.rb:357 Finished in 0.087370s, 34.3366 runs/s, 34.3366 assertions/s. 3 runs, 3 assertions, 3 failures, 0 errors, 0 skips $ ```
* Remove and flip `index: true` for `references` in the doc [ci skip]Ryuta Kamizono2018-10-172-8/+8
| | | | Follow up #32146.
* Consistently extract checking version for all adaptersRyuta Kamizono2018-10-174-27/+29
| | | | | | | I don't prefer to extract it for one adapter even though all adapters also does. Related to #34227.
* Refactored abstract MySQL adapter to support lazy version check.Brooke Kuhlmann2018-10-161-4/+11
| | | | | | | | | Will allow sub classes to override the protected `#check_version` method hook if desired. For example, this will be most helpful in sub classes that wish to support lazy initialization because the version check can be postponed until the connection is ready to be initialized.
* 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.