Commit Graph

21820 Commits

Author SHA1 Message Date
fatkodima
86519996f9 Enable Style/RedundantCondition cop 2022-08-11 11:38:14 +03:00
Eduardo Hernandez
c619cec014 Docs: Update belongs_to [:default] option section
Just added some more details about how `belongs_to` behaves when using
this option based on what I found here:

- https://github.com/rails/rails/blob/main/activerecord/lib/active_record/associations/builder/belongs_to.rb#L102-L106
- https://github.com/rails/rails/blob/main/activerecord/lib/active_record/associations/belongs_to_association.rb#L35-L37
2022-08-09 21:27:46 -05:00
Andrew Hoglund
be0a58b6c4 Ensure collation is set to database default when changing column
A recent change made it so that when you perform a change column
it will preserve collation on the column rather than nil it out·
which would select the database default. This works fine except in
the case of a binary type. In this case the collation should be
set to nil in the options so that the database default collation
type can be used on the column.
2022-08-09 17:42:18 +00:00
Eileen M. Uchitelle
c54b26d90c
Merge pull request #45772 from adrianna-chang-shopify/ac-remove-ddl-from-schema-definitions
Don't store `ddl` on schema definitions
2022-08-08 08:45:20 -04:00
Adrianna Chang
47141261c3 Don't store ddl on schema definitions
We'll ensure that any consumers that require ddl obtain it by visiting
the schema definition. Consequently, we should also stop visiting the
schema definition in the "schema definition builder" methods -- callers
will need to both build a schema definition, and then visit it using a
SchemaCreation object. This means schema definitions will not be populated
with SQL type information, or any other information that is set when
the definition is visited.
2022-08-08 08:12:24 -04:00
Yasuo Honda
aa00ef35eb
Merge pull request #45740 from ghiculescu/enum-types-schema
Support schemas in Postgresql `enum_types`
2022-08-07 10:46:17 +09:00
Alex Ghiculescu
3ac1d8fe27 Support schemas in Postgresql enum_types
Follow up to https://github.com/rails/rails/pull/44896 and https://github.com/rails/rails/pull/45707
2022-08-06 09:07:56 -05:00
Jonathan Hefner
32c169d014 Linkify code references [ci-skip] 2022-08-05 21:36:18 -05:00
Jonathan Hefner
b13107fe5e Fix typos [ci-skip] 2022-08-05 21:36:18 -05:00
Jonathan Hefner
afe98a0b5d
Merge pull request #45774 from jonathanhefner/drop_enum-always-reversible
Ensure `drop_enum` is always reversible
2022-08-05 21:35:17 -05:00
Jonathan Hefner
a24879d0cd
Merge pull request #45765 from sampatbadhe/patch-1
[ci skip] Add basic create_enum and drop_enum in CommandRecorder documentations
2022-08-05 12:10:15 -05:00
Jonathan Hefner
2aa16fa561 Ensure drop_enum is always reversible
Follow-up to #45735.

Prior to this commit, `drop_enum` could not be reversed when called with
the `if_exists: true` option, because `create_enum` could not accept
options.

This commit fixes that and adds test coverage.

This commit also ensures that reversing `drop_enum` will raise an
`ActiveRecord::IrreversibleMigration` error if no enum values are
specified.
2022-08-05 12:07:47 -05:00
eileencodes
70f2ae001e
Only setup shared pools if we have a connection
Previously `setup_shared_connection_pool` was called anytime
`setup_fixtures` saw a future connection established. Sometimes those
connections are `nil` but we were setting up the shared pools before
determining that.

This change moves the `setup_shared_connection_pool` into the check for
if we have a connection. The tests that were dealing with these settings
were originally set to not use transacitons. But without this we can't
test the change here so I've updated that as well.

This is an alternate fix to #45769. I realized after opening the PR it
wasn't quite right to skip if we don't have a writing pool config,
because the question is how were we ending up with that anyway? Well the
answer was that we were calling the shared setup in the wrong place.

Fixes #45767
2022-08-05 12:40:52 -04:00
sampatbadhe
76d2185c60 [ci skip] adding basic create_enum and drop_enum in CommandRecorder documentations 2022-08-05 12:41:35 +05:30
eileencodes
1623892582
Fix rubocop warning re unused argument 2022-08-04 14:50:11 -04:00
eileencodes
8d0da3d568
Refactor changes in #44931 and add a test
The PR #44931 fell off my radar a few months ago so I decided to add a
test myself to ensure this behavior change is tested. While doing that I
decided to refactor the changes from that PR to make the code a little
cleaner and easier to read.
2022-08-04 12:58:39 -04:00
Matt Larraz
8af4165a18
Improve #configured_migrate_path logic (#44931)
* Improve #configured_migrate_path logic

This solves two issues:

* `migrations_paths` is an Array, which here gets coerced to a string.
  This is fine if it's a single path, but otherwise the generated path
  is a concatenation of all the paths. Instead, pick the first one.

* If the `--database` flag is not passed in, fall back to the primary
  database's `migrations_paths` entry if available before falling back
  to the global default migrate path.

* migrations_paths can be a String, Array, or nil
2022-08-04 12:58:20 -04:00
Eileen M. Uchitelle
0c9e4069f7
Merge pull request #45751 from adrianna-chang-shopify/ac-build-change-column-default-definition
Define #build_change_column_definition for MySQL and PG
2022-08-04 08:37:11 -04:00
Alex Ghiculescu
bd0fdc8094 Add drop_enum command for Postgres 2022-08-03 18:50:43 -05:00
Adrianna Chang
7455336638 Define #build_change_column_definition for MySQL and PG
Define APIs on the MySQL and PostgreSQL connection adapters for building
ChangeColumnDefaultDefinition objects. These provide information on what a
column default change would look like, when called with the same arguments as
would be passed to #change_column_default.
2022-08-03 16:27:25 -04:00
eileencodes
58e6f11878
Fix indentation
I messed up copying and pasting this. Rubocop failures caught
this in https://github.com/rails/rails/runs/7658743379?check_suite_focus=true
2022-08-03 14:46:11 -04:00
eileencodes
25f97a66bd
Add support for if_exists option when removing a check constraint
The `remove_check_constraint` method now accepts an `if_exists` option. If set
to true an error won't be raised if the check constraint doesn't exist.

This commit is a combination of PR #45726 and #45718 with some
additional changes to improve wording, testing, and implementation.

Usage:

```ruby
remove_check_constraint :products, name: "price_check", if_exists: true
```

Fixes #45634

Co-authored-by: Margaret Parsa <mparsa@actbluetech.com>
Co-authored-by: Aditya Bhutani <adi_bhutani16@yahoo.in>
2022-08-03 14:39:54 -04:00
Jean Boussier
1288289ed0
Merge pull request #45697 from adrianna-chang-shopify/ac-change-column-default-mysql
Use SET DEFAULT when changing a column default in MySQL
2022-08-03 20:23:08 +02:00
Jean Boussier
e1f566a6f4
Merge pull request #45734 from fatkodima/fix-columnless-enums
Raise when defining an enum not backed by a database column
2022-08-03 20:20:39 +02:00
Adrianna Chang
b9b8852adc Use SET DEFAULT when changing a column default in MySQL
Aligns the SQL produced for changing a column default between MySQL and PostgreSQL.
MySQL is currently producing an <ALTER TABLE x CHANGE COLUMN y> query just to
change the default for a column, which is less efficient than using the SET DEFAULT syntax.

Additionally, extracts ChangeColumnDefaultDefinition and moves the SQL generation to the visitor class.
2022-08-03 11:16:06 -04:00
fatkodima
3a0d0f4bd8 Preserve collaction when changing column in MySQL 2022-08-03 13:09:25 +03:00
fatkodima
6c5fab0668 Raise when defining an enum not backed by a database column 2022-08-03 11:14:45 +03:00
Jean Boussier
408d061a80
Merge pull request #45720 from Shopify/find-or-create-or-find-by
find_or_create_by: handle race condition by finding again
2022-08-02 15:11:20 +02:00
fatkodima
cd3508607d Accept empty list of attributes for insert_all, insert_all! and upsert_all 2022-08-02 13:21:31 +03:00
Jean Boussier
023a3eb3c0 find_or_create_by: handle race condition by finding again
Using `create_or_find_by` in codepaths where most of the time
the record already exist is wasteful on several accounts.

`create_or_find_by` should be the method to use when most of the
time the record doesn't already exist, not a race condition safe
version of `find_or_create_by`.

