|
| 1 | +# Squashing Migrations |
| 2 | + |
| 3 | +If you have a long list of migrations, sometimes it can take a while to migrate |
| 4 | +each of those files every time the project is reset or spun up by a new |
| 5 | +developer. Thankfully, Ecto comes with mix tasks to `dump` and `load` a database |
| 6 | +structure which will represent the state of the database up to a certain point |
| 7 | +in time, not including content. |
| 8 | + |
| 9 | +- `mix ecto.dump` |
| 10 | +- `mix ecto.load` |
| 11 | + |
| 12 | +Schema dumping and loading is only supported by external binaries `pg_dump` and |
| 13 | +`mysqldump`, which are used by the Postgres, MyXQL, and MySQL Ecto adapters (not |
| 14 | +supported in MSSQL adapter). |
| 15 | + |
| 16 | +For example: |
| 17 | + |
| 18 | +``` |
| 19 | +20210101000000 - First Migration |
| 20 | +20210201000000 - Second Migration |
| 21 | +20210701000000 - Third Migration <-- we are here now. run `mix ecto.dump` |
| 22 | +``` |
| 23 | + |
| 24 | +We can "squash" the migrations up to the current day which will effectively |
| 25 | +fast-forward migrations to that structure. The Ecto Migrator will detect that |
| 26 | +the database is already migrated to the third migration, and so it begins there |
| 27 | +and migrates forward. |
| 28 | + |
| 29 | +Let's add a new migration: |
| 30 | + |
| 31 | +``` |
| 32 | +20210101000000 - First Migration |
| 33 | +20210201000000 - Second Migration |
| 34 | +20210701000000 - Third Migration <-- `structure.sql` represents up to here |
| 35 | +20210801000000 - New Migration <-- This is where migrations will begin |
| 36 | +``` |
| 37 | + |
| 38 | +The new migration will still run, but the first-through-third migrations will |
| 39 | +not need to be run since the structure already represents the changes applied by |
| 40 | +those migrations. At this point, you can safely delete the first, second, and |
| 41 | +third migration files or keep them for historical auditing. |
| 42 | + |
| 43 | +Let's make this work: |
| 44 | + |
| 45 | +1. Run `mix ecto.dump` which will dump the current structure into |
| 46 | + `priv/repo/structure.sql` by default. Check `mix help ecto.dump` for more |
| 47 | + options. |
| 48 | +2. During project setup with an empty database, run `mix ecto.load` to load |
| 49 | + `structure.sql`. |
| 50 | +3. Run `mix ecto.migrate` to run any additional migrations created after the |
| 51 | + structure was dumped. |
| 52 | + |
| 53 | +To simplify these actions into one command, we can leverage mix aliases: |
| 54 | + |
| 55 | +```elixir |
| 56 | +# mix.exs |
| 57 | + |
| 58 | +defp aliases do |
| 59 | + [ |
| 60 | + "ecto.reset": ["ecto.drop", "ecto.setup"], |
| 61 | + "ecto.setup": ["ecto.load", "ecto.migrate"], |
| 62 | + # ... |
| 63 | + ] |
| 64 | +end |
| 65 | +``` |
| 66 | + |
| 67 | +Now you can run `mix ecto.setup` and it will load the database structure and run |
| 68 | +remaining migrations. Or, run `mix ecto.reset` and it will drop and run setup. |
| 69 | +Of course, you can continue running `mix ecto.migrate` as you create them. |
0 commit comments