pg_ha_migrations 1.8.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6858f02b9a874bbaf79865c789a490c9aa1140240537d0eded8f48486c6348f3
4
- data.tar.gz: 733394b7f83821f71821816777765d982d1580761522e0751534d3e3c62f598b
3
+ metadata.gz: cd0329ccdae5b3bf68ac759bc0eca2dbc5089c3d91f9ee6444c6791a2bd42e93
4
+ data.tar.gz: ecf840233b36ede3ef278411bac124cefa32036b43d62119792a8d74ac126b5e
5
5
  SHA512:
6
- metadata.gz: 55f3f8e3730fc11183e71cbf6ba9d32dabf1c8b0ec532da6861b47d045cffb348184350831f564999ecee22b601931ab577ca20f8c4e831e0d4b776babe21a58
7
- data.tar.gz: e260ebe93cafba7f3119c41bd2594a797293713f95d072378dbc5733f176d86ce8a93c8a24b53b6fbb9ec77756f4db9ab5fdb92f6080de6ac0fb56e158e5b5d3
6
+ metadata.gz: f744006470a25bff85e10026f750e1cada2b49a19809cc2279208f9ec10ac86bfacf38dbbe32bd839f655475d0dde773d1c219b584c4a5cccc7c911f570c10bd
7
+ data.tar.gz: 5626ac149515ef764e63797cceed133c87d6032f22100a4940573a42a0b1ae483cbebc8a26596cc77494c4420e94e55d08e74b44ebe6fb83cd7f18e1a502d731
@@ -5,20 +5,18 @@ jobs:
5
5
  strategy:
6
6
  matrix:
7
7
  pg:
8
- - 11
9
- - 12
10
8
  - 13
11
9
  - 14
12
10
  - 15
13
11
  - 16
14
12
  ruby:
15
- - "3.0"
16
- - "3.1"
17
13
  - "3.2"
14
+ - "3.3"
15
+ - "3.4"
18
16
  gemfile:
19
- - rails_6.1
20
- - rails_7.0
21
17
  - rails_7.1
18
+ - rails_7.2
19
+ - rails_8.0
22
20
  name: PostgreSQL ${{ matrix.pg }} - Ruby ${{ matrix.ruby }} - ${{ matrix.gemfile }}
23
21
  runs-on: ubuntu-latest
24
22
  env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
@@ -27,7 +25,7 @@ jobs:
27
25
  steps:
28
26
  - uses: actions/checkout@v3
29
27
  - name: Build postgres image and start the container
30
- run: docker-compose up -d --build
28
+ run: docker compose up -d --build
31
29
  env:
32
30
  PGVERSION: ${{ matrix.pg }}
33
31
  - name: Setup Ruby using .ruby-version file
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-3.0
1
+ ruby-3.4.2
data/Appraisals CHANGED
@@ -1,11 +1,11 @@
1
- appraise "rails-6.1" do
2
- gem "rails", "6.1.7.6"
1
+ appraise "rails-7.1" do
2
+ gem "rails", "~> 7.1.0"
3
3
  end
4
4
 
5
- appraise "rails-7.0" do
6
- gem "rails", "7.0.8"
5
+ appraise "rails-7.2" do
6
+ gem "rails", "~> 7.2.0"
7
7
  end
8
8
 
9
- appraise "rails-7.1" do
10
- gem "rails", "7.1.0"
9
+ appraise "rails-8.0" do
10
+ gem "rails", "~> 8.0.0"
11
11
  end
data/Gemfile CHANGED
@@ -4,4 +4,3 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in pg_ha_migrations.gemspec
6
6
  gemspec
7
-
data/README.md CHANGED
@@ -30,7 +30,36 @@ Or install it yourself as:
30
30
 
31
31
  $ gem install pg_ha_migrations
32
32
 
