Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save apoorv-2204/c83bf9597fb097098fdac0be4e4acff4 to your computer and use it in GitHub Desktop.
Save apoorv-2204/c83bf9597fb097098fdac0be4e4acff4 to your computer and use it in GitHub Desktop.
change function and the up & down functions in Ecto migrations:
change Function:
Automatic Reversibility: Used when Ecto can automatically infer how to reverse the migration.
Simple Operations: Suitable for straightforward schema changes like creating/dropping tables,
adding/removing columns, and modifying indexes.
up and down Functions:
Explicit Reversibility: Required when Ecto cannot automatically reverse the migration steps.
Complex Operations: Ideal for complex migrations involving data transformation, executing raw SQL, or irreversible commands.
Use change: For reversible migrations where Ecto can handle rollback automatically.
Use up and down: When migrations involve operations that need explicit instructions for both applying and rolling back.
Key Differences:
Automatic vs. Manual: change relies on Ecto's automatic reversal capabilities, while up and down require manual definitions.
Complexity Handling: change is for simple, reversible changes; up and down are for complex or non-reversible changes.
Best Practices:
Assess Reversibility: Use change when all operations are reversible; otherwise, opt for up and down.
Maintain Clarity: Use explicit functions (up, down) for clarity in complex migrations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment