strong_migrations 0.7.9 → 0.8.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 +5 -0
- data/LICENSE.txt +1 -1
- data/README.md +4 -29
- data/lib/strong_migrations/checker.rb +23 -28
- data/lib/strong_migrations/migration.rb +3 -0
- data/lib/strong_migrations/safe_methods.rb +0 -15
- data/lib/strong_migrations/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f885cc0a8b0fb30e3bc4d000e82584fdf3850943563b4b426d3cfc39d70015c
|
4
|
+
data.tar.gz: d0dd29ea45ccc3b7c571e8bf03fa98c401d5489d1f5459488a0eff411cf6880e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c03a023d4c593d2868e524ca0f3e7ebd93e07e087e02f0d7090210b00fdb1fdc50d2abb74ab7df4131bddc6052fc5b9c10ce902ede8c708927476e40ad49d07f
|
7
|
+
data.tar.gz: 479e34db0101a15e1db1a593f01773da5665baa5cda3403ad32ea58dca00a0d0faee0ef813cf1975b7e9f99f1591e7f4f2be5c456c96fd6ae4633b48b6a0b3b4
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -15,7 +15,7 @@ Supports for PostgreSQL, MySQL, and MariaDB
|
|
15
15
|
Add this line to your application’s Gemfile:
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
gem
|
18
|
+
gem "strong_migrations"
|
19
19
|
```
|
20
20
|
|
21
21
|
And run:
|
@@ -119,6 +119,7 @@ end
|
|
119
119
|
```
|
120
120
|
|
121
121
|
4. Deploy and run migration
|
122
|
+
5. Remove the line added in step 1
|
122
123
|
|
123
124
|
### Adding a column with a default value
|
124
125
|
|
@@ -540,9 +541,7 @@ end
|
|
540
541
|
|
541
542
|
#### Good
|
542
543
|
|
543
|
-
Add the foreign key without validating existing rows
|
544
|
-
|
545
|
-
For Rails 5.2+, use:
|
544
|
+
Add the foreign key without validating existing rows:
|
546
545
|
|
547
546
|
```ruby
|
548
547
|
class AddForeignKeyOnUsers < ActiveRecord::Migration[7.0]
|
@@ -552,7 +551,7 @@ class AddForeignKeyOnUsers < ActiveRecord::Migration[7.0]
|
|
552
551
|
end
|
553
552
|
```
|
554
553
|
|
555
|
-
Then
|
554
|
+
Then validate them in a separate migration.
|
556
555
|
|
557
556
|
```ruby
|
558
557
|
class ValidateForeignKeyOnUsers < ActiveRecord::Migration[7.0]
|
@@ -562,30 +561,6 @@ class ValidateForeignKeyOnUsers < ActiveRecord::Migration[7.0]
|
|
562
561
|
end
|
563
562
|
```
|
564
563
|
|
565
|
-
For Rails < 5.2, use:
|
566
|
-
|
567
|
-
```ruby
|
568
|
-
class AddForeignKeyOnUsers < ActiveRecord::Migration[5.1]
|
569
|
-
def change
|
570
|
-
safety_assured do
|
571
|
-
execute 'ALTER TABLE "users" ADD CONSTRAINT "fk_rails_c1e9b98e31" FOREIGN KEY ("order_id") REFERENCES "orders" ("id") NOT VALID'
|
572
|
-
end
|
573
|
-
end
|
574
|
-
end
|
575
|
-
```
|
576
|
-
|
577
|
-
Then:
|
578
|
-
|
579
|
-
```ruby
|
580
|
-
class ValidateForeignKeyOnUsers < ActiveRecord::Migration[5.1]
|
581
|
-
def change
|
582
|
-
safety_assured do
|
583
|
-
execute 'ALTER TABLE "users" VALIDATE CONSTRAINT "fk_rails_c1e9b98e31"'
|
584
|
-
end
|
585
|
-
end
|
586
|
-
end
|
587
|
-
```
|
588
|
-
|
589
564
|
### Adding a json column
|
590
565
|
|
591
566
|
#### Bad
|
@@ -86,8 +86,7 @@ module StrongMigrations
|
|
86
86
|
options ||= {}
|
87
87
|
default = options[:default]
|
88
88
|
|
89
|
-
if !default.nil? && !
|
90
|
-
|
89
|
+
if !default.nil? && !add_column_default_safe?
|
91
90
|
if options[:null] == false
|
92
91
|
options = options.except(:null)
|
93
92
|
append = "
|
@@ -230,8 +229,10 @@ Then add the foreign key in separate migrations."
|
|
230
229
|
validate_code = constraint_str("ALTER TABLE %s VALIDATE CONSTRAINT %s", [table, constraint_name])
|
231
230
|
remove_code = constraint_str("ALTER TABLE %s DROP CONSTRAINT %s", [table, constraint_name])
|
232
231
|
|
232
|
+
constraint_methods = ar_version >= 6.1
|
233
|
+
|
233
234
|
validate_constraint_code =
|
234
|
-
if
|
235
|
+
if constraint_methods
|
235
236
|
String.new(command_str(:validate_check_constraint, [table, {name: constraint_name}]))
|
236
237
|
else
|
237
238
|
String.new(safety_assured_str(validate_code))
|
@@ -242,7 +243,7 @@ Then add the foreign key in separate migrations."
|
|
242
243
|
|
243
244
|
validate_constraint_code << "\n #{command_str(:change_column_null, change_args)}"
|
244
245
|
|
245
|
-
if
|
246
|
+
if constraint_methods
|
246
247
|
validate_constraint_code << "\n #{command_str(:remove_check_constraint, [table, {name: constraint_name}])}"
|
247
248
|
else
|
248
249
|
validate_constraint_code << "\n #{safety_assured_str(remove_code)}"
|
@@ -252,7 +253,7 @@ Then add the foreign key in separate migrations."
|
|
252
253
|
return safe_change_column_null(add_code, validate_code, change_args, remove_code) if StrongMigrations.safe_by_default
|
253
254
|
|
254
255
|
add_constraint_code =
|
255
|
-
if
|
256
|
+
if constraint_methods
|
256
257
|
# only quote when needed
|
257
258
|
expr_column = column.to_s =~ /\A[a-z0-9_]+\z/ ? column : connection.quote_column_name(column)
|
258
259
|
command_str(:add_check_constraint, [table, "#{expr_column} IS NOT NULL", {name: constraint_name, validate: false}])
|
@@ -275,32 +276,14 @@ Then add the foreign key in separate migrations."
|
|
275
276
|
from_table, to_table, options = args
|
276
277
|
options ||= {}
|
277
278
|
|
278
|
-
|
279
|
-
validate = options.fetch(:validate, true) || ar_version < 5.2
|
279
|
+
validate = options.fetch(:validate, true)
|
280
280
|
|
281
281
|
if postgresql? && validate
|
282
|
-
|
283
|
-
# fk name logic from rails
|
284
|
-
primary_key = options[:primary_key] || "id"
|
285
|
-
column = options[:column] || "#{to_table.to_s.singularize}_id"
|
286
|
-
hashed_identifier = Digest::SHA256.hexdigest("#{from_table}_#{column}_fk").first(10)
|
287
|
-
fk_name = options[:name] || "fk_rails_#{hashed_identifier}"
|
288
|
-
|
289
|
-
add_code = constraint_str("ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s) NOT VALID", [from_table, fk_name, column, to_table, primary_key])
|
290
|
-
validate_code = constraint_str("ALTER TABLE %s VALIDATE CONSTRAINT %s", [from_table, fk_name])
|
291
|
-
|
292
|
-
return safe_add_foreign_key_code(from_table, to_table, add_code, validate_code) if StrongMigrations.safe_by_default
|
293
|
-
|
294
|
-
raise_error :add_foreign_key,
|
295
|
-
add_foreign_key_code: safety_assured_str(add_code),
|
296
|
-
validate_foreign_key_code: safety_assured_str(validate_code)
|
297
|
-
else
|
298
|
-
return safe_add_foreign_key(from_table, to_table, options) if StrongMigrations.safe_by_default
|
282
|
+
return safe_add_foreign_key(from_table, to_table, options) if StrongMigrations.safe_by_default
|
299
283
|
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
end
|
284
|
+
raise_error :add_foreign_key,
|
285
|
+
add_foreign_key_code: command_str("add_foreign_key", [from_table, to_table, options.merge(validate: false)]),
|
286
|
+
validate_foreign_key_code: command_str("validate_foreign_key", [from_table, to_table])
|
304
287
|
end
|
305
288
|
when :validate_foreign_key
|
306
289
|
if postgresql? && writes_blocked?
|
@@ -598,5 +581,17 @@ Then add the foreign key in separate migrations."
|
|
598
581
|
def new_table?(table)
|
599
582
|
@new_tables.include?(table.to_s)
|
600
583
|
end
|
584
|
+
|
585
|
+
def add_column_default_safe?
|
586
|
+
if postgresql?
|
587
|
+
postgresql_version >= Gem::Version.new("11")
|
588
|
+
elsif mysql?
|
589
|
+
mysql_version >= Gem::Version.new("8.0.12")
|
590
|
+
elsif mariadb?
|
591
|
+
mariadb_version >= Gem::Version.new("10.3.2")
|
592
|
+
else
|
593
|
+
false
|
594
|
+
end
|
595
|
+
end
|
601
596
|
end
|
602
597
|
end
|
@@ -9,6 +9,9 @@ module StrongMigrations
|
|
9
9
|
def method_missing(method, *args)
|
10
10
|
return super if is_a?(ActiveRecord::Schema)
|
11
11
|
|
12
|
+
# Active Record 7.0.2+ versioned schema
|
13
|
+
return super if defined?(ActiveRecord::Schema::Definition) && is_a?(ActiveRecord::Schema::Definition)
|
14
|
+
|
12
15
|
strong_migrations_checker.perform(method, *args) do
|
13
16
|
super
|
14
17
|
end
|
@@ -56,21 +56,6 @@ module StrongMigrations
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
def safe_add_foreign_key_code(from_table, to_table, add_code, validate_code)
|
60
|
-
@migration.reversible do |dir|
|
61
|
-
dir.up do
|
62
|
-
@migration.safety_assured do
|
63
|
-
@migration.execute(add_code)
|
64
|
-
disable_transaction
|
65
|
-
@migration.execute(validate_code)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
dir.down do
|
69
|
-
@migration.remove_foreign_key(from_table, to_table)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
59
|
def safe_add_check_constraint(table, expression, add_options, validate_options)
|
75
60
|
@migration.reversible do |dir|
|
76
61
|
dir.up do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strong_migrations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2022-02-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -18,14 +18,14 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '5'
|
21
|
+
version: '5.2'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: '5'
|
28
|
+
version: '5.2'
|
29
29
|
description:
|
30
30
|
email:
|
31
31
|
- andrew@ankane.org
|
@@ -61,14 +61,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
61
|
requirements:
|
62
62
|
- - ">="
|
63
63
|
- !ruby/object:Gem::Version
|
64
|
-
version: '2.
|
64
|
+
version: '2.6'
|
65
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
67
|
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
|
-
rubygems_version: 3.
|
71
|
+
rubygems_version: 3.3.3
|
72
72
|
signing_key:
|
73
73
|
specification_version: 4
|
74
74
|
summary: Catch unsafe migrations in development
|