From 419f8248b6ac92c0a7f5b8b832542eac6cf52b51 Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 7 Jun 2026 16:23:59 -0700 Subject: [PATCH 1/9] Fix auto_increment on string columns with integer initial When the DB column is a string type (e.g. varchar in PostgreSQL) but initial is an integer (the default), the SQL MAX() function does lexicographic comparison, causing '9' > '10' and producing duplicate values from record 10 onward. Fix by checking the actual DB column type via columns_hash. If the column is :string or :text, use the length-aware ORDER BY query strategy and String#next for incrementing, regardless of the @initial type. --- lib/auto_increment/incrementor.rb | 7 ++++++- spec/lib/active_record_spec.rb | 10 ++++++++++ spec/lib/incrementor_spec.rb | 11 +++++++++++ spec/models/post.rb | 6 ++++++ spec/support/active_record.rb | 5 +++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 spec/models/post.rb diff --git a/lib/auto_increment/incrementor.rb b/lib/auto_increment/incrementor.rb index 69ca149..adb7f02 100644 --- a/lib/auto_increment/incrementor.rb +++ b/lib/auto_increment/incrementor.rb @@ -74,7 +74,12 @@ def increment end def string? - @initial.instance_of?(String) + @initial.instance_of?(String) || column_string? + end + + def column_string? + col = @record.class.columns_hash[@column.to_s] + col && col.type.in?(%i[string text]) end end end diff --git a/spec/lib/active_record_spec.rb b/spec/lib/active_record_spec.rb index 8273bff..52c69cf 100644 --- a/spec/lib/active_record_spec.rb +++ b/spec/lib/active_record_spec.rb @@ -3,6 +3,7 @@ require "spec_helper" require "models/account" require "models/user" +require "models/post" describe AutoIncrement do before :all do @@ -73,4 +74,13 @@ describe "uses model scopes" do it { expect(@user3_account2.letter_code).to eq("C") } end + + describe "string column with integer initial" do + it "increments correctly past the 9-to-10 boundary" do + 15.times do |i| + post = Post.create! + expect(post.ref.to_i).to eq(i + 1) + end + end + end end diff --git a/spec/lib/incrementor_spec.rb b/spec/lib/incrementor_spec.rb index 64be7f4..4ddf8e1 100644 --- a/spec/lib/incrementor_spec.rb +++ b/spec/lib/incrementor_spec.rb @@ -62,6 +62,17 @@ def create_user(code:, name: "seed") end end + describe "string column in DB with integer initial" do + it "uses length-aware ordering, not SQL MAX" do + values = %w[1 2 3 4 5 6 7 8 9 10] + values.each { |v| create_user(code: v) } + + user = User.new + AutoIncrement::Incrementor.new(user, column: :letter_code, initial: 1).run + expect(user.letter_code).to eq "11" + end + end + context "when column value is already set" do it "does not change the column if force is false" do account = Account.new(code: 5) diff --git a/spec/models/post.rb b/spec/models/post.rb new file mode 100644 index 0000000..e4b6067 --- /dev/null +++ b/spec/models/post.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# Spec +Post+ — string column with default integer initial +class Post < ActiveRecord::Base + auto_increment :ref +end diff --git a/spec/support/active_record.rb b/spec/support/active_record.rb index afe67d5..a42ec64 100644 --- a/spec/support/active_record.rb +++ b/spec/support/active_record.rb @@ -12,3 +12,8 @@ t.integer :account_id t.string :letter_code end + +# +ActiveRecord+ migration for Posts (string column, integer initial) +ActiveRecord::Migration.create_table :posts do |t| + t.string :ref +end From 123f08b06790f60e30be25d3e3dbe1b89af718d1 Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 7 Jun 2026 20:36:03 -0700 Subject: [PATCH 2/9] Document String#next behavior in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb9dd60..459277e 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ AB ... ``` -String sequences follow the same pattern as Excel columns. +String sequences follow Ruby's [`String#next`](https://ruby-doc.org/3.4/Object.html#method-i-next) logic. ### Scoped Sequences From 9189b34fbe512426a7ad207b8cba1c45534c9901 Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 7 Jun 2026 20:36:32 -0700 Subject: [PATCH 3/9] Apply standard formatting --- lib/auto_increment/incrementor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/auto_increment/incrementor.rb b/lib/auto_increment/incrementor.rb index adb7f02..363d8df 100644 --- a/lib/auto_increment/incrementor.rb +++ b/lib/auto_increment/incrementor.rb @@ -79,7 +79,7 @@ def string? def column_string? col = @record.class.columns_hash[@column.to_s] - col && col.type.in?(%i[string text]) + col&.type&.in?(%i[string text]) end end end From 05d6db7fa42943d41d28c76f8212fc2cfa688445 Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 14 Jun 2026 10:58:05 -0700 Subject: [PATCH 4/9] Remove @initial string check from string? method string? now only relies on column_string? to detect string/text columns, instead of also checking @initial.instance_of?(String). This avoids incorrect LENGTH ordering when a string initial is used on an integer column. Also reorganize incrementor_spec.rb for clarity. --- lib/auto_increment/incrementor.rb | 2 +- spec/lib/incrementor_spec.rb | 69 ++++++++++++++----------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/lib/auto_increment/incrementor.rb b/lib/auto_increment/incrementor.rb index 363d8df..0df18a3 100644 --- a/lib/auto_increment/incrementor.rb +++ b/lib/auto_increment/incrementor.rb @@ -74,7 +74,7 @@ def increment end def string? - @initial.instance_of?(String) || column_string? + column_string? end def column_string? diff --git a/spec/lib/incrementor_spec.rb b/spec/lib/incrementor_spec.rb index 4ddf8e1..9e222d9 100644 --- a/spec/lib/incrementor_spec.rb +++ b/spec/lib/incrementor_spec.rb @@ -17,7 +17,7 @@ def create_user(code:, name: "seed") end describe "#run" do - describe "integer" do + describe "integer column" do it "sets initial value to 1 when no records exist" do account = Account.new AutoIncrement::Incrementor.new(account).run @@ -39,8 +39,8 @@ def create_user(code:, name: "seed") end end - describe "string" do - it "uses the initial value when no records exist" do + describe "string column" do + it "sets initial value when no records exist" do user = User.new AutoIncrement::Incrementor.new(user, column: :letter_code, initial: "A").run expect(user.letter_code).to eq "A" @@ -60,12 +60,9 @@ def create_user(code:, name: "seed") expect(user.letter_code).to eq next_value end end - end - describe "string column in DB with integer initial" do - it "uses length-aware ordering, not SQL MAX" do - values = %w[1 2 3 4 5 6 7 8 9 10] - values.each { |v| create_user(code: v) } + it "uses length-aware ordering inferred from the column schema" do + %w[1 2 3 4 5 6 7 8 9 10].each { |v| create_user(code: v) } user = User.new AutoIncrement::Incrementor.new(user, column: :letter_code, initial: 1).run @@ -73,14 +70,14 @@ def create_user(code:, name: "seed") end end - context "when column value is already set" do - it "does not change the column if force is false" do + describe "force" do + it "does not overwrite an existing value when force is false" do account = Account.new(code: 5) expect { AutoIncrement::Incrementor.new(account).run } .not_to change { account.code } end - it "changes the column if force is true" do + it "overwrites an existing value when force is true" do create_account(code: 10) account = Account.new(code: 5) AutoIncrement::Incrementor.new(account, force: true).run @@ -94,7 +91,7 @@ def create_user(code:, name: "seed") end end - context "scoped increment" do + describe "scope" do it "only considers records within the same scope" do create_account(code: 10, name: "other") @@ -112,38 +109,36 @@ def create_user(code:, name: "seed") AutoIncrement::Incrementor.new(user, column: :letter_code, initial: "A", model_scope: :with_mark).run expect(user.letter_code).to eq "D" end - end - end - describe "model_scope option" do - it "applies model scopes when building the query" do - create_user(code: "C", name: "Mark") - create_user(code: "A", name: "Other") + it "applies model scopes when building the query" do + create_user(code: "C", name: "Mark") + create_user(code: "A", name: "Other") - user = User.new(name: "Mark") - AutoIncrement::Incrementor.new(user, column: :letter_code, initial: "A", model_scope: :with_mark).run - expect(user.letter_code).to eq "D" - end + user = User.new(name: "Mark") + AutoIncrement::Incrementor.new(user, column: :letter_code, initial: "A", model_scope: :with_mark).run + expect(user.letter_code).to eq "D" + end - it "only considers records matching the model scope for integer columns" do - create_account(code: 10, name: "Mark") - create_account(code: 5, name: "Other") - account = Account.new(name: "Mark") - AutoIncrement::Incrementor.new(account, column: :code, initial: 1, model_scope: :only_mark).run - expect(account.code).to eq 11 + it "only considers records matching the model scope" do + create_account(code: 10, name: "Mark") + create_account(code: 5, name: "Other") + account = Account.new(name: "Mark") + AutoIncrement::Incrementor.new(account, column: :code, initial: 1, model_scope: :only_mark).run + expect(account.code).to eq 11 + end end - end - describe "locking the query" do - it "increments correctly with lock enabled" do - create_account(code: 10) - account = Account.new - incrementor = AutoIncrement::Incrementor.new(account, lock: true) + describe "lock" do + it "increments correctly with lock enabled" do + create_account(code: 10) + account = Account.new + incrementor = AutoIncrement::Incrementor.new(account, lock: true) - expect(incrementor.send(:maximum_query).lock_value).to eq true + expect(incrementor.send(:maximum_query).lock_value).to eq true - incrementor.run - expect(account.code).to eq 11 + incrementor.run + expect(account.code).to eq 11 + end end end end From 34ef21225ceaf8b8fa754447de7c9d2922551f51 Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 14 Jun 2026 11:44:11 -0700 Subject: [PATCH 5/9] Clarify column type inference in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 459277e..99e3dab 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ AB ... ``` -String sequences follow Ruby's [`String#next`](https://ruby-doc.org/3.4/Object.html#method-i-next) logic. +String sequences follow Ruby's [`String#next`](https://ruby-doc.org/3.4/Object.html#method-i-next) logic. The column type is inferred from the database schema. ### Scoped Sequences @@ -249,7 +249,7 @@ auto_increment :number, | Option | Description | Default | | ------------- | ------------------------------------------------------------------ | --------- | | `column` | Column to increment. Can be integer or string. | `:code` | -| `initial` | Starting value. Integer or string. | `1` | +| `initial` | Starting value. Inferred from the database column type. | `1` | | `scope` | Restricts the sequence to matching column values. | `nil` | | `model_scope` | Applies Active Record scopes before calculating the maximum value. | `nil` | | `force` | Overwrites an already assigned value. | `false` | From 5cb1923ccf0b34ec21fb7b8dc56f9cd221f264e7 Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 14 Jun 2026 11:56:48 -0700 Subject: [PATCH 6/9] Add deprecation warning for initial value / column type mismatch --- lib/auto_increment/active_record.rb | 15 ++++++++++++ spec/lib/active_record_spec.rb | 38 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/auto_increment/active_record.rb b/lib/auto_increment/active_record.rb index 32e3fdf..e154aa6 100644 --- a/lib/auto_increment/active_record.rb +++ b/lib/auto_increment/active_record.rb @@ -9,6 +9,21 @@ module ActiveRecord # +AutoIncrement::ActiveRecord::ClassMethods+ module ClassMethods def auto_increment(column = nil, **options) + column ||= options.fetch(:column, :code) + initial = options.fetch(:initial, 1) + col = columns_hash[column.to_s] + if col + col_type = col.type + if (col_type == :integer && !initial.is_a?(Integer)) || + (col_type.in?(%i[string text]) && !initial.is_a?(String)) + warn( + "[DEPRECATION] The initial value type (#{initial.class}) does not match " \ + "the column type (#{col_type}) for column '#{column}' on #{name}. " \ + "This behavior is deprecated and will raise an error in the future." + ) + end + end + send("before_#{options.fetch(:before, :create)}") do |record| Incrementor.new(record, column, **options).run end diff --git a/spec/lib/active_record_spec.rb b/spec/lib/active_record_spec.rb index 52c69cf..abe75db 100644 --- a/spec/lib/active_record_spec.rb +++ b/spec/lib/active_record_spec.rb @@ -83,4 +83,42 @@ end end end + + describe "deprecation warning" do + it "warns when initial is a string on an integer column" do + expect { + Class.new(ActiveRecord::Base) do + self.table_name = "accounts" + auto_increment :code, initial: "A" + end + }.to output(/\[DEPRECATION\] The initial value type \(String\) does not match the column type \(integer\) for column 'code'.*raise an error in the future/).to_stderr + end + + it "warns when initial is an integer on a string column" do + expect { + Class.new(ActiveRecord::Base) do + self.table_name = "posts" + auto_increment :ref, initial: 1 + end + }.to output(/\[DEPRECATION\] The initial value type \(Integer\) does not match the column type \(string\) for column 'ref'.*raise an error in the future/).to_stderr + end + + it "does not warn when types match (integer column, integer initial)" do + expect { + Class.new(ActiveRecord::Base) do + self.table_name = "accounts" + auto_increment :code, initial: 100 + end + }.not_to output(/\[DEPRECATION\]/).to_stderr + end + + it "does not warn when types match (string column, string initial)" do + expect { + Class.new(ActiveRecord::Base) do + self.table_name = "posts" + auto_increment :ref, initial: "X" + end + }.not_to output(/\[DEPRECATION\]/).to_stderr + end + end end From 6855e43d54f897b85b628a76fa103fb40de0fb81 Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 14 Jun 2026 12:15:41 -0700 Subject: [PATCH 7/9] Refactor deprecation warning into private method --- README.md | 4 +++- lib/auto_increment/active_record.rb | 32 +++++++++++++++++------------ lib/auto_increment/incrementor.rb | 10 +++++---- spec/lib/active_record_spec.rb | 9 ++++++++ spec/lib/incrementor_spec.rb | 9 +++++++- 5 files changed, 45 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 99e3dab..5085e87 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,8 @@ AB String sequences follow Ruby's [`String#next`](https://ruby-doc.org/3.4/Object.html#method-i-next) logic. The column type is inferred from the database schema. +> **Deprecation**: Explicitly passing an initial value whose type differs from the database column type is deprecated. For example, `auto_increment :ref, initial: 1` on a `string` column will emit a warning. When `initial` is not set, the default is automatically inferred from the column type (`"1"` for string columns, `1` for integer columns). + ### Scoped Sequences Generate independent sequences within a scope: @@ -249,7 +251,7 @@ auto_increment :number, | Option | Description | Default | | ------------- | ------------------------------------------------------------------ | --------- | | `column` | Column to increment. Can be integer or string. | `:code` | -| `initial` | Starting value. Inferred from the database column type. | `1` | +| `initial` | Starting value. Must match the database column type (`Integer` for integer columns, `String` for string columns). | `1` | | `scope` | Restricts the sequence to matching column values. | `nil` | | `model_scope` | Applies Active Record scopes before calculating the maximum value. | `nil` | | `force` | Overwrites an already assigned value. | `false` | diff --git a/lib/auto_increment/active_record.rb b/lib/auto_increment/active_record.rb index e154aa6..a4d2096 100644 --- a/lib/auto_increment/active_record.rb +++ b/lib/auto_increment/active_record.rb @@ -10,24 +10,30 @@ module ActiveRecord module ClassMethods def auto_increment(column = nil, **options) column ||= options.fetch(:column, :code) - initial = options.fetch(:initial, 1) - col = columns_hash[column.to_s] - if col - col_type = col.type - if (col_type == :integer && !initial.is_a?(Integer)) || - (col_type.in?(%i[string text]) && !initial.is_a?(String)) - warn( - "[DEPRECATION] The initial value type (#{initial.class}) does not match " \ - "the column type (#{col_type}) for column '#{column}' on #{name}. " \ - "This behavior is deprecated and will raise an error in the future." - ) - end - end + + auto_increment_deprecate_type_mismatch(column, options[:initial]) if options.key?(:initial) send("before_#{options.fetch(:before, :create)}") do |record| Incrementor.new(record, column, **options).run end end + + private + + def auto_increment_deprecate_type_mismatch(column, initial) + col = columns_hash[column.to_s] + return unless col + + col_type = col.type + return if col_type == :integer && initial.is_a?(Integer) + return if col_type.in?(%i[string text]) && initial.is_a?(String) + + warn( + "[DEPRECATION] The initial value type (#{initial.class}) does not match " \ + "the column type (#{col_type}) for column '#{column}' on #{name}. " \ + "This behavior is deprecated and will raise an error in the future." + ) + end end end end diff --git a/lib/auto_increment/incrementor.rb b/lib/auto_increment/incrementor.rb index 0df18a3..50088cd 100644 --- a/lib/auto_increment/incrementor.rb +++ b/lib/auto_increment/incrementor.rb @@ -7,7 +7,7 @@ class Incrementor def initialize(record, column = nil, **options) @record = record @column = column || options.fetch(:column, :code) - @initial = options.fetch(:initial, 1) + @initial = resolve_initial(options) @force = options.fetch(:force, false) @scope = Array.wrap(options[:scope]).compact @model_scope = Array.wrap(options[:model_scope]).compact @@ -54,7 +54,7 @@ def build_model_scope(query) def maximum query = maximum_query - if string? + if column_string? query.select("#{@column} max") .order(Arel.sql("LENGTH(#{@column}) DESC, #{@column} DESC")) .first.try :max @@ -73,8 +73,10 @@ def increment max.blank? ? @initial : max.next end - def string? - column_string? + def resolve_initial(options) + return options[:initial] if options.key?(:initial) + + column_string? ? "1" : 1 end def column_string? diff --git a/spec/lib/active_record_spec.rb b/spec/lib/active_record_spec.rb index abe75db..1059e6c 100644 --- a/spec/lib/active_record_spec.rb +++ b/spec/lib/active_record_spec.rb @@ -120,5 +120,14 @@ end }.not_to output(/\[DEPRECATION\]/).to_stderr end + + it "does not warn when initial is omitted on a string column (auto-detects)" do + expect { + Class.new(ActiveRecord::Base) do + self.table_name = "posts" + auto_increment :ref + end + }.not_to output(/\[DEPRECATION\]/).to_stderr + end end end diff --git a/spec/lib/incrementor_spec.rb b/spec/lib/incrementor_spec.rb index 9e222d9..b89ac49 100644 --- a/spec/lib/incrementor_spec.rb +++ b/spec/lib/incrementor_spec.rb @@ -3,6 +3,7 @@ require "spec_helper" require "models/account" require "models/user" +require "models/post" describe AutoIncrement::Incrementor do def create_account(code:, name: "seed") @@ -18,7 +19,7 @@ def create_user(code:, name: "seed") describe "#run" do describe "integer column" do - it "sets initial value to 1 when no records exist" do + it "auto-detects initial value 1 when no initial is given" do account = Account.new AutoIncrement::Incrementor.new(account).run expect(account.code).to eq 1 @@ -46,6 +47,12 @@ def create_user(code:, name: "seed") expect(user.letter_code).to eq "A" end + it "auto-detects initial value '1' when no initial is given" do + post = Post.new + AutoIncrement::Incrementor.new(post, column: :ref).run + expect(post.ref).to eq "1" + end + { "A" => "B", "Z" => "AA", From 1d3645c1c47742d869432781c6367e9393676365 Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 14 Jun 2026 12:33:54 -0700 Subject: [PATCH 8/9] Address review comments: use quote_column_name, narrow rescue, fix README link and default --- README.md | 4 ++-- lib/auto_increment/active_record.rb | 6 +++++- lib/auto_increment/incrementor.rb | 5 +++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5085e87..526cfd3 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ AB ... ``` -String sequences follow Ruby's [`String#next`](https://ruby-doc.org/3.4/Object.html#method-i-next) logic. The column type is inferred from the database schema. +String sequences follow Ruby's [`String#next`](https://ruby-doc.org/3.4/String.html#method-i-next) logic. The column type is inferred from the database schema. > **Deprecation**: Explicitly passing an initial value whose type differs from the database column type is deprecated. For example, `auto_increment :ref, initial: 1` on a `string` column will emit a warning. When `initial` is not set, the default is automatically inferred from the column type (`"1"` for string columns, `1` for integer columns). @@ -251,7 +251,7 @@ auto_increment :number, | Option | Description | Default | | ------------- | ------------------------------------------------------------------ | --------- | | `column` | Column to increment. Can be integer or string. | `:code` | -| `initial` | Starting value. Must match the database column type (`Integer` for integer columns, `String` for string columns). | `1` | +| `initial` | Starting value. Must match the database column type (`Integer` for integer columns, `String` for string columns). When omitted, inferred from the column type. | `1` or `"1"` (inferred) | | `scope` | Restricts the sequence to matching column values. | `nil` | | `model_scope` | Applies Active Record scopes before calculating the maximum value. | `nil` | | `force` | Overwrites an already assigned value. | `false` | diff --git a/lib/auto_increment/active_record.rb b/lib/auto_increment/active_record.rb index a4d2096..f13e091 100644 --- a/lib/auto_increment/active_record.rb +++ b/lib/auto_increment/active_record.rb @@ -28,11 +28,15 @@ def auto_increment_deprecate_type_mismatch(column, initial) return if col_type == :integer && initial.is_a?(Integer) return if col_type.in?(%i[string text]) && initial.is_a?(String) + model_name = name.presence || "anonymous" + warn( "[DEPRECATION] The initial value type (#{initial.class}) does not match " \ - "the column type (#{col_type}) for column '#{column}' on #{name}. " \ + "the column type (#{col_type}) for column '#{column}' on #{model_name}. " \ "This behavior is deprecated and will raise an error in the future." ) + rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError + # Ignore connection/schema errors during class loading end end end diff --git a/lib/auto_increment/incrementor.rb b/lib/auto_increment/incrementor.rb index 50088cd..a87bb8a 100644 --- a/lib/auto_increment/incrementor.rb +++ b/lib/auto_increment/incrementor.rb @@ -55,8 +55,9 @@ def maximum query = maximum_query if column_string? - query.select("#{@column} max") - .order(Arel.sql("LENGTH(#{@column}) DESC, #{@column} DESC")) + quoted_column = @record.class.connection.quote_column_name(@column) + query.select("#{quoted_column} max") + .order(Arel.sql("LENGTH(#{quoted_column}) DESC, #{quoted_column} DESC")) .first.try :max else query.maximum @column From c336c8d1b0311991d399abca8fc2176e40d8281a Mon Sep 17 00:00:00 2001 From: Felipe Diesel Date: Sun, 14 Jun 2026 12:38:41 -0700 Subject: [PATCH 9/9] Simplify rescue style: add comment and narrow to columns_hash call --- lib/auto_increment/active_record.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/auto_increment/active_record.rb b/lib/auto_increment/active_record.rb index f13e091..78aa0fd 100644 --- a/lib/auto_increment/active_record.rb +++ b/lib/auto_increment/active_record.rb @@ -21,7 +21,12 @@ def auto_increment(column = nil, **options) private def auto_increment_deprecate_type_mismatch(column, initial) - col = columns_hash[column.to_s] + col = begin + columns_hash[column.to_s] + rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError + # Ignore connection/schema errors during class loading + return + end return unless col col_type = col.type @@ -35,8 +40,6 @@ def auto_increment_deprecate_type_mismatch(column, initial) "the column type (#{col_type}) for column '#{column}' on #{model_name}. " \ "This behavior is deprecated and will raise an error in the future." ) - rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError - # Ignore connection/schema errors during class loading end end end