activerecord-databasevalidations 1.0.0 → 1.0.2
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/.ruby-version +1 -0
- data/CHANGELOG.md +8 -0
- data/activerecord-databasevalidations.gemspec +4 -1
- data/dev.yml +14 -0
- data/lib/active_record/database_validations/version.rb +1 -1
- data/lib/active_record/validations/database_constraints.rb +8 -0
- data/shipit.rubygems.yml +0 -0
- data/test/database_constraints_validator_test.rb +25 -0
- data/test/test_helper.rb +14 -5
- metadata +23 -10
- data/test/database.yml +0 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2a7d5be15344fd9bf8b1c3acfca03f3ac9e0c3ca97b18360b120b291b6a6b4d1
|
|
4
|
+
data.tar.gz: 6c87a9818c8bbf67a5ff8af94a078f6b93a53f46d7492c046489bcfd0668d29b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 77bbf9e2ac7a0876889abb7c338dfc06934740bac6ef4e13aab64e92a4184552bce96729196e0e9f548ec7a1cc3ab14b83c82d57064acee5abc8a7f88bf1f19e
|
|
7
|
+
data.tar.gz: da0d21684e48d7f3569014881bf87ea1ddc4fd3872e970f2a5b079f8b95f2dc780a922327e694b8ad7f4f7befb8e10c8ee2faa394835025df1c128a41a8f7062
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.4.1
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Unreleased
|
|
2
2
|
|
|
3
|
+
# 1.0.2 (Feb 5, 2025)
|
|
4
|
+
|
|
5
|
+
* Disable size + range validations for non-MySQL adapters.
|
|
6
|
+
|
|
7
|
+
# 1.0.1 (May 9, 2023)
|
|
8
|
+
|
|
9
|
+
* Updated gemspec to point to the proper home page.
|
|
10
|
+
|
|
3
11
|
# 1.0.0 (May 12, 2021)
|
|
4
12
|
|
|
5
13
|
* Removed the utf8mb3 validator as it is a bit anachronistic in 2021.
|
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
|
10
10
|
spec.email = ["willem@railsdoctors.com"]
|
|
11
11
|
spec.summary = %q{Add validations to your ActiveRecord models based on MySQL database constraints.}
|
|
12
12
|
spec.description = %q{Opt-in validations for your ActiveRecord models based on your MySQL database constraints, including text field size, UTF-8 encoding issues, and NOT NULL constraints.}
|
|
13
|
-
spec.homepage = "https://github.com/
|
|
13
|
+
spec.homepage = "https://github.com/Shopify/activerecord-databasevalidations"
|
|
14
14
|
spec.license = "MIT"
|
|
15
15
|
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
|
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
19
|
spec.require_paths = ["lib"]
|
|
20
20
|
|
|
21
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
|
22
|
+
|
|
21
23
|
spec.required_ruby_version = '>= 2.5'
|
|
22
24
|
|
|
23
25
|
spec.add_runtime_dependency "activerecord", ">= 5.2"
|
|
@@ -26,4 +28,5 @@ Gem::Specification.new do |spec|
|
|
|
26
28
|
spec.add_development_dependency "rake"
|
|
27
29
|
spec.add_development_dependency "minitest"
|
|
28
30
|
spec.add_development_dependency "mysql2"
|
|
31
|
+
spec.add_development_dependency "sqlite3"
|
|
29
32
|
end
|
data/dev.yml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# This file is for Shopify employees development environment.
|
|
2
|
+
# If you are an external contributor you don't have to bother with it.
|
|
3
|
+
name: activerecord-databasevalidations
|
|
4
|
+
|
|
5
|
+
up:
|
|
6
|
+
- packages:
|
|
7
|
+
- mysql_client
|
|
8
|
+
- ruby
|
|
9
|
+
- bundler
|
|
10
|
+
- mysql
|
|
11
|
+
- custom:
|
|
12
|
+
name: Create database
|
|
13
|
+
meet: mysql -uroot -h $MYSQL_HOST -P $MYSQL_PORT -e "CREATE DATABASE database_validations"
|
|
14
|
+
met?: mysql -uroot -h $MYSQL_HOST -P $MYSQL_PORT database_validations -e "SELECT 1" &> /dev/null
|
|
@@ -35,6 +35,7 @@ module ActiveRecord
|
|
|
35
35
|
|
|
36
36
|
def size_validator(klass, column)
|
|
37
37
|
return unless constraints.include?(:size)
|
|
38
|
+
return unless mysql_adapter?(klass)
|
|
38
39
|
return unless column.text? || column.binary?
|
|
39
40
|
|
|
40
41
|
maximum, type, encoding = ActiveRecord::DatabaseValidations::MySQL.column_size_limit(column)
|
|
@@ -47,6 +48,7 @@ module ActiveRecord
|
|
|
47
48
|
|
|
48
49
|
def range_validator(klass, column)
|
|
49
50
|
return unless constraints.include?(:range)
|
|
51
|
+
return unless mysql_adapter?(klass)
|
|
50
52
|
return unless column.number?
|
|
51
53
|
|
|
52
54
|
args = { attributes: [column.name.to_sym], class: klass, allow_nil: true }
|
|
@@ -77,6 +79,12 @@ module ActiveRecord
|
|
|
77
79
|
validator.validate(record)
|
|
78
80
|
end
|
|
79
81
|
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def mysql_adapter?(klass)
|
|
86
|
+
klass.connection.class < ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
|
|
87
|
+
end
|
|
80
88
|
end
|
|
81
89
|
end
|
|
82
90
|
end
|
data/shipit.rubygems.yml
ADDED
|
File without changes
|
|
@@ -43,6 +43,23 @@ class Num < ActiveRecord::Base
|
|
|
43
43
|
validates :decimal, :unsigned_decimal, :tinyint, :smallint, :mediumint, :int, :bigint, :unsigned_int, database_constraints: :range
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
+
class SQLiteModel < ActiveRecord::Base
|
|
47
|
+
self.abstract_class = true
|
|
48
|
+
establish_connection(adapter: "sqlite3", database: ":memory:")
|
|
49
|
+
connection.create_table("not_my_sql_models", force: true) do |t|
|
|
50
|
+
t.text "text_column"
|
|
51
|
+
t.bigint "bigint_column"
|
|
52
|
+
t.string "not_null_column", null: false
|
|
53
|
+
t.string "null_column"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class NotMySQLModel < SQLiteModel
|
|
58
|
+
validates :text_column, database_constraints: :size
|
|
59
|
+
validates :bigint_column, database_constraints: :range
|
|
60
|
+
validates :not_null_column, database_constraints: :not_null
|
|
61
|
+
end
|
|
62
|
+
|
|
46
63
|
class DatabaseConstraintsValidatorTest < Minitest::Test
|
|
47
64
|
include DataLossAssertions
|
|
48
65
|
|
|
@@ -103,6 +120,14 @@ class DatabaseConstraintsValidatorTest < Minitest::Test
|
|
|
103
120
|
assert_equal Encoding.find('utf-8'), subvalidators.first.encoding
|
|
104
121
|
end
|
|
105
122
|
|
|
123
|
+
def test_skips_size_and_range_validators_for_non_mysql_adapter
|
|
124
|
+
record = NotMySQLModel.new(not_null_column: "something")
|
|
125
|
+
assert(record.valid?)
|
|
126
|
+
|
|
127
|
+
record.not_null_column = nil
|
|
128
|
+
refute(record.valid?)
|
|
129
|
+
end
|
|
130
|
+
|
|
106
131
|
def test_not_null_columns_with_a_default_value
|
|
107
132
|
assert Foo.new.valid?
|
|
108
133
|
assert Foo.new(checked: 1).valid?
|
data/test/test_helper.rb
CHANGED
|
@@ -18,7 +18,7 @@ module DataLossAssertions
|
|
|
18
18
|
|
|
19
19
|
persisted_values = record.reload.attributes.slice(*attributes)
|
|
20
20
|
refute_equal provided_values, persisted_values
|
|
21
|
-
rescue RangeError
|
|
21
|
+
rescue ActiveRecord::RangeError, ActiveRecord::StatementInvalid, ActiveModel::RangeError
|
|
22
22
|
pass
|
|
23
23
|
end
|
|
24
24
|
|
|
@@ -33,8 +33,17 @@ module DataLossAssertions
|
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
mysql_host = ENV.fetch("MYSQL_HOST") { "localhost" }
|
|
37
|
+
mysql_port = ENV.fetch("MYSQL_PORT") { 3306 }
|
|
38
|
+
mysql_connection_config = {
|
|
39
|
+
adapter: "mysql2",
|
|
40
|
+
database: "database_validations",
|
|
41
|
+
username: "root",
|
|
42
|
+
encoding: "utf8mb4",
|
|
43
|
+
strict: false,
|
|
44
|
+
host: mysql_host,
|
|
45
|
+
port: mysql_port,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
ActiveRecord::Base.establish_connection(mysql_connection_config)
|
|
40
49
|
I18n.enforce_available_locales = false
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activerecord-databasevalidations
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Willem van Bergen
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 2025-02-05 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activerecord
|
|
@@ -80,6 +79,20 @@ dependencies:
|
|
|
80
79
|
- - ">="
|
|
81
80
|
- !ruby/object:Gem::Version
|
|
82
81
|
version: '0'
|
|
82
|
+
- !ruby/object:Gem::Dependency
|
|
83
|
+
name: sqlite3
|
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
type: :development
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
83
96
|
description: Opt-in validations for your ActiveRecord models based on your MySQL database
|
|
84
97
|
constraints, including text field size, UTF-8 encoding issues, and NOT NULL constraints.
|
|
85
98
|
email:
|
|
@@ -89,6 +102,7 @@ extensions: []
|
|
|
89
102
|
extra_rdoc_files: []
|
|
90
103
|
files:
|
|
91
104
|
- ".gitignore"
|
|
105
|
+
- ".ruby-version"
|
|
92
106
|
- ".travis.yml"
|
|
93
107
|
- CHANGELOG.md
|
|
94
108
|
- Gemfile
|
|
@@ -96,6 +110,7 @@ files:
|
|
|
96
110
|
- README.md
|
|
97
111
|
- Rakefile
|
|
98
112
|
- activerecord-databasevalidations.gemspec
|
|
113
|
+
- dev.yml
|
|
99
114
|
- gemfiles/Gemfile.activerecord52
|
|
100
115
|
- gemfiles/Gemfile.activerecord60
|
|
101
116
|
- lib/active_model/validations/bytesize.rb
|
|
@@ -111,19 +126,19 @@ files:
|
|
|
111
126
|
- lib/active_record/validations/typed_column.rb
|
|
112
127
|
- lib/activerecord-databasevalidations.rb
|
|
113
128
|
- lib/activerecord/databasevalidations.rb
|
|
129
|
+
- shipit.rubygems.yml
|
|
114
130
|
- test/bytesize_validator_test.rb
|
|
115
131
|
- test/data_loss_test.rb
|
|
116
|
-
- test/database.yml
|
|
117
132
|
- test/database_constraints_validator_test.rb
|
|
118
133
|
- test/not_null_validator_test.rb
|
|
119
134
|
- test/string_truncator_test.rb
|
|
120
135
|
- test/test_helper.rb
|
|
121
136
|
- test/varchar_default_size_test.rb
|
|
122
|
-
homepage: https://github.com/
|
|
137
|
+
homepage: https://github.com/Shopify/activerecord-databasevalidations
|
|
123
138
|
licenses:
|
|
124
139
|
- MIT
|
|
125
|
-
metadata:
|
|
126
|
-
|
|
140
|
+
metadata:
|
|
141
|
+
allowed_push_host: https://rubygems.org
|
|
127
142
|
rdoc_options: []
|
|
128
143
|
require_paths:
|
|
129
144
|
- lib
|
|
@@ -138,14 +153,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
138
153
|
- !ruby/object:Gem::Version
|
|
139
154
|
version: '0'
|
|
140
155
|
requirements: []
|
|
141
|
-
rubygems_version: 3.
|
|
142
|
-
signing_key:
|
|
156
|
+
rubygems_version: 3.6.3
|
|
143
157
|
specification_version: 4
|
|
144
158
|
summary: Add validations to your ActiveRecord models based on MySQL database constraints.
|
|
145
159
|
test_files:
|
|
146
160
|
- test/bytesize_validator_test.rb
|
|
147
161
|
- test/data_loss_test.rb
|
|
148
|
-
- test/database.yml
|
|
149
162
|
- test/database_constraints_validator_test.rb
|
|
150
163
|
- test/not_null_validator_test.rb
|
|
151
164
|
- test/string_truncator_test.rb
|