pironim
I’m trying to find good way to migrate data for Phoenix application.
Phoenix has regular migrations and it looks like rails migration.
My previous rails project was big and we often write data migrations(to update some records or delete)
rule of thumb - separate regular(schema) migrations from data migrations
for rails we have several libraries like GitHub - ilyakatz/data-migrate: Migrate and update data alongside your database structure. · GitHub
and you can call it like:
rake db:migrate:with_data
I find GitHub - samsamai/ecto_immigrant: Data migrations for your ecto-backed elixir application · GitHub but it looks outdated and there is no options.
Maybe somebody know good library to add migrations and have ability to run data migration after specific schema migration?
there different opinions how to run data migrations
link them to release and run after schema migrations
, mix schema and data migrations
run migrations after schema migrations.
schema depends on your app
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
Can you explain the benefit from that? To me this sounds like it’s just waiting for some inconsistency between those two to happen.
pironim
In two years you can get many migrations with outdated code and you need to fix them from time to time(module removed or renamed). It’s hard to do If you mix schema changes and data changes
with separate file for data migration you can get some control
e.g. run only schema migrations on local machine (dev, test env)
disable specific data migration and skip evaluation.
it’s all about control, testability and managebility.
Usually data migration have short life time
LostKobrakai
Ok, I don’t have such problems because I migrate data using plain sql, therefore I can keep them around without problems.
shanesveller
I’d highly encourage writing these kinds of transformations as pure modules and functions that wrap straightforward Ecto calls, i.e.
Repo.update_all. This approach lends itself well to testing, repeatable execution, and will look familiar to developers as they’re essentially specialized context modules. Tying them to the mindset of migrations that can be “checked off” as completed and don’t need to happen again doesn’t often mesh with my experience in reality. More often I see situations where it takes several passes through the data with the same intent to fully clean it in-place before a stricter DDL design can be applied. You can still drop the modules and their tests from your codebase once the need is conclusively finished, but you won’t need any acrobatics to repeat the effort again.That’s also why I generally recommend that traditional Ecto migrations only contain DDL statements and no updates or insertions if at all avoidable.
LostKobrakai
I even skip anything
MyApp.Repo, because it means:my_appwould need to be started to be able to run migrations. If I really don’t know the sql I doMyApp.Repo.to_sql/2and copy it in the migration, where it’s run usingexecute(sql)orexecute(up, down).dimitarvp
True, but for one-off, deterministic and simple conversions from an old column to a new column Ecto migratory updating code works okay even months in the future.
Agreed with everything else.
pironim
I understand your point.
It’s mostly linked to migrations structure and testability
On previous rails project we use similar approach class which contain everything and it’s easy to test it.
Data migration was kind of humble object to run Migrator class with all logic
let me show rails example with data migration file
You can test Migrator like regular class and run it during db:migrate
Also to turn off or remove migration it should be executed everywhere and after certain period e.g. 1 year it can be disabled/removed.
dimitarvp
One thing that saved the sanity of me and my colleagues both in Ruby on Rails and Elixir/Phoenix projects was to periodically squish all migrations into a singular
.sqlfile with the contemporary schema and then just “restart” migrations (i.e. have zero of them after the squish).At one point you can absolutely feel how maintaining the old migration scripts and making sure every new onboarded dev goes through them without errors is just not worth it.
But I still don’t recommend the squish / pruning to take place before 6 months (or 50-100 migrations).
shanesveller
A really nice compromise on this idea is Ecto.Migrator.with_repo/2 which was introduced in the 3.x series. You won’t have your full OTP app running, but you can start a skeleton Ecto Repo with minimal connections for the duration of your DB interactions, which don’t have to be traditional migrations. You can readily use Repo calls or context modules, as long as the connection count is set appropriately for your needs. We use this in Distillery custom commands, seeds, etc. to avoid having the whole supervision tree start up just to do some INSERTs.
Something fairly similar is built-in as
mix ecto.dumpandmix ecto.load, and doesn’t require you to fully discard the previous migration code content. Instead it just loads a structure snapshot and then marks previous migrations up to that point as applied, and they can still be replayed from-scratch using the normalecto.migrateto ensure continuity for environments that aren’t able to use this approach, like your Production database. I don’t personally endorse fully squashing or discarding the historical content ourselves when you can get all of the same speed improvements using built-in functionality and still have a great escape hatch when needed.shanesveller
This is part of the disconnect, perhaps, because I’m advocating for treating data-munging tasks as operationally and conceptually separate from schema evolution, and considering that a virtue rather than an inconvenience.