pg_ha_migrations 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae8983eec2507ce164936c65c32a7c296d0d3ef338ada2d90d68f37c572d00f7
4
- data.tar.gz: 9c07cb77837a23a603e713e94cdae71f26acce82dbc2332d6fd77260c54d7c8c
3
+ metadata.gz: f6bc07b7a31ace17d8b98c1a4cd2d6c23086e81da40cc3ef6c27006effa29114
4
+ data.tar.gz: 0dafb9db37d66f7723cb96d38cbd44cc3747f61f8c2c7695f51a64559ff3b3fb
5
5
  SHA512:
6
- metadata.gz: 247dd7ac257a208ef985d86b94710fbf41c0e622d02ce1acecb46d490ab17adb6aa080060d6e48ddf56e8ef4a5289121dcf4edd41e83f3bb7418ce20cfc91d4c
7
- data.tar.gz: 0f0fb4ac98e6732d7d020a55e0f2608e256dbf34b4fe669ccfcde7e1878df43b3ccd8e153af61e6681d5eec62b8269ea435303376136207af54341d8bb19da41
6
+ metadata.gz: 973a40f102ede133528abe11ff9bbe89104d0ca47f2b2e8d36436ab241adddd5c3cc575d57e5bc33aaf37006bd5046df0cad530671b515b5ae6b64b904870b52
7
+ data.tar.gz: c386d4f0ee49f49ef31514556fb93a7554356a440899e3641a91071b0cdebbe6900f5c7e1df20803f2c6eb8a4259b6c1039676523b4a18b551ef30ee9e8c1cc5
@@ -0,0 +1,41 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ strategy:
6
+ matrix:
7
+ pg:
8
+ - 9.6
9
+ - 10
10
+ - 11
11
+ - 12
12
+ gemfile:
13
+ - rails_5.0
14
+ - rails_5.1
15
+ - rails_5.2
16
+ - rails_6.0
17
+ name: PostgreSQL ${{ matrix.pg }}
18
+ runs-on: ubuntu-latest
19
+ env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
20
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
21
+ ImageOS: ubuntu20
22
+ services:
23
+ postgresql:
24
+ image: postgres:${{ matrix.pg }}
25
+ env:
26
+ POSTGRES_PASSWORD: postgres
27
+ # Set health checks to wait until postgres has started
28
+ options: >-
29
+ --health-cmd pg_isready
30
+ --health-interval 10s
31
+ --health-timeout 5s
32
+ --health-retries 5
33
+ ports:
34
+ - 5432:5432
35
+ steps:
36
+ - uses: actions/checkout@v2
37
+ - name: Setup Ruby using .ruby-version file
38
+ uses: ruby/setup-ruby@v1
39
+ with:
40
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
41
+ - run: bundle exec rake spec
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.5.7
1
+ ruby-2.7
data/.travis.yml CHANGED
@@ -2,22 +2,21 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.5
5
- global:
6
- env:
7
- PGPORT: 5433
8
5
  env:
9
- - PGVERSION: "9.6"
10
- - PGVERSION: "10"
11
- - PGVERSION: "11"
12
- - PGVERSION: "12"
6
+ jobs:
7
+ - PGVERSION: "9.6"
8
+ - PGVERSION: "10"
9
+ - PGVERSION: "11"
10
+ - PGVERSION: "12"
13
11
  services:
14
12
  - postgresql
15
13
  before_install:
16
- - sudo service postgresql stop
14
+ - "for CLUSTER_VERSION in $(pg_lsclusters -h | cut -d' ' -f1); do sudo pg_dropcluster $CLUSTER_VERSION main --stop || true; done"
17
15
  - sudo apt-get update
18
16
  - sudo apt-get -y install postgresql-$PGVERSION postgresql-client-$PGVERSION postgresql-server-dev-$PGVERSION postgresql-client-common postgresql-common
