Skip to content

Fix auto_increment on string columns with integer initial#49

Merged
felipediesel merged 9 commits into
mainfrom
fix-string-column-increment
Jun 29, 2026
Merged

Fix auto_increment on string columns with integer initial#49
felipediesel merged 9 commits into
mainfrom
fix-string-column-increment

Conversation

@felipediesel

@felipediesel felipediesel commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Changes

Auto-detect initial value type

When initial is not explicitly set, Incrementor now infers the default based on the database column type: "1" for string/text columns, 1 for integer columns. This prevents the common pitfall of a mismatched default.

Deprecation warning

When initial is explicitly passed with a type that doesn't match the column (e.g. auto_increment :ref, initial: 1 on a string column), a deprecation warning is emitted at class-load time. This fires once per model, not on every record creation.

String column fix

When the DB column is a string type but initial is an integer, the SQL MAX() function does lexicographic comparison, causing '9' > '10' and duplicate values from record 10 onward. Fixed by checking the actual DB column type via columns_hash, string/text columns now use length-aware ORDER BY and String#next for incrementing.

Closes #18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect auto-increment behavior when the database column is a string/text type but the configured initial value is an integer (default 1). In that scenario, using SQL MAX() on a string column can compare lexicographically (e.g., '9' > '10'), causing duplicates; this PR instead detects the DB column type and applies the string-safe “length-aware ordering + String#next” strategy.

Changes:

  • Detect string/text DB columns via columns_hash and treat them as string sequences even when initial is an integer.
  • Add regression coverage for the 9 → 10 boundary on string columns with integer initial values.
  • Update README wording/badges to reflect current behavior/docs.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
spec/support/active_record.rb Adds a posts table with a string ref column to reproduce the reported scenario.
spec/models/post.rb Introduces a Post model using auto_increment :ref for integration-style verification.
spec/lib/incrementor_spec.rb Adds a unit/regression spec ensuring length-aware ordering is used for string DB columns even with integer initial.
spec/lib/active_record_spec.rb Adds an end-to-end spec verifying increments continue correctly past 9 on a string column.
README.md Updates the maintainability badge and clarifies that string sequences follow Ruby’s String#next.
lib/auto_increment/incrementor.rb Extends string detection to include string/text DB column types via columns_hash.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@felipediesel felipediesel force-pushed the fix-string-column-increment branch 2 times, most recently from 5587af6 to b789aef Compare June 14, 2026 17:20
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.
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.
@felipediesel felipediesel force-pushed the fix-string-column-increment branch from a9d54fe to 05d6db7 Compare June 14, 2026 18:39
@felipediesel felipediesel force-pushed the fix-string-column-increment branch from a0b6c07 to 5cb1923 Compare June 14, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Comment thread lib/auto_increment/incrementor.rb
Comment thread lib/auto_increment/active_record.rb
Comment thread README.md Outdated
```

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. The column type is inferred from the database schema.
Comment thread README.md
@felipediesel felipediesel merged commit bed15ac into main Jun 29, 2026
10 checks passed
@felipediesel felipediesel deleted the fix-string-column-increment branch June 29, 2026 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not incrementing when field type is string in Postgres

2 participants