strzibny

strzibny

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?

Showing Posts 1 to 6

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 OP

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.

Hermanverschooten

Hermanverschooten

I don’t see the difference between doing a

rake db:schema:load
or 
rake db:structure:load

and

mix ecto.load

and

mix ecto.dump

I do this all the time, I have a Rails app that is my source of truth, it has all the migrations, for running my tests on CI, I do a mix ecto.dump, commit it and in my CI-script then mix ecto.load.

Qqwy

Qqwy

TypeCheck Core Team

I think an important difference between Ecto’s approach vs. Rails’ ActiveRecord approach is that Ecto requires you (for normal usage) to specify the structure of your table columns of your structs.
In ActiveRecord this happens implicitly, so the only place where you can reliably look them up is in the schema.rb, whose structure changes depending on how many migrations have already been executed.

strzibny

strzibny OP

Yes, but in my case I don’t really care about having a structure.sql to look at. I just don’t want to be dependent on migrations.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews