aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #34800 from mqchau/mysqlCountDeleteRowInLockMatthew Draper2019-04-091-0/+28
|\ | | | | 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-0/+28
| |
* | Raise `ArgumentError` for invalid `:limit` and `:precision` like as other ↵Ryuta Kamizono2019-04-072-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 ```
* Optimizer hints should be applied on Top level query as much as possibleRyuta Kamizono2019-04-042-0/+16
| | | | | I've experienced this issue in our app, some hints only works on Top level query (e.g. `MAX_EXECUTION_TIME`).
* Ensure `reset_table_name` when table name prefix/suffix is changedRyuta Kamizono2019-04-041-5/+8
| | | | | Also, `reset_column_information` is unnecessary since `reset_table_name` does that too.
* Merge pull request #35795 from alimi/cache-database-versionEileen M. Uchitelle2019-04-034-6/+3
|\ | | | | Cache database version in schema cache
| * Cache database version in schema cacheAli Ibrahim2019-04-034-6/+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.
* | 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.
* Merge pull request #19333 from palkan/dirty-storeKasper Timm Hansen2019-03-311-0/+16
|\ | | | | Add dirty methods for store accessors
| * Add dirty methods for store accessorspalkan2019-03-251-0/+16
| |
* | 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-213-0/+111
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-182-0/+20
|
* Add test case for unscoping `:optimizer_hints`Ryuta Kamizono2019-03-182-0/+12
|
* Extract `truncate` and `truncate_tables` into database statementsRyuta Kamizono2019-03-173-37/+0
| | | | This is to easier make `truncate_tables` to bulk statements.
* Support Optimizer HintsRyuta Kamizono2019-03-162-0/+52
| | | | | | | | | | | | | | | | | | 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.
* Allow `truncate` for SQLite3 adapter and add `rails db:seed:replant` (#34779)Bogdan2019-03-041-0/+18
| | | | | | | | | | | | | * Add `ActiveRecord::Base.connection.truncate` for SQLite3 adapter. SQLite doesn't support `TRUNCATE TABLE`, but SQLite3 adapter can support `ActiveRecord::Base.connection.truncate` by using `DELETE FROM`. `DELETE` without `WHERE` uses "The Truncate Optimization", see https://www.sqlite.org/lang_delete.html. * Add `rails db:seed:replant` that truncates database tables and loads the seeds Closes #34765
* Update READ_QUERY regexAli Ibrahim2019-02-253-0/+28
| | | | | | | * 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.
* Remove duplicated protected params definitionsRyuta Kamizono2019-02-241-7/+2
| | | | Use "support/stubs/strong_parameters" instead.
* Merge pull request #35263 from hatch-carl/reduce-postgres-uuid-allocationsRyuta Kamizono2019-02-211-1/+13
|\ | | | | Reduce unused allocations when casting UUIDs for Postgres
| * Reduce unused allocations when casting UUIDs for PostgresCarl Thuringer2019-02-201-1/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using the subscript method `#[]` on a string has several overloads and rather complex implementation. One of the overloads is the capability to accept a regular expression and then run a match, then return the receiver (if it matched) or one of the groups from the MatchData. The function of the `UUID#cast` method is to cast a UUID to a type and format acceptable by postgres. Naturally UUIDs are supposed to be string and of a certain format, but it had been determined that it was not ideal for the framework to send just any old string to Postgres and allow the engine to complain when "foobar" or "" was sent, being obviously of the wrong format for a valid UUID. Therefore this code was written to facilitate the checking, and if it were not of the correct format, a `nil` would be returned as is conventional in Rails. Now, the subscript method will allocate one or more strings on a match and return one of them, based on the index parameter. However, there is no need for a new string, as a UUID of the correct format is already such, and so long as the format was verified then the string supplied is adequate for consumption by the database. The subscript method also creates a MatchData object which will never be used, and so must eventually be garbage collected. Garbage collection indeed. This innocuous method tends to be called quite a lot, for example if the primary key of a table is a uuid, then this method will be called. If the foreign key of a relation is a UUID, once again this method is called. If that foreign key is belonging to a has_many relationship with dozens of objects, then again dozens of UUIDs shall be cast to a dup of themselves, and spawn dozens of MatchData objects, and so on. So, for users that: * Use UUIDs as primary keys * Use Postgres * Operate on collections of objects This accomplishes a significant savings in total allocations, and may save many garbage collections.
* | PostgreSQL: Support endless range values for range typesRyuta Kamizono2019-02-201-0/+16
|/
* Don't allow `where` with invalid value matches to nil valuesRyuta Kamizono2019-02-181-0/+6
| | | | | | | | | That is considered as silently leaking information. If type casting doesn't return any actual value, it should not be matched to any record. Fixes #33624. Closes #33946.
* 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.
* Fix type casting column default in `change_column`Ryuta Kamizono2019-01-201-1/+13
| | | | | | | | | | | | | | Since #31230, `change_column` is executed as a bulk statement. That caused incorrect type casting column default by looking up the before changed type, not the after changed type. In a bulk statement, we can't use `change_column_default_for_alter` if the statement changes the column type. This fixes the type casting to use the constructed target sql_type. Fixes #34938.
* Remove deprecated `#insert_fixtures` from the database adaptersRafael Mendonça França2019-01-171-8/+0
|
* Remove deprecated ↵Rafael Mendonça França2019-01-171-4/+0
| | | | `ActiveRecord::ConnectionAdapters::SQLite3Adapter#valid_alter_table_type?`
* Change `SQLite3Adapter` to always represent boolean values as integersRafael Mendonça França2019-01-171-13/+1
|
* Fix `test_case_insensitiveness` to follow up eb5fef5Ryuta Kamizono2019-01-111-9/+8
|
* 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-214-49/+37
| | | | | | | | | | | | | | | | | | | | 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.
* | Module#{define_method,alias_method,undef_method,remove_method} become public ↵Ryuta Kamizono2018-12-211-4/+4
|/ | | | | | since Ruby 2.5 https://bugs.ruby-lang.org/issues/14133
* Rename error that occurs when writing on a readEileen Uchitelle2018-12-073-11/+11
| | | | | | | 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-303-0/+170
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-0/+5
| | | | | Fixes issue where "user post" is misinterpreted as "\"user\".\"post\"" when quoting table names with the postgres adapter.
* Bump the minimum version of PostgreSQL to 9.3Yasuo Honda2018-11-253-377/+359
| | | | | | | | | | | | | | | | | | | | 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
* Use squiggly heredoc to strip odd indentation in the executed SQLRyuta Kamizono2018-11-229-27/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-0/+12
| | | | | | | | in indexdef to be wrapped up by double quotes Fixes #34493. *Thomas Bianchini*
* Merge pull request #34436 from gmcgibbon/fix_default_max_bind_length_sqliteRafael França2018-11-131-0/+20
|\ | | | | Adjust bind length of SQLite to default (999)
| * Adjust bind length of SQLite to default (999)Gannon McGibbon2018-11-131-0/+20
| | | | | | | | | | 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-131-0/+74
|/ | | | | | | | | | | 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.
* Fix test case for money schema defaultRyuta Kamizono2018-11-121-1/+1
| | | | | | | | Follow up a741208f80dd33420a56486bd9ed2b0b9862234a. Since a741208, `Decimal#serialize` which is superclass of `Money` type is no longer no-op, so it consistently serialize/deserialize a value as a decimal even if schema default.
* Change the empty block style to have space inside of the blockRafael Mendonça França2018-09-252-2/+2
|
* Enable `Performance/UnfreezeString` copyuuji.yaginuma2018-09-233-8/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* 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.
* SQLite3: Support multiple args function for expression indexesRyuta Kamizono2018-09-141-8/+9
| | | | | Follow up #33874. Related #23393.
* SQLite3 adapter supports expression indexesgkemmey2018-09-131-0/+35
|