nandi 3.2.0 → 3.3.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: '068eff75d4f385070e3da2d3738019cf4e6be7554a16e235f3701e3c0db79419'
4
- data.tar.gz: cfe9e766d00073ab040888cad17bedb8c4b2b294d7e353d5298a9ccce9a130f2
3
+ metadata.gz: ab45002075955602da710e92a39cc3b9fe8bdc9371a33e97319e9e24d68a3385
4
+ data.tar.gz: 25f3e29b999a37eeb5d46ea272b12e313ae11de5c272dd81120dde4d2dd6ecfe
5
5
  SHA512:
6
- metadata.gz: 86c97f9217b2fc3819d8204f80e0f29d7d00e63b3929b40cd684d66b6e701d97b69bb1094714ef8e33bf3c451d67fb31803a18c6625ac2f2a06e4c159ff2dd98
7
- data.tar.gz: 83b8831e08ddcfedf0e558acf5b4813866605757f17d3ca74ec21f9815f774f8207d0b2e7dd1d50aa87674c39e06420fac106454996b2a8f37eb4a660a6e1b95
6
+ metadata.gz: a916d9acc8c50060a472e2c6630529ac6b6832380d2198f1d8e19785ce7da5ff211e24e44b33ce51d6f305fab24e98ba7297cba55904f89d394cc939cb202883
7
+ data.tar.gz: 6b1674699aa6e907474c00d31c6a337ae4af7d9e29d1528811e8dcc19e2235a3340fd59ae1f4f1b4176f21c882508e6d20ec992a77b292d7c31a78c05f671315
data/README.md CHANGED
@@ -315,6 +315,10 @@ Drops an existing NOT NULL constraint. Please not that this migration is not saf
315
315
 
316
316
  Changes the default value for this column when new rows are inserted into the table.
317
317
 
318
+ ### `#extend_varchar_column_limit(table, column, from:, to:)`
319
+
320
+ Widens (or removes, with `to: nil`) the character limit on an existing varchar column. Since Postgres 9.2 this is metadata-only; _narrowing_ a limit requires a full-table scan and isn't supported. The generated migration re-checks the column's actual type and limit in the database against `from:` before changing it, so this is safe to recompile even after it has already run. Not safely reversible: `down` should be `irreversible_migration`.
321
+
318
322
  ### `#remove_index(table, target)`
319
323
 
320
324
  Drop an index from the database.
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nandi
4
+ module Instructions
5
+ class ExtendVarcharColumnLimit
6
+ attr_reader :table, :column, :from, :to
7
+
8
+ def initialize(table:, column:, from:, to:)
9
+ unless from.is_a?(Integer)
10
+ raise ArgumentError, "extend_varchar_column_limit requires an integer `from:` limit"
11
+ end
12
+
13
+ unless to.nil? || (to.is_a?(Integer) && to > from)
14
+ raise ArgumentError,
15
+ "extend_varchar_column_limit only supports widening a character limit, or " \
16
+ "removing it entirely (to: nil), but #{table}.#{column} would go from #{from} " \
17
+ "to #{to.inspect}. Narrowing a limit requires a full-table scan under an " \
18
+ "ACCESS EXCLUSIVE lock and isn't supported here."
19
+ end
20
+
21
+ @table = table
22
+ @column = column
23
+ @from = from
24
+ @to = to
25
+ end
26
+
27
+ def procedure
28
+ :extend_varchar_column_limit
29
+ end
30
+
31
+ def lock
32
+ Nandi::Migration::LockWeights::ACCESS_EXCLUSIVE
33
+ end
34
+ end
35
+ end
36
+ end
@@ -12,6 +12,7 @@ require "nandi/instructions/add_foreign_key"
12
12
  require "nandi/instructions/drop_constraint"
13
13
  require "nandi/instructions/remove_not_null_constraint"
14
14
  require "nandi/instructions/change_column_default"
15
+ require "nandi/instructions/extend_varchar_column_limit"
15
16
  require "nandi/instructions/validate_constraint"
16
17
  require "nandi/instructions/add_check_constraint"
17
18
  require "nandi/instructions/irreversible_migration"
@@ -311,6 +311,19 @@ module Nandi
311
311
  )
