nd-pg-enum-migrations 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +6 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +97 -0
- data/Guardfile +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +159 -0
- data/Rakefile +8 -0
- data/config/database.yml +8 -0
- data/docker-compose.yml +14 -0
- data/lib/nd/pg/enum/migrations/version.rb +11 -0
- data/lib/nd/pg/enum/migrations.rb +124 -0
- data/nd-pg-enum-migrations.gemspec +36 -0
- data/sig/nd/pg/enum/migrations.rbs +10 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c152be4a4c0c1de0763ea9c4af1bb3a5ed73c95fad4b5c689c369ad080ab3718
|
4
|
+
data.tar.gz: 772f691fb69e01270ca54b2a7d31d4d8c1d4ef566c8e4dc50702af18aa95e8c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2ac6dc3304b56483d336638ad5fc7de055febebf60945c6763a9e717aaaf7b77d7fa1ff68c7fd05d3e8db870b162555bed38d407e6815384611dbcdc23d3e461
|
7
|
+
data.tar.gz: f0284f5004f8af1f0a8a01732e4a5e76e136025f891901d1cb8100e9e4ef25c3f705c1d079894cc87baa5dcb191517758b433cbf85ca5317b2fdcf79bd778945
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in nd-pg-enum-migrations.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'pg'
|
9
|
+
gem 'rake', '~> 13.0'
|
10
|
+
gem 'rspec', '~> 3.0'
|
11
|
+
gem 'mocha', '~> 1.13'
|
12
|
+
gem 'pry', '~> 0.13'
|
13
|
+
gem 'guard', '~> 2.18'
|
14
|
+
gem 'guard-rspec', '~> 4.7', require: false
|
15
|
+
gem 'database_cleaner-active_record'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nd-pg-enum-migrations (1.0.0)
|
5
|
+
activerecord (>= 6.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (7.0.3.1)
|
11
|
+
activesupport (= 7.0.3.1)
|
12
|
+
activerecord (7.0.3.1)
|
13
|
+
activemodel (= 7.0.3.1)
|
14
|
+
activesupport (= 7.0.3.1)
|
15
|
+
activesupport (7.0.3.1)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
coderay (1.1.3)
|
21
|
+
concurrent-ruby (1.1.10)
|
22
|
+
database_cleaner-active_record (2.0.1)
|
23
|
+
activerecord (>= 5.a)
|
24
|
+
database_cleaner-core (~> 2.0.0)
|
25
|
+
database_cleaner-core (2.0.1)
|
26
|
+
diff-lcs (1.5.0)
|
27
|
+
ffi (1.15.5)
|
28
|
+
formatador (1.1.0)
|
29
|
+
guard (2.18.0)
|
30
|
+
formatador (>= 0.2.4)
|
31
|
+
listen (>= 2.7, < 4.0)
|
32
|
+
lumberjack (>= 1.0.12, < 2.0)
|
33
|
+
nenv (~> 0.1)
|
34
|
+
notiffany (~> 0.0)
|
35
|
+
pry (>= 0.13.0)
|
36
|
+
shellany (~> 0.0)
|
37
|
+
thor (>= 0.18.1)
|
38
|
+
guard-compat (1.2.1)
|
39
|
+
guard-rspec (4.7.3)
|
40
|
+
guard (~> 2.1)
|
41
|
+
guard-compat (~> 1.1)
|
42
|
+
rspec (>= 2.99.0, < 4.0)
|
43
|
+
i18n (1.12.0)
|
44
|
+
concurrent-ruby (~> 1.0)
|
45
|
+
listen (3.7.1)
|
46
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
47
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
48
|
+
lumberjack (1.2.8)
|
49
|
+
method_source (1.0.0)
|
50
|
+
minitest (5.16.2)
|
51
|
+
mocha (1.14.0)
|
52
|
+
nenv (0.3.0)
|
53
|
+
notiffany (0.1.3)
|
54
|
+
nenv (~> 0.1)
|
55
|
+
shellany (~> 0.0)
|
56
|
+
pg (1.4.3)
|
57
|
+
pry (0.14.1)
|
58
|
+
coderay (~> 1.1)
|
59
|
+
method_source (~> 1.0)
|
60
|
+
rake (13.0.6)
|
61
|
+
rb-fsevent (0.11.1)
|
62
|
+
rb-inotify (0.10.1)
|
63
|
+
ffi (~> 1.0)
|
64
|
+
rspec (3.11.0)
|
65
|
+
rspec-core (~> 3.11.0)
|
66
|
+
rspec-expectations (~> 3.11.0)
|
67
|
+
rspec-mocks (~> 3.11.0)
|
68
|
+
rspec-core (3.11.0)
|
69
|
+
rspec-support (~> 3.11.0)
|
70
|
+
rspec-expectations (3.11.0)
|
71
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
72
|
+
rspec-support (~> 3.11.0)
|
73
|
+
rspec-mocks (3.11.1)
|
74
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
75
|
+
rspec-support (~> 3.11.0)
|
76
|
+
rspec-support (3.11.0)
|
77
|
+
shellany (0.0.1)
|
78
|
+
thor (1.2.1)
|
79
|
+
tzinfo (2.0.5)
|
80
|
+
concurrent-ruby (~> 1.0)
|
81
|
+
|
82
|
+
PLATFORMS
|
83
|
+
x86_64-darwin-21
|
84
|
+
|
85
|
+
DEPENDENCIES
|
86
|
+
database_cleaner-active_record
|
87
|
+
guard (~> 2.18)
|
88
|
+
guard-rspec (~> 4.7)
|
89
|
+
mocha (~> 1.13)
|
90
|
+
nd-pg-enum-migrations!
|
91
|
+
pg
|
92
|
+
pry (~> 0.13)
|
93
|
+
rake (~> 13.0)
|
94
|
+
rspec (~> 3.0)
|
95
|
+
|
96
|
+
BUNDLED WITH
|
97
|
+
2.3.12
|
data/Guardfile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Rails files
|
44
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
45
|
+
dsl.watch_spec_files_for(rails.app_files)
|
46
|
+
dsl.watch_spec_files_for(rails.views)
|
47
|
+
|
48
|
+
watch(rails.controllers) do |m|
|
49
|
+
[
|
50
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
51
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
52
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Rails config changes
|
57
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
58
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
59
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
60
|
+
|
61
|
+
# Capybara features specs
|
62
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
63
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
64
|
+
|
65
|
+
# Turnip features and steps
|
66
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
69
|
+
end
|
70
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Romain Clavel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
# ND::PG::Enum::Migrations
|
2
|
+
|
3
|
+
This gem allows you to create, update and destroy PG enums in your Rails project.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add nd-pg-enum-migrations
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install nd-pg-enum-migrations
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
All migrations can be reverted.
|
18
|
+
|
19
|
+
- [Create enum](#create-enum)
|
20
|
+
- [Delete enum](#delete-enum)
|
21
|
+
- [Add a value to enum](#add-a-value-to-enum)
|
22
|
+
- [Remove a value from enum](#remove-a-value-from-enum)
|
23
|
+
- [Change enum values](#change-enum-values)
|
24
|
+
- [Rename enum](#rename-enum)
|
25
|
+
- [List enum values](#list-enum-values)
|
26
|
+
- [Get all columns using enum](#get-all-columns-using-enum)
|
27
|
+
|
28
|
+
### Create enum
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class CreateUserRoleEnum < ActiveRecord::Migration[7.0]
|
32
|
+
def change
|
33
|
+
create_enum(:user_role, %w(admin owner user))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
### Delete enum
|
39
|
+
|
40
|
+
The second attribute is optional, but is required if the migration is reverted.
|
41
|
+
Otherwise, a `ActiveRecord::IrreversibleMigration` exception will be raised.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class DropUserRoleEnum < ActiveRecord::Migration[7.0]
|
45
|
+
def change
|
46
|
+
drop_enum(:user_role, %w(admin owner user))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
### Add a value to enum
|
52
|
+
|
53
|
+
#### Add one value
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class AddValueToUserRoleEnum < ActiveRecord::Migration[7.0]
|
57
|
+
def change
|
58
|
+
add_enum_value(:user_role, 'banned')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
#### Add several values
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class AddValuesToUserRoleEnum < ActiveRecord::Migration[7.0]
|
67
|
+
def change
|
68
|
+
add_enum_values(:user_role, 'banned', 'expired')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
### Remove a value from enum
|
74
|
+
|
75
|
+
#### Add one value
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class RemoveValueFromUserRoleEnum < ActiveRecord::Migration[7.0]
|
79
|
+
def change
|
80
|
+
remove_enum_value(:user_role, 'banned')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
#### Add several values
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class RemoveValuesFromUserRoleEnum < ActiveRecord::Migration[7.0]
|
89
|
+
def change
|
90
|
+
remove_enum_values(:user_role, 'banned', 'expired')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
### Change enum values
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class ChangeValuesFromUserRoleEnum < ActiveRecord::Migration[7.0]
|
99
|
+
def change
|
100
|
+
change_enum_values(:user_role, add: %w(banned expired), remove: %w(admin))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
### Rename enum
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
class RenameUserRoleEnum < ActiveRecord::Migration[7.0]
|
109
|
+
def change
|
110
|
+
rename_enum(:user_role, :user_kind)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
### List enum values
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
class ListValuesFromUserRoleEnum < ActiveRecord::Migration[7.0]
|
119
|
+
def change
|
120
|
+
values = enum_values(:user_role)
|
121
|
+
# => ["admin", "owner", "user"]
|
122
|
+
|
123
|
+
# ...
|
124
|
+
end
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
### Get all columns using enum
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
class ListColumnsUsingEnum < ActiveRecord::Migration[7.0]
|
132
|
+
def up
|
133
|
+
values = columns_from_type(:user_role)
|
134
|
+
# => [
|
135
|
+
# {:table_name=>"users", :column_name=>"role"},
|
136
|
+
# {:table_name=>"users", :column_name=>"kind"},
|
137
|
+
# {:table_name=>"profiles", :column_name=>"role"}
|
138
|
+
# ]
|
139
|
+
|
140
|
+
# ...
|
141
|
+
end
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
## Development
|
146
|
+
|
147
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
148
|
+
|
149
|
+
Guard is also installed: `bundle exec guard`.
|
150
|
+
|
151
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, run `gem bump` (or manually update the version number in `version.rb`), and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
152
|
+
|
153
|
+
## Contributing
|
154
|
+
|
155
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rclavel/nd-pg-enum-migrations.
|
156
|
+
|
157
|
+
## License
|
158
|
+
|
159
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/config/database.yml
ADDED
data/docker-compose.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
version: "3"
|
2
|
+
services:
|
3
|
+
database:
|
4
|
+
container_name: nd-pg-enum-migrations
|
5
|
+
image: postgres:9.6.8
|
6
|
+
ports:
|
7
|
+
- "127.0.0.1:5432:5432"
|
8
|
+
environment:
|
9
|
+
POSTGRES_USER: nd-pg-enum-migrations
|
10
|
+
volumes:
|
11
|
+
- pgdata:/var/lib/postgresql/data
|
12
|
+
volumes:
|
13
|
+
pgdata:
|
14
|
+
name: nd-pg-enum-migrations-postgres
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require_relative 'migrations/version'
|
5
|
+
|
6
|
+
module ND::PG::Enum::Migrations::Concern
|
7
|
+
def create_enum(name, values)
|
8
|
+
reversible do |direction|
|
9
|
+
direction.up { _create_enum(name, values) }
|
10
|
+
direction.down { _drop_enum(name) }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def drop_enum(name, values = nil)
|
15
|
+
if values
|
16
|
+
reversible do |direction|
|
17
|
+
direction.up { _drop_enum(name) }
|
18
|
+
direction.down { _create_enum(name, values) }
|
19
|
+
end
|
20
|
+
else
|
21
|
+
# If reverted, an ActiveRecord::IrreversibleMigration error will be raised.
|
22
|
+
_drop_enum(name)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_enum_value(name, *values)
|
27
|
+
change_enum_values(name, add: values.flatten)
|
28
|
+
end
|
29
|
+
alias_method :add_enum_values, :add_enum_value
|
30
|
+
|
31
|
+
def remove_enum_value(name, *values)
|
32
|
+
change_enum_values(name, remove: values.flatten)
|
33
|
+
end
|
34
|
+
alias_method :remove_enum_values, :remove_enum_value
|
35
|
+
|
36
|
+
def change_enum_values(name, add: [], remove: [])
|
37
|
+
reversible do |direction|
|
38
|
+
direction.up { _change_enum_values(name, add: add, remove: remove) }
|
39
|
+
direction.down { _change_enum_values(name, add: remove, remove: add) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def rename_enum(from, to)
|
44
|
+
reversible do |direction|
|
45
|
+
direction.up { _rename_enum(from, to) }
|
46
|
+
direction.down { _rename_enum(to, from) }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def enum_values(name)
|
51
|
+
values = ActiveRecord::Base.connection.execute(<<~SQL.squish)
|
52
|
+
SELECT unnest(enum_range(null, null::#{name})) AS value;
|
53
|
+
SQL
|
54
|
+
|
55
|
+
values.map { |value| value['value'] }
|
56
|
+
end
|
57
|
+
|
58
|
+
def columns_from_type(type)
|
59
|
+
values = ActiveRecord::Base.connection.execute(<<~SQL.squish)
|
60
|
+
SELECT table_name, column_name FROM information_schema.columns WHERE table_schema = 'public' AND udt_name = '#{type}';
|
61
|
+
SQL
|
62
|
+
|
63
|
+
values.to_a.map(&:symbolize_keys)
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def _create_enum(name, values)
|
69
|
+
logger.debug("Create enum `#{name}` with values #{values.inspect}")
|
70
|
+
execute <<-SQL
|
71
|
+
CREATE TYPE #{name} AS ENUM (#{values.map { |value| "'#{value}'" }.join(', ')});
|
72
|
+
SQL
|
73
|
+
end
|
74
|
+
|
75
|
+
def _drop_enum(name)
|
76
|
+
logger.debug("Drop enum `#{name}`")
|
77
|
+
execute <<-SQL
|
78
|
+
DROP TYPE #{name};
|
79
|
+
SQL
|
80
|
+
end
|
81
|
+
|
82
|
+
def _change_enum_values(name, add: [], remove: [])
|
83
|
+
logger.debug("Change enum `#{name}` values: Add #{add.inspect}, remove #{remove.inspect}")
|
84
|
+
temporary_name = "new_#{name}".to_sym
|
85
|
+
existing_values = enum_values(name)
|
86
|
+
new_values = existing_values + add - remove
|
87
|
+
|
88
|
+
_create_enum(temporary_name, existing_values)
|
89
|
+
_change_enum_type(from: name, to: temporary_name)
|
90
|
+
_drop_enum(name)
|
91
|
+
|
92
|
+
_create_enum(name, new_values)
|
93
|
+
_change_enum_type(from: temporary_name, to: name)
|
94
|
+
_drop_enum(temporary_name)
|
95
|
+
end
|
96
|
+
|
97
|
+
def _rename_enum(old_name, new_name)
|
98
|
+
logger.debug("Rename enum from `#{old_name}` to `#{new_name}`")
|
99
|
+
values = enum_values(old_name)
|
100
|
+
|
101
|
+
_create_enum(new_name, values)
|
102
|
+
_change_enum_type(from: old_name, to: new_name)
|
103
|
+
_drop_enum(old_name)
|
104
|
+
end
|
105
|
+
|
106
|
+
def _change_enum_type(from:, to:)
|
107
|
+
logger.debug("Change enum type from `#{from}` to `#{to}`")
|
108
|
+
source_type = from
|
109
|
+
destination_type = to
|
110
|
+
|
111
|
+
columns_from_type(source_type).each do |data|
|
112
|
+
table, column = data.values_at(:table_name, :column_name)
|
113
|
+
change_column(table, column, "#{destination_type} USING #{column}::varchar::#{destination_type}")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def logger
|
118
|
+
@_logger ||= Logger.new(STDOUT)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
class ActiveRecord::Migration
|
123
|
+
include ND::PG::Enum::Migrations::Concern
|
124
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/nd/pg/enum/migrations/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'nd-pg-enum-migrations'
|
7
|
+
spec.version = ND::PG::Enum::Migrations::VERSION
|
8
|
+
spec.authors = ['Romain Clavel']
|
9
|
+
spec.email = ['romain@clavel.io']
|
10
|
+
|
11
|
+
spec.summary = 'Helpers to create, update and delete PG Enum in your Rails project.'
|
12
|
+
spec.homepage = 'https://github.com/rclavel/nd-pg-enum-migrations'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
spec.required_ruby_version = '>= 2.6.0'
|
15
|
+
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['source_code_uri'] = 'https://github.com/rclavel/nd-pg-enum-migrations'
|
18
|
+
spec.metadata['changelog_uri'] = 'https://github.com/rclavel/nd-pg-enum-migrations/blob/master/CHANGELOG.md'
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(__dir__) do
|
23
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
spec.add_dependency 'activerecord', '>= 6.0.0'
|
33
|
+
|
34
|
+
# For more information and examples about making a new gem, check out our
|
35
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nd-pg-enum-migrations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Romain Clavel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 6.0.0
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- romain@clavel.io
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- CHANGELOG.md
|
36
|
+
- Gemfile
|
37
|
+
- Gemfile.lock
|
38
|
+
- Guardfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- config/database.yml
|
43
|
+
- docker-compose.yml
|
44
|
+
- lib/nd/pg/enum/migrations.rb
|
45
|
+
- lib/nd/pg/enum/migrations/version.rb
|
46
|
+
- nd-pg-enum-migrations.gemspec
|
47
|
+
- sig/nd/pg/enum/migrations.rbs
|
48
|
+
homepage: https://github.com/rclavel/nd-pg-enum-migrations
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://github.com/rclavel/nd-pg-enum-migrations
|
53
|
+
source_code_uri: https://github.com/rclavel/nd-pg-enum-migrations
|
54
|
+
changelog_uri: https://github.com/rclavel/nd-pg-enum-migrations/blob/master/CHANGELOG.md
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.6.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Helpers to create, update and delete PG Enum in your Rails project.
|
74
|
+
test_files: []
|