postgresql_adapter_extensions 1.0.0 → 1.1.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 +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile.lock +1 -1
- data/lib/postgresql_adapter_extensions/base.rb +4 -0
- data/lib/postgresql_adapter_extensions/command_recorder.rb +121 -0
- data/lib/postgresql_adapter_extensions/sequence_methods.rb +12 -11
- data/lib/postgresql_adapter_extensions/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db065d0487684a0647e17b2db8e5ef1f2131a6b15a7b0316d1fcd9856e46c07a
|
4
|
+
data.tar.gz: bd6cb683c11d9e27c1225d5c29e638adf6f14b96b1f7c3bf1803d96944383478
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 986daa750a1e762d710dbda29ba587ea8c76a8894f2c7ff346b2d64dc974e4c3fe54098e9549646d584c8b8c9efb5ebe00e0581a3d00c76dc3be253e7d5af9c5
|
7
|
+
data.tar.gz: 8dc3a0571ff79ce9dd058ab3c22e0876c07cd748ddc0b4c42b5f642e51869aaddc353b144be5f1feff165cbe967bacaf08f0e70dbe0828fba8ffe8fa5703c5fc
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## [1.1.0](https://github.com/shivam091/postgresql_adapter_extensions/compare/v1.0.0...v1.1.0) - 2025-03-21
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
- Extended `ActiveRecord::Migration::CommandRecorder` to support PostgreSQL sequence-related commands.
|
5
|
+
- Added `create_sequence` method to record sequence creation in migrations.
|
6
|
+
- Added `alter_sequence` method to record sequence alterations (irreversible).
|
7
|
+
- Added `drop_sequence` method to record sequence deletions (irreversible).
|
8
|
+
- Implemented `invert_create_sequence` to allow rollback by dropping the sequence.
|
9
|
+
- Implemented `invert_alter_sequence` and `invert_drop_sequence` to raise `ActiveRecord::IrreversibleMigration`.
|
10
|
+
|
11
|
+
### Notes
|
12
|
+
- `create_sequence` can be reversed by dropping the sequence.
|
13
|
+
- `alter_sequence` and `drop_sequence` are irreversible operations.
|
14
|
+
|
15
|
+
----------
|
16
|
+
|
1
17
|
## [1.0.0](https://github.com/shivam091/postgresql_adapter_extensions/compare/v0.1.0...v1.0.0) - 2025-03-12
|
2
18
|
|
3
19
|
### What's new
|
data/Gemfile.lock
CHANGED
@@ -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,121 @@
|
|
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
|
+
private
|
81
|
+
|
82
|
+
##
|
83
|
+
# Generates the inverse command for creating a sequence, which is to drop the sequence.
|
84
|
+
#
|
85
|
+
# @param args [Array] Arguments passed to the create_sequence method (sequence name).
|
86
|
+
# @return [Array] An array with the inverse command `:drop_sequence` and its arguments.
|
87
|
+
#
|
88
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
89
|
+
# @since 1.0.0
|
90
|
+
#
|
91
|
+
def invert_create_sequence(args)
|
92
|
+
[:drop_sequence, args]
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Generates the inverse command for altering a sequence, which is not possible.
|
97
|
+
#
|
98
|
+
# @param args [Array] Arguments passed to the alter_sequence method.
|
99
|
+
# @raise [ActiveRecord::IrreversibleMigration] This operation cannot be reversed.
|
100
|
+
#
|
101
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
102
|
+
# @since 1.0.0
|
103
|
+
#
|
104
|
+
def invert_alter_sequence(args)
|
105
|
+
raise ActiveRecord::IrreversibleMigration, "Alter sequence is irreversible."
|
106
|
+
end
|
107
|
+
|
108
|
+
##
|
109
|
+
# Generates the inverse command for dropping a sequence, which is not possible.
|
110
|
+
#
|
111
|
+
# @param args [Array] Arguments passed to the drop_sequence method (sequence name).
|
112
|
+
# @raise [ActiveRecord::IrreversibleMigration] This operation cannot be reversed.
|
113
|
+
#
|
114
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
115
|
+
# @since 1.0.0
|
116
|
+
#
|
117
|
+
def invert_drop_sequence(args)
|
118
|
+
raise ActiveRecord::IrreversibleMigration, "Drop sequence is irreversible."
|
119
|
+
end
|
120
|
+
end
|
121
|
+
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
|
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.
|
4
|
+
version: 1.1.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-
|
11
|
+
date: 2025-03-21 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
|