To make `find_or_create_by` race-condition free, we can search
the record again if the creation failed because of an unicity
constraint.

Co-Authored-By: Alex Kitchens <alexcameron98@gmail.com>
2022-08-02 09:58:26 +02:00
Jean Boussier
765caa44a7
Merge pull request #45714 from simi/proper-verbose-cleanup
Store previous verbose setting before it can fail in tests.
2022-08-01 19:29:17 +02:00
Jean Boussier
7de5053923 Refactor ExtendedDeterministicQueries to not mutate the arguments
Ref: https://github.com/rails/rails/pull/41659

Mutating the attributes hash requires to workaround the mutation
in `find_or_create_by` etc.

One extra Hash dup is not big deal.
2022-08-01 16:21:33 +02:00
Josef Šimánek
4f4dd2ef02 Set collation for postgresql test DBs. 2022-07-31 16:23:00 +02:00
Josef Šimánek
bfdf158a15 Store previous verbose setting before it can fail in tests.
- thanks to hoisting it can be wrongly set to nil instead of previous value
2022-07-31 14:54:43 +02:00
fatkodima
438862b5d3 Support explicit schemas in PostgreSQL's create_enum 2022-07-31 00:12:07 +03:00
Matthew Draper
1adb6170d5 Add changelog entries for #44576 and #44591 2022-07-30 06:09:12 +09:30
Matthew Draper
72cdf1584a
Merge pull request #44591 from rails/defer-db-connect
Simplify adapter construction; defer connect until first use
2022-07-30 06:08:55 +09:30
Matthew Draper
7fe221d898
Merge pull request #44576 from rails/defer-db-verify
Defer verification of database connections
2022-07-30 06:08:34 +09:30
Eileen M. Uchitelle
b623494a29
Merge pull request #45685 from adrianna-chang-shopify/ac-build-create-join-table-definition
Extract `#build_create_join_table_definition`
2022-07-29 13:38:22 -04:00
Matthew Draper
91a3a2d0cc Don't expect rollback to cause a reconnect 2022-07-30 00:55:54 +09:30
Matthew Draper
e4193dee1d Be a bit clearer in the new-adapter-API argument error 2022-07-30 00:55:54 +09:30
Matthew Draper
0223967ab6 Drop default retry count to 1
This is enough to enable basic recovery for everyone by default, while
leaving more time-consuming repeated attempts for those who configure
them.
2022-07-30 00:55:54 +09:30
Matthew Draper
d56188dc7a We're still inside the transaction until the ROLLBACK has run
This previously made no difference, but it now saves us reconnecting to
run a ROLLBACK: because we're still inside a transaction at that moment,
we won't be #restorable?.
2022-07-30 00:55:54 +09:30
Matthew Draper
fe2846a154 Non-retryable exceptions also break connection verification 2022-07-30 00:55:54 +09:30
Matthew Draper
deec3004d8 Configure legacy-API-supplied connection before first use 2022-07-30 00:55:54 +09:30
Matthew Draper
0e9267767f Ensure a lost connection upon COMMIT fails "cleanly"
There's no point attempting to rollback in this case; we can instead
just invalidate the now-lost transaction.

Similarly, even though we can't immediately reconnect for a connection
failure mid-transaction, we can and should drop and prior verification
of the connection: the next out-of-transaction query attempt needs to
fix it, not assume it's working.
2022-07-30 00:55:54 +09:30
Matthew Draper
8551e64e24 Simplify adapter construction; defer connect until first use 2022-07-30 00:55:48 +09:30
Adrianna Chang
c59cbb55da Define #build_change_column_definition on connection adapters
Define APIs on the MySQL and PostgreSQL connection adapters for building
ChangeColumnDefinition objects. These provide information on what a
column change would look like, when called with the same arguments as
would be passed to #change_column.
2022-07-28 16:15:47 -04:00
Adrianna Chang
108c30e114 Extract build_create_join_table_definition
Exposes an API for building a TableDefinition for a join table operation.
2022-07-28 16:11:47 -04:00
Adrianna Chang
e91b6421ec Extract #build_add_column_definition
Exposes AlterTable schema definition for adding new columns through new API,
and stores ddl on the schema definition. Refactors existing #add_column method to use
this as an intermediate method to build the create add_column definition.
2022-07-28 15:19:29 -04:00