33
- ## Usage
33
+ ## Migration Safety
34
+
35
+ There are two major classes of concerns we try to handle in the API:
36
+
37
+ - Database safety (e.g., long-held locks)
38
+ - Application safety (e.g., dropping columns the app uses)
39
+
40
+ ### Migration Method Renaming
41
+
42
+ We rename migration methods with prefixes to explicitly denote their safety level:
43
+
44
+ - `safe_*`: These methods check for both application and database safety concerns, prefer concurrent operations where available, set low lock timeouts where appropriate, and decompose operations into multiple safe steps.
45
+ - `unsafe_*`: Using these methods is a signal that the DDL operation is not necessarily safe for a running application. They include basic safety features like safe lock acquisition and dependent object checking, but otherwise dispatch directly to the native ActiveRecord migration method.
46
+ - `raw_*`: These methods are a direct dispatch to the native ActiveRecord migration method.
47
+
48
+ Calling the original migration methods without a prefix will raise an error.
49
+
50
+ The API is designed to be explicit yet remain flexible. There may be situations where invoking the `unsafe_*` method is preferred (or the only option available for definitionally unsafe operations).
51
+
52
+ While `unsafe_*` methods were historically (before 2.0) pure wrappers for invoking the native ActiveRecord migration method, there is a class of problems that we can't handle easily without breaking that design rule a bit. For example, dropping a column is unsafe from an application perspective, so we make the application safety concerns explicit by using an `unsafe_` prefix. Using `unsafe_remove_column` calls out the need to audit the application to confirm the migration won't break the application. Because there are no safe alternatives we don't define a `safe_remove_column` analogue. However there are still conditions we'd like to assert before dropping a column. For example, dropping an unused column that's used in one or more indexes may be safe from an application perspective, but the cascading drop of the index won't use a `CONCURRENT` operation to drop the dependent indexes and is therefore unsafe from a database perspective.
53
+
54
+ For `unsafe_*` migration methods which support checks of this type you can bypass the checks by passing an `:allow_dependent_objects` key in the method's `options` hash containing an array of dependent object types you'd like to allow. These checks will run by default, but you can opt-out by setting `config.check_for_dependent_objects = false` [in your configuration initializer](#configuration).
55
+
56
+ ### Disallowed Migration Methods
57
+
58
+ We disallow the use of `unsafe_change_table`, as the equivalent operation can be composed with explicit `safe_*` / `unsafe_*` methods. If you _must_ use `change_table`, it is still available as `raw_change_table`.
59
+
60
+ ### Migration Method Arguments
61
+
62
+ We believe the `force: true` option to ActiveRecord's `create_table` method is always unsafe because it's not possible to denote exactly how the current state will change. Therefore we disallow using `force: true` even when calling `unsafe_create_table`. This option is enabled by default, but you can opt-out by setting `config.allow_force_create_table = true` [in your configuration initializer](#configuration).
34
63
 
35
64
  ### Rollback
36
65
 
@@ -46,40 +75,36 @@ end
46
75
 
47
76
  and never use `def change`. We believe that this is the only safe approach in production environments. For development environments we iterate by recreating the database from scratch every time we make a change.
48
77
 
49
- ### Migrations
78
+ ### Transactional DDL
50
79
 
51
- There are two major classes of concerns we try to handle in the API:
80
+ Individual DDL statements in PostgreSQL are transactional by default (as are all Postgres statements). Concurrent index creation and removal are two exceptions: these utility commands manage their own transaction state (and each uses multiple transactions to achieve the desired concurrency).
52
81
 
53
- - Database safety (e.g., long-held locks)
54
- - Application safety (e.g., dropping columns the app uses)
82
+ We [disable ActiveRecord's DDL transactions](./lib/pg_ha_migrations/hacks/disable_ddl_transaction.rb) (which wrap the entire migration file in a transaction) by default for the following reasons:
55
83
 
56
- We rename migration methods with prefixes denoting their safety level:
84
+ * [Running multiple DDL statements inside a transaction acquires exclusive locks on all of the modified objects](https://medium.com/paypal-tech/postgresql-at-scale-database-schema-changes-without-downtime-20d3749ed680#cc22).
85
+ * Acquired locks are held until the end of the transaction.
86
+ * Multiple locks creates the possibility of deadlocks.
87
+ * Increased exposure to long waits:
88
+ * Each newly acquired lock has its own timeout applied (so total lock time is additive).
89
+ * [Safe lock acquisition](#safely_acquire_lock_for_table) (which is used in each migration method where locks will be acquired) can issue multiple lock attempts on lock timeouts (with sleep delays between attempts).
57
90
 
58
- - `safe_*`: These methods check for both application and database safety concerns, prefer concurrent operations where available, set low lock timeouts where appropriate, and decompose operations into multiple safe steps.
59
- - `unsafe_*`: These methods are generally a direct dispatch to the native ActiveRecord migration method.
60
-
61
- Calling the original migration methods without a prefix will raise an error.
91
+ Because of the above issues attempting to re-enable transaction migrations forfeits many of the safety guarantees this library provides and may even break certain functionally. If you'd like to experiment with it anyway you can re-enable transactional migrations by adding `self.disable_ddl_transaction = false` to your migration class definition.
62
92
 
63
- The API is designed to be explicit yet remain flexible. There may be situations where invoking the `unsafe_*` method is preferred (or the only option available for definitionally unsafe operations).
64
-
65
- While `unsafe_*` methods were historically (through 1.0) pure wrappers for invoking the native ActiveRecord migration method, there is a class of problems that we can't handle easily without breaking that design rule a bit. For example, dropping a column is unsafe from an application perspective, so we make the application safety concerns explicit by using an `unsafe_` prefix. Using `unsafe_remove_column` calls out the need to audit the application to confirm the migration won't break the application. Because there are no safe alternatives we don't define a `safe_remove_column` analogue. However there are still conditions we'd like to assert before dropping a column. For example, dropping an unused column that's used in one or more indexes may be safe from an application perspective, but the cascading drop of the index won't use a `CONCURRENT` operation to drop the dependent indexes and is therefore unsafe from a database perspective.
66
-
67
- When `unsafe_*` migration methods support checks of this type you can bypass the checks by passing an `:allow_dependent_objects` key in the method's `options` hash containing an array of dependent object types you'd like to allow. Until 2.0 none of these checks will run by default, but you can opt-in by setting `config.check_for_dependent_objects = true` [in your configuration initializer](#configuration).
68
-
69
- Similarly we believe the `force: true` option to ActiveRecord's `create_table` method is always unsafe, and therefore we disallow it even when calling `unsafe_create_table`. This option won't be enabled by default until 2.0, but you can opt-in by setting `config.allow_force_create_table = false` [in your configuration initializer](#configuration).
93
+ ## Usage
70
94
 
71
- [Running multiple DDL statements inside a transaction acquires exclusive locks on all of the modified objects](https://medium.com/paypal-tech/postgresql-at-scale-database-schema-changes-without-downtime-20d3749ed680#cc22). For that reason, this gem [disables DDL transactions](./lib/pg_ha_migrations/hacks/disable_ddl_transaction.rb) by default. You can change this by resetting `ActiveRecord::Migration.disable_ddl_transaction` in your application.
95
+ ### Unsupported ActiveRecord Features
72
96
 
73
97
  The following functionality is currently unsupported:
74
98
 
75
- - Rollbacks
99
+ - [Rollback methods in migrations](#rollback)
76
100
  - Generators
77
101
  - schema.rb
78
102
 
79
- Compatibility notes:
103
+ ### Compatibility Notes
80
104
 
81
- - While some features may work with other versions, this gem is currently tested against PostgreSQL 11+ and Partman 4.x
82
- - There is a [bug](https://github.com/rails/rails/pull/41490) in early versions of Rails 6.1 when using `algorithm: :concurrently`. To add / remove indexes concurrently, please upgrade to at least Rails 6.1.4.
105
+ - While some features may work with other versions, this gem is currently tested against PostgreSQL 13+ and Partman 4.x
106
+
107
+ ### Migration Methods
83
108
 
84
109
  #### safe\_create\_table
85
110
 
@@ -153,7 +178,7 @@ safe_change_column_default :table, :column, -> { "NOW()" }
153
178
  safe_change_column_default :table, :column, -> { "'NOW()'" }
154
179
  ```
155
180
 
156
- Note: On Postgres 11+ adding a column with a constant default value does not rewrite or scan the table (under a lock or otherwise). In that case a migration adding a column with a default should do so in a single operation rather than the two-step `safe_add_column` followed by `safe_change_column_default`. We enforce this best practice with the error `PgHaMigrations::BestPracticeError`, but if your prefer otherwise (or are running in a mixed Postgres version environment), you may opt out by setting `config.prefer_single_step_column_addition_with_default = true` [in your configuration initializer](#configuration).
181
+ Note: On Postgres 11+ adding a column with a constant default value does not rewrite or scan the table (under a lock or otherwise). In that case a migration adding a column with a default should do so in a single operation rather than the two-step `safe_add_column` followed by `safe_change_column_default`. We enforce this best practice with the error `PgHaMigrations::BestPracticeError`, but if your prefer otherwise (or are running in a mixed Postgres version environment), you may opt out by setting `config.prefer_single_step_column_addition_with_default = false` [in your configuration initializer](#configuration).
157
182
 
158
183
  #### safe\_make\_column\_nullable
159
184
 
@@ -162,6 +187,19 @@ Safely make the column nullable.
162
187
  ```ruby
163
188
  safe_make_column_nullable :table, :column
164
189
  ```
190
+ #### safe\_make\_column\_not\_nullable
191
+
192
+ Safely make the column not nullable - adds a temporary constraint and uses that constraint to validate no values are null before altering the column, then removes the temporary constraint.
193
+
194
+ ```ruby
195
+ safe_make_column_not_nullable :table, :column
196
+ ```
197
+
198
+ > **Note:**
199
+ > - This method performs a full table scan to validate that no NULL values exist in the column. While no exclusive lock is held for this scan, on large tables the scan may take a long time.
200
+ > - The method runs multiple DDL statements non-transactionally. Validating the constraint can fail. In such cases an exception will be raised, and an INVALID constraint will be left on the table.
201
+
202
+ If you want to avoid a full table scan and have already added and validated a suitable CHECK constraint, consider using [`safe_make_column_not_nullable_from_check_constraint`](#safe_make_column_not_nullable_from_check_constraint) instead.
165
203
 
166
204
  #### unsafe\_make\_column\_not\_nullable
167
205
 
@@ -171,6 +209,23 @@ Unsafely make a column not nullable.
171
209
  unsafe_make_column_not_nullable :table, :column
172
210
  ```
173
211
 
212
+ #### safe\_make\_column\_not\_nullable\_from\_check\_constraint
213
+
214
+ Variant of `safe_make_column_not_nullable` that safely makes a column NOT NULL using an existing validated CHECK constraint that enforces non-null values for the column. This method is expected to always be fast because it avoids a full table scan.
215
+
216
+ ```ruby
217
+ safe_make_column_not_nullable_from_check_constraint :table, :column, constraint_name: :constraint_name
218
+ ```
219
+
220
+ - `constraint_name` (required): The name of a validated CHECK constraint that enforces `column IS NOT NULL`.
221
+ - `drop_constraint:` (optional, default: true): Whether to drop the constraint after making the column NOT NULL.
222
+
223
+ You should use [`safe_make_column_not_nullable`](#safe_make_column_not_nullable) when neither a CHECK constraint or a NOT NULL constraint exists already. You should use this method when you already have an equivalent CHECK constraint on the table.
224
+
225
+ This method will raise an error if the constraint does not exist, is not validated, or does not strictly enforce non-null values for the column.
226
+
227
+ > **Note:** We do not attempt to catch all possible proofs of `column IS NOT NULL` by means of an existing constraint; only a constraint with the exact definition `column IS NOT NULL` will be recognized.
228
+
174
229
  #### safe\_add\_index\_on\_empty\_table
175
230
 
176
231
  Safely add an index on a table with zero rows. This will raise an error if the table contains data.
@@ -368,23 +423,7 @@ safe_partman_create_parent :table,
368
423
  premake: 10,
369
424
  start_partition: Time.current + 1.month,
370
425
  infinite_time_partitions: false,
371
- inherit_privileges: false
372
- ```
373
-
374
- #### unsafe\_partman\_create\_parent
375
-
376
- We have chosen to flag the use of `retention` and `retention_keep_table` as an unsafe operation.
377
- While we recognize that these options are useful, we think they fit in the same category as `drop_table` and `rename_table`, and are therefore unsafe from an application perspective.
378
- If you wish to define these options, you must use this method.
379
-
380
- ```ruby
381
- safe_create_partitioned_table :table, type: :range, partition_key: :created_at do |t|
382
- t.timestamps null: false
383
- end
384
-
385
- unsafe_partman_create_parent :table,
386
- partition_key: :created_at,
387
- interval: "weekly",
426
+ inherit_privileges: false,
388
427
  retention: "60 days",
389
428
  retention_keep_table: false
390
429
  ```
@@ -392,7 +431,7 @@ unsafe_partman_create_parent :table,
392
431
  #### safe\_partman\_update\_config
393
432
 
394
433
  There are some partitioning options that cannot be set in the call to `create_parent` and are only available in the `part_config` table.
395
- As mentioned previously, you can specify these args in the call to `safe_partman_create_parent` or `unsafe_partman_create_parent` which will be delegated to this method.
434
+ As mentioned previously, you can specify these args in the call to `safe_partman_create_parent` which will be delegated to this method.
396
435
  Calling this method directly will be useful if you need to modify your partitioned table after the fact.
397
436
 
398
437
  Allowed keyword args:
@@ -414,8 +453,9 @@ safe_partman_update_config :table,
414
453
 
415
454
  #### unsafe\_partman\_update\_config
416
455
 
417
- As with creating a partman parent table, we have chosen to flag the use of `retention` and `retention_keep_table` as an unsafe operation.
418
- If you wish to define these options, you must use this method.
456
+ We have chosen to flag the use of `retention` and `retention_keep_table` as an unsafe operation.
457
+ While we recognize that these options are useful, changing these values fits in the same category as `drop_table` and `rename_table`, and is therefore unsafe from an application perspective.
458
+ If you wish to change these options, you must use this method.
419
459
 
420
460
  ```ruby
421
461
  unsafe_partman_update_config :table,
@@ -435,7 +475,13 @@ safe_partman_reapply_privileges :table
435
475
 
436
476
  #### safely\_acquire\_lock\_for\_table
437
477
 
438
- Safely acquire an access exclusive lock for a table.
478
+ Acquires a lock (in `ACCESS EXCLUSIVE` mode by default) on a table using the following algorithm:
479
+
480
+ 1. Verify that no long-running queries are using the table.
481
+ - If long-running queries are currently using the table, sleep `PgHaMigrations::LOCK_TIMEOUT_SECONDS` and check again.
482
+ 2. If no long-running queries are currently using the table, optimistically attempt to lock the table (with a timeout of `PgHaMigrations::LOCK_TIMEOUT_SECONDS`).
483
+ - If the lock is not acquired, sleep `PgHaMigrations::LOCK_FAILURE_RETRY_DELAY_MULTLIPLIER * PgHaMigrations::LOCK_TIMEOUT_SECONDS`, and start again at step 1.
484
+ 3. If the lock is acquired, proceed to run the given block.
439
485
 
440
486
  ```ruby
441
487
  safely_acquire_lock_for_table(:table) do
@@ -443,7 +489,7 @@ safely_acquire_lock_for_table(:table) do
443
489
  end
444
490
  ```
445
491
 
446
- Safely acquire a lock for a table in a different mode.
492
+ Safely acquire a lock on a table in `SHARE` mode.
447
493
 
448
494
  ```ruby
449
495
  safely_acquire_lock_for_table(:table, mode: :share) do
@@ -451,10 +497,18 @@ safely_acquire_lock_for_table(:table, mode: :share) do
451
497
  end
452
498
  ```
453
499
 
500
+ Safely acquire a lock on multiple tables in `EXCLUSIVE` mode.
501
+
502
+ ```ruby
503
+ safely_acquire_lock_for_table(:table_a, :table_b, mode: :exclusive) do
504
+ ...
505
+ end
506
+ ```
507
+
454
508
  Note:
455
509
 
456
- We enforce that only one table (or a table and its partitions) can be locked at a time.
457
- Attempting to acquire a nested lock on a different table will result in an error.
510
+ We enforce that only one set of tables can be locked at a time.
511
+ Attempting to acquire a nested lock on a different set of tables will result in an error.
458
512
 
459
513
  #### adjust\_lock\_timeout
460
514
 
@@ -513,9 +567,9 @@ end
513
567
  #### Available options
514
568
 
515
569
  - `disable_default_migration_methods`: If true, the default implementations of DDL changes in `ActiveRecord::Migration` and the PostgreSQL adapter will be overridden by implementations that raise a `PgHaMigrations::UnsafeMigrationError`. Default: `true`
516
- - `check_for_dependent_objects`: If true, some `unsafe_*` migration methods will raise a `PgHaMigrations::UnsafeMigrationError` if any dependent objects exist. Default: `false`
517
- - `prefer_single_step_column_addition_with_default`: If true, raise an error when adding a column and separately setting a constant default value for that column in the same migration. Default: `false`
518
- - `allow_force_create_table`: If false, the `force: true` option to ActiveRecord's `create_table` method is disallowed. Default: `true`
570
+ - `check_for_dependent_objects`: If true, some `unsafe_*` migration methods will raise a `PgHaMigrations::UnsafeMigrationError` if any dependent objects exist. Default: `true`
571
+ - `prefer_single_step_column_addition_with_default`: If true, raise an error when adding a column and separately setting a constant default value for that column in the same migration. Default: `true`
572
+ - `allow_force_create_table`: If false, the `force: true` option to ActiveRecord's `create_table` method is disallowed. Default: `false`
519
573
  - `infer_primary_key_on_partitioned_tables`: If true, the primary key for partitioned tables will be inferred on PostgreSQL 11+ databases (identifier column + partition key columns). Default: `true`
520
574
 
521
575
  ### Rake Tasks
@@ -542,6 +596,10 @@ Rake::Task["pg_ha_migrations:check_blocking_database_transactions"].enhance ["db
542
596
 
543
597
  After checking out the repo, run `bin/setup` to install dependencies and start a postgres docker container. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. This project uses Appraisal to test against multiple versions of ActiveRecord; you can run the tests against all supported version with `bundle exec appraisal rspec`.
544
598
 
599
+ > **Warning**: If you rebuild the Docker container _without_ using `docker-compose build` (or the `--build` flag), it will not respect the `PGVERSION` environment variable that you've set if image layers from a different version exist. The Dockerfile uses a build-time argument that's only evaluated during the initial build. To change the Postgres version, you should explicitly provide the build argument: `docker-compose build --build-arg PGVERSION=15`. **Using `bin/setup` handles this for you.**
600
+
601
+ > **Warning**: The Postgres Dockerfile automatically creates an anonymous volume for the data directory. When changing the specified `PGVERSION` environment variable this volume must be reset using `--renew-anon-volumes` or booting Postgres will fail. **Using `bin/setup` handles this for you.**
602
+
545
603
  Running tests will automatically create a test database in the locally running Postgres server. You can find the connection parameters in `spec/spec_helper.rb`, but setting the environment variables `PGHOST`, `PGPORT`, `PGUSER`, and `PGPASSWORD` will override the defaults.
546
604
 
547
605
  To install this gem onto your local machine, run `bundle exec rake install`.
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
  require "appraisal"
4
+ # In Rails 6 this isn't required in the right order and worked by accident; fixed in rails@0f5e7a66143
5
+ require "logger"
4
6
  require_relative File.join("lib", "pg_ha_migrations")
5
7
 
6
8
  RSpec::Core::RakeTask.new(:spec)
data/bin/setup CHANGED
@@ -9,4 +9,6 @@ bundle exec appraisal install
9
9
  # Do any other automated setup that you need to do here
10
10
 
11
11
  # Launch a blank postgres image with partman for testing
12
- docker-compose up -d --build
12
+ # Because the Postgres image volumizes by default, we have to reset the volumes
13
+ # or launching the setup with different PGVERSION env vars will fail.
14
+ docker compose up -d --build --renew-anon-volumes
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "7.1.0"
5
+ gem "rails", "~> 7.1.0"
6
6
 
7
7
  gemspec path: "../"
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "6.1.7.6"
5
+ gem "rails", "~> 7.2.0"
6
6
 
7
7
  gemspec path: "../"
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "7.0.8"
5
+ gem "rails", "~> 8.0.0"
6
6
 
7
7
  gemspec path: "../"
@@ -1,7 +1,7 @@
1
1
  require "active_record/migration/compatibility"
2
2
 
3
3
  module PgHaMigrations::AllowedVersions
4
- ALLOWED_VERSIONS = [4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1].map do |v|
4
+ ALLOWED_VERSIONS = [4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1, 7.2, 8.0].map do |v|
5
5
  begin
6
6
  ActiveRecord::Migration[v]
7
7
  rescue ArgumentError
@@ -0,0 +1 @@
1
+ PgHaMigrations::CheckConstraint = Struct.new(:name, :definition, :validated)
@@ -93,6 +93,18 @@ module PgHaMigrations
93
93
  MODE_CONFLICTS.keys.index(mode) <=> MODE_CONFLICTS.keys.index(other.mode)
94
94
  end
95
95
 
96
+ def eql?(other)
97
+ other.is_a?(LockMode) && mode == other.mode
98
+ end
99
+
100
+ def ==(other)
101
+ eql?(other)
102
+ end
103
+
104
+ def hash
105
+ mode.hash
106
+ end
107
+
96
108
  def conflicts_with?(other)
97
109
  MODE_CONFLICTS[mode].include?(other.mode)
98
110
  end
@@ -1,4 +1,8 @@
1
1
  module PgHaMigrations
2
+ # This object represents a pointer to an actual relation in Postgres.
3
+ # The mode attribute is optional metadata which can represent a lock
4
+ # that has already been acquired or a potential lock that we are
5
+ # looking to acquire.
2
6
  Relation = Struct.new(:name, :schema, :mode) do
3
7
  def self.connection
4
8
  ActiveRecord::Base.connection
@@ -13,12 +17,6 @@ module PgHaMigrations
13
17
  self.mode = LockMode.new(mode) if mode.present?
14
18
  end
15
19
 
16
- def conflicts_with?(other)
17
- self == other && (
18
- mode.nil? || other.mode.nil? || mode.conflicts_with?(other.mode)
19
- )
20
- end
21
-
22
20
  def fully_qualified_name
23
21
  @fully_qualified_name ||= [
24
22
  PG::Connection.quote_ident(schema),
@@ -30,8 +28,26 @@ module PgHaMigrations
30
28
  name.present? && schema.present?
31
29
  end
32
30
 
31
+ def conflicts_with?(other)
32
+ eql?(other) && (
33
+ mode.nil? || other.mode.nil? || mode.conflicts_with?(other.mode)
34
+ )
35
+ end
36
+
37
+ # Some code paths need to compare lock modes, while others just need
38
+ # to determine if the relation is the same object in Postgres, so
39
+ # equality here is simply looking at the relation name / schema.
40
+ # To also compare lock modes, #conflicts_with? is used.
41
+ def eql?(other)
42
+ other.is_a?(Relation) && hash == other.hash
43
+ end
44
+
33
45
  def ==(other)
34
- other.is_a?(Relation) && name == other.name && schema == other.schema
46
+ eql?(other)
47
+ end
48
+
49
+ def hash
50
+ [name, schema].hash
35
51
  end
36
52
  end
37
53
 
@@ -97,6 +113,18 @@ module PgHaMigrations
97
113
  tables
98
114
  end
99
115
 
116
+ def check_constraints
117
+ connection.structs_from_sql(PgHaMigrations::CheckConstraint, <<~SQL)
118
+ SELECT conname AS name, pg_get_constraintdef(pg_constraint.oid) AS definition, convalidated AS validated
119
+ FROM pg_constraint, pg_class, pg_namespace
120
+ WHERE pg_class.oid = pg_constraint.conrelid
121
+ AND pg_class.relnamespace = pg_namespace.oid
122
+ AND pg_class.relname = #{connection.quote(name)}
123
+ AND pg_namespace.nspname = #{connection.quote(schema)}
124
+ AND pg_constraint.contype = 'c' -- 'c' stands for check constraints
125
+ SQL
126
+ end
127
+
100
128
  def has_rows?
101
129
  connection.select_value("SELECT EXISTS (SELECT 1 FROM #{fully_qualified_name} LIMIT 1)")
102
130
  end
@@ -152,4 +180,45 @@ module PgHaMigrations
152
180
  SQL
153
181
  end
154
182
  end
183
+
184
+ class TableCollection
185
+ include Enumerable
186
+
187
+ attr_reader :raw_set
188
+
189
+ delegate :each, to: :raw_set
190
+ delegate :mode, to: :first
191
+
192
+ def self.from_table_names(tables, mode=nil)
193
+ new(tables) { |table| Table.from_table_name(table, mode) }
194
+ end
195
+
196
+ def initialize(tables, &blk)
197
+ @raw_set = tables.map(&blk).to_set
198
+
199
+ if raw_set.empty?
200
+ raise ArgumentError, "Expected a non-empty list of tables"
201
+ end
202
+
203
+ if raw_set.uniq(&:mode).size > 1
204
+ raise ArgumentError, "Expected all tables in collection to have the same lock mode"
205
+ end
206
+ end
207
+
208
+ def subset?(other)
209
+ raw_set.subset?(other.raw_set)
210
+ end
211
+
212
+ def to_sql
213
+ map(&:fully_qualified_name).join(", ")
214
+ end
215
+
216
+ def with_partitions
217
+ tables = flat_map do |table|
218
+ table.partitions(include_sub_partitions: true, include_self: true)
219
+ end
220
+
221
+ self.class.new(tables)
222
+ end
223
+ end
155
224
  end