Skip to content

Clarify the reasoning for Define Model Class Migrations #284

Open
@pirj

Description

@pirj
  1. https://rails.rubystyle.guide/#define-model-class-migrations claims that

or cause serious data corruption

Can you think of a use case when an old migration could cause a serious data corruption?
Yes, it will most probably break db:migrate on a clean database.

  1. There's yet another reason not to use real models in migrations.
    If you refer to a real model in an older migration (e.g. for update_all), Rails will cache column information.
    And subsequent migrations that would alter that table's schema won't affect the column cache.
    This will lead to breakages due to an outdated (fixed for the moment when the model was first referred to in migrations) table schema. Specifically, if you add a column in a later migration and update_all that new column, this will result in an error.
    Also, if you run rake db:migrate db:seed in one command, this will also affect your seeds, and it will also fail with:
ActiveModel::UnknownAttributeError: unknown attribute 'some_attr' for ModelName

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions