postgresql_adapter_extensions 1.0.0 → 1.2.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: cd87ca1ccdc1fede8e8b7ef90eacff3ba354b36917313100d53057e120801eeb
4
- data.tar.gz: 687551a9e6dfa33f62ccca04d7baf365084372a6f45621f8eb6c4b436de72d60
3
+ metadata.gz: fedbb03323e871885e04d3799efbdbae9f5f058acc2148ed2e5005a273bedf9f
4
+ data.tar.gz: 73bfe4528be9a2a28395fe40ee40e920052058b5a1a1276fe58c4bb857e57bbf
5
5
  SHA512:
6
- metadata.gz: a51140bd27d50bcfb9818dfe861e32e6b04ce15391af9982f817489af66b0dac65dab04da0feac9ba17694e9579ca0f1e16710b9b54663e3f575261a62d93124
7
- data.tar.gz: 01d16059bdcab91eba009cd6c644c17e8aa908d2ace1d0c2cb20d28f0260a8c92a119e0ad0331aae38b07bdd56e71630fc239591170ab847531a8cf1d4540458
6
+ metadata.gz: e3e30ccebbbbc5eae45c6daa70d953a8479ce2fecfeedb46c5e545be614e8c5080ec48c99057d2e4fefc668e7775cb9d9bac187401e1637a47cefefd092d244b
7
+ data.tar.gz: c2c2a06aedbcaa1d1ebef70fcb61d411ad81f8d5d787b24a43a60a7f2f8c84ffab48be36531f3147a859c807edd78d094a9a689fb8be54b810ac826a827ab3e5
data/CHANGELOG.md CHANGED
@@ -1,25 +1,46 @@
1
- ## [1.0.0](https://github.com/shivam091/postgresql_adapter_extensions/compare/v0.1.0...v1.0.0) - 2025-03-12
1
+ ## [1.2.0](https://github.com/shivam091/postgresql_adapter_extensions/compare/v1.1.0...v1.2.0) - 2025-03-27
2
2
 
3
3
  ### What's new
4
4
 
5
- - Added `create_sequence` method
5
+ #### PostgreSQLAdapterExtensions::SequenceMethods
6
+ - Added `rename_sequence` method to rename a PostgreSQL sequences with optional IF EXISTS.
6
7
 
7
- Allows creating PostgreSQL sequences with customizable options such as start value, increment step, min/max values, caching, cycling, and ownership.
8
+ #### PostgreSQLAdapterExtensions::CommandRecorder
9
+ - Added `rename_sequence` method to record sequence rename in migrations.
10
+ - Implemented `invert_rename_sequence` to allow rollback by renaming the sequence back to its previous name.
8
11
 
9
- - Added `alter_sequence` method
12
+ ### Notes
13
+ - `rename_sequence` can be reversed by renaming the sequence back to its previous name.
10
14
 
11
- Enables modifying existing PostgreSQL sequences, supporting changes to increment steps, restart values, min/max limits, caching, cycling behavior, and ownership.
15
+ ## [1.1.0](https://github.com/shivam091/postgresql_adapter_extensions/compare/v1.0.0...v1.1.0) - 2025-03-21
12
16
 
13
- - Added `drop_sequence` method
17
+ ### What's new
14
18
 
15
- Provides functionality to remove a PostgreSQL sequence with optional IF EXISTS and CASCADE/RESTRICT behaviors to manage dependencies.
19
+ #### PostgreSQLAdapterExtensions::CommandRecorder
20
+ - Added `create_sequence` method to record sequence creation in migrations.
21
+ - Added `alter_sequence` method to record sequence alterations (irreversible).
22
+ - Added `drop_sequence` method to record sequence deletions (irreversible).
23
+ - Implemented `invert_create_sequence` to allow rollback by dropping the sequence.
24
+ - Implemented `invert_alter_sequence` and `invert_drop_sequence` to raise `ActiveRecord::IrreversibleMigration`.
16
25
 
17
- ----------
26
+ ### Notes
27
+ - `create_sequence` can be reversed by dropping the sequence.
28
+ - `alter_sequence` and `drop_sequence` are irreversible operations.
18
29
 
19
- ## 0.1.0 - 2025-03-11
30
+ ## [1.0.0](https://github.com/shivam091/postgresql_adapter_extensions/compare/v0.1.0...v1.0.0) - 2025-03-12
20
31
 
21
- ### Initial release
32
+ ### What's new
22
33
 
23
- -----------
34
+ #### PostgreSQLAdapterExtensions::SequenceMethods
35
+ - Added `create_sequence` method to allow creating PostgreSQL sequences with customizable options such as
36
+ start value, increment step, min/max values, caching, cycling, and ownership.
24
37
 
25
- ### Unreleased
38
+ - Added `alter_sequence` method to enable modifying existing PostgreSQL sequences, supporting changes to
39
+ increment steps, restart values, min/max limits, caching, cycling behavior, and ownership.
40
+
41
+ - Added `drop_sequence` method to provides functionality to remove a PostgreSQL sequence with optional
42
+ IF EXISTS and CASCADE/RESTRICT behaviors to manage dependencies.
43
+
44
+ ## 0.1.0 - 2025-03-11
45
+
46
+ ### Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- postgresql_adapter_extensions (1.0.0)
4
+ postgresql_adapter_extensions (1.2.0)
5
5
  activerecord (~> 8)
6
6
  pg (~> 1.5)
7
7
 
data/README.md CHANGED
@@ -121,8 +121,8 @@ alter_sequence(:order_id_seq, cycle: false)
121
121
  alter_sequence(:order_id_seq, owned_by: "orders.id")
122
122
  ```
123
123
 
124
- This method provides flexibility in managing sequences dynamically in your Rails application,
125
- ensuring that sequence-related database behavior can be modified as needed.
124
+ This method provides flexibility in managing sequences dynamically in your Rails
125
+ application, ensuring that sequence-related database behavior can be modified as needed.
126
126
 
127
127
  ### drop_sequence
128
128
 
@@ -156,6 +156,26 @@ drop_sequence(:order_id_seq, drop_behavior: :cascade)
156
156
  drop_sequence(:order_id_seq, drop_behavior: :restrict)
157
157
  ```
158
158
 
159
+ ### rename_sequence
160
+
161
+ The `rename_sequence` method allows you to rename an existing sequence in your PostgreSQL database.
162
+
163
+ ```ruby
164
+ rename_sequence(name, options = {})
165
+ ```
166
+
167
+ **Rename a sequence:**
168
+
169
+ ```ruby
170
+ rename_sequence(:order_id_seq, to: :new_order_id_seq)
171
+ ```
172
+
173
+ **Rename a sequence only if it exists:**
174
+
175
+ ```ruby
176
+ rename_sequence(:order_id_seq, to: :new_order_id_seq, if_exists: true)
177
+ ```
178
+
159
179
  ## PostgreSQL Setup for Contributors
160
180
 
161
181
  If you're contributing to this gem and need to set up PostgreSQL for local development or testing,
@@ -5,6 +5,7 @@
5
5
  require "active_record"
6
6
  require "active_record/connection_adapters/postgresql_adapter"
7
7
 
8
+ require "postgresql_adapter_extensions/command_recorder"
8
9
  require "postgresql_adapter_extensions/sequence_methods"
9
10
 
10
11
  module PostgreSQLAdapterExtensions
@@ -20,4 +21,7 @@ module PostgreSQLAdapterExtensions
20
21
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(SequenceMethods)
21
22
  end
22
23
 
24
+ if defined?(ActiveRecord::Migration::CommandRecorder)
25
+ ActiveRecord::Migration::CommandRecorder.include(CommandRecorder)
26
+ end
23
27
  end
@@ -0,0 +1,152 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module PostgreSQLAdapterExtensions
6
+ ##
7
+ # Module that extends ActiveRecord's CommandRecorder to handle sequence-related commands.
8
+ #
9
+ # This module is designed to work with reversible migrations in ActiveRecord, specifically to manage PostgreSQL sequence operations.
10
+ # The methods capture forward migration commands for sequences and generate their inverse using simple metaprogramming.
11
+ #
12
+ # @example
13
+ # class AddSomeSequence < ActiveRecord::Migration[6.0]
14
+ # def change
15
+ # create_sequence :some_sequence
16
+ # end
17
+ # end
18
+ #
19
+ # This will create a sequence, and during rollback, it will drop the sequence.
20
+ #
21
+ # @see ActiveRecord::Migration::CommandRecorder
22
+ #
23
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
24
+ # @since 1.0.0
25
+ #
26
+ module CommandRecorder
27
+ ##
28
+ # Records the creation of a PostgreSQL sequence during a migration.
29
+ #
30
+ # This method is invoked when creating a sequence in the database. The corresponding
31
+ # inverse operation will be to drop the sequence during rollback.
32
+ #
33
+ # @param args [Array] Arguments required to create the sequence (usually the sequence name).
34
+ # @param block [Proc] An optional block passed to the command.
35
+ # @return [void]
36
+ #
37
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
38
+ # @since 1.0.0
39
+ #
40
+ def create_sequence(*args, &block)
41
+ record(:create_sequence, args, &block)
42
+ end
43
+
44
+ ##
45
+ # Records the alteration of a PostgreSQL sequence during a migration.
46
+ #
47
+ # This method is invoked when altering a sequence in the database.
48
+ # The corresponding inverse operation is not possible, so it will raise an error during rollback.
49
+ #
50
+ # @param args [Array] Arguments required to alter the sequence.
51
+ # @param block [Proc] An optional block passed to the command.
52
+ # @raise [ActiveRecord::IrreversibleMigration] When attempting to rollback an altered sequence.
53
+ # @return [void]
54
+ #
55
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
56
+ # @since 1.0.0
57
+ #
58
+ def alter_sequence(*args, &block)
59
+ record(:alter_sequence, args, &block)
60
+ end
61
+
62
+ ##
63
+ # Records the dropping of a PostgreSQL sequence during a migration.
64
+ #
65
+ # This method is invoked when dropping a sequence from the database.
66
+ # The corresponding inverse operation is not possible, so it will raise an error during rollback.
67
+ #
68
+ # @param args [Array] Arguments required to drop the sequence (usually the sequence name).
69
+ # @param block [Proc] An optional block passed to the command.
70
+ # @raise [ActiveRecord::IrreversibleMigration] When attempting to rollback a dropped sequence.
71
+ # @return [void]
72
+ #
73
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
74
+ # @since 1.0.0
75
+ #
76
+ def drop_sequence(*args, &block)
77
+ record(:drop_sequence, args, &block)
78
+ end
79
+
80
+ ##
81
+ # Records the renaming of a PostgreSQL sequence during a migration.
82
+ #
83
+ # This method is invoked when renaming a sequence in the database.
84
+ # The corresponding inverse operation will be to rename the sequence back to
85
+ # its original name during rollback.
86
+ #
87
+ # @param args [Array] Arguments required to rename the sequence (usually the old and new sequence names).
88
+ #
89
+ # @return [void]
90
+ #
91
+ # @since 1.2.0
92
+ #
93
+ def rename_sequence(*args)
94
+ record(:rename_sequence, args)
95
+ end
96
+
97
+ private
98
+
99
+ ##
100
+ # Generates the inverse command for creating a sequence, which is to drop the sequence.
101
+ #
102
+ # @param args [Array] Arguments passed to the create_sequence method (sequence name).
103
+ # @return [Array] An array with the inverse command `:drop_sequence` and its arguments.
104
+ #
105
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
106
+ # @since 1.0.0
107
+ #
108
+ def invert_create_sequence(args)
109
+ [:drop_sequence, args]
110
+ end
111
+
112
+ ##
113
+ # Generates the inverse command for altering a sequence, which is not possible.
114
+ #
115
+ # @param args [Array] Arguments passed to the alter_sequence method.
116
+ # @raise [ActiveRecord::IrreversibleMigration] This operation cannot be reversed.
117
+ #
118
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
119
+ # @since 1.0.0
120
+ #
121
+ def invert_alter_sequence(args)
122
+ raise ActiveRecord::IrreversibleMigration, "Alter sequence is irreversible."
123
+ end
124
+
125
+ ##
126
+ # Generates the inverse command for dropping a sequence, which is not possible.
127
+ #
128
+ # @param args [Array] Arguments passed to the drop_sequence method (sequence name).
129
+ # @raise [ActiveRecord::IrreversibleMigration] This operation cannot be reversed.
130
+ #
131
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
132
+ # @since 1.0.0
133
+ #
134
+ def invert_drop_sequence(args)
135
+ raise ActiveRecord::IrreversibleMigration, "Drop sequence is irreversible."
136
+ end
137
+
138
+ ##
139
+ # Generates the inverse command for renaming a sequence, which renames it
140
+ # back to its original name.
141
+ #
142
+ # @param args [Array] Arguments passed to the rename_sequence method (old sequence name and new sequence name).
143
+ #
144
+ # @return [Array] An array with the inverse command `:rename_sequence` and its arguments swapped.
145
+ #
146
+ # @since 1.2.0
147
+ #
148
+ def invert_rename_sequence(args)
149
+ [:rename_sequence, [args.last[:to], to: args.first]]
150
+ end
151
+ end
152
+ end
@@ -7,18 +7,15 @@ module PostgreSQLAdapterExtensions
7
7
  # This module provides methods for managing PostgreSQL sequences, including
