Explicitly set limit: 255 on string columns in migrations#275
Merged
Explicitly set limit: 255 on string columns in migrations#275
limit: 255 on string columns in migrations#275Conversation
A typo in 20131001083843_create_file_attachments.rb accidentally created a column called "string". This column was removed using a later migration. Since these changes have long been applied to the production database and cancel each other out, we can safely remove them. This makes it easier to perform automated changes on the code.
This makes it easier to perform automated changes on the code, and is the style enforced by "standard". Done using the following command: ``` sed -i -E 's/:(\w*) =>/\1:/g' db/migrate/* ```
In a table definition, `t.references :mycol, polymorphic: true` is a shorthand for defining two columns, `t.integer :mycol_id` and `t.string :mycol_type` [1]. We are going to change the migrations to explicitly set `limit: 255` on string columns (see the next commit for details). There is no way to directly set `limit: 255` on the string column defined by `t.references`, so we expand it out into the explicit integer and string column definitions. (This commit doesn't actually set `limit: 255`; that will be done in in the next commit.) [1] https://github.com/rails/rails/blob/v4.2.0/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L300-L319 Suggested-by: Ben Anderson <me@benanderson.nz>
tom93
added a commit
that referenced
this pull request
Mar 20, 2025
There used to be a default limit of 255 characters on string columns,
but that was removed in Rails 4.2 [1][2].
Explicitly set the limit to 255 in the migrations so that they will
retain their old behaviour.
In the future we might remove the limit (and match the new default).
Note that this commit only changes the migrations -- the schema also
needs to explicitly set the limits, and that will be done in the next
commit.
This change was made using the following commands:
```
sed -i -E '/limit:/! { /_column/ s/:string/:string, limit: 255/; }' db/migrate/*
sed -i -E '/limit:/! { s/t\.string *:\w*$/&, limit: 255/; }' db/migrate/*
sed -i -E '/limit:/! { s/t\.string *:\w*, */&limit: 255, /; }' db/migrate/*
```
Explanation:
All three commands ignore lines that already set a limit. The first
command is for lines such as `add_column :users, :email, :string`. The
second command is for lines such as `t.string :email`. The third
command is for lines such as `t.string :email, null: false`
(taking care to preserve alignment).
[1] https://guides.rubyonrails.org/v4.2/4_2_release_notes.html#active-record-notable-changes
[2] https://www.github.com/rails/rails/pull/14579
There used to be a default limit of 255 characters on string columns,
but that was removed in Rails 4.2 [1][2].
Explicitly set the limit to 255 in the migrations so that they will
retain their old behaviour.
In the future we might remove the limit (and match the new default).
Note that this commit only changes the migrations -- the schema also
needs to explicitly set the limits, and that will be done in the next
commit.
With this commit, the schema dump of a fresh database is identical to
the current schema dump of the production database.
This change was made using the following commands:
```
sed -i -E '/limit:/! { /_column/ s/:string/:string, limit: 255/; }' db/migrate/*
sed -i -E '/limit:/! { s/t\.string *:\w*$/&, limit: 255/; }' db/migrate/*
sed -i -E '/limit:/! { s/t\.string *:\w*, */&limit: 255, /; }' db/migrate/*
```
Explanation:
All three commands ignore lines that already set a limit. The first
command is for lines such as `add_column :users, :email, :string`. The
second command is for lines such as `t.string :email`. The third
command is for lines such as `t.string :email, null: false`
(taking care to preserve alignment).
[1] https://guides.rubyonrails.org/v4.2/4_2_release_notes.html#active-record-notable-changes
[2] https://www.github.com/rails/rails/pull/14579
This adds an explicit `limit: 255` the string columns. See the previous commit for details. Now db/schema.rb is identical to the schema dump of the production database.
Holmes98
approved these changes
Mar 21, 2025
Holmes98
pushed a commit
that referenced
this pull request
Mar 21, 2025
There used to be a default limit of 255 characters on string columns,
but that was removed in Rails 4.2 [1][2].
Explicitly set the limit to 255 in the migrations so that they will
retain their old behaviour.
In the future we might remove the limit (and match the new default).
Note that this commit only changes the migrations -- the schema also
needs to explicitly set the limits, and that will be done in the next
commit.
With this commit, the schema dump of a fresh database is identical to
the current schema dump of the production database.
This change was made using the following commands:
```
sed -i -E '/limit:/! { /_column/ s/:string/:string, limit: 255/; }' db/migrate/*
sed -i -E '/limit:/! { s/t\.string *:\w*$/&, limit: 255/; }' db/migrate/*
sed -i -E '/limit:/! { s/t\.string *:\w*, */&limit: 255, /; }' db/migrate/*
```
Explanation:
All three commands ignore lines that already set a limit. The first
command is for lines such as `add_column :users, :email, :string`. The
second command is for lines such as `t.string :email`. The third
command is for lines such as `t.string :email, null: false`
(taking care to preserve alignment).
[1] https://guides.rubyonrails.org/v4.2/4_2_release_notes.html#active-record-notable-changes
[2] https://www.github.com/rails/rails/pull/14579
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
From the main commit's message:
Discussed in #225 (comment) and #243 (comment).
(Intended merge strategy: rebase or merge commit.)