aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
Commit message (Collapse)AuthorAgeFilesLines
* Clear schema cache when a table is created/dropped/renamedRyuta Kamizono2019-06-131-0/+3
| | | | Otherwise `Model.table_exists?` returns the staled cache result.
* make change_column_comment and change_table_comment invertibleYoshiyuki Kinjo2019-04-151-2/+4
| | | | | | | | | We can revert migrations using `change_column_comment` or `change_table_comment` at current master. However, results are not what we expect: comments are remained in new status. This change tells previous comment to these methods in a way like `change_column_default`.
* use PostgreSQL's bulk_alter_table implementationYoshiyuki Kinjo2019-04-131-25/+0
| | | | | | | | | | | | | | | Running this migration on mysql at current master fails because `add_references_for_alter` is missing. ``` change_table :users, bulk: true do |t| t.references :article end ``` This is also true for postgresql adapter, but its `bulk_alter_table` implementation can fallback in such case. postgresql's implementation is desirable to prevent unknown failure like this.
* Refactor around sql_type metadata and columnRyuta Kamizono2019-04-121-1/+1
| | | | | | * remove useless `@type_metadata` and `@array` * move the compatibility code (for array) into column * etc.
* Merge pull request #35887 from kamipo/argument_errorRyuta Kamizono2019-04-091-3/+3
|\ | | | | Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options
| * Raise `ArgumentError` for invalid `:limit` and `:precision` like as other ↵Ryuta Kamizono2019-04-071-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* | Except `table_name` from column objectsRyuta Kamizono2019-04-081-4/+23
|/ | | | | | | | | The `table_name` was added at #23677 to detect whether serial column or not correctly. We can do that detection before initialize column object, it makes column object size smaller, and it probably helps column object de-duplication.
* Cache database version in schema cacheAli Ibrahim2019-04-031-1/+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.
* Allow `remove_foreign_key` with both `to_table` and `options`Ryuta Kamizono2019-03-061-2/+2
| | | | | | | Foreign keys could be created to the same table. So `remove_foreign_key :from_table, :to_table` is sometimes ambiguous. This allows `remove_foreign_key` to remove the select one on the same table with giving both `to_table` and `options`.
* Refactor `type_to_sql` to handle converting `limit` to `size` in itselfRyuta Kamizono2019-02-261-2/+2
| | | | | Also, improving an argument error message for `limit`, extracting around `type_to_sql` code into schema statements, and more exercise tests.
* Make `t.timestamps` with precision by defaultRyuta Kamizono2019-01-261-1/+5
|
* Fix `t.timestamps` missing `null: false` in `change_table bulk: true`Ryuta Kamizono2019-01-261-0/+2
|
* Fix type casting column default in `change_column`Ryuta Kamizono2019-01-201-24/+6
| | | | | | | | | | | | | | 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.
* Enable `Lint/UselessAssignment` cop to avoid unused variable warnings (#34904)Ryuta Kamizono2019-01-091-2/+2
| | | | | | | | | | | | | | * Enable `Lint/UselessAssignment` cop to avoid unused variable warnings Since we've addressed the warning "assigned but unused variable" frequently. 370537de05092aeea552146b42042833212a1acc 3040446cece8e7a6d9e29219e636e13f180a1e03 5ed618e192e9788094bd92c51255dda1c4fd0eae 76ebafe594fc23abc3764acc7a3758ca473799e5 And also, I've found the unused args in c1b14ad which raises no warnings by the cop, it shows the value of the cop.
* Use squiggly heredoc to strip odd indentation in the executed SQLRyuta Kamizono2018-11-221-7/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-1/+1
| | | | | | | | in indexdef to be wrapped up by double quotes Fixes #34493. *Thomas Bianchini*
* Enable `Performance/UnfreezeString` copyuuji.yaginuma2018-09-231-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* Fix bulk change table ignores comment option on PostgreSQL.Yoshiyuki Kinjo2018-08-151-1/+5
|
* PostgreSQL 10 new relkind for partitioned tables (#31336)Yannick Schutz2018-07-271-2/+2
| | | | | | | | | | | | | | | | | | | | | | | * PostgreSQL 10 new relkind for partitioned tables Starting with PostgreSQL 10, we can now have partitioned tables natively * Add comment * Remove extra space * Add test for partition table in postgreSQL10 * Select 'p' for "BASE TABLE" and add a test case to support PostgreSQL 10 partition tables * Address RuboCop offense * Addressed incorrect `postgresql_version` Fixes #33008. [Yannick Schutz & Yasuo Honda & Ryuta Kamizono]
* Fix multiline expression indexes for postgresql (#31621)fatkodima2018-03-161-1/+1
|
* Fix `#columsn_for_distinct` of MySQL and PostgreSQLkg8m2018-02-271-1/+1
| | | | | | | 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.
* Remove usage of strip_heredoc in the framework in favor of <<~Rafael Mendonça França2018-02-161-4/+4
| | | | | Some places we can't remove because Ruby still don't have a method equivalent to strip_heredoc to be called in an already existent string.
* Dump correctly index nulls order for postgresqlfatkodima2018-01-281-2/+6
|
* Merge pull request #31773 from dinahshi/postgresql_bulk_patchMatthew Draper2018-01-241-1/+1
|\ | | | | Postgresql bulk_change_table should flatten procs array
| * Use concat to join procs arrays in bulk_change_tableDinah Shi2018-01-231-1/+1
| |
* | Support for PostgreSQL foreign tablesfatkodima2018-01-221-1/+11
| |
* | Enable autocorrect for `Lint/EndAlignment` copKoichi ITO2018-01-181-1/+1
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ### Summary This PR changes .rubocop.yml. Regarding the code using `if ... else ... end`, I think the coding style that Rails expects is as follows. ```ruby var = if cond a else b end ``` However, the current .rubocop.yml setting does not offense for the following code. ```ruby var = if cond a else b end ``` I think that the above code expects offense to be warned. Moreover, the layout by autocorrect is unnatural. ```ruby var = if cond a else b end ``` This PR adds a setting to .rubocop.yml to make an offense warning and autocorrect as expected by the coding style. And this change also fixes `case ... when ... end` together. Also this PR itself is an example that arranges the layout using `rubocop -a`. ### Other Information Autocorrect of `Lint/EndAlignment` cop is `false` by default. https://github.com/bbatsov/rubocop/blob/v0.51.0/config/default.yml#L1443 This PR changes this value to `true`. Also this PR has changed it together as it is necessary to enable `Layout/ElseAlignment` cop to make this behavior.
* Add bulk alter support for PostgreSQLDinah Shi2017-12-061-5/+36
|
* `change_column_default` should be executed after type changingRyuta Kamizono2017-12-031-7/+8
| | | | | | | | | | | | | | | | | | | | If do not execute a type changing first, filling in default value may be failed. ``` % ARCONN=postgresql be ruby -w -Itest test/cases/migration/compatibility_test.rb -n test_legacy_change_column_with_null_executes_update Using postgresql Run options: -n test_legacy_change_column_with_null_executes_update --seed 20459 E Error: ActiveRecord::Migration::CompatibilityTest#test_legacy_change_column_with_null_executes_update: StandardError: An error has occurred, this and all later migrations canceled: PG::StringDataRightTruncation: ERROR: value too long for type character varying(5) : UPDATE "testings" SET "foo"='foobar' WHERE "foo" IS NULL ```
* Extract duplicated index column options normalization as ↵Ryuta Kamizono2017-12-031-0/+12
| | | | | | | `options_for_index_columns` And placed `add_options_for_index_columns` in `schema_statements.rb` consistently to ease to find related code.
* Refactor `length`, `order`, and `opclass` index options dumpingRyuta Kamizono2017-12-031-11/+5
|
* Merge pull request #31230 from dinahshi/postgresql_extract_sqlMatthew Draper2017-12-031-33/+53
|\ | | | | Extract sql fragment generators from PostgreSQL adapter
| * Extract sql fragment generators for alter table from PostgreSQL adapterDinah Shi2017-12-021-33/+53
| |
* | Fix method name in `validate_constraint` doc [ci skip]yuuji.yaginuma2017-12-021-1/+1
| |
* | Add support for invalid foreign keys in PostgresTravis Hunter2017-12-011-1/+43
| | | | | | | | Add validate_constraint and update naming
* | Add support for PostgreSQL operator classes to add_indexGreg Navis2017-11-301-12/+20
|/ | | | | | | | | | | | Add support for specifying non-default operator classes in PostgreSQL indexes. An example CREATE INDEX query that becomes possible is: CREATE INDEX users_name ON users USING gist (name gist_trgm_ops); Previously it was possible to specify the `gist` index but not the custom operator class. The `add_index` call for the above query is: add_index :users, :name, using: :gist, opclasses: {name: :gist_trgm_ops}
* Remove deprecated argument `name` from `#indexes`Rafael Mendonça França2017-10-231-9/+1
|
* Remove deprecated argument `default` from `index_name_exists?`Rafael Mendonça França2017-10-231-6/+1
|
* Fix longer sequence name detection for serial columns (#28339)Ryuta Kamizono2017-10-151-1/+2
| | | | | | | | We already found the longer sequence name, but we could not consider whether it was the sequence name created by serial type due to missed a max identifier length limitation. I've addressed the sequence name consideration to respect the max identifier length. Fixes #28332.
* Refactor `SchemaDumper` to make it possible to adapter specific customizationRyuta Kamizono2017-08-221-0/+4
| | | | | | | Currently `SchemaDumper` is only customizable for column options. But 3rd party connection adapters (oracle-enhanced etc) need to customizable for table or index dumping also. To make it possible, I introduced adapter specific `SchemaDumper` classes for that.
* Place `update_table_definition` consistently in `SchemaStatements`Ryuta Kamizono2017-08-211-0/+4
|
* Change http postgresql.org links to https [ci skip]yuuji.yaginuma2017-07-301-1/+1
| | | | | It seems that it accepts only HTTPS connections. Ref: https://github.com/postgres/postgres/commit/7f77cbd996855a06fb742ea11adbe55c42b48fe2
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Merge pull request #29540 from kirs/rubocop-frozen-stringMatthew Draper2017-07-021-0/+1
|\ | | | | | | Enforce frozen string in Rubocop
| * Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
| |
* | Merge pull request #29506 from pat/frozen-string-literalsMatthew Draper2017-07-021-3/+3
|\ \ | | | | | | | | | Make ActiveSupport frozen-string-literal friendly.
| * | Make ActiveRecord frozen string literal friendly.Pat Allan2017-06-201-3/+3
| | |
* | | Don't cache queries for schema statementsRyuta Kamizono2017-06-301-21/+19
| |/ |/| | | | | | | | | `test_middleware_caches` is sometimes failed since #29454. The failure is due to schema statements are affected by query caching. Bypassing query caching for schema statements to avoid the issue.
* | Use `quote` method rather than single quotes to identifiers in SQLRyuta Kamizono2017-06-291-6/+6
|/ | | | | | Because identifiers in SQL could include a single quote. Related #24950, #26784.