312
312
  end
313
313
 
314
+ # Widens (or removes) the character limit on an existing varchar column.
315
+ # Since Postgres 9.2 this is metadata-only (a brief ACCESS EXCLUSIVE lock).
316
+ # Narrowing remains a different, unsafe operation and is not supported.
317
+ #
318
+ # Not safely reversible: `down` should be `irreversible_migration`.
319
+ # @param table [Symbol, String] The name of the table with the column
320
+ # @param column [Symbol, String] The name of the column to change
321
+ # @param from [Integer] The column's current character limit
322
+ # @param to [Integer, nil] The new character limit, or nil to remove it entirely
323
+ def extend_varchar_column_limit(table, column, from:, to:)
324
+ current_instructions << Instructions::ExtendVarcharColumnLimit.new(table:, column:, from:, to:)
325
+ end
326
+
314
327
  # Raises an `ActiveRecord::IrreversibleMigration` error for use in
315
328
  # irreversible migrations
316
329
  def irreversible_migration
@@ -105,6 +105,16 @@ module Nandi
105
105
  formatted_property :value
106
106
  end
107
107
 
108
+ class ExtendVarcharColumnLimitCell < Base
109
+ property :table
110
+ property :column
111
+ property :from
112
+
113
+ def to_type_sql
114
+ model.to.nil? ? "character varying" : "character varying(#{model.to})"
115
+ end
116
+ end
117
+
108
118
  class RemoveNotNullConstraintCell < Base
109
119
  formatted_property :table
110
120
  formatted_property :column
data/lib/nandi/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nandi
4
- VERSION = "3.2.0"
4
+ VERSION = "3.3.0"
5
5
  end
@@ -0,0 +1,16 @@
1
+ execute <<-SQL
2
+ DO $$
3
+ DECLARE
4
+ actual_type text;
5
+ BEGIN
6
+ SELECT format_type(atttypid, atttypmod) INTO actual_type
7
+ FROM pg_attribute
8
+ WHERE attrelid = '<%= table %>'::regclass AND attname = '<%= column %>' AND NOT attisdropped;
9
+
10
+ IF actual_type IS DISTINCT FROM 'character varying(<%= from %>)' THEN
11
+ RAISE EXCEPTION 'expected <%= table %>.<%= column %> to be character varying(<%= from %>), found %', actual_type;
12
+ END IF;
13
+ END $$;
14
+
15
+ ALTER TABLE <%= table %> ALTER COLUMN "<%= column %>" TYPE <%= to_type_sql %>;
16
+ SQL
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nandi
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GoCardless Engineering
@@ -112,6 +112,7 @@ files:
112
112
  - lib/nandi/instructions/create_table.rb
113
113
  - lib/nandi/instructions/drop_constraint.rb
114
114
  - lib/nandi/instructions/drop_table.rb
115
+ - lib/nandi/instructions/extend_varchar_column_limit.rb
115
116
  - lib/nandi/instructions/irreversible_migration.rb
116
117
  - lib/nandi/instructions/remove_column.rb
117
118
  - lib/nandi/instructions/remove_index.rb
@@ -155,6 +156,7 @@ files:
155
156
  - lib/templates/nandi/renderers/active_record/instructions/create_table/show.rb.erb
156
157
  - lib/templates/nandi/renderers/active_record/instructions/drop_constraint/show.rb.erb
157
158
  - lib/templates/nandi/renderers/active_record/instructions/drop_table/show.rb.erb
159
+ - lib/templates/nandi/renderers/active_record/instructions/extend_varchar_column_limit/show.rb.erb
158
160
  - lib/templates/nandi/renderers/active_record/instructions/irreversible_migration/show.rb.erb
159
161
  - lib/templates/nandi/renderers/active_record/instructions/remove_column/show.rb.erb
160
162
  - lib/templates/nandi/renderers/active_record/instructions/remove_index/show.rb.erb
@@ -180,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
182
  - !ruby/object:Gem::Version
181
183
  version: '0'
182
184
  requirements: []
183
- rubygems_version: 4.0.9
185
+ rubygems_version: 4.0.20
184
186
  specification_version: 4
185
187
  summary: Fear-free migrations for PostgreSQL
186
188
  test_files: []