Skip to content

Explicitly set limit: 255 on string columns in migrations#275

Merged
Holmes98 merged 5 commits intomasterfrom
schema-limit-255
Mar 21, 2025
Merged

Explicitly set limit: 255 on string columns in migrations#275
Holmes98 merged 5 commits intomasterfrom
schema-limit-255

Conversation

@tom93
Copy link
Contributor

@tom93 tom93 commented Mar 20, 2025

From the main commit's message:

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.

...

[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

Discussed in #225 (comment) and #243 (comment).

(Intended merge strategy: rebase or merge commit.)

tom93 added 3 commits March 20, 2025 17:37
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
@tom93 tom93 force-pushed the schema-limit-255 branch from f6277ee to 85f63e3 Compare March 20, 2025 04:58
@coveralls
Copy link

coveralls commented Mar 20, 2025

Coverage Status

coverage: 37.537%. remained the same
when pulling 225f401 on schema-limit-255
into f5d9866 on master.

tom93 added 2 commits March 20, 2025 18:09
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.
@tom93 tom93 force-pushed the schema-limit-255 branch from 85f63e3 to 225f401 Compare March 20, 2025 05:12
@Holmes98 Holmes98 merged commit 8079920 into master Mar 21, 2025
6 checks passed
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
@Holmes98 Holmes98 deleted the schema-limit-255 branch March 21, 2025 07:42
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.

3 participants