Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

ruby on rails - Migration changed various unrelated models columns from Integer to Bigint type

I created various migrations to add a boolean column to model :foo, backfill it and add a null constraint to it.

I ran the migrations and checked with git diff the schema.rb file which had obviously changed.

Upon further examination I noticed that various columns all called cover_file_size in 5 other models had changed it's type from integer to bigint when I ran the migrations.

The models are not even related to each other and I cannot think of a reason this happened without my consent.

Is there some kind of default behaviour in Rails that could have originated all these changes? Is there anything wrong with a column of type integer becoming bigint? So far I haven't experienced any problems running the platform.

question from:https://stackoverflow.com/questions/65835990/migration-changed-various-unrelated-models-columns-from-integer-to-bigint-type

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem with changing columns in a database is downtime. When you deploy and migrate it on production it will lock the entire table while changing this column. If it's a large table it might be noticeable downtime.

Longer explanation: You didn't specify which SQL (Postgres/MySQL?) you are using. But most of the time changing the type of the column means:

  • creating a new column
  • copying all the data from the old column
  • deleting the old column

To make sure everything is copied correctly the entire table will be locked for this time. Locked means no reading or writing. Hence, the downtime.

As for why this happened, can you provide the Ruby, Rails, and SQL versions? Maybe you updated one of them recently?

Also, are you absolutely sure you had integer in your database before the migration? I mean in the database, not in the schema (these are not always the same).

The schema can come from two places:

  • It's something that rails generated the last time you run the migration. If since then, you made some changes to your database in some other way it will not be visible in the schema. But the next time you run a migration rails will update the schema to match the current db state.
  • It's something you downloaded from git. Maybe another developer has a database that uses integers, they run a migration that changed the values to integer and pushed it. But your local database uses bigint, so now you run the migration it was updated again.

If you are working with other people check the git blame on the schema file to see if these fields were always an integer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...