8
8
  # creating, altering, and dropping sequences with various customization options.
9
9
  #
10
+ # @note This module is designed for PostgreSQL databases and may not be compatible with other database systems.
11
+ #
10
12
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
11
13
  # @since 1.0.0
12
14
  #
13
- # @note This module is designed for PostgreSQL databases and may not be compatible with other database systems.
14
- #
15
15
  module SequenceMethods
16
16
  ##
17
17
  # Creates a new sequence in the PostgreSQL database with customizable options.
18
18
  #
19
- # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
20
- # @since 1.0.0
21
- #
22
19
  # @param name [String, Symbol] The name of the sequence to create.
23
20
  # @param options [Hash] Additional options to configure the sequence.
24
21
  # @option options [Boolean] :if_not_exists (false) Includes +IF NOT EXISTS+ to avoid errors if the sequence exists.
@@ -47,6 +44,9 @@ module PostgreSQLAdapterExtensions
47
44
  #
48
45
  # @note Uses `CREATE SEQUENCE` SQL statement with PostgreSQL-specific options.
49
46
  #
47
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
48
+ # @since 1.0.0
49
+ #
50
50
  def create_sequence(name, options = {})
51
51
  options = options.reverse_merge(
52
52
  start: 1,
@@ -73,11 +73,9 @@ module PostgreSQLAdapterExtensions
73
73
  execute(sql).tap { reload_type_map }
74
74
  end
75
75
 
76
+ ##
76
77
  # Alters an existing PostgreSQL sequence with the given options.
77
78
  #
78
- # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
79
- # @since 1.0.0
80
- #
81
79
  # @param name [String, Symbol] The name of the sequence to alter.
82
80
  # @param options [Hash] A hash of options to modify the sequence behavior.
83
81
  # @option options [Boolean] :if_exists Includes +IF EXISTS+ to avoid errors if the sequence does not exist.
@@ -113,6 +111,9 @@ module PostgreSQLAdapterExtensions
113
111
  #
114
112
  # @note Uses `ALTER SEQUENCE` SQL statement with PostgreSQL-specific options.
115
113
  #
114
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
115
+ # @since 1.0.0
116
+ #
116
117
  def alter_sequence(name, options = {})
117
118
  sql = +"ALTER SEQUENCE"
118
119
  sql << " IF EXISTS" if options[:if_exists]
@@ -135,9 +136,6 @@ module PostgreSQLAdapterExtensions
135
136
  ##
136
137
  # Drops an existing sequence from the PostgreSQL database with optional conditions.
137
138
  #
138
- # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
139
- # @since 1.0.0
140
- #
141
139
  # @param name [String, Symbol] The name of the sequence to drop.
142
140
  # @param options [Hash] Additional options to modify the behavior of the drop operation.
143
141
  # @option options [Boolean] :if_exists (false) Adds +IF EXISTS+ to avoid errors if the sequence does not exist.
@@ -161,6 +159,9 @@ module PostgreSQLAdapterExtensions
161
159
  #
162
160
  # @note Uses `DROP SEQUENCE` SQL statement with PostgreSQL-specific options.
163
161
  #
162
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
163
+ # @since 1.0.0
164
+ #
164
165
  def drop_sequence(name, options = {})
165
166
  options = options.reverse_merge(
166
167
  if_exists: false
@@ -174,5 +175,43 @@ module PostgreSQLAdapterExtensions
174
175
 
175
176
  execute(sql).tap { reload_type_map }
176
177
  end
178
+
179
+ ##
180
+ # Renames an existing PostgreSQL sequence.
181
+ #
182
+ # @param name [String, Symbol] The current name of the sequence.
183
+ # @param options [Hash] A hash of options for renaming the sequence.
184
+ # @option options [Boolean] :if_exists (false) Includes +IF EXISTS+ to avoid errors if the sequence does not exist.
185
+ # @option options [String, Symbol] :to The new name for the sequence.
186
+ #
187
+ # @raise [ArgumentError] If the +:to+ option is not provided.
188
+ #
189
+ # @example Rename a sequence
190
+ # rename_sequence(:order_id_seq, to: :new_order_id_seq)
191
+ #
192
+ # @example Rename a sequence only if it exists
193
+ # rename_sequence(:order_id_seq, to: :new_order_id_seq, if_exists: true)
194
+ #
195
+ # @return [void]
196
+ #
197
+ # @note Uses `ALTER SEQUENCE ... RENAME TO` SQL statement in PostgreSQL.
198
+ #
199
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
200
+ # @since 1.2.0
201
+ #
202
+ def rename_sequence(name, options = {})
203
+ to = options.fetch(:to) { raise ArgumentError, ":to is required" }
204
+
205
+ options = options.reverse_merge(
206
+ if_exists: false
207
+ )
208
+
209
+ sql = +"ALTER SEQUENCE"
210
+ sql << " IF EXISTS" if options[:if_exists]
211
+ sql << " #{quote_table_name(name)}"
212
+ sql << " RENAME TO #{quote_table_name(to)}"
213
+
214
+ execute(sql).tap { reload_type_map }
215
+ end
177
216
  end
178
217
  end
@@ -4,5 +4,5 @@
4
4
 
5
5
  module PostgreSQLAdapterExtensions
6
6
  # Current stable version.
7
- VERSION = "1.0.0"
7
+ VERSION = "1.2.0"
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgresql_adapter_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-03-12 00:00:00.000000000 Z
11
+ date: 2025-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -121,6 +121,7 @@ files:
121
121
  - bin/setup_postgresql.sh
122
122
  - lib/postgresql_adapter_extensions.rb
123
123
  - lib/postgresql_adapter_extensions/base.rb
124
+ - lib/postgresql_adapter_extensions/command_recorder.rb
124
125
  - lib/postgresql_adapter_extensions/sequence_methods.rb
125
126
  - lib/postgresql_adapter_extensions/version.rb
126
127
  - postgresql_adapter_extensions.gemspec