aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
Commit message (Collapse)AuthorAgeFilesLines
* Simplify and speed up Postgres query for primary_keysJordan Lewis2017-02-091-10/+8
| | | | | | | | | | | | | | | | | | primary_keys(table) needs to query various metadata tables in Postgres to determine the primary key for the table. Previously, it did so using a complex common table expression against pg_constraint and pg_attribute. This patch simplifies the query by joining pg_index against pg_attribute instead of going through pg_constraint. This avoids an expensive unnest, window function query, and common table expression. EXPLAINing these queries in Postgres against a database with a single table with a composite primary key shows a 66% reduction in the plan and execute latencies. This is significant during application startup time, especially against very large schemas, where these queries would be even slower and more numerous. Closes #27949
* Refactor `ColumnDefinition` to contain `options` hashRyuta Kamizono2017-02-0911-109/+50
| | | | | | Column options are passed as an hash args then used as `options` hash in `add_column_options!`. Converting args to attributes is inconvinient for using options as an hash.
* Merge pull request #27334 from ↵Jeremy Daer2017-02-074-8/+16
|\ | | | | | | | | kamipo/pk_and_ref_columns_should_be_identical_type `primary_key` and `references` columns should be identical type
| * `primary_key` and `references` columns should be identical typeRyuta Kamizono2017-02-074-8/+16
| | | | | | | | | | | | | | | | Follow up to #26266. The default type of `primary_key` and `references` were changed to `bigint` since #26266. But legacy migration and sqlite3 adapter should keep its previous behavior.
* | Merge pull request #26378 from ↵Jeremy Daer2017-02-062-7/+7
|\ \ | |/ |/| | | | | kamipo/decouple_building_arel_ast_for_uniqueness_validator Decouple the building Arel ASTs for uniqueness validator
| * Add `:nodoc:` to `case_sensitive_comparison` and `case_insensitive_comparison`Ryuta Kamizono2017-01-202-3/+3
| | | | | | | | These methods are obviously for internal use.
| * Decouple the building Arel ASTs for uniqueness validatorRyuta Kamizono2016-12-252-4/+4
| | | | | | | | | | | | Currently uniqueness validator is coupled with building Arel ASTs. This commit extracts `WhereClauseFactory#build_for_case_sensitive` for decouple the building Arel ASTs.
* | Merge pull request #27389 from kamipo/fix_mysql_pk_dumping_correctlyJeremy Daer2017-02-065-18/+19
|\ \ | | | | | | | | | | | | | | | | | | | | | | | | Restore the behaviour of the compatibility layer for integer-like PKs * kamipo/fix_mysql_pk_dumping_correctly: Restore custom primary key tests lost at #26266 Restore the behaviour of the compatibility layer for integer-like PKs Correctly dump integer-like primary key with default nil
| * | Restore the behaviour of the compatibility layer for integer-like PKsRyuta Kamizono2017-02-041-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The PR #27384 changed migration compatibility behaviour. ```ruby class CreateMasterData < ActiveRecord::Migration[5.0] def change create_table :master_data, id: :integer do |t| t.string :name end end end ``` Previously this migration created non-autoincremental primary key expected. But after the PR, the primary key changed to autoincremental, it is unexpected. This change restores the behaviour of the compatibility layer.
| * | Correctly dump integer-like primary key with default nilRyuta Kamizono2017-02-044-20/+21
| | | | | | | | | | | | | | | | | | | | | The PR #27384 changed integer-like primary key to be autoincrement unless an explicit default. This means that integer-like primary key is restored as autoincrement unless dumping the default nil explicitly. We should dump integer-like primary key with default nil correctly.
* | | Deprecate `ColumnDumper#migration_keys`Ryuta Kamizono2017-02-075-6/+10
|/ / | | | | | | | | | | `ColumnDumper#migration_keys` was extracted to customize keys for standardized column arguments widths. But the feature was removed in df84e98. The internal method is no longer used for that.
* | Virtual/generated column support for MySQL 5.7.5+ and MariaDB 5.2.0+Ryuta Kamizono2017-02-019-14/+76
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | MySQL generated columns: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html MariaDB virtual columns: https://mariadb.com/kb/en/mariadb/virtual-computed-columns/ Declare virtual columns with `t.virtual name, type: …, as: "expression"`. Pass `stored: true` to persist the generated value (false by default). Example: create_table :generated_columns 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.index :name_length # May be indexed, too! end Closes #22589
* | Merge pull request #27767 from benoittgt/freeze_sql_type_with_get_oid_typeArthur Nogueira Neves2017-01-311-1/+1
|\ \ | | | | | | Freeze default empty string for sql_type in get_oid_type
| * | Freeze default empty string for sql_type in get_oid_typeBenoit Tigeot2017-01-221-1/+1
| | | | | | | | | | | | One empty string allocation per column per row.
* | | Simplify Postgres query for column_definitions()Jordan Lewis2017-01-301-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | column_definitions() needs to fetch the collation for every column, if present. Previously, it did so using a correlated subquery - a subquery that references results from the outer scope. This patch updates the query to remove the subquery in favor of a simpler and more efficient JOIN clause. Running the two queries through EXPLAIN against Postgres additionally shows that the original form with a correlated subquery requires a Nested Loop Left Join, while the new form with a simple JOIN can use a more efficient Merge Left Join.
* | | Missing require "active_support/multibyte/chars"Akira Matsuda2017-01-271-0/+1
|/ / | | | | | | this probably fixes #25840
* | Deprecate `initialize_schema_migrations_table` and ↵Ryuta Kamizono2017-01-201-4/+4
| | | | | | | | | | | | `initialize_internal_metadata_table` These internal initialize methods are no longer used internally.
* | Revert "Merge pull request #27718 from kamipo/remove_internal_public_methods"Matthew Draper2017-01-201-0/+10
| | | | | | | | | | This reverts commit 39c77eb1843f79925c7195e8869afc7cb7323682, reversing changes made to 9f6f51be78f8807e18fc6562c57af2fdbf8ccb56.
* | Merge pull request #27384 from matthewd/bigint-pk-tweaksMatthew Draper2017-01-204-7/+6
|\ \ | | | | | | Tweak bigint PK handling
| * | Tweak bigint PK handlingMatthew Draper2017-01-184-7/+6
| | | | | | | | | | | | | | | | | | * Don't force PKs on tables that have explicitly opted out * All integer-like PKs are autoincrement unless they have an explicit default
* | | Remove `initialize_schema_migrations_table` and ↵Ryuta Kamizono2017-01-181-10/+0
|/ / | | | | | | | | | | | | | | | | | | | | | | `initialize_internal_metadata_table` internal public methods These internal methods accidentally appeared in the doc, and so almost useless. It is enough to create these internal tables directly, and indeed do so in several places. https://github.com/rails/rails/blob/v5.0.1/activerecord/lib/active_record/schema.rb#L55 https://github.com/rails/rails/blob/v5.0.1/activerecord/lib/active_record/railties/databases.rake#L6 https://github.com/rails/rails/blob/v5.0.1/activerecord/lib/active_record/tasks/database_tasks.rb#L230
* | SQLite: Foreign Key SupportRyuta Kamizono2017-01-173-3/+34
| | | | | | | | https://www.sqlite.org/foreignkeys.html
* | Translate Foreign Key violation to the specific exception for SQLite3 adapterRyuta Kamizono2017-01-161-0/+21
| | | | | | | | | | | | Raise `ActiveRecord::InvalidForeignKey` when a record cannot be inserted or updated because it references a non-existent record for SQLite3 adapter.
* | Fix pool_from_any_process to use most recent speceileencodes2017-01-131-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | If a process is forked more than once, the pool was grabbing the oldest spec, not the most recent spec. This wasn't noticed before because most folks are lilely forking the process only once. If you're forking the process multiple times however the wrong spec name will be returned and an incorrect connection will be used for the process. This fixes the issue by reversing the list of spec names so we can grab the most recent spec rather than the oldest spec.
* | class Foo < Struct.new(:x) creates an extra unneeded anonymous classAkira Matsuda2017-01-131-10/+6
| | | | | | | | because Struct.new returns a Class, we just can give it a name and use it directly without inheriting from it
* | Reduce string objects by using \ instead of + or << for concatenating stringsAkira Matsuda2017-01-121-3/+3
| | | | | | | | (I personally prefer writing one string in one line no matter how long it is, though)
* | Optimizing information_schema query for `foreign_keys`Ryuta Kamizono2017-01-051-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Need `table_name` to avoid all databases scan. See https://dev.mysql.com/doc/refman/5.7/en/information-schema-optimization.html. ``` > EXPLAIN SELECT fk.referenced_table_name AS 'to_table', fk.referenced_column_name AS 'primary_key', fk.column_name AS 'column', fk.constraint_name AS 'name', rc.update_rule AS 'on_update', rc.delete_rule AS 'on_delete' FROM information_schema.key_column_usage fk JOIN information_schema.referential_constraints rc USING (constraint_schema, constraint_name) WHERE fk.referenced_column_name IS NOT NULL AND fk.table_schema = 'activerecord_unittest' AND fk.table_name = 'fk_test_has_pk' AND rc.table_name = 'fk_test_has_pk'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: fk partitions: NULL type: ALL possible_keys: NULL key: TABLE_SCHEMA,TABLE_NAME key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: Using where; Open_full_table; Scanned 0 databases *************************** 2. row *************************** id: 1 select_type: SIMPLE table: rc partitions: NULL type: ALL possible_keys: NULL key: TABLE_NAME key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: Using where; Open_full_table; Scanned 1 database; Using join buffer (Block Nested Loop) 2 rows in set, 1 warning (0.00 sec) ``` Fixes #27579.
* | Revert "Merge pull request #21233 from ↵Rafael Mendonça França2017-01-032-49/+4
| | | | | | | | | | | | | | | | | | | | mtsmfm/disable-referential-integrity-without-superuser-privileges" This reverts commit eeac6151a55cb7d5f799e1ae33aa64a839cbc3aa, reversing changes made to 5c40239d3104543e70508360d27584a3e4dc5baf. Reason: Broke the isolated tests. https://travis-ci.org/rails/rails/builds/188721346
* | Merge pull request #21233 from ↵Rafael França2017-01-032-4/+49
|\ \ | | | | | | | | | | | | mtsmfm/disable-referential-integrity-without-superuser-privileges Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser privileges
| * | Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser ↵Fumiaki MATSUSHIMA2016-12-032-4/+49
| | | | | | | | | | | | | | | | | | | | | | | | | | | privileges ref: 72c1557254 - We must use `authors` fixture with `author_addresses` because of its foreign key constraint. - Tests require PostgreSQL >= 9.4.2 because it had a bug about `ALTER CONSTRAINTS` and fixed in 9.4.2.
* | | Merge pull request #25522 from ↵Rafael França2017-01-033-20/+24
|\ \ \ | | | | | | | | | | | | | | | | kamipo/fix_select_rows_method_signature_for_consistency Fix `select_rows` method signature for consistency
| * | | Fix `select_rows` method signature for consistencyRyuta Kamizono2017-01-043-20/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Related #22973, #24708. `select_all`, `select_one`, `select_value`, and `select_values` method signature is `(arel, name = nil, binds = [])`. But `select_rows` is `(sql, name = nil, binds = [])`.
* | | | Remove unneeded requires at active recordRafael Mendonça França2017-01-031-2/+0
| | | |
* | | | Deprecate passing `name` to `indexes` like `tables`Ryuta Kamizono2017-01-044-2/+22
|/ / / | | | | | | | | | | | | Passing `name` to `tables` is already deprecated at #21601. Passing `name` to `indexes` is also unused.
* | | Compare deserialized values for `PostgreSQL::OID::Hstore` typesJon Moss2017-01-031-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Per the regression commit below, the commit changes the behavior of `#changed?`to consult the `#changed_in_place?` method on `Type::Value` classes. Per this change, `PostgreSQL::OID::Hstore` needs to override this method in order to compare the deserialized forms of the two arguments. In Ruby, two hashes are considered equal even if their key order is different. This commit helps to bring that behavior to `Hstore` values. Fixes regression introduced by 8e633e505880755e7e366ccec2210bbe2b5436e7 Fixes #27502
* | | Consistently apply adapter behavior when serializing arraysSean Griffin2017-01-032-13/+41
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In f1a0fa9 we moved backend specific timestamp behavior out of the type and into the adapter. This was in line with our general attempt to reduce the number of adapter specific type subclasses. However, on PG, the array type performs all serialization, including database encoding in its serialize method. This means that we have converted the value into a string before reaching the database, so no adapter specific logic can be applied (and this also means that timestamp arrays were using the default `.to_s` method on the given object, which likely meant timestamps were being ignored in certain cases as well) Ultimately I want to do a more in depth refactoring which separates database serializer objects from the active model type objects, to give us a less awkward API for introducing the attributes API onto Active Model. However, in the short term, we follow the solution we've applied elsewhere for this. Move behavior off of the type and into the adapter, and use a data object to allow the type to communicate information up the stack. Fixes #27514.
* | | Merge pull request #27435 from kamipo/follow_up_25307Sean Griffin2017-01-031-2/+2
|\ \ \ | | | | | | | | Active Record supports MySQL >= 5.1.10
| * | | Active Record supports MySQL >= 5.1.10Ryuta Kamizono2016-12-221-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Follow up to #25307 and #23458. Related with #27422. We are using `information_schema.referential_constraints` since #25307. The table was introduced in MySQL 5.1.10. MySQL 5.0 is too old. It is enough to support >= 5.1.10 at least. MySQL 5.0 GA was released in Dec 2005 and already EOL in Dec 2011. MySQL 5.1 GA was released in Dec 2008 and already EOL in Dec 2013.
* | | | Revert "Merge pull request #27528 from kamipo/extract_casted_booleans"Kasper Timm Hansen2017-01-011-6/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | As pointed out by @matthewd this change makes ImmutableString aware of MysqlString's existence whereas previously MysqlString was only overriding public API. cc @kamipo This reverts commit e632c2fa4cb60072a778ce95c952a0fa95e5b074, reversing changes made to 334a7dcf107cd3ff1697163d331d289d6d65dcd7.
* | | | Extract `casted_true`/`casted_false` for `Type::ImmutableString`Ryuta Kamizono2017-01-011-14/+6
| | | | | | | | | | | | | | | | | | | | | | | | The only difference between `Type::ImmutableString` and its subclasses is the representation of the casted booleans. Prefer extracting `casted_true`/`casted_false` and override these by subclasses.
* | | | Dump array subtype options correctlyRyuta Kamizono2017-01-011-1/+1
| | | | | | | | | | | | | | | | | | | | Currently schema dumper does not dump array subtype `precision` and `scale` options. This commit fixes the issue.
* | | | Merge pull request #27441 from koic/quoted_insert_version_sqlKasper Timm Hansen2016-12-311-4/+4
|\ \ \ \ | | | | | | | | | | Add a quote to an SQL insert statement of schema migration
| * | | | Add a quote to an SQL insert statement of schema migrationKoichi ITO2016-12-231-4/+4
| |/ / /
* | | | Remove deprecated `#insert_sql`, `#update_sql`, and `#delete_sql`Ryuta Kamizono2016-12-301-6/+0
| | | |
* | | | Remove deprecated support to query using commas on LIMITRafael Mendonça França2016-12-291-6/+1
| | | |
* | | | `#tables` and `#table_exists?` and returns only tables and not viewsRafael Mendonça França2016-12-294-69/+55
| | | |
* | | | Remove deprecated `name` argument from `#tables`Rafael Mendonça França2016-12-294-22/+4
| | | |
* | | | Remove deprecated support to passing a column to #quoteRafael Mendonça França2016-12-291-11/+1
| | | |
* | | | Sync the visibility of `sql_for_insert` to privateRyuta Kamizono2016-12-291-1/+1
| | | | | | | | | | | | | | | | | | | | The visibility of all internal protected methods was changed to private since 5b14129.
* | | | Merge pull request #27402 from yui-knk/executeRafael França2016-12-281-1/+1
|\ \ \ \ | | | | | | | | | | Use `#execute` instead of `@connection.query`