strzibny

strzibny

Do you stick to Phoenix (Ecto)'s default migrations without schema file?

Hello all,

I am coming from the Rails world and what I like about Rails is the approach to migrations with a final schema.rb or structure.sql file. This means we don’t have to run all previous migrations to setup a database, just import the schema and you are good to go.

In my opinion this has the following advantages:

1, it’s fast to populate the db
2, there is one source of truth of how the db looks like (commited to source control)
3, it’s easy to delete old migrations and move on

I start to feel that our Phoenix migrations are a little bit unwieldy especially when your colleagues puts things in some migrations that should be just import scripts.

Since Phoenix have a lot of great and thought-out design decisions I would like to know if I am missing something. What’s the benefit of not having a schema file?

I remember that might be some conflicts when merging, but apart from that I don’t know.

What’s your take? Do you follow the Phoenix default?

Most Liked

LostKobrakai

LostKobrakai

You’re talking of phoenix in your post, but actually phoenix doesn’t handle any of that, ecto does.

About your mentioned advantages:

  1. I’ve never had any problems with speed of migrating the db. Especially given full migrations from 0 to everything don’t happen all to often I’m not sure how relevant this metric even is. Do you have an concrete example?

  2. You’ll have one source of truth in both options. Either with many migration scripts or with a structure.sql. The difference is one is more like event sourcing, where changes are stored and the result is implicit, but rebuildable, while the other is more like a crud db, where the latest state is stored, but history is lost.
    In event sourcing if rebuilding state becomes a problem (for speed or other reasons) there’s the idea of snapshots, which save the state at certain times. Ecto does support mix ecto.dump/mix ecto.load to dump your current db schema and load it back into the db. You can use this to get a similar effect. This is probably how ruby is doing it.

  3. Once you use those you could delete the migrations leading up to the schema dump.

In my opinion consolidating migrations can make sense for as long as those parts have not yet hit production. But once they do they’re no longer useful, as you’re no longer dealing with just the plain schema, but also with data stored in those. A dumped structure.sql will not contain data migrations, but they are a very important part of not developing, but maintaining a production database. I’m aware that’s no longer the pure developer perspective, but when debugging issues you’ll need to find out how your db got into a certain state and it might not be a bug in your code, but it might have been a migration.

If you’re handling those necessary data migrations in code outside of your ecto migrations, then I guess you can just use mix ecto.dump/mix ecto.load.

P.S.: Colleagues putting logic in migrations, which doesn’t belong there is a people problem. Changing how migrations are handled is likely not a solution.

strzibny

strzibny

Yes, it’s Ecto, you are right.

1, I actually edit seeds from time to time and when I do I always rebuild. I also rebuild a lot of times in development to simply start clean.

2, The point is not just one source of truth, but simply the ability to see it. Maybe I was just used to it. I am aware of dump/load that’s why I am not asking on how to mimick it, but rather what others actually do.

there is a people problem

…and I did not say otherwise, just saying what can happen. The point is that once the mistake is done, it stays there…

Thanks for your input, appreciate it!

Phillipp

Phillipp

I would also like to see such a functionality. Having one definition and ecto would just do whatever it needs to do in order to transform the current state of the DB to the desired state of the DB. Doing this during runtime would be even better (e.g. building structs/maps with the desired config on runtime and handling it off to ecto to transform the DB).

PS: I know this is highly controversial, but it has its uses.

Where Next?

Popular in Discussions Top

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&*())^% %%:>" From this string, I want to only keep the integers, ...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement