When running rake db:migrate, Rails scrambles the entire db/structure.sql file, making git diffs unreadable. This command helps you manually add only the new changes while preserving the original structure.
-
Before running migrations: Always backup or check the current state of
db/structure.sql -
After running
rake db:migrate:- DO NOT commit the scrambled
structure.sqlfile - Revert
db/structure.sqlto its previous state:git checkout HEAD -- db/structure.sql
- DO NOT commit the scrambled
-
Manual addition process:
- Look at the migration file to understand what table/changes were added
- Manually add the new table definition to
db/structure.sqlin the appropriate alphabetical location - Add the new migration version to the
schema_migrationsINSERT statement at the bottom - Ensure proper formatting matches the existing style
-
Verify the changes:
- Run
git diff db/structure.sqlto see only the actual changes - The diff should show only the new table definition and migration version
- Commit these clean, minimal changes
- Run
This approach keeps the git history clean and makes it easy to see exactly what database changes were made.