activerecord-databasevalidations 1.0.1 → 1.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ef47e663a6ba33d804fe4c0535eb31f6f49027358600b0f737c44c4a77365a9
4
- data.tar.gz: cdaced0a4508f5eed741d3392b81c90f63b46feb6d4400a70f4135580d4db155
3
+ metadata.gz: 19a316a681935a58dfcc28e3b1f61533b939f238c33b5e59168815b659fd46e9
4
+ data.tar.gz: 652292e8f015dd4a68ed58e3bda8875dc9cd692a073f62690dc25dacb6bd27b5
5
5
  SHA512:
6
- metadata.gz: eed94b2b60f67811ae807a40c1bc98ef6a55692d80142dd0c4ec79bc8815b357c7e33851fe2d05535f9f71e5e30c71c46ffd0284e43ed0003bca585e3d5c8dd1
7
- data.tar.gz: 83f0d060a77ede319afd46ea30deb0ac5271d204948f9f89300d2b818b283adcac739fc5f9a04e8d54ada6b3133c32bdbbc3ebb371911aa1be4065ab191fe76f
6
+ metadata.gz: 1a3e18729c91ed650c349a68daf2b2187453d8516fac073ca6f518469d775c9180e70e9f775236e8ca2ab5b2e4395ba7a9d8f2d14c0f5ffd142b4fad7a7713f9
7
+ data.tar.gz: '0659a85b324c4e939b7321e62c651c27a51f61f32fa42dc2f65abdb0c21f2db18f69b078c39864d632125290d144bec6fc5632db736a7c11d394680a4f17d59a'
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.3 (Mar 6, 2025)
4
+
5
+ * Disable string truncation helpers for non-MySQL adapters.
6
+
7
+ # 1.0.2 (Feb 5, 2025)
8
+
9
+ * Disable size + range validations for non-MySQL adapters.
10
+
3
11
  # 1.0.1 (May 9, 2023)
4
12
 
5
13
  * Updated gemspec to point to the proper home page.
@@ -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
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module DatabaseValidations
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,9 @@
1
+ module ActiveRecord
2
+ module Validations
3
+ module AdapterHelper
4
+ def mysql_adapter?(connection)
5
+ connection.is_a?(ConnectionAdapters::AbstractMysqlAdapter)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,9 +1,12 @@
1
1
  require 'active_model/validations/bytesize'
2
2
  require 'active_model/validations/not_null'
3
+ require 'active_record/validations/adapter_helper'
3
4
 
4
5
  module ActiveRecord
5
6
  module Validations
6
7
  class DatabaseConstraintsValidator < ActiveModel::EachValidator
8
+ include Validations::AdapterHelper
9
+
7
10
  attr_reader :constraints
8
11
 
9
12
  VALID_CONSTRAINTS = Set[:size, :not_null, :range]
@@ -35,6 +38,7 @@ module ActiveRecord
35
38
 
36
39
  def size_validator(klass, column)
37
40
  return unless constraints.include?(:size)
41
+ return unless mysql_adapter?(klass.connection)
38
42
  return unless column.text? || column.binary?
39
43
 
40
44
  maximum, type, encoding = ActiveRecord::DatabaseValidations::MySQL.column_size_limit(column)
@@ -47,6 +51,7 @@ module ActiveRecord
47
51
 
48
52
  def range_validator(klass, column)
49
53
  return unless constraints.include?(:range)
54
+ return unless mysql_adapter?(klass.connection)
50
55
  return unless column.number?
51
56
 
52
57
  args = { attributes: [column.name.to_sym], class: klass, allow_nil: true }
@@ -1,9 +1,13 @@
1
+ require 'active_record/validations/adapter_helper'
2
+
1
3
  module ActiveRecord
2
4
  module DatabaseValidations
3
5
  module StringTruncator
4
6
  extend ActiveSupport::Concern
7
+ include Validations::AdapterHelper
5
8
 
6
9
  def truncate_value_to_field_limit(field, value)
10
+ return value unless mysql_adapter?(self.class.connection)
7
11
  return if value.nil?
8
12
 
9
13
  column = self.class.columns_hash[field.to_s]
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?
@@ -23,6 +23,19 @@ class MagicalCreature < ActiveRecord::Base
23
23
  truncate_to_field_limit :another_string
