webdack-uuid_migration 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +9 -0
- data/README.md +30 -2
- data/lib/webdack/uuid_migration/helpers.rb +31 -17
- data/lib/webdack/uuid_migration/version.rb +1 -1
- data/spec/uuid_migrate_helper_spec.rb +164 -140
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9826dc874d0b0545b500caa8d1ddaac9f2d13905730e0d1a32271df4624266e9
|
4
|
+
data.tar.gz: 1b372945e222bca7777cfdf67908745be2032df93de044e6c11617b03e61309c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a44d513dd4cb66befcd8b697ebb8fa3d750f1da464e62d49e787a7063a96588e2c237fa48e159ee556920175d04ce58266afd3293a20a4255b0ce7a6906a3263
|
7
|
+
data.tar.gz: fbd07db878a5560566dfc5dba04dc9e2808949429059c45db9666a140eb2cd0dd5696e88427fb035d97a9cb3e38af0d8bd68e9698cf880f662612d324282cb5c
|
data/README.md
CHANGED
@@ -49,6 +49,7 @@ this gem can safely be removed from your applications Gemfile.
|
|
49
49
|
|
50
50
|
- Put `require 'webdack/uuid_migration/helpers'` in your migration file.
|
51
51
|
- Enable `'pgcrypto'` directly in Postgres database or by adding `enable_extension 'pgcrypto'` to your migration.
|
52
|
+
If you want to generate random UUIDs, enable `uuid-ossp` as well.
|
52
53
|
- Use methods from {Webdack::UUIDMigration::Helpers} as appropriate.
|
53
54
|
|
54
55
|
Example:
|
@@ -80,8 +81,9 @@ Example:
|
|
80
81
|
end
|
81
82
|
```
|
82
83
|
|
83
|
-
|
84
|
-
retrieve old id in future.
|
84
|
+
By default, integer values are converted to UUID by padding 0's to the left.
|
85
|
+
This makes it possible to retrieve old id in future. See **Generating random UUIDs** below to
|
86
|
+
convert integer into random UUIDs using a seed.
|
85
87
|
|
86
88
|
See {Webdack::UUIDMigration::Helpers} for more details. {Webdack::UUIDMigration::Helpers} is mixed
|
87
89
|
into {ActiveRecord::Migration}, so that all methods can directly be used within migrations.
|
@@ -153,6 +155,31 @@ Example:
|
|
153
155
|
# See the rspec test case in spec folder for full example
|
154
156
|
```
|
155
157
|
|
158
|
+
## Generating random UUIDs
|
159
|
+
|
160
|
+
You can provide a `seed` parameter with a valid UUID, which will be used to convert integers into random
|
161
|
+
UUIDs using [uuid_generate_v5](https://www.postgresql.org/docs/current/uuid-ossp.html). You will only be
|
162
|
+
able to recover the old IDs in the future if you use the seed you used during the migration to generate a
|
163
|
+
rainbow table.
|
164
|
+
|
165
|
+
Examples:
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
seed = SecureRandom.uuid
|
169
|
+
|
170
|
+
primary_key_to_uuid :colleges, seed: seed
|
171
|
+
columns_to_uuid :students, :institution_id, seed: seed
|
172
|
+
polymorphic_column_data_for_uuid :students, :institution, 'College', seed: seed
|
173
|
+
primary_key_and_all_references_to_uuid :cities: seed: seed
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
**Note:** Given a set of IDs (which are used, for example, as primary key in one table and foreign
|
178
|
+
key in multiple tables), you will need to use the same seed in all method calls. Otherwise, the
|
179
|
+
conversion will happen differently in each one of the tables, and the relations will be effectively
|
180
|
+
lost. In general, it is adviced to use a different seed per ID set (primary key) to maintain data
|
181
|
+
consistency and guarantee uniqueness across tables.
|
182
|
+
|
156
183
|
## Compatibility
|
157
184
|
|
158
185
|
Works with Rails 4.2+, 5 & 6. It uses Rails 4's out-of-the-box UUID support for PostgreSQL.
|
@@ -174,6 +201,7 @@ To run the test suite:
|
|
174
201
|
- [Nick Schwaderer](https://github.com/Schwad) Rails 5.2.x compatibility
|
175
202
|
- [Kelsey Hannan](https://github.com/KelseyDH) Upgrading to `pgcrypto`
|
176
203
|
- [Sébastien Dubois](https://github.com/sedubois) Ruby 3.0 compatibility
|
204
|
+
- [Manuel Bustillo Alonso](https://github.com/bustikiller) Random UUID support
|
177
205
|
|
178
206
|
## Contributing
|
179
207
|
|
@@ -5,7 +5,6 @@ module Webdack
|
|
5
5
|
module Helpers
|
6
6
|
include SchemaHelpers
|
7
7
|
|
8
|
-
|
9
8
|
# Converts primary key from Serial Integer to UUID, migrates all data by left padding with 0's
|
10
9
|
# sets gen_random_uuid() as default for the column
|
11
10
|
#
|
@@ -14,15 +13,17 @@ module Webdack
|
|
14
13
|
# @option options [Symbol] :primary_key if not supplied queries the schema (should work most of the times)
|
15
14
|
# @option options [String] :default mechanism to generate UUID for new records, default gen_random_uuid(),
|
16
15
|
# which is Rails 4.0.0 default as well
|
16
|
+
# @option options [String] :seed used as namespace to generate UUIDv5 from the existing ID
|
17
17
|
# @return [none]
|
18
|
-
def primary_key_to_uuid(table, options={})
|
19
|
-
default= options[:default] || 'gen_random_uuid()'
|
18
|
+
def primary_key_to_uuid(table, options = {})
|
19
|
+
default = options[:default] || 'gen_random_uuid()'
|
20
|
+
seed = options[:seed]
|
20
21
|
|
21
|
-
column= connection.primary_key(table)
|
22
|
+
column = connection.primary_key(table)
|
22
23
|
|
23
24
|
execute %Q{ALTER TABLE #{table}
|
24
25
|
ALTER COLUMN #{column} DROP DEFAULT,
|
25
|
-
ALTER COLUMN #{column} SET DATA TYPE UUID USING (#{to_uuid_pg(column)}),
|
26
|
+
ALTER COLUMN #{column} SET DATA TYPE UUID USING (#{to_uuid_pg(column, seed)}),
|
26
27
|
ALTER COLUMN #{column} SET DEFAULT #{default}}
|
27
28
|
|
28
29
|
execute %Q{DROP SEQUENCE IF EXISTS #{table}_#{column}_seq} rescue nil
|
@@ -32,22 +33,24 @@ module Webdack
|
|
32
33
|
#
|
33
34
|
# @param table [Symbol]
|
34
35
|
# @param column [Symbol]
|
36
|
+
# @param seed [String]
|
35
37
|
#
|
36
38
|
# @return [none]
|
37
|
-
def column_to_uuid(table, column)
|
39
|
+
def column_to_uuid(table, column, seed: nil)
|
38
40
|
execute %Q{ALTER TABLE #{table}
|
39
|
-
ALTER COLUMN #{column} SET DATA TYPE UUID USING (#{to_uuid_pg(column)})}
|
41
|
+
ALTER COLUMN #{column} SET DATA TYPE UUID USING (#{to_uuid_pg(column, seed)})}
|
40
42
|
end
|
41
43
|
|
42
44
|
# Converts columns to UUID, migrates all data by left padding with 0's
|
43
45
|
#
|
44
46
|
# @param table [Symbol]
|
45
47
|
# @param columns
|
48
|
+
# @param seed [String]
|
46
49
|
#
|
47
50
|
# @return [none]
|
48
|
-
def columns_to_uuid(table, *columns)
|
51
|
+
def columns_to_uuid(table, *columns, seed: nil)
|
49
52
|
columns.each do |column|
|
50
|
-
column_to_uuid(table, column)
|
53
|
+
column_to_uuid(table, column, seed: seed)
|
51
54
|
end
|
52
55
|
end
|
53
56
|
|
@@ -68,10 +71,12 @@ module Webdack
|
|
68
71
|
# @param table[Symbol]
|
69
72
|
# @param column [Symbol] it will change data in corresponding <column>_id
|
70
73
|
# @param entities [String] data referring these entities will be converted
|
71
|
-
|
72
|
-
|
74
|
+
# @param seed [String]
|
75
|
+
|
76
|
+
def polymorphic_column_data_for_uuid(table, column, *entities, seed: nil)
|
77
|
+
list_of_entities = entities.map { |e| "'#{e}'" }.join(', ')
|
73
78
|
execute %Q{
|
74
|
-
UPDATE #{table} SET #{column}_id= #{to_uuid_pg("#{column}_id")}
|
79
|
+
UPDATE #{table} SET #{column}_id= #{to_uuid_pg("#{column}_id", seed)}
|
75
80
|
WHERE #{column}_type in (#{list_of_entities})
|
76
81
|
}
|
77
82
|
end
|
@@ -88,29 +93,38 @@ module Webdack
|
|
88
93
|
# - Restore all foreign keys
|
89
94
|
#
|
90
95
|
# @param table[Symbol]
|
96
|
+
# @param seed [String]
|
97
|
+
|
91
98
|
# @note Works only with Rails 4.2 or newer
|
92
|
-
def primary_key_and_all_references_to_uuid(table)
|
99
|
+
def primary_key_and_all_references_to_uuid(table, seed: nil)
|
93
100
|
fk_specs = foreign_keys_into(table)
|
94
101
|
|
95
102
|
drop_foreign_keys(fk_specs)
|
96
103
|
|
97
|
-
primary_key_to_uuid(table)
|
104
|
+
primary_key_to_uuid(table, seed: seed)
|
98
105
|
|
99
106
|
fk_specs.each do |fk_spec|
|
100
|
-
columns_to_uuid fk_spec[:from_table], fk_spec[:column]
|
107
|
+
columns_to_uuid fk_spec[:from_table], fk_spec[:column], seed: seed
|
101
108
|
end
|
102
109
|
|
103
110
|
create_foreign_keys(fk_specs.deep_dup)
|
104
111
|
end
|
105
112
|
|
106
113
|
private
|
114
|
+
|
107
115
|
# Prepare a fragment that can be used in SQL statements that converts teh data value
|
108
116
|
# from integer, string, or UUID to valid UUID string as per Postgres guidelines
|
109
117
|
#
|
110
118
|
# @param column [Symbol]
|
119
|
+
# @param seed [String]
|
120
|
+
|
111
121
|
# @return [String]
|
112
|
-
def to_uuid_pg(column)
|
113
|
-
|
122
|
+
def to_uuid_pg(column, seed)
|
123
|
+
if seed
|
124
|
+
"uuid_generate_v5('#{seed}'::uuid, #{column}::text)"
|
125
|
+
else
|
126
|
+
"uuid(lpad(replace(text(#{column}),'-',''), 32, '0'))"
|
127
|
+
end
|
114
128
|
end
|
115
129
|
end
|
116
130
|
end
|
@@ -1,13 +1,25 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
|
+
class ActiveRecordMigration
|
4
|
+
def self.seed_with(seed)
|
5
|
+
@@seed = seed
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
def seed
|
10
|
+
@@seed
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
3
14
|
class BasicMigration < ActiveRecordMigration
|
4
15
|
def change
|
5
16
|
reversible do |dir|
|
6
17
|
dir.up do
|
7
18
|
enable_extension 'pgcrypto'
|
19
|
+
enable_extension 'uuid-ossp'
|
8
20
|
|
9
|
-
primary_key_to_uuid :students
|
10
|
-
columns_to_uuid :students, :city_id, :institution_id
|
21
|
+
primary_key_to_uuid :students, seed: seed
|
22
|
+
columns_to_uuid :students, :city_id, :institution_id, seed: seed
|
11
23
|
end
|
12
24
|
|
13
25
|
dir.down do
|
@@ -22,13 +34,14 @@ class MigrateAllOneGo < ActiveRecordMigration
|
|
22
34
|
reversible do |dir|
|
23
35
|
dir.up do
|
24
36
|
enable_extension 'pgcrypto'
|
37
|
+
enable_extension 'uuid-ossp'
|
25
38
|
|
26
|
-
primary_key_to_uuid :cities
|
27
|
-
primary_key_to_uuid :colleges
|
28
|
-
primary_key_to_uuid :schools
|
39
|
+
primary_key_to_uuid :cities, seed: seed
|
40
|
+
primary_key_to_uuid :colleges, seed: seed
|
41
|
+
primary_key_to_uuid :schools, seed: seed
|
29
42
|
|
30
|
-
primary_key_to_uuid :students
|
31
|
-
columns_to_uuid :students, :city_id, :institution_id
|
43
|
+
primary_key_to_uuid :students, seed: seed
|
44
|
+
columns_to_uuid :students, :city_id, :institution_id, seed: seed
|
32
45
|
end
|
33
46
|
|
34
47
|
dir.down do
|
@@ -43,8 +56,9 @@ class MigrateWithFk < ActiveRecordMigration
|
|
43
56
|
reversible do |dir|
|
44
57
|
dir.up do
|
45
58
|
enable_extension 'pgcrypto'
|
59
|
+
enable_extension 'uuid-ossp'
|
46
60
|
|
47
|
-
primary_key_and_all_references_to_uuid :cities
|
61
|
+
primary_key_and_all_references_to_uuid :cities, seed: seed
|
48
62
|
end
|
49
63
|
|
50
64
|
dir.down do
|
@@ -59,15 +73,16 @@ class MigrateStep01 < ActiveRecordMigration
|
|
59
73
|
reversible do |dir|
|
60
74
|
dir.up do
|
61
75
|
enable_extension 'pgcrypto'
|
76
|
+
enable_extension 'uuid-ossp'
|
62
77
|
|
63
|
-
primary_key_to_uuid :cities
|
64
|
-
primary_key_to_uuid :colleges
|
78
|
+
primary_key_to_uuid :cities, seed: seed
|
79
|
+
primary_key_to_uuid :colleges, seed: seed
|
65
80
|
|
66
|
-
primary_key_to_uuid :students
|
67
|
-
columns_to_uuid :students, :city_id
|
81
|
+
primary_key_to_uuid :students, seed: seed
|
82
|
+
columns_to_uuid :students, :city_id, seed: seed
|
68
83
|
|
69
84
|
change_column :students, :institution_id, :string
|
70
|
-
polymorphic_column_data_for_uuid :students, :institution, 'College'
|
85
|
+
polymorphic_column_data_for_uuid :students, :institution, 'College', seed: seed
|
71
86
|
end
|
72
87
|
|
73
88
|
dir.down do
|
@@ -81,8 +96,8 @@ class MigrateStep02 < ActiveRecordMigration
|
|
81
96
|
def change
|
82
97
|
reversible do |dir|
|
83
98
|
dir.up do
|
84
|
-
primary_key_to_uuid :schools
|
85
|
-
|
99
|
+
primary_key_to_uuid :schools, seed: seed
|
100
|
+
polymorphic_column_data_for_uuid :students, :institution, 'School', seed: seed
|
86
101
|
end
|
87
102
|
|
88
103
|
dir.down do
|
@@ -117,152 +132,161 @@ describe Webdack::UUIDMigration::Helpers do
|
|
117
132
|
initial_setup
|
118
133
|
end
|
119
134
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
135
|
+
[nil, SecureRandom.uuid].each do |seed|
|
136
|
+
context "when the seed is #{seed}" do
|
137
|
+
let(:seed_value) { seed }
|
138
|
+
|
139
|
+
describe 'Basic Test' do
|
140
|
+
it 'should migrate keys correctly' do
|
141
|
+
# Select a random student
|
142
|
+
student = Student.all.to_a.sample
|
143
|
+
|
144
|
+
# Store these values to check against later
|
145
|
+
original_name= student.name
|
146
|
+
original_ids= [student.id, student.city_id, student.institution_id].map{|i| i.to_i}
|
147
|
+
|
148
|
+
# Migrate and verify that all indexes and primary keys are intact
|
149
|
+
expect {
|
150
|
+
BasicMigration.seed_with(seed_value).migrate(:up)
|
151
|
+
reset_columns_data
|
152
|
+
}.to_not change {
|
153
|
+
indexes= Student.connection.indexes(:students).sort_by { |i| i.name }.map do |i|
|
154
|
+
[i.table, i.name, i.unique, i.columns, i.lengths, i.orders, i.where]
|
155
|
+
end
|
156
|
+
|
157
|
+
[indexes, Student.connection.primary_key(:students)]
|
158
|
+
}
|
159
|
+
|
160
|
+
# Verify that our data is still there
|
161
|
+
student= Student.where(name: original_name).first
|
162
|
+
|
163
|
+
# Verify that data in id columns have been migrated to UUID by verifying the format
|
164
|
+
[student.id, student.city_id, student.institution_id].each do |id|
|
165
|
+
expect(id).to match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
|
166
|
+
end
|
167
|
+
|
168
|
+
unless seed_value
|
169
|
+
# Verify that it is possible to retirve original id values (only without seed!)
|
170
|
+
ids= [student.id, student.city_id, student.institution_id].map{|i| i.gsub('-','').to_i}
|
171
|
+
expect(ids).to eq(original_ids)
|
172
|
+
end
|
173
|
+
|
174
|
+
# Verify that schema reprts the migrated columns to be uuid type
|
175
|
+
columns= Student.connection.columns(:students)
|
176
|
+
[:id, :city_id, :institution_id].each do |column|
|
177
|
+
expect(columns.find{|c| c.name == column.to_s}.type).to eq :uuid
|
178
|
+
end
|
179
|
+
|
180
|
+
# Verify that primary key has correct default
|
181
|
+
expect(columns.find{|c| c.name == 'id'}.default_function).to eq 'gen_random_uuid()'
|
136
182
|
end
|
137
|
-
|
138
|
-
[indexes, Student.connection.primary_key(:students)]
|
139
|
-
}
|
140
|
-
|
141
|
-
# Verify that our data is still there
|
142
|
-
student= Student.where(name: original_name).first
|
143
|
-
|
144
|
-
# Verify that data in id columns have been migrated to UUID by verifying the format
|
145
|
-
[student.id, student.city_id, student.institution_id].each do |id|
|
146
|
-
expect(id).to match(/^0{8}-0{4}-0{4}-0{4}-\d{12}$/)
|
147
183
|
end
|
148
184
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
expect(columns.find{|c| c.name == column.to_s}.type).to eq :uuid
|
185
|
+
it 'should migrate entire database in one go' do
|
186
|
+
expect {
|
187
|
+
MigrateAllOneGo.seed_with(seed_value).migrate(:up)
|
188
|
+
reset_columns_data
|
189
|
+
}.to_not change {
|
190
|
+
key_relationships
|
191
|
+
}
|
157
192
|
end
|
158
193
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
end
|
163
|
-
|
164
|
-
it 'should migrate entire database in one go' do
|
165
|
-
expect {
|
166
|
-
MigrateAllOneGo.migrate(:up)
|
167
|
-
reset_columns_data
|
168
|
-
}.to_not change {
|
169
|
-
key_relationships
|
170
|
-
}
|
171
|
-
end
|
172
|
-
|
173
|
-
it 'should migrate a primary key and all columns referencing it using foreign keys', rails_4_2_or_newer: true do
|
174
|
-
# Create 2 more tables similar to the way new version of Rails will do
|
175
|
-
create_tables_with_fk
|
194
|
+
it 'should migrate a primary key and all columns referencing it using foreign keys', rails_4_2_or_newer: true do
|
195
|
+
# Create 2 more tables similar to the way new version of Rails will do
|
196
|
+
create_tables_with_fk
|
176
197
|
|
177
|
-
|
178
|
-
|
198
|
+
# Add Foreign key for this reference as well
|
199
|
+
ActiveRecord::Base.connection.add_foreign_key :students, :cities
|
179
200
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
201
|
+
expect {
|
202
|
+
MigrateWithFk.seed_with(seed_value).migrate(:up)
|
203
|
+
reset_columns_data
|
204
|
+
}.to_not change {
|
205
|
+
key_relationships
|
206
|
+
}
|
207
|
+
end
|
187
208
|
|
188
|
-
|
189
|
-
|
209
|
+
it 'should handle nulls' do
|
210
|
+
Student.create(name: 'Student without city or institution')
|
190
211
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
212
|
+
expect {
|
213
|
+
MigrateAllOneGo.seed_with(seed_value).migrate(:up)
|
214
|
+
reset_columns_data
|
215
|
+
}.to_not change {
|
216
|
+
key_relationships
|
217
|
+
}
|
218
|
+
end
|
198
219
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
220
|
+
it 'should migrate in steps for polymorphic association' do
|
221
|
+
expect {
|
222
|
+
MigrateStep01.seed_with(seed_value).migrate(:up)
|
223
|
+
reset_columns_data
|
224
|
+
}.to_not change {
|
225
|
+
key_relationships
|
226
|
+
}
|
227
|
+
|
228
|
+
expect {
|
229
|
+
MigrateStep02.seed_with(seed_value).migrate(:up)
|
230
|
+
reset_columns_data
|
231
|
+
|
232
|
+
}.to_not change {
|
233
|
+
key_relationships
|
234
|
+
}
|
235
|
+
end
|
214
236
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
237
|
+
it 'should allow running same migration data even if it was already migrated' do
|
238
|
+
expect {
|
239
|
+
MigrateStep01.seed_with(seed_value).migrate(:up)
|
240
|
+
# Run again
|
241
|
+
MigrateStep01.seed_with(seed_value).migrate(:up)
|
242
|
+
reset_columns_data
|
243
|
+
}.to_not change {
|
244
|
+
key_relationships
|
245
|
+
}
|
246
|
+
|
247
|
+
expect {
|
248
|
+
MigrateStep02.seed_with(seed_value).migrate(:up)
|
249
|
+
# Run again
|
250
|
+
MigrateStep02.seed_with(seed_value).migrate(:up)
|
251
|
+
reset_columns_data
|
252
|
+
}.to_not change {
|
253
|
+
key_relationships
|
254
|
+
}
|
255
|
+
end
|
234
256
|
|
235
|
-
|
236
|
-
|
237
|
-
|
257
|
+
it 'should allow updation, deletion, and new entity creation' do
|
258
|
+
MigrateAllOneGo.seed_with(seed_value).migrate(:up)
|
259
|
+
reset_columns_data
|
238
260
|
|
239
|
-
|
240
|
-
|
261
|
+
# Select a random student
|
262
|
+
student = Student.all.to_a.sample
|
241
263
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
264
|
+
id= student.id
|
265
|
+
student.name= 'New student 01'
|
266
|
+
student.save
|
267
|
+
student = Student.find(id)
|
246
268
|
|
247
|
-
|
269
|
+
expect(student.name).to eq 'New student 01'
|
248
270
|
|
249
|
-
|
271
|
+
expect { student.destroy }.to change { Student.count }.by(-1)
|
250
272
|
|
251
|
-
|
273
|
+
expect {Student.find(id)}.to raise_exception(ActiveRecord::RecordNotFound)
|
252
274
|
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
275
|
+
student= Student.create(
|
276
|
+
name: 'New student 02',
|
277
|
+
city: City.where(name: 'City 2').first,
|
278
|
+
institution: School.where(name: 'School 1').first
|
279
|
+
)
|
258
280
|
|
259
|
-
|
260
|
-
|
281
|
+
expect(City.where(name: 'City 2').first.students.where(name: 'New student 02').first.name).to eq 'New student 02'
|
282
|
+
expect(School.where(name: 'School 1').first.students.where(name: 'New student 02').first.name).to eq 'New student 02'
|
261
283
|
|
262
|
-
|
284
|
+
College.where(name: 'College 3').first.students << student
|
263
285
|
|
264
|
-
|
286
|
+
student.reload
|
265
287
|
|
266
|
-
|
288
|
+
expect(student.institution.name).to eq 'College 3'
|
289
|
+
end
|
290
|
+
end
|
267
291
|
end
|
268
292
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webdack-uuid_migration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Deepak Kumar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,6 +116,7 @@ executables: []
|
|
116
116
|
extensions: []
|
117
117
|
extra_rdoc_files: []
|
118
118
|
files:
|
119
|
+
- ".github/dependabot.yml"
|
119
120
|
- ".gitignore"
|
120
121
|
- ".travis.yml"
|
121
122
|
- Gemfile
|
@@ -163,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
164
|
- !ruby/object:Gem::Version
|
164
165
|
version: '0'
|
165
166
|
requirements: []
|
166
|
-
rubygems_version: 3.
|
167
|
+
rubygems_version: 3.2.3
|
167
168
|
signing_key:
|
168
169
|
specification_version: 4
|
169
170
|
summary: Useful helpers to migrate Integer id columns to UUID in PostgreSql.
|