Best parts of "What's New in Ecto 2.0"

SeriesPhoenix on Rails clock7 min read

Plataformatec's official book about the newest Ecto revamp is finally out and it's a must-read for any serious Elixir/Rails developer.

I must say this free e-book release was on my waiting list for a long time. Beta versions were available earlier on but I've decided to wait for the final edition. And hell, it was worth the wait. The reasoning behind this article is to list some of those real gems waiting for you in this book and to make it clear that "What's New in Ecto 2.0" is much more than just an "extended changelog". But first, let's go over a really basic question...

Why this book exists

Ecto is the data layer solution of choice in Phoenix. Although perfectly capable on its own, it's arguably one of the most important building blocks of the web framework. Also, it used to be one of of the best showcases of what metaprogramming in Elixir is really capable of, both in terms of performance and in making developer's life easier with clean syntax and compile time safety.

OK, so it's a pretty useful piece of packaging and they've released a new major version of it half a year ago. But why did they work for all those months on a whole book about it? Wouldn't a well-organized changelog or a blog article just suffice? The answer is no. Of course, the changelogs are in place, as are the blog articles, but this book happens to be so much more than just an extended changelog with code samples. That's partly because of what Ecto 2.0 actually is.

Well, 2.0 doesn't just bring new features and conveniences (which it also does), but instead it takes some serious steps back in order to revisit and revamp the whole philosophy of version 1.0. The aim here is to cut the cord with some of the long-standing bad web development habits (like the use of model callbacks) and replace them with viable alternatives, thus encouraging better app architecture and concern separation.

But the best part is that the book doesn't just list and showcase the biggest changes. Instead, it puts those changes in context by telling the whole story starting with the reasoning behind version 1.0 followed by a case study of issues that arose since its release. It's a spot-on discussion of generic pitfalls at managing data layer code which is worth reading even by those web developers who won't ever write a single line of code in Elixir, Phoenix or Ecto.

Best parts

(Re)defining "ORM"

Many developers use the term ORM to randomly define the data layer in a web framework. In "What's New in Ecto 2.0" you'll get to see by example what ORM is and what it isn't. Then, it's explained how much of an actual ORM the new Ecto really is. Also, it's made clear that Ecto 2.0 won't automatically save the developer from all traps behind ORMs but instead it gives versatile tools for choosing the best approach based on specific project's scale and needs.

Flexible schemas

ORMs like ActiveRecord decide for you that it's that monolithic thing called model that's in the middle between your app code and the database table. Ecto has schemas for defining relational mapping and it's absolutely possible to make them fat too. That's why it's encouraged to split them into multiple per-domain subsets or even ditch schemas entirely and go with plain data structures in some one-shot scenarios like reports.

It's clear a lot of work was put into making Ecto 2.0 flexible enough to be able to operate with or without defined structures like schemas and changesets, depending on the use case. And of course, the book will show you examples of such cases.

Flexible queries

Ecto 1.0 featured two syntaxes for building queries - keyword syntax (from) and pipe-based syntax (where, order_by etc). Unfortunately, the developer was often forced to pick the only one that was capable of completing the querying task at hand, instead of picking the one that would be most compact and fitting to the code structure.

In Ecto 2.0, plain data structures are made a first class citizen in querying just as they are made a first class alternative to schemas. Combined with the dynamic macro, it seems now developers will have a choice of many alternative paths for assembling the same query and will be able to pick the best one based on what they're coding and how they want to code it.

Chapters about query and schema flexibility improvements serve as a perfect excuse for showcasing common pitfalls of ORMs that decide for you at which abstraction layer should you operate. Looks like we may finally have the jack of all (or at least more than before) DB trades.

Multi-tenancy in depth

You may have wondered what goes into making a multi-tenant app when you included a gem like apartment in your Rails app. In "What's New in Ecto 2.0" you'll be taken on a tour through all ingredients on the database end and you'll get a grasp on how to assemble them into a working whole using Ecto 2.0's support for prefixes. Here's what you'll learn, among others:

  • Initializing the default prefix (or just using the default public one)
  • Defining prefixes per schema (eg. in order to have global/public tables)
  • Defining prefixes per query while being protected from cross-tenant data leaks
  • Migrating multiple tenants at once or separately

Testing: 3rd party factories are dead & more

Being able to do one-shot queries and inserts without schemas and changesets, among others, makes seeding the database really smooth and compact. In the book, you'll see how easy it is to build your own test factories without having to rely on 3rd party packages just to have that one-liner for creating a record. It's surprising how much of the boilerplate will now just go away.

As far as testing goes, you'll also learn how to assemble shorter database queries and selects for sake of making clear test assertions. Lastly, with the new SQL Sandbox feature, also thoroughly covered in the book, your acceptance tests will gain unprecedented performance boost.

Polymorphism in depth

Ecto 2.0 adds many_to_many relationships which apparently aren't quite what you may expect when coming from Rails. But the "What's New in Ecto 2.0" book doesn't just show how to leverage their power for bringing polymorphic relationships to life. It also discusses the limitations of possible approaches to modeling such relationships, once again offering the "why" part of the Ecto story. You'll learn why the parent_id + parent_type tandem, which you may know from Rails, isn't such a great idea for polymorphism and what to replace it with.

 Introducing UPSERT

As you'll learn from the book, Ecto 2.1 supports the UPSERT feature added in PostgreSQL 9.5. You'll be presented with an extensive real-life scenario as to how to leverage this new DB feature to reduce the number of calls to the database for bulk operations with insert_all.

By the way, it's just one of multiple places in which the book shows the features of not just Ecto 2.0, but also the 2.1 update which seemingly builds upon 2.0's foundations in order to take some of its features one step further.

Working with transactions

The description of the all new Ecto.Multi serves as a playground for an overview of common challenges when it comes to implementing large idempotent actions with multiple DB interactions. Multiple approaches to structuring such actions' code are presented, solving both the main challenge of rolling back all chained changes on failure and a non-trivial side quest for accessing previous operation's result from within the chain.


There's a lot of useful information in the book which I didn't cover here (eg. subqueries), but my objective here is to encourage you to read "What's New in Ecto 2.0" and not to create a short version of it.

I'm really grateful to the Plataformatec team for all the effort they've put into this book. Once again, I was so happy to see all those RoR development problems that I've stumbled upon for years being listed, tackled, discussed and, most importantly, solved. Why "once again"? Because this straight, pragmatic, honest and iterative approach to making web development a better experience is something that I've seen from my day one in the Elixir/Phoenix universe.

Thank you, Ecto crew and Plataformatec!