19
- - if [ $PGVERSION != '9.6' ]; then sudo cp /etc/postgresql/{9.6,$PGVERSION}/main/pg_hba.conf; fi
20
- - sudo service postgresql restart $PGVERSION
17
+ - sudo pg_dropcluster $PGVERSION main --stop || true
18
+ - sudo pg_createcluster $PGVERSION main -D /var/ramfs/postgresql/11/main -- --auth=trust
19
+ - sudo pg_ctlcluster start $PGVERSION main
21
20
  - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
22
21
  - gem install bundler -v 1.15.4
23
22
  gemfile:
data/README.md CHANGED
@@ -55,7 +55,7 @@ There are two major classes of concerns we try to handle in the API:
55
55
 
56
56
  We rename migration methods with prefixes denoting their safety level:
57
57
 
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.
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
59
  - `unsafe_*`: These methods are generally a direct dispatch to the native ActiveRecord migration method.
60
60
 
61
61
  Calling the original migration methods without a prefix will raise an error.
@@ -106,6 +106,18 @@ Safely add a new enum value.
106
106
  safe_add_enum_value :enum, "value"
107
107
  ```
108
108
 
109
+ #### unsafe\_rename\_enum\_value
110
+
111
+ Unsafely change the value of an enum type entry.
112
+
113
+ ```ruby
114
+ unsafe_rename_enum_value(:enum, "old_value", "new_value")
115
+ ```
116
+
117
+ Note:
118
+
119
+ Changing an enum value does not issue any long-running scans or acquire locks on usages of the enum type. Therefore multiple queries within a transaction concurrent with the change may see both the old and new values. To highlight these potential pitfalls no `safe_rename_enum_value` equivalent exists. Before modifying an enum type entry you should verify that no concurrently executing queries will attempt to write the old value and that read queries understand the new value.
120
+
109
121
  #### safe\_add\_column
110
122
 
111
123
  Safely add a column.
@@ -283,7 +295,7 @@ Rake::Task["pg_ha_migrations:check_blocking_database_transactions"].enhance ["db
283
295
 
284
296
  ## Development
285
297
 
286
- After checking out the repo, run `bin/setup` to install dependencies and start a postgres docker container. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
298
+ 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`.
287
299
 
288
300
  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.
289
301
 
data/bin/setup CHANGED
@@ -4,6 +4,7 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
+ bundle exec appraisal install
7
8
 
8
9
  # Do any other automated setup that you need to do here
9
10
 
@@ -25,9 +25,21 @@ module PgHaMigrations::SafeStatements
25
25
  unsafe_execute("ALTER TYPE #{PG::Connection.quote_ident(name.to_s)} ADD VALUE '#{PG::Connection.escape_string(value)}'")
26
26
  end
27
27
 
28
+ def unsafe_rename_enum_value(name, old_value, new_value)
29
+ if ActiveRecord::Base.connection.postgresql_version < 10_00_00
30
+ raise PgHaMigrations::InvalidMigrationError, "Renaming an enum value is not supported on Postgres databases before version 10"
31
+ end
32
+
33
+ unsafe_execute("ALTER TYPE #{PG::Connection.quote_ident(name.to_s)} RENAME VALUE '#{PG::Connection.escape_string(old_value)}' TO '#{PG::Connection.escape_string(new_value)}'")
34
+ end
35
+
28
36
  def safe_add_column(table, column, type, options = {})
29
- if options.has_key? :default
30
- raise PgHaMigrations::UnsafeMigrationError.new(":default is NOT SAFE! Use safe_change_column_default afterwards then backfill the data to prevent locking the table")
37
+ if options.has_key?(:default)
38
+ if ActiveRecord::Base.connection.postgresql_version < 11_00_00
39
+ raise PgHaMigrations::UnsafeMigrationError.new(":default is NOT SAFE! Use safe_change_column_default afterwards then backfill the data to prevent locking the table")
40
+ elsif options[:default].is_a?(Proc) || (options[:default].is_a?(String) && !([:string, :text, :binary].include?(type.to_sym) || _type_is_enum(type)))
41
+ raise PgHaMigrations::UnsafeMigrationError.new(":default is not safe if the default value is volatile. Use safe_change_column_default afterwards then backfill the data to prevent locking the table")
42
+ end
31
43
  end
