aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/mysql
Commit message (Collapse)AuthorAgeFilesLines
* 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.
* Prefer `@connection.abandon_results!` than `@connection.next_result while ↵Ryuta Kamizono2018-02-041-1/+1
| | | | @connection.more_results?`
* Extract `discard_remaining_results` for mysql2 adapterRyuta Kamizono2018-01-291-1/+5
|
* Bring back ability to insert zero value on primary key for fixtures (#31795)Ryuta Kamizono2018-01-261-0/+3
| | | | | | Since #29504, mysql2 adapter lost ability to insert zero value on primary key due to enforce `NO_AUTO_VALUE_ON_ZERO` disabled. That is for using `DEFAULT` on auto increment column, but we can use `NULL` instead in that case.
* Emulate JSON types for SQLite3 adapter (#29664)Ryuta Kamizono2017-12-031-4/+0
| | | | | 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-0/+12
| | | | | | | `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-031-6/+9
|
* Remove deprecated argument `name` from `#indexes`Rafael Mendonça França2017-10-231-7/+1
|
* MySQL: Don't lose `auto_increment: true` in the `db/schema.rb`Ryuta Kamizono2017-10-151-0/+7
| | | | | | | | | | Currently `AUTO_INCREMENT` is implicitly used in the default primary key definition. But `AUTO_INCREMENT` is not only used for single column primary key, but also for composite primary key. In that case, `auto_increment: true` should be dumped explicitly in the `db/schema.rb`. Fixes #30894.
* Extract `integer_like_primary_key_type` to ease to handle it for adaptersRyuta Kamizono2017-09-251-5/+5
|
* Move integer-like primary key normalization to `new_column_definition`Ryuta Kamizono2017-09-231-5/+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.
* Refactor `SchemaDumper` to make it possible to adapter specific customizationRyuta Kamizono2017-08-222-9/+16
| | | | | | | 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-4/+0
|
* Update links to use https instead of http [ci skip]Yoshiyuki Hirano2017-08-221-1/+1
|
* Place `update_table_definition` consistently in `SchemaStatements`Ryuta Kamizono2017-08-211-0/+4
|
* Don't expose `prepare_column_options`Ryuta Kamizono2017-08-211-13/+12
| | | | | This is only used for the internal `column_spec` and `column_spec_for_primary_key`.
* Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Merge pull request #29870 from kamipo/use_true_false_literalsSean Griffin2017-07-221-10/+0
|\ | | | | Use `TRUE` and `FALSE` boolean literals for MySQL
| * Use `TRUE` and `FALSE` boolean literals for MySQLRyuta Kamizono2017-07-201-10/+0
| | | | | | | | | | | | Since #29699, abstract boolean serialization has been changed to use `TRUE` and `FALSE` literals. MySQL also support the literals. So we can use the abstract boolean serialization even for MySQL.
* | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-199-0/+18
|/
* Fix type casting a time for MariaDBRyuta Kamizono2017-07-191-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | Context #24542. Since 8ebe1f2, it has lost stripping date part for a time value. But I confirmed it is still needed even if MariaDB 10.2.6 GA. MariaDB 10.2.6, `prepared_statements: true`: ``` % ARCONN=mysql2 be ruby -w -Itest test/cases/time_precision_test.rb -n test_formatting_time_according_to_precision Using mysql2 Run options: -n test_formatting_time_according_to_precision --seed 37614 F Failure: TimePrecisionTest#test_formatting_time_according_to_precision [test/cases/time_precision_test.rb:53]: Failed assertion, no message given. bin/rails test test/cases/time_precision_test.rb:46 Finished in 0.040279s, 24.8268 runs/s, 24.8268 assertions/s. 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips ```
* Don't convert dates to strings when using prepared statements in mysqlSean Griffin2017-07-181-0/+8
| | | | | | | | Dates are able to be natively handled by the mysql2 gem. libmysql (and the wire protocol) represent each portion of the date as an integer, which is significantly faster to encode and decode. By passing the Ruby date objects through directly, we can save a good bit of time and memory.
* Merge pull request #29706 from ↵Matthew Draper2017-07-091-1/+1
|\ | | | | | | | | kamipo/use_information_schema_to_extract_expression Use `information_schema` to extract `generation_expression` for MariaDB
| * Use `information_schema` to extract `generation_expression` for MariaDBRyuta Kamizono2017-07-071-1/+1
| | | | | | | | | | | | Since MariaDB 10.2.5, `information_schema` supports Virtual Columns. Fixes #29670.
* | Fix default `CURRENT_TIMESTAMP` in schema dumping for MariaDB 10.2Ryuta Kamizono2017-07-071-2/+2
|/ | | | | | | | Since MariaDB 10.2, `CURRENT_TIMESTAMP` is shown as a function (`current_timestamp()`). Fix matching column default to address that case. Fixes #29698.
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-029-9/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Merge pull request #29540 from kirs/rubocop-frozen-stringMatthew Draper2017-07-029-0/+9
|\ | | | | | | Enforce frozen string in Rubocop
| * Enforce frozen string in RubocopKir Shatrov2017-07-019-0/+9
| |
* | Merge pull request #29506 from pat/frozen-string-literalsMatthew Draper2017-07-022-3/+3
|\ \ | | | | | | | | | Make ActiveSupport frozen-string-literal friendly.