24
24
  end
25
25
 
26
+ class MagicalSqliteCreature < ActiveRecord::Base
27
+ establish_connection(adapter: "sqlite3", database: ":memory:")
28
+ connection.create_table("magical_sqlite_creatures", force: true) do |t|
29
+ t.string :string, limit: 255
30
+ t.string :another_string, limit: 255
31
+ end
32
+
33
+ include ActiveRecord::DatabaseValidations::StringTruncator
34
+
35
+ before_validation truncate_string(:string)
36
+ truncate_to_field_limit :another_string
37
+ end
38
+
26
39
  class StringTruncatorTest < Minitest::Test
27
40
  def test_handles_nil_gracefully
28
41
  u_nil = MagicalCreature.create!(string: 'present', tinytext: 'present')
@@ -72,4 +85,14 @@ class StringTruncatorTest < Minitest::Test
72
85
  u6 = MagicalCreature.new(another_string: 'a' * 256)
73
86
  assert_equal 'a' * 255, u6.another_string
74
87
  end
88
+
89
+ def test_skips_truncate_for_non_mysql_adapter
90
+ record = MagicalSqliteCreature.new(string: 'a' * 256)
91
+ assert(record.valid?)
92
+ assert_equal 'a' * 256, record.string
93
+
94
+ record.another_string = 'a' * 256
95
+ assert(record.valid?)
96
+ assert_equal 'a' * 256, record.another_string
97
+ end
75
98
  end
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
- Minitest::Test = MiniTest::Unit::TestCase unless defined?(MiniTest::Test)
37
-
38
- database_yml = YAML.load_file(File.expand_path('../database.yml', __FILE__))
39
- ActiveRecord::Base.establish_connection(database_yml['test'])
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.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willem van Bergen
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-05-09 00:00:00.000000000 Z
10
+ date: 2025-03-06 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
@@ -106,14 +121,15 @@ files:
106
121
  - lib/active_record/database_validations/varchar_191.rb
107
122
  - lib/active_record/database_validations/varchar_255.rb
108
123
  - lib/active_record/database_validations/version.rb
124
+ - lib/active_record/validations/adapter_helper.rb
109
125
  - lib/active_record/validations/database_constraints.rb
110
126
  - lib/active_record/validations/string_truncator.rb
111
127
  - lib/active_record/validations/typed_column.rb
112
128
  - lib/activerecord-databasevalidations.rb
113
129
  - lib/activerecord/databasevalidations.rb
130
+ - shipit.rubygems.yml
114
131
  - test/bytesize_validator_test.rb
115
132
  - test/data_loss_test.rb
116
- - test/database.yml
117
133
  - test/database_constraints_validator_test.rb
118
134
  - test/not_null_validator_test.rb
119
135
  - test/string_truncator_test.rb
@@ -122,8 +138,8 @@ files:
122
138
  homepage: https://github.com/Shopify/activerecord-databasevalidations
123
139
  licenses:
124
140
  - MIT
125
- metadata: {}
126
- post_install_message:
141
+ metadata:
142
+ allowed_push_host: https://rubygems.org
127
143
  rdoc_options: []
128
144
  require_paths:
129
145
  - lib
@@ -138,14 +154,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
154
  - !ruby/object:Gem::Version
139
155
  version: '0'
140
156
  requirements: []
141
- rubygems_version: 3.4.10
142
- signing_key:
157
+ rubygems_version: 3.6.5
143
158
  specification_version: 4
144
159
  summary: Add validations to your ActiveRecord models based on MySQL database constraints.
145
160
  test_files:
146
161
  - test/bytesize_validator_test.rb
147
162
  - test/data_loss_test.rb
148
- - test/database.yml
149
163
  - test/database_constraints_validator_test.rb
150
164
  - test/not_null_validator_test.rb
151
165
  - test/string_truncator_test.rb
data/test/database.yml DELETED
@@ -1,8 +0,0 @@
1
- test:
2
- adapter: mysql2
3
- database: database_validations
4
- #username: travis
5
- host: localhost
6
- username: root
7
- encoding: utf8mb4
8
- strict: false