aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/mysql
Commit message (Collapse)AuthorAgeFilesLines
* Share the column and table name quote cache between connectionsJean Boussier2019-07-091-2/+2
|
* MySQL: Fix schema dumping `enum` and `set` columns correctlyRyuta Kamizono2019-07-051-1/+3
| | | | | | | | | `enum` and `set` are typed cast as `:string`, but currently the `:string` type is incorrectly reused for schema dumping. A cast type on columns is not always the same with `sql_type`, this fixes schema dumping `enum` and `set` columns to use `sql_type` instead of `type` correctly.
* Merge pull request #35891 from Shopify/schema-cache-deduplicationKasper Timm Hansen2019-06-191-1/+10
|\ | | | | Deduplicate various Active Record schema cache structures
| * Deduplicate various Active Record schema cache structuresJean Boussier2019-06-031-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | Real world database schemas contain a lot of duplicated data. Some column names like `id`, `created_at` etc can easily be repeated hundreds of times. Same for SqlTypeMetada, most database will contain only a limited number of possible combinations. This result in a lot of wasted memory. The idea here is to make these data sctructures immutable, use a registry to substitute similar instances with pre-existing ones.
* | Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-132-2/+0
| | | | | | | | | | | | | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* | Allow column name with function (e.g. `length(title)`) as safe SQL stringRyuta Kamizono2019-06-101-2/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, almost all "Dangerous query method" warnings are false alarm. As long as almost all the warnings are false alarm, developers think "Let's ignore the warnings by using `Arel.sql()`, it actually is false alarm in practice.", so I think we should effort to reduce false alarm in order to make the warnings valuable. This allows column name with function (e.g. `length(title)`) as safe SQL string, which is very common false alarm pattern, even in the our codebase. Related 6c82b6c99, 6607ecb2a, #36420. Fixes #32995.
* | Allow `column_name AS alias` as safe SQL stringRyuta Kamizono2019-06-101-0/+1
| |
* | Refactor `disallow_raw_sql!` to avoid `split(/\s*,\s*/)` to order argsRyuta Kamizono2019-06-091-4/+13
| | | | | | | | | | `split(/\s*,\s*/)` to order args and then `permit.match?` one by one is much slower than `permit.match?` once.
* | Allow quoted identifier string as safe SQL stringRyuta Kamizono2019-06-061-5/+26
|/ | | | | | | | | | | | | Currently `posts.title` is regarded as a safe SQL string, but `"posts"."title"` (it is a result of `quote_table_name("posts.title")`) is regarded as an unsafe SQL string even though a result of `quote_table_name` should obviously be regarded as a safe SQL string, since the column name matcher doesn't respect quotation, it is a little annoying. This changes the column name matcher to allow quoted identifiers as safe SQL string, now all results of the `quote_table_name` are regarded as safe SQL string.
* Put all `explain` methods into `DatabaseStatements` moduleRyuta Kamizono2019-05-221-0/+9
| | | | | Almost all database statements methods except `explain` was moved into `DatabaseStatements` at #35922. This moves the last one method.
* Refactor around sql_type metadata and columnRyuta Kamizono2019-04-121-2/+1
| | | | | | * remove useless `@type_metadata` and `@array` * move the compatibility code (for array) into column * etc.
* Improve == and hash methods on various schema cache structs to be allocation ↵Jean Boussier2019-04-091-8/+5
| | | | | | | | free. The previous implementation would allocate 2 arrays per comparisons. I tried relying on Struct, but they do allocate one Hash inside `Struct#hash`.
* Merge pull request #34800 from mqchau/mysqlCountDeleteRowInLockMatthew Draper2019-04-091-1/+3
|\ | | | | Wrap Mysql count of deleted rows in lock block to avoid conflict in test
| * Wrap Mysql count of deleted rows in lock block to avoid conflict in testQuan Chau2019-04-081-1/+3
| |
* | Merge pull request #35887 from kamipo/argument_errorRyuta Kamizono2019-04-091-2/+2
|\ \ | | | | | | Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options
| * | Raise `ArgumentError` for invalid `:limit` and `:precision` like as other ↵Ryuta Kamizono2019-04-071-2/+2
| |/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | options When I've added new `:size` option in #35071, I've found that invalid `:limit` and `:precision` raises `ActiveRecordError` unlike other invalid options. I think that is hard to distinguish argument errors and statement invalid errors since the `StatementInvalid` is a subclass of the `ActiveRecordError`. https://github.com/rails/rails/blob/c9e4c848eeeb8999b778fa1ae52185ca5537fffe/activerecord/lib/active_record/errors.rb#L103 ```ruby begin # execute any migration rescue ActiveRecord::StatementInvalid # statement invalid rescue ActiveRecord::ActiveRecordError, ArgumentError # `ActiveRecordError` except `StatementInvalid` is maybe an argument error end ``` I'd say this is the inconsistency worth fixing. Before: ```ruby add_column :items, :attr1, :binary, size: 10 # => ArgumentError add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError add_column :items, :attr3, :integer, limit: 10 # => ActiveRecordError add_column :items, :attr4, :datetime, precision: 10 # => ActiveRecordError ``` After: ```ruby add_column :items, :attr1, :binary, size: 10 # => ArgumentError add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError add_column :items, :attr3, :integer, limit: 10 # => ArgumentError add_column :items, :attr4, :datetime, precision: 10 # => ArgumentError ```
* | Except `table_name` from column objectsRyuta Kamizono2019-04-082-5/+4
| | | | | | | | | | | | | | | | | | The `table_name` was added at #23677 to detect whether serial column or not correctly. We can do that detection before initialize column object, it makes column object size smaller, and it probably helps column object de-duplication.
* | Fix GROUP BY with calculate longer name field to respect `table_alias_length`Ryuta Kamizono2019-04-081-0/+4
|/ | | | Follow up of c9e4c848eeeb8999b778fa1ae52185ca5537fffe.
* Merge pull request #35795 from alimi/cache-database-versionEileen M. Uchitelle2019-04-032-3/+3
|\ | | | | Cache database version in schema cache
| * Cache database version in schema cacheAli Ibrahim2019-04-032-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * The database version will get cached in the schema cache file during the schema cache dump. When the database version check happens, the version will be pulled from the schema cache and thus avoid querying the database for the version. * If the schema cache file doesn't exist, we'll query the database for the version and cache it on the schema cache object. * To facilitate this change, all connection adapters now implement #get_database_version and #database_version. #database_version returns the value from the schema cache. * To take advantage of the cached database version, the database version check will now happen after the schema cache is set on the connection in the connection pool.
* | `bytes_margin` is only needed when `previous_packet` existsRyuta Kamizono2019-04-031-5/+2
|/
* SQLite3: Make fixture loading to bulk statementsRyuta Kamizono2019-03-171-1/+1
|
* 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-171-12/+5
|
* Make `truncate_tables` to bulk statementsRyuta Kamizono2019-03-171-0/+46
| | | | | | | | | | | | | | | | | | 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` ```
* Refactor `type_to_sql` to handle converting `limit` to `size` in itselfRyuta Kamizono2019-02-262-10/+58
| | | | | Also, improving an argument error message for `limit`, extracting around `type_to_sql` code into schema statements, and more exercise tests.
* Refactor to extract defining column methods as `define_column_methods`Ryuta Kamizono2019-02-091-32/+40
| | | | | It makes to ease to handle all short-hand methods (e.g. validates arguments etc).
* Allow changing text and blob size without giving the `limit` optionRyuta Kamizono2019-01-292-2/+18
| | | | | | | | | | | | | | In MySQL, the text column size is 65,535 bytes by default (1 GiB in PostgreSQL). It is sometimes too short when people want to use a text column, so they sometimes change the text size to mediumtext (16 MiB) or longtext (4 GiB) by giving the `limit` option. Unlike MySQL, PostgreSQL doesn't allow the `limit` option for a text column (raises ERROR: type modifier is not allowed for type "text"). So `limit: 4294967295` (longtext) couldn't be used in Action Text. I've allowed changing text and blob size without giving the `limit` option, it prevents that migration failure on PostgreSQL.
* Make `t.timestamps` with precision by defaultRyuta Kamizono2019-01-261-1/+1
|
* MySQL: `ROW_FORMAT=DYNAMIC` create table option by defaultRyuta Kamizono2018-12-191-0/+18
| | | | | | | | | | | | | | | | | | | Since MySQL 5.7.9, the `innodb_default_row_format` option defines the default row format for InnoDB tables. The default setting is `DYNAMIC`. The row format is required for indexing on `varchar(255)` with `utf8mb4` columns. As long as using MySQL 5.6, CI won't be passed even if MySQL server setting is properly configured the same as MySQL 5.7 (`innodb_file_per_table = 1`, `innodb_file_format = 'Barracuda'`, and `innodb_large_prefix = 1`) since InnoDB table is created as the row format `COMPACT` by default on MySQL 5.6, therefore indexing on string with `utf8mb4` columns aren't succeeded. Making `ROW_FORMAT=DYNAMIC` create table option by default for legacy MySQL version would mitigate the indexing issue on the user side, and it makes CI would be passed on MySQL 5.6 which is configured properly.
* Use `utf8mb4` charset for internal tables if the row format `DYNAMIC` by defaultRyuta Kamizono2018-12-191-1/+9
| | | | The indexing issue on `utf8mb4` columns is resolved since MySQL 5.7.9.
* Ensure that preventing writes is invoked before `materialize_transactions` ↵Ryuta Kamizono2018-12-121-4/+2
| | | | consistently
* An empty transaction does not raise the `ReadOnlyError` if preventing writesRyuta Kamizono2018-12-111-1/+1
| | | | | | BEGIN transaction would cause COMMIT or ROLLBACK, so unless COMMIT and ROLLBACK aren't treated as write queries as well as BEGIN, the `ReadOnlyError` would be raised.
* An explain query does not raise the `ReadOnlyError` if preventing writesRyuta Kamizono2018-12-111-1/+1
|
* Don't treat begin and rollback transactions as write queriesRyuta Kamizono2018-12-111-1/+1
| | | | | Otherwise `save` method would raise the `ReadOnlyError` against `BEGIN` and `ROLLBACK` queries.
* Prevent write queries with prepared statements for mysql2 adapterRyuta Kamizono2018-12-111-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this change, mysql2 adapter with prepared statements won't pass `base_test.rb`. ``` % ARCONN=mysql2 be ruby -w -Itest test/cases/base_test.rb Using mysql2 Run options: --seed 27614 # Running: ....S..............................F Failure: BasicsTest#test_creating_a_record_raises_if_preventing_writes [test/cases/base_test.rb:1493]: ActiveRecord::ReadOnlyError expected but nothing was raised. rails test test/cases/base_test.rb:1492 ...F Failure: BasicsTest#test_deleting_a_record_raises_if_preventing_writes [test/cases/base_test.rb:1513]: ActiveRecord::ReadOnlyError expected but nothing was raised. rails test test/cases/base_test.rb:1510 ............................................................................................................F Failure: BasicsTest#test_updating_a_record_raises_if_preventing_writes [test/cases/base_test.rb:1503]: ActiveRecord::ReadOnlyError expected but nothing was raised. rails test test/cases/base_test.rb:1500 .......... Finished in 2.534490s, 62.7345 runs/s, 149.5370 assertions/s. 159 runs, 379 assertions, 3 failures, 0 errors, 1 skips ```
* Rename error that occurs when writing on a readEileen Uchitelle2018-12-071-1/+1
| | | | | | | I originally named this `StatementInvalid` because that's what we do in GitHub, but `@tenderlove` pointed out that this means apps can't test for or explitly rescue this error. `StatementInvalid` is pretty broad so I've renamed this to `ReadOnlyError`.
* Address "warning: shadowing outer local variable - parts"Ryuta Kamizono2018-12-031-1/+2
| | | | And hide the `READ_QUERY` internal constant.
* Add ability to prevent writes to a databaseEileen Uchitelle2018-11-301-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* 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`.
* 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-251-4/+30
| | | | | | | 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
* Add `Style/RedundantFreeze` to remove redudant `.freeze`Yasuo Honda2018-09-291-1/+1
| | | | | | | | | | | | | | | | | | | | | 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'
* Enable `Performance/UnfreezeString` copyuuji.yaginuma2018-09-232-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* Omit BEGIN/COMMIT statements for empty transactionsEugene Kenny2018-08-131-0/+4
| | | | | | | | | | | | | | | | | | | | | | 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.
* Turn on performance based copsDillon Welch2018-07-231-1/+1
| | | | | | | | | | | | | | | | Use attr_reader/attr_writer instead of methods method is 12% slower Use flat_map over map.flatten(1) flatten is 66% slower Use hash[]= instead of hash.merge! with single arguments merge! is 166% slower See https://github.com/rails/rails/pull/32337 for more conversation
* Fix `insert_fixtures_set` to be restored original connection flagsRyuta Kamizono2018-07-191-0/+36
| | | | | | #33363 has two regressions. First one is that `insert_fixtures_set` is failed if flags is an array. Second one is that connection flags are not restored if `set_server_option` is not supported.
* Fix default value for mysql time types with specified precisionNikolay Kondratyev2018-07-041-2/+2
| | | | | | | | | | | | | | | | The TIME, DATETIME, and TIMESTAMP types [have supported](https://mariadb.com/kb/en/library/microseconds-in-mariadb/) a fractional seconds precision from 0 to 6. Default values from time columns with specified precision is read as `current_timestamp(n)` from information schema. rake `db:schema:dump` produces `schema.rb` **without** default values for time columns with the specified precision: t.datetime "last_message_at", precision: 6, null: false rake `db:schema:dump` produces `schema.rb` **with** default values for time columns with the specified precision: t.datetime "last_message_at", precision: 6, default: -> { "current_timestamp(6)" }, null: false
* Save a hash allocation in MySQL statement poolEugene Kenny2018-04-231-4/+1
| | | | There's no need to wrap the statement in a hash with a single key.
* Use `delegate private: true` for `SchemaCreation`Ryuta Kamizono2018-03-021-2/+1
| | | | Duplicated method name list is no longer needed.