aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #18080 from korbin/fix_reaping_frequency_configurationRafael Mendonça França2015-01-021-1/+1
|\ | | | | | | Fix issue with reaping_frequency type.
| * fix issue with reaping_frequency typekorbin2014-12-171-1/+1
| | | | | | | | | | | | | | | | When using DATABASE_URL to configure ActiveRecord, :reaping_frequency does not get converted from a string to a numeric value. This value is eventually passed to 'sleep' and must be numeric to avoid exceptions. This commit converts :reaping_frequency to a float when present.
* | 💣 I forgot to commit the arity changeSean Griffin2015-01-011-1/+1
| |
* | Stop passing the column to the connection adapter when quoting defaultsSean Griffin2015-01-011-2/+2
| | | | | | | | | | The column is no longer used for anything besides type casting, which is what we're trying to remove from the column entirely.
* | Extract the index length validation to a auxiliar methodRafael Mendonça França2014-12-301-3/+8
| |
* | Minor documentation edits [ci skip]Robin Dupret2014-12-281-6/+5
| |
* | Add bigint primary key support for MySQL.Ryuta Kamizono2014-12-282-1/+3
| | | | | | | | | | | | | | Example: create_table :foos, id: :bigint do |t| end
* | Support for any type primary key.Ryuta Kamizono2014-12-281-1/+5
| |
* | reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]Yves Senn2014-12-271-27/+38
| | | | | | | | | | | | Most of the documentation very closely mirrors the matching docs from `SchemaStatements`. I reduced duplicated copy and added links to the underlying methods for the user to follow.
* | Refactor a common class to reduce the duplication for `references`Sean Griffin2014-12-232-59/+86
| | | | | | | | | | | | | | | | The code for `TableDefinition#references` and `SchemaStatements#add_reference` were almost identical both structurally, and in terms of domain knowledge. This removes that duplication into a common class, using the `Table` API as the expected interface of its collaborator.
* | Fix connection leak when a thread checks in additional connections.Matt Jones2014-12-231-4/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The code in `ConnectionPool#release` assumed that a single thread only ever holds a single connection, and thus that releasing a connection only requires the owning thread_id. There is a trivial counterexample to this assumption: code that checks out additional connections from the pool in the same thread. For instance: connection_1 = ActiveRecord::Base.connection connection_2 = ActiveRecord::Base.connection_pool.checkout ActiveRecord::Base.connection_pool.checkin(connection_2) connection_3 = ActiveRecord::Base.connection At this point, connection_1 has been removed from the `@reserved_connections` hash, causing a NEW connection to be returned as connection_3 and the loss of any tracking info on connection_1. As long as the thread in this example lives, connection_1 will be inaccessible and un-reapable. If this block of code runs more times than the size of the connection pool in a single thread, every subsequent connection attempt will timeout, as all of the available connections have been leaked. Reverts parts of 9e457a8654fa89fe329719f88ae3679aefb21e56 and essentially all of 4367d2f05cbeda855820e25a08353d4b7b3457ac
* | Add `foreign_key` as an option to `references` for `change_table`Sean Griffin2014-12-222-4/+22
| | | | | | | | | | | | | | | | | | | | | | This has the same comments as 9af90ffa00ba35bdee888e3e1ab775ba0bdbe72c, however it affects the `add_reference` method, and `t.references` in the context of a `change_table` block. There is a lot of duplication of code between creating and updating tables. We should re-evaluate the structure of this code from a high level so changes like this don't need to be made in two places. (Note to self)
* | Convert `add_references` to use kwargsSean Griffin2014-12-221-6/+18
| | | | | | | | | | | | While we still aren't accepting PRs that only make changes like this, it's fine when we're actively working on a method if it makes our lives easier.
* | Add a `foreign_key` option to `references` while creating the tableSean Griffin2014-12-222-5/+34
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Rather than having to do: create_table :posts do |t| t.references :user end add_foreign_key :posts, :users You can instead do: create_table :posts do |t| t.references :user, foreign_key: true end Similar to the `index` option, you can also pass a hash. This will be passed as the options to `add_foreign_key`. e.g.: create_table :posts do |t| t.references :user, foreign_key: { primary_key: :other_id } end is equivalent to create_table :posts do |t| t.references :user end add_foreign_key :posts, :users, primary_key: :other_id
* | Convert `references` to kwargsSean Griffin2014-12-221-7/+17
| | | | | | | | | | | | While we aren't taking PRs with these kinds of changes just yet, they are fine if we're actively working on the method and it makes things easier.
* | `force: :cascade` to recreate tables referenced by foreign-keys.Yves Senn2014-12-191-2/+7
|/
* minor sentences fixesNeeraj Singh2014-12-071-3/+3
|
* Failure to rollback t.timestamps when within a change_table migrationnoam2014-12-032-3/+3
| | | | | | | | | | | | | When running the following migration: change_table(:table_name) { |t| t/timestamps } The following error was produced: wrong number of arguments (2 for 1) .... /connection_adapters/abstract/schema_statements.rb:851:in `remove_timestamps' This is due to `arguments` containing an empty hash as its second argument.
* no need to pass native_database_types aroundYves Senn2014-12-021-4/+4
|
* Pass symbol as an argument instead of a blockErik Michaels-Ober2014-11-292-4/+2
|
* Merge pull request #17799 from kamipo/refactor_add_column_optionsRafael Mendonça França2014-11-281-2/+2
|\ | | | | Refactor `add_column_options!`, to move the quoting of default value for :uuid in `quote_value`.
| * Rename to `quote_default_expression` from `quote_value`Ryuta Kamizono2014-11-281-2/+2
| |
* | Refactor `SchemaCreation#visit_AddColumn`Ryuta Kamizono2014-11-271-3/+1
|/
* raise a better exception for renaming long indexesAaron Patterson2014-11-201-0/+3
|
* synchronize code and docs for `timestamps` and `add_timestamps`.Yves Senn2014-11-202-8/+10
| | | | | | | | This makes the following changes: * warn if `:null` is not passed to `add_timestamps` * `timestamps` method docs link to `add_timestamps` docs * explain where additional options go * adjust examples to include `null: false` (to prevent deprecation warnings)
* Support symbol foreign key to deletedtaniwaki2014-11-191-1/+1
|
* Merge pull request #17580 from ccutrer/change_table_nameYves Senn2014-11-111-17/+19
|\ | | | | | | add a Table#name accessor like TableDefinition#name
| * add a Table#name accessor like TableDefinition#nameCody Cutrer2014-11-101-17/+19
| |
* | remove never called method `limited_update_conditions`Andrey Deryabin2014-11-111-4/+0
|/
* docs, the abstract data type `:timestamp` was removed. See #15184 [ci skip]Yves Senn2014-11-071-2/+2
|
* Remove redundant `to_s` in interpolationclaudiob2014-10-302-2/+2
|
* edit pass over all warningsXavier Noria2014-10-282-5/+4
| | | | | | | | | | | | | | | This patch uniformizes warning messages. I used the most common style already present in the code base: * Capitalize the first word. * End the message with a full stop. * "Rails 5" instead of "Rails 5.0". * Backticks for method names and inline code. Also, converted a few long strings into the new heredoc convention.
* let's warn with heredocsXavier Noria2014-10-282-9/+12
| | | | | | | | | | | | The current style for warning messages without newlines uses concatenation of string literals with manual trailing spaces where needed. Heredocs have better readability, and with `squish` we can still produce a single line. This is a similar use case to the one that motivated defining `strip_heredoc`, heredocs are super clean.
* Merge pull request #14143 from derekprior/dp-compound-index-orderingYves Senn2014-10-272-2/+2
|\ | | | | | | | | | | | | Use type column first in multi-column indexes Conflicts: activerecord/CHANGELOG.md
| * Use type column first in multi-column indexesDerek Prior2014-10-242-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `add_reference` can very helpfully add a multi-column index when you use it to add a polymorphic reference. However, the first column in the index is the `id` column, which is less than ideal. The [PostgreSQL docs][1] say: > A multicolumn B-tree index can be used with query conditions that > involve any subset of the index's columns, but the index is most > efficient when there are constraints on the leading (leftmost) > columns. The [MySQL docs][2] say: > MySQL can use multiple-column indexes for queries that test all the > columns in the index, or queries that test just the first column, the > first two columns, the first three columns, and so on. If you specify > the columns in the right order in the index definition, a single > composite index can speed up several kinds of queries on the same > table. In a polymorphic relationship, the type column is much more likely to be useful as the first column in an index than the id column. That is, I'm more likely to query on type without an id than I am to query on id without a type. [1]: http://www.postgresql.org/docs/9.3/static/indexes-multicolumn.html [2]: http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html
* | Prefix internal method with _Rafael Mendonça França2014-10-251-2/+2
|/ | | | This will avoid naming clash with user defined methods
* Remove duplicate 'select' database statementclaudiob2014-10-201-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `select` method has the same definition in almost all database adapters, so it can be moved from the database-specific adapters (PostgreSQl, MySQL, SQLite) to the abstract `database_statement`: ```ruby def select(sql, name = nil, binds = []) exec_query(sql, name, binds) end ``` --- More details about this commit: the only two DB-specific adapters that have a different definition of `select` are MySQLAdapter and MySQL2Adapter. In MySQLAdapter, `select` invokes `exec_query(sql, name, binds)`, so calling `super` achieves the same goal with less repetition. In MySQL2Adapter, `select` invokes `exec_query(sql, name)`, that is, it does not pass the `binds` parameter like other methods do. However, [MySQL2Adapter's `exec_query`](https://github.com/rails/rails/blob/74a527cc63ef56f3d0a42cf638299958dc7cb08c/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb#L228L231) works exactly the same whether this parameters is passed or not, so the output does not change: ```ruby def exec_query(sql, name = 'SQL', binds = []) result = execute(sql, name) ActiveRecord::Result.new(result.fields, result.to_a) end ```
* we don't need a HWIA and a hash allocated for just one k/v pairAaron Patterson2014-10-151-5/+3
|
* just look up the primary key from the columns hashAaron Patterson2014-10-151-6/+1
|
* add table.bigint supportAaron Patterson2014-10-151-1/+1
| | | | | | | | In the DSL you can now do: create_table(:foos) do |t| t.bigint :hi end
* Reduce allocations when running AR callbacks.Pete Higgins2014-09-281-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Inspired by @tenderlove's work in c363fff29f060e6a2effe1e4bb2c4dd4cd805d6e, this reduces the number of strings allocated when running callbacks for ActiveRecord instances. I measured that using this script: ``` require 'objspace' require 'active_record' require 'allocation_tracer' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Base.connection.instance_eval do create_table(:articles) { |t| t.string :name } end class Article < ActiveRecord::Base; end a = Article.create name: "foo" a = Article.find a.id N = 10 result = ObjectSpace::AllocationTracer.trace do N.times { Article.find a.id } end result.sort.each do |k,v| p k => v end puts "total: #{result.values.map(&:first).inject(:+)}" ``` When I run this against master and this branch I get this output: ``` pete@balloon:~/projects/rails/activerecord$ git checkout master M Gemfile Switched to branch 'master' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_before pete@balloon:~/projects/rails/activerecord$ git checkout remove-dynamic-send-on-built-in-callbacks M Gemfile Switched to branch 'remove-dynamic-send-on-built-in-callbacks' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_after pete@balloon:~/projects/rails/activerecord$ diff allocations_before allocations_after 39d38 < {["/home/pete/projects/rails/activesupport/lib/active_support/callbacks.rb", 81]=>[40, 0, 0, 0, 0, 0]} 42c41 < total: 630 --- > total: 590 ``` In addition to this, there are two micro-optimizations present: * Using `block.call if block` vs `yield if block_given?` when the block was being captured already. ``` pete@balloon:~/projects$ cat benchmark_block_call_vs_yield.rb require 'benchmark/ips' def block_capture_with_yield &block yield if block_given? end def block_capture_with_call &block block.call if block end def no_block_capture yield if block_given? end Benchmark.ips do |b| b.report("block_capture_with_yield") { block_capture_with_yield } b.report("block_capture_with_call") { block_capture_with_call } b.report("no_block_capture") { no_block_capture } end pete@balloon:~/projects$ ruby benchmark_block_call_vs_yield.rb Calculating ------------------------------------- block_capture_with_yield 124979 i/100ms block_capture_with_call 138340 i/100ms no_block_capture 136827 i/100ms ------------------------------------------------- block_capture_with_yield 5703108.9 (±2.4%) i/s - 28495212 in 4.999368s block_capture_with_call 6840730.5 (±3.6%) i/s - 34169980 in 5.002649s no_block_capture 5821141.4 (±2.8%) i/s - 29144151 in 5.010580s ``` * Defining and calling methods instead of using send. ``` pete@balloon:~/projects$ cat benchmark_method_call_vs_send.rb require 'benchmark/ips' class Foo def tacos nil end end my_foo = Foo.new Benchmark.ips do |b| b.report('send') { my_foo.send('tacos') } b.report('call') { my_foo.tacos } end pete@balloon:~/projects$ ruby benchmark_method_call_vs_send.rb Calculating ------------------------------------- send 97736 i/100ms call 151142 i/100ms ------------------------------------------------- send 2683730.3 (±2.8%) i/s - 13487568 in 5.029763s call 8005963.9 (±2.7%) i/s - 40052630 in 5.006604s ``` The result of this is making typical ActiveRecord operations slightly faster: https://gist.github.com/phiggins/e46e51dcc7edb45b5f98
* ar/connection_pool: honor overriden rack.test in middlewareSimon Eskildsen2014-09-231-1/+1
| | | | | | | Honoring an overidden `rack.test` allows testing closed connection between multiple requests. This is useful if you're working on database resiliency, to ensure the connection is in the expected state from one request to another on the same worker.
* add a truncate method to the connectionAaron Patterson2014-09-221-0/+5
| | | | | | it doesn't work on SQLite3 since it doesn't support truncate, but that's OK. If you call truncate on the connection, you're now bound to that database (same as if you use hstore or any other db specific feature).
* Merge pull request #13656 from chanks/rollback_transactions_in_killed_threadsMatthew Draper2014-09-141-5/+11
|\ | | | | | | Data corruption risk: Roll back open transactions when the running thread is killed.
| * Roll back open transactions when the running thread is killed.Chris Hanks2014-08-221-1/+5
| |
* | Include default column limits in schema.rbJeremy Kemper2014-09-101-4/+8
| | | | | | | | | | | | Allows :limit defaults to be changed without pulling the rug out from under old migrations that omitted :limit because it matched the default at the time.
* | Merge pull request #16781 from kamipo/move_column_option_handlingYves Senn2014-09-031-1/+0
|\ \ | | | | | | Move column option handling to new_column_definition
| * | Move column option handling to new_column_definitionRyuta Kamizono2014-09-031-1/+0
| | | | | | | | | | | | | | | TableDefinition#column is not called from `add_column`. Use TableDefinition#new_column_definition for column option handling.
* | | Add and Remove string/strip requireArthur Neves2014-09-022-1/+2
|/ / | | | | | | | | | | | | Method .strip_heredoc is defined in active_support/core_ext/string/strip.rb so we need to require it. [fixes #16677]
* / Avoid using heredoc for user warningsGodfrey Chan2014-08-281-6/+5
|/ | | | | | | | | | Using heredoc would enforce line wrapping to whatever column width we decided to use in the code, making it difficult for the users to read on some consoles. This does make the source code read slightly worse and a bit more error-prone, but this seems like a fair price to pay since the primary purpose for these messages are for the users to read and the code will not stick around for too long.