aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
Commit message (Collapse)AuthorAgeFilesLines
* [ci skip] Add comment to remove silenced code.schneems2016-01-071-0/+1
|
* Prevent destructive action on production databaseschneems2016-01-071-0/+4
| | | | | | | This PR introduces a key/value type store to Active Record that can be used for storing internal values. It is an alternative implementation to #21237 cc @sgrif @matthewd. It is possible to run your tests against your production database by accident right now. While infrequently, but as an anecdotal data point, Heroku receives a non-trivial number of requests for a database restore due to this happening. In these cases the loss can be large. To prevent against running tests against production we can store the "environment" version that was used when migrating the database in a new internal table. Before executing tests we can see if the database is a listed in `protected_environments` and abort. There is a manual escape valve to force this check from happening with environment variable `DISABLE_DATABASE_ENVIRONMENT_CHECK=1`.
* Merge pull request #22562 from sblackstone/masterJeremy Daer2015-12-151-2/+2
|\ | | | | Allow users to pass flags from database.yml
| * Allow users to pass flags from database.ymlStephen Blackstone2015-12-151-2/+2
| | | | | | | | | | | | Fix white-space Add test case demonstrating flags are received by the adapter
* | Use a real migration version number in docsMatthew Draper2015-12-151-1/+1
| | | | | | | | | | Even though this means more things to change when we bump after a release, it's more important that our examples are directly copyable.
* | Use a deliberately-invalid migration version in all doc examplesMatthew Draper2015-12-151-1/+1
| | | | | | | | | | | | | | | | | | | | If we use a real version, at best that'll be an onerous update required for each release; at worst, it will encourage users to write new migrations against an older version than they're using. The other option would be to leave these bare, without any version specifier. But as that's just a variant spelling of "4.2", it would seem to raise the same concerns as above.
* | Merge pull request #22381 from yahonda/use_adapter_subsecond_precision_supportedAaron Patterson2015-12-131-0/+4
|\ \ | |/ |/| Use adapter supports_datetime_with_precision
| * Support supports_datetime_with_precision? for sqlite3Yasuo Honda2015-11-301-0/+4
| |
* | Merge pull request #22449 from dnagir/database_connection_messageYves Senn2015-12-011-1/+1
|\ \ | |/ |/| Explain the connection pool error message better [ci skip]
| * Explain the connection pool error message betterDmytrii Nagirniak2015-12-011-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | The previous message was misleading (especially for Ops guys) when diagnosing problems related to the database connection. The message was suggesting that the connection cannot be obtained which normally assumes the need to look at the database. But this isn't the case as the connection could not be retrieved from the application's internal connection pool. The new message should make it more explicit and remove the confusion.
* | Refactor `AbstractAdapter#initialize`Ryuta Kamizono2015-11-304-8/+7
|/ | | | `pool` in args is unused anymore. And `config` is used in all adapters.
* `connection_options` is only needed for `MysqlAdapter`Ryuta Kamizono2015-11-293-3/+3
| | | | Not needed for `Mysql2Adapter` and `AbstractMysqlAdapter`.
* Revert "Add prepared statements support for `Mysql2Adapter`"Sean Griffin2015-11-263-145/+151
|
* Add prepared statements support for `Mysql2Adapter`Ryuta Kamizono2015-11-263-151/+145
|
* Merge pull request #22304 from ↵Yves Senn2015-11-241-6/+12
|\ | | | | | | | | kamipo/schema_dumping_support_for_postgresql_geometric_types Add schema dumping support for PostgreSQL geometric data types
| * Add schema dumping support for PostgreSQL geometric data typesRyuta Kamizono2015-11-241-6/+12
| |
* | Merge pull request #22214 from ↵Rafael França2015-11-244-15/+6
|\ \ | | | | | | | | | | | | kamipo/not_passing_native_database_types_to_table_definition Not passing `native_database_types` to `TableDefinition`
| * | Not passing `native_database_types` to `TableDefinition`Ryuta Kamizono2015-11-084-15/+6
| | | | | | | | | | | | | | | | | | The `native_database_types` only used in `TableDefinition` for look up the default `:limit` option. But this is duplicated process with `type_to_sql`. Passing `native_database_types` is not needed.
* | | `set_field_encoding` is only needed for `MysqlAdapter`Ryuta Kamizono2015-11-243-9/+8
| |/ |/| | | | | Not needed for `Mysql2Adapter` and `AbstractMysqlAdapter`.
* | Revert "Allow specifying the default table options for mysql adapters"Sean Griffin2015-11-191-2/+1
| | | | | | | | | | | | | | This reverts commit 8246b593bff71f2cebf274c133bb8917f1e094c8. There was concern about this modifying the behavior of past migrations. We're going to add an way to modify the migration generator instead.
* | Allow specifying the default table options for mysql adaptersSean Griffin2015-11-191-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | It's often the case that you want to have an option that you cannot specify at the database level, but want applied to *all* tables that you create. For example, you might want to specify `ROW_FORMAT=DYNAMIC` to not have to limit text columns to length 171 for indexing when using utf8mb4. This allows an easy way to specify this in your database configuration. While this change affects both MySQL and MySQL2, the test only covers MySQL2, as the legacy mysql adapter appears to always return ASCII strings, and is tangential to what we're actually doing.
* | Rename 'key' to 'lock_id' or 'lock_name' for advisory lockingSam Davies2015-11-183-14/+14
| | | | | | | | | | | | | | | | | | - key was a poor choice of name. A key implies something that will unlock a lock. The concept is actually more like a 'lock identifier' - mysql documentation calls this a 'lock name' - postgres documentation calls it a 'lock_id' - Updated variable names to reflect the preferred terminology for the database in question
* | Remove not needed `NATIVE_DATABASE_TYPES` entriesRyuta Kamizono2015-11-162-4/+0
| |
* | Deprecate `#table_exists?`, `#tables` and passing arguments to `#talbes`yui-knk2015-11-094-7/+70
|/ | | | | | | | | | Reported on #21509, how views is treated by `#tables` are differ by each adapters. To fix this different behavior, after Rails 5.0 is released, deprecate `#tables`. And `#table_exists?` would check both tables and views. To make their behavior consistent with `#tables`, after Rails 5.0 is released, deprecate `#table_exists?`.
* Avoids mutating the original response in connection management middlewareKevin Buchanan2015-11-061-4/+3
|
* Require only necessary concurrent-ruby classes.Jerry D'Antonio2015-11-041-1/+1
|
* Remove incorrect commentsAndrew White2015-11-041-4/+0
| | | | | | Columns are no longer stored in an attribute since b8a533d. [ci skip]
* Deprecate exception#original_exception in favor of exception#causeYuki Nishijima2015-11-036-11/+11
|
* Allow bigint with default nil for avoiding auto increment primary keyRyuta Kamizono2015-11-022-3/+6
| | | | Such as #10404, #18206.
* Merge pull request #22122 from ↵Sean Griffin2015-10-303-0/+51
|\ | | | | | | | | samphilipd/sam/manual_locking_on_schema_migrations Make migrations concurrent safe (using advisory locks)
| * Use advisory locks to prevent concurrent migrationsSam Davies2015-10-303-0/+51
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - Addresses issue #22092 - Works on Postgres and MySQL - Uses advisory locks because of two important properties: 1. The can be obtained outside of the context of a transaction 2. They are automatically released when the session ends, so if a migration process crashed for whatever reason the lock is not left open perpetually - Adds get_advisory_lock and release_advisory_lock methods to database adapters - Attempting to run a migration while another one is in process will raise a ConcurrentMigrationError instead of attempting to run in parallel with undefined behavior. This could be rescued and the migration could exit cleanly instead. Perhaps as a configuration option? Technical Notes ============== The Migrator uses generate_migrator_advisory_lock_key to build the key for the lock. In order to be compatible across multiple adapters there are some constraints on this key. - Postgres limits us to 64 bit signed integers - MySQL advisory locks are server-wide so we have to scope to the database - To fulfil these requirements we use a Migrator salt (a randomly chosen signed integer with max length of 31 bits) that identifies the Rails migration process as the owner of the lock. We multiply this salt with a CRC32 unsigned integer hash of the database name to get a signed 64 bit integer that can also be converted to a string to act as a lock key in MySQL databases. - It is important for subsequent versions of the Migrator to use the same salt, otherwise different versions of the Migrator will not see each other's locks.
* | Merge pull request #19511 from larskanis/replace_const_conn_paramsSean Griffin2015-10-291-7/+2
|\ \ | | | | | | PostgreSQL, Replace static connection param list by libpq's dynamic list
| * | PostgreSQL, Replace static connection param list by the one built into libpq.Lars Kanis2015-03-251-7/+2
| | | | | | | | | | | | | | | | | | | | | This makes the connection adapter future-proof regarding to new parameters. To maintain backward compatibility, :requiressl is added by hand. It is deprecated by PostgreSQL since 2003, but still accepted by libpq.
* | | Don't disable errors when turning standard_conforming_strings onHarry Marr2015-10-291-7/+2
| | |
* | | Check standard_conforming_strings is not readonlyHarry Marr2015-10-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | In Postgres 8.1 the standard_conforming_strings setting was read-only, meaning you got an error if you tried to update it. By filtering on `context = 'user'` we only try to update the setting if it's user-writable[1]. [1]: http://www.postgresql.org/docs/9.4/static/view-pg-settings.html
* | | Avoid disabling postgres errorsHarry Marr2015-10-281-4/+5
| |/ |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The standard_conforming_strings setting doesn't exist on all versions of Postgres, but if it does exist, Rails turns it on. Previously this was done by effectively disabling errors on the Postgres connection, issuing a SET to turn the setting on, then re-enabling errors on the connection. However, if you're running pgbouncer in transaction-pooling mode, you can't guarantee that successive calls to `#execute` will be sent to the same pgbouncer-postgres connection, so you can end up disabling errors on a different postgres connection, and never re-enabling them. Future queries on that connection that result in errors (e.g. violating unique constraints) will leave the connection in a bad state where successive queries will fail. This commit sets standard_conforming_strings by issuing an UPDATE to pg_settings, which will update the setting if it exists, and do nothing if it doesn't (rather than erroring out like SET would), which means we can remove the error-disabling code. It's also worth noting that Postgres has allowed standard_conforming_strings to be updated since 8.2 (which is the oldest version Rails supports), so technically we probably don't even need to be defensive here.
* | Remove no need `binds.empty?` checkingRyuta Kamizono2015-10-231-3/+3
| | | | | | | | | | `#exec_stmt` is private method and only called in `#exec_query`. it means `binds` is provided always. No need `binds.empty?` checking.
* | Remove `#tables` extra args againRyuta Kamizono2015-10-222-20/+11
| | | | | | | | | | | | This issue was resolved by #21687 already. But re-add args by #18856. `#tables` extra args was only using by `#table_exists?`. This is for internal API. This commit will remove these extra args again.
* | Extract native getter to attr_reader.jbranchaud2015-10-211-5/+1
| | | | | | | | | | The getter is doing nothing more than returning the ivar, so it can be extracted to an attr_reader.
* | move documentation of column options to `add_column`. Closes #20400.Yves Senn2015-10-212-79/+81
| | | | | | | | | | | | | | | | | | | | | | | | [ci skip] It's been a source of confusion that the lower-level `add_column` referenced the higher level `column` method for available options. `column` supports additional functionality like `index: true` that is not present on `add_column`. This patch moves common option documentation to `add_column` and only documents the additional options in `column`.
* | Fix merge conflicts for #18856Sean Griffin2015-10-201-2/+10
|\ \
| * | Match table names exactly on MySQLMatt Jones2015-02-081-3/+5
| | | | | | | | | | | | | | | | | | | | | | | | The `SHOW TABLES LIKE` command accepts metacharacters `%` and `_` in potentially unexpected ways. This can be avoided by querying `information_schema.tables` directly. Fixes #17897
* | | Don't add classes to the top level namespaceSean Griffin2015-10-201-13/+17
| | | | | | | | | | | | | | | I've been writing too much Rust. My mind is still in the mode of things being auto-namespaced based on the file...
* | | Do not cache prepared statements that are unlikely to have cache hitsSean Griffin2015-10-209-18/+62
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Prior to this commit, Rails makes no differentiation between whether a query uses bind parameters, and whether or not we cache that query as a prepared statement. This leads to the cache populating extremely fast in some cases, with the statements never being reused. In particular, the two problematic cases are `where(foo: [1, 2, 3])` and `where("foo = ?", 1)`. In both cases we'll end up quoting the values rather than using a bind param, causing a cache entry for every value ever used in that query. It was noted that we can probably eventually change `where("foo = ?", 1)` to use a bind param, which would resolve that case. Additionally, on PG we can change our generated query to be `WHERE foo = ANY($1)`, and pass an array for the bind param. I hope to accomplish both in the future. For SQLite and MySQL, we still end up preparing the statements anyway, we just don't cache it. The statement will be cleaned up after it is executed. On postgres, we skip the prepare step entirely, as an API is provided to execute with bind params without preparing the statement. I'm not 100% happy on the way this ended up being structured. I was hoping to use a decorator on the visitor, rather than mixing a module into the object, but the way Arel has it's visitor pattern set up makes it very difficult to extend without inheritance. I'd like to remove the duplication from the various places that are extending it, but that'll require a larger restructuring of that initialization logic. I'm going to take another look at the structure of it soon. This changes the signature of one of the adapter's internals, and will require downstream changes from third party adapters. I'm not too worried about this, as worst case they can simply add the parameter and always ignore it, and just keep their previous behavior. Fixes #21992.
* | | Merge pull request #21932 from kamipo/add_stored_procedure_test_in_mysql2Sean Griffin2015-10-202-5/+8
|\ \ \ | | | | | | | | Add stored procedure test in mysql2
| * | | Add stored procedure test in mysql2Ryuta Kamizono2015-10-151-1/+4
| | | |
| * | | Make `AbstractMysqlAdapter#version` publicRyuta Kamizono2015-10-151-4/+4
| | | |
* | | | Merge pull request #21962 from kamipo/fix_tinyblobSean Griffin2015-10-203-11/+31
|\ \ \ \ | | | | | | | | | | Fix to correctly schema dump the `tinyblob`
| * | | | Fix to correctly schema dump the `tinyblob`Ryuta Kamizono2015-10-153-11/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently `tinyblob` is dumped to `t.binary "tiny_blob", limit: 255`. But `t.binary ... limit: 255` is generating SQL to `varchar(255)`. It is incorrect. This commit fixes this problem.
* | | | | freeze the column name to drop string allocations in dirty checksAaron Patterson2015-10-141-1/+1
| |/ / / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Dirty checking keeps a hash where the keys are the column name and the value is a dup of the value from the database[1]. This hash is kept for every AR object, which means that we dup every column name for every AR object that does dirty checking. Freezing the column name prevents the column name from being duped and reduced overall string allocations. Here is a benchmark to demonstrate: ```ruby require 'active_record' class Topic < ActiveRecord::Base end 20.times do |i| Process.waitpid fork { ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:' ActiveRecord::Base.connection.instance_eval do create_table(:topics) do |t| t.string :title, limit: 250 t.string :author_name t.string :author_email_address t.string :parent_title t.string :type t.string :group i.times do |j| t.string :"aaa#{j}" end t.timestamps null: true end end ObjectSpace::AllocationTracer.setup(%i{type}) Topic.create title: "aaron" # heat cache result = ObjectSpace::AllocationTracer.trace do 10.times do |i| Topic.create title: "aaron #{i}" end end puts "#{Topic.columns.length},#{(result.find { |k,v| k.first == :T_STRING }.last.first / 10)}" } end ``` 1. https://github.com/rails/rails/blob/3ad381c3f8598d9920998c8949a96b5f62b280dd/activerecord/lib/active_record/attribute_set/builder.rb#L102