32
44
  if options[:null] == false
33
45
  raise PgHaMigrations::UnsafeMigrationError.new(":null => false is NOT SAFE if the table has data! If you _really_ want to do this, use unsafe_make_column_not_nullable")
@@ -201,6 +213,10 @@ module PgHaMigrations::SafeStatements
201
213
  raise PgHaMigrations::UnsupportedAdapter, "This gem only works with the #{expected_adapter} adapter, found #{actual_adapter} instead" unless actual_adapter == expected_adapter
202
214
  end
203
215
 
216
+ def _type_is_enum(type)
217
+ ActiveRecord::Base.connection.select_values("SELECT typname FROM pg_type JOIN pg_enum ON pg_type.oid = pg_enum.enumtypid").include?(type.to_s)
218
+ end
219
+
204
220
  def migrate(direction)
205
221
  if respond_to?(:change)
206
222
  raise PgHaMigrations::UnsupportedMigrationError, "Tracking changes for automated rollback is not supported; use explicit #up instead."
@@ -39,7 +39,6 @@ module PgHaMigrations::UnsafeStatements
39
39
  delegate_unsafe_method_to_migration_base_class :change_column
40
40
  delegate_unsafe_method_to_migration_base_class :change_column_default
41
41
  delegate_unsafe_method_to_migration_base_class :remove_column
42
- delegate_unsafe_method_to_migration_base_class :add_index
43
42
  delegate_unsafe_method_to_migration_base_class :execute
44
43
  delegate_unsafe_method_to_migration_base_class :remove_index
45
44
  delegate_unsafe_method_to_migration_base_class :add_foreign_key
@@ -69,6 +68,16 @@ module PgHaMigrations::UnsafeStatements
69
68
  execute_ancestor_statement(:create_table, table, options, &block)
70
69
  end
71
70
 
71
+ def unsafe_add_index(table, column_names, options = {})
72
+ if ((ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 2) || ActiveRecord::VERSION::MAJOR > 5) &&
73
+ column_names.is_a?(String) && /\W/.match?(column_names) && options.key?(:opclass)
74
+ raise PgHaMigrations::InvalidMigrationError, "ActiveRecord drops the :opclass option when supplying a string containing an expression or list of columns; instead either supply an array of columns or include the opclass in the string for each column"
75
+ end
76
+
77
+ execute_ancestor_statement(:add_index, table, column_names, options)
78
+ end
79
+
80
+
72
81
  def execute_ancestor_statement(method_name, *args, &block)
73
82
  # Dispatching here is a bit complicated: we need to execute the method
74
83
  # belonging to the first member of the inheritance chain (besides
@@ -1,3 +1,3 @@
1
1
  module PgHaMigrations
2
- VERSION = "1.2.3"
2
+ VERSION = "1.2.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_ha_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - celeen
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: exe
16
16
  cert_chain: []
17
- date: 2020-03-12 00:00:00.000000000 Z
17
+ date: 2021-03-05 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: rake
@@ -156,6 +156,7 @@ executables: []
156
156
  extensions: []
157
157
  extra_rdoc_files: []
158
158
  files:
159
+ - ".github/workflows/ci.yml"
159
160
  - ".gitignore"
160
161
  - ".pryrc"
161
162
  - ".rspec"
@@ -206,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
207
  - !ruby/object:Gem::Version
207
208
  version: '0'
208
209
  requirements: []
209
- rubygems_version: 3.0.8
210
+ rubygems_version: 3.1.4
210
211
  signing_key:
211
212
  specification_version: 4
212
213
  summary: Enforces DDL/migration safety in Ruby on Rails project with an emphasis on