aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/mysql2
Commit message (Collapse)AuthorAgeFilesLines
* Optimizer hints should be applied on Top level query as much as possibleRyuta Kamizono2019-04-041-0/+8
| | | | | I've experienced this issue in our app, some hints only works on Top level query (e.g. `MAX_EXECUTION_TIME`).
* Merge pull request #35795 from alimi/cache-database-versionEileen M. Uchitelle2019-04-032-4/+1
|\ | | | | Cache database version in schema cache
| * Cache database version in schema cacheAli Ibrahim2019-04-032-4/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * 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.
* | Use official database name [ci skip]Ryuta Kamizono2019-04-031-1/+1
|/ | | | | | | | * s/Postgres/PostgreSQL/ * s/MYSQL/MySQL/, s/Mysql/MySQL/ * s/Sqlite/SQLite/ Replaced all newly added them after 6089b31.
* Don't change `collation_connection` in the current connectionRyuta Kamizono2019-03-271-1/+1
| | | | | | | We have a test case for `collation_connection` session variable, so it should not be changed in any other test. Fixes #35458.
* Add Relation#annotate for SQL commentingMatt Yoho2019-03-211-0/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch has two main portions: 1. Add SQL comment support to Arel via Arel::Nodes::Comment. 2. Implement a Relation#annotate method on top of that. == Adding SQL comment support Adds a new Arel::Nodes::Comment node that represents an optional SQL comment and teachers the relevant visitors how to handle it. Comment nodes may be added to the basic CRUD statement nodes and set through any of the four (Select|Insert|Update|Delete)Manager objects. For example: manager = Arel::UpdateManager.new manager.table table manager.comment("annotation") manager.to_sql # UPDATE "users" /* annotation */ This new node type will be used by ActiveRecord::Relation to enable query annotation via SQL comments. == Implementing the Relation#annotate method Implements `ActiveRecord::Relation#annotate`, which accepts a comment string that will be appeneded to any queries generated by the relation. Some examples: relation = Post.where(id: 123).annotate("metadata string") relation.first # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123 # LIMIT 1 /* metadata string */ class Tag < ActiveRecord::Base scope :foo_annotated, -> { annotate("foo") } end Tag.foo_annotated.annotate("bar").first # SELECT "tags".* FROM "tags" LIMIT 1 /* foo */ /* bar */ Also wires up the plumbing so this works with `#update_all` and `#delete_all` as well. This feature is useful for instrumentation and general analysis of queries generated at runtime.
* Add test case to prevent possible SQL injectionRyuta Kamizono2019-03-181-0/+10
|
* Add test case for unscoping `:optimizer_hints`Ryuta Kamizono2019-03-181-0/+6
|
* Extract `truncate` and `truncate_tables` into database statementsRyuta Kamizono2019-03-171-11/+0
| | | | This is to easier make `truncate_tables` to bulk statements.
* Support Optimizer HintsRyuta Kamizono2019-03-161-0/+24
| | | | | | | | | | | | | | | | | | 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.
* Update READ_QUERY regexAli Ibrahim2019-02-251-0/+8
| | | | | | | * 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.
* Merge pull request #35299 from kamipo/fix_mismatched_foreign_keyRyuta Kamizono2019-02-171-3/+80
|\ | | | | | | Fix the regex that extract mismatched foreign key information
| * Fix the regex that extract mismatched foreign key informationRyuta Kamizono2019-02-171-3/+80
|/ | | | | | | | | | | | | 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
* Allow `column_exists?` to be passed `type` argument as a stringRyuta Kamizono2019-01-241-9/+4
| | | | | | | | | | | Currently `conn.column_exists?("testings", "created_at", "datetime")` returns false even if the table has the `created_at` column. That reason is that `column.type` is a symbol but passed `type` is not normalized to symbol unlike `column_name`, it is surprising behavior to me. I've improved that to normalize a value before comparison.
* Merge pull request #34742 from kamipo/row_format_dynamic_by_defaultRyuta Kamizono2018-12-211-7/+14
|\ | | | | MySQL: `ROW_FORMAT=DYNAMIC` create table option by default
| * MySQL: `ROW_FORMAT=DYNAMIC` create table option by defaultRyuta Kamizono2018-12-191-7/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* | Enable `Style/RedundantBegin` cop to avoid newly adding redundant begin blockRyuta Kamizono2018-12-211-17/+13
|/ | | | | | | | | | Currently we sometimes find a redundant begin block in code review (e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205). I'd like to enable `Style/RedundantBegin` cop to avoid that, since rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5 (https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with that situation than before.
* Rename error that occurs when writing on a readEileen Uchitelle2018-12-071-4/+4
| | | | | | | 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`.
* Add ability to prevent writes to a databaseEileen Uchitelle2018-11-301-0/+58
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* Use utf8mb4 in all tests and examplesRyuta Kamizono2018-09-212-7/+7
| | | | | Since #33875, Rails dropped supporting MySQL 5.1 which does not support utf8mb4. We no longer need to use legacy utf8 (utf8mb3) conservatively.
* Use utf8mb4 character set by default for MySQL database (#33608)Yasuo Honda2018-09-112-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * 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/
* `supports_xxx?` returns whether a feature is supported by the backendRyuta Kamizono2018-09-081-8/+0
| | | | Rather than a configuration on the connection.
* Merge pull request #32647 from eugeneius/lazy_transactionsMatthew Draper2018-08-231-0/+2
|\ | | | | Omit BEGIN/COMMIT statements for empty transactions
| * Omit BEGIN/COMMIT statements for empty transactionsEugene Kenny2018-08-131-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* | Add database configuration to disable advisory locks.Guo Xiang Tan2018-08-221-0/+8
|/ | | | https://github.com/rails/rails/issues/31190
* Replace Mocha#stubs with assert_called_withutilum2018-07-221-6/+11
| | | | A correct, but not obvious use of `ActiveSupport::Testing::MethodCallAssertions`, which might also have been part of #33337 or #33391.
* Replace some more Mocha#stub calls with Minitestutilum2018-07-171-3/+4
| | | | Missed these in preparing #33337
* Remove unnecessary Mocha stubsutilum2018-07-071-1/+0
| | | | | | Step 1 in #33162 [utilum + bogdanvlviv]
* CI against MariaDB 10.3Yasuo Honda2018-06-011-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - MariaDB 10.3.7 is the first GA release https://mariadb.com/kb/en/library/mariadb-1037-release-notes/ - MariaDB 10.3 translates `LENGTH()` to `OCTET_LENGTH()` function https://mariadb.com/kb/en/library/sql_modeoracle-from-mariadb-103/ > MariaDB translates LENGTH() to OCTET_LENGTH() - MySQL does NOT translate `LENGTH()` to `OCTET_LENGTH()` However, it translates `OCTET_LENGTH()` to `LENGTH()` Here are generated schema dumps of this test to show the differences between MySQL and MariaDB: * MySQL 8.0 (Server version: 8.0.11 MySQL Community Server - GPL) ```ruby create_table \"virtual_columns\", options: \"ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci\", force: :cascade do |t| t.string \"name\" t.virtual \"upper_name\", type: :string, as: \"upper(`name`)\" t.virtual \"name_length\", type: :integer, as: \"length(`name`)\", stored: true t.virtual \"name_octet_length\", type: :integer, as: \"length(`name`)\", stored: true end ``` * Maria DB 10.3 (Server version: 10.3.7-MariaDB-1:10.3.7+maria~bionic-log mariadb.org binary distribution) ```ruby create_table \"virtual_columns\", options: \"ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci\", force: :cascade do |t| t.string \"name\" t.virtual \"upper_name\", type: :string, as: \"ucase(`name`)\" t.virtual \"name_length\", type: :integer, as: \"octet_length(`name`)\", stored: true t.virtual \"name_octet_length\", type: :integer, as: \"octet_length(`name`)\", stored: true end ```
* Fix `CustomCops/AssertNot` to allow it to have failure messageRyuta Kamizono2018-05-131-1/+1
| | | | Follow up of #32605.
* Replace `assert !` with `assert_not`Daniel Colson2018-04-191-2/+2
| | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Remove unnecessary `respond_to?(:report_on_exception)` checkingyuuji.yaginuma2018-03-021-2/+2
| | | | Since Rails 6 requires Ruby 2.4.1+.
* Fix `#columsn_for_distinct` of MySQL and PostgreSQLkg8m2018-02-271-5/+5
| | | | | | | Prevent `ActiveRecord::FinderMethods#limited_ids_for` from using correct primary key values even if `ORDER BY` columns include other table's primary key. Fixes #28364.
* Deprecate update_attributes and update_attributes!Eddie Lebow2018-02-171-2/+2
| | | | Closes #31998
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-254-11/+11
|
* Refactor migration to move migrations paths to connectioneileencodes2018-01-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Rails has some support for multiple databases but it can be hard to handle migrations with those. The easiest way to implement multiple databases is to contain migrations into their own folder ("db/migrate" for the primary db and "db/seconddb_migrate" for the second db). Without this you would need to write code that allowed you to switch connections in migrations. I can tell you from experience that is not a fun way to implement multiple databases. This refactoring is a pre-requisite for implementing other features related to parallel testing and improved handling for multiple databases. The refactoring here moves the class methods from the `Migrator` class into it's own new class `MigrationContext`. The goal was to move the `migrations_paths` method off of the `Migrator` class and onto the connection. This allows users to do the following in their `database.yml`: ``` development: adapter: mysql2 username: root password: development_seconddb: adapter: mysql2 username: root password: migrations_paths: "db/second_db_migrate" ``` Migrations for the `seconddb` can now be store in the `db/second_db_migrate` directory. Migrations for the primary database are stored in `db/migrate`". The refactoring here drastically reduces the internal API for migrations since we don't need to pass `migrations_paths` around to every single method. Additionally this change does not require any Rails applications to make changes unless they want to use the new public API. All of the class methods from the `Migrator` class were `nodoc`'d except for the `migrations_paths` and `migrations_path` getter/setters respectively.
* `create_database` should not add default charset when `collation` is givenRyuta Kamizono2018-01-151-1/+1
| | | | | | | | | | | | | If `collation` is given without `charset`, it may generate invalid SQL. For example `create_database(:matt_aimonetti, collation: "utf8mb4_bin")`: ``` > CREATE DATABASE `matt_aimonetti` DEFAULT CHARACTER SET `utf8` COLLATE `utf8mb4_bin`; ERROR 1253 (42000): COLLATION 'utf8mb4_bin' is not valid for CHARACTER SET 'utf8' ``` In MySQL, charset is used to find the default collation. If `collation` is given explicitly, it is not necessary to give extra charset.
* Remove passing needless empty string `options` in `create_table`Ryuta Kamizono2017-12-201-3/+3
| | | | Follow up of #31177.
* Merge pull request #31177 from ↵Matthew Draper2017-12-202-3/+78
|\ | | | | | | | | albertoalmagro/remove-default-mysql-engine-from-ar-5-2 Remove default ENGINE=InnoDB for Mysql2 adapter
| * Remove default ENGINE=InnoDB for Mysql2 adapterAlberto Almagro2017-12-112-3/+78
| | | | | | | | | | | | | | | | | | | | | | | | | | Before this commit ENGINE=InnoDB was added by default to Mysql2 adapter +create_table+ if no +options+ option was provided. This default ENGINE was lost as soon as something was passed in at +options+ option, making its goal and propagation inconsistent, as the programmer needed to remember including ENGINE=InnoDB when something was passed in. This commit removes default ENGINE as its use isn't needed anymore for current MySQL and MariaDB versions. It adds compatibility support and tests to ensure that default ENGINE is still present for migrations with version 5.1 and before. It also ensures we still dump the ENGINE option to +schema.rb+ in order to avoid inconsistencies.
* | Suppress expected exceptions by `report_on_exception` = `false`Yasuo Honda2017-12-141-0/+2
|/ | | | Follow up #31428 to address similar exceptions with mysql2 adapter
* Merge pull request #31230 from dinahshi/postgresql_extract_sqlMatthew Draper2017-12-031-2/+2
|\ | | | | Extract sql fragment generators from PostgreSQL adapter
| * Extract sql fragment generators for alter table from PostgreSQL adapterDinah Shi2017-12-021-2/+2
| |
* | Add new error class `QueryCanceled` which will be raised when canceling ↵Ryuta Kamizono2017-11-271-0/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | statement due to user request (#31235) This changes `StatementTimeout` to `QueryCanceled` for PostgreSQL. In MySQL, errno 1317 (`ER_QUERY_INTERRUPTED`) is only used when the query is manually cancelled. But in PostgreSQL, `QUERY_CANCELED` error code (57014) which is used `StatementTimeout` is also used when the both case. And, we can not tell which reason happened. So I decided to introduce new error class `QueryCanceled` closer to the error code name.
* | Rename `TransactionTimeout` to more descriptive `LockWaitTimeout` (#31223)Ryuta Kamizono2017-11-271-2/+2
|/ | | | | | Since #31129, new error class `StatementTimeout` has been added. `TransactionTimeout` is caused by the timeout shorter than `StatementTimeout`, but its name is too generic. I think that it should be a name that understands the difference with `StatementTimeout`.
* Merge pull request #30980 from sobrinho/sobrinho/arel-star-ignored-columnsRafael França2017-11-131-11/+11
|\ | | | | Do not use `Arel.star` when `ignored_columns`
| * Change tests to use models which don't ignore any columnsJon Moss2017-11-131-11/+11
| |
* | Add new error class `StatementTimeout` which will be raised when statement ↵Ryuta Kamizono2017-11-131-0/+29
| | | | | | | | | | | | | | | | | | | | | | | | timeout exceeded (#31129) We are sometimes using The MAX_EXECUTION_TIME hint for MySQL depending on the situation. It will prevent catastrophic performance down by wrong performing queries. The new error class `StatementTimeout` will make to be easier to handle that case. https://dev.mysql.com/doc/refman/5.7/en/optimizer-hints.html#optimizer-hints-execution-time
* | Raise `TransactionTimeout` when lock wait timeout exceeded for PG adapterRyuta Kamizono2017-11-111-1/+1
| | | | | | | | Follow up of #30360.
* | Should test actual error which is raised from the databaseRyuta Kamizono2017-11-111-1/+23
| |