Programming Ecto (Pragprog)

This is a great book! I’m always on the lookout for new beta versions to be published so I can read the new chapters that are released.

I don’t think PragProg allows forums anymore on their site (only errata) and I have a question about the latest chapter 12. First, I really appreciate that chapter since many times books don’t get into higher level application design topics (maybe to avoid “flame wars”) :slight_smile:

Question I have is about the good “general rule” advice to put all pure functions manipulating queries, changesets, and multis into their associated schema modules and put all the impure code into context modules. Right now I put all changeset code in the schema modules but usually leave the multi (and query) code in the context module… generally since my multis and queries are affecting multiple DB tables/schemas so they don’t really belong in any specific schema module. Are you recommending maybe using Multi.merge extensively to build up the whole multi in the context module calling functions in the schema modules… or something else?

I looked at the sample code that comes with the book and can’t find much use of multi (yet). Are there any large/decent scale OSS projects I can study that follow the recommended pattern… especially as it relates to Multi or query use defined in schema modules?

@darinwilson

3 Likes

Hi, glad you are liking the book.

Chapter 12 was a difficult chapter to write, balancing our experiences writing Ecto applications while still maintaining that there is no right or wrong and that there may be other equally valid ways of organizing applications. Where Multi fits best was something we discussed while writing the chapter and it seems like somewhere during the discussions some things were missed.

After discussing this again we are amending this part of the chapter and adding a paragraph that clarifies that we believe multi fits better in context modules than schema modules. The reason being, while multi is a pure data structure like changesets, with multi you usually compose functions from multiple schemas and with changesets you compose functions within a single schema. Since multi is working with multiple schemas it fits better on a higher level inside a context.

Thank you for bringing this up and helping us improve the book during the beta! :purple_heart:

7 Likes

You can look at hexpm the application that runs https://hex.pm. Examples of context modules using multis are: Keys, Users, and Releases.

6 Likes

I have started reading the book and got problem with fist exercise. I think the code provided with the book though pragrpog website is incomplete:

    08:54:23.966 [debug] QUERY OK db=2.4ms
INSERT INTO "genres" ("name","wiki_tag","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["jazz", "Jazz", {{2018, 8, 22}, {22, 54, 23, 958894}}, {{2018, 8, 22}, {22, 54, 23, 958900}}]

08:54:23.970 [debug] QUERY OK db=4.0ms
commit []

08:54:23.971 [debug] QUERY OK db=0.4ms queue=0.1ms
begin []

08:54:23.973 [debug] QUERY OK db=2.4ms
INSERT INTO "genres" ("name","wiki_tag","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["live", "Concert", {{2018, 8, 22}, {22, 54, 23, 971190}}, {{2018, 8, 22}, {22, 54, 23, 971196}}]

08:54:23.974 [debug] QUERY OK db=0.4ms
commit []
** (UndefinedFunctionError) function MusicDB.Note.__schema__/1 is undefined (module MusicDB.Note is not available)
    MusicDB.Note.__schema__(:primary_key)
    (ecto) lib/ecto/changeset/relation.ex:138: Ecto.Changeset.Relation.change/3
    (ecto) lib/ecto/repo/schema.ex:597: anonymous fn/4 in Ecto.Repo.Schema.surface_changes/3
    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/repo/schema.ex:584: Ecto.Repo.Schema.surface_changes/3
    (ecto) lib/ecto/repo/schema.ex:189: Ecto.Repo.Schema.do_insert/4
    (ecto) lib/ecto/repo/schema.ex:125: Ecto.Repo.Schema.insert!/4
    priv/repo/seeds.exs:15: (file) 

I have check the code and function MusicDB.Note is missing.

Hi @sergek. Sorry for the trouble - I screwed up the source code when we pushed the last beta, and didn’t catch this. :disappointed:

I’ve submitted a fix, so if you download the latest version of the source from the book’s webpage, everything should be working again.

Please give that a try, and post again if you’re still having trouble. Thanks for your patience! :heart:

2 Likes

Thank you for the prompt fix! :grinning:

It did work much better this time. Although this time I got warnings like the ones below:
==> music_db
Compiling 14 files (.ex)
warning: invalid association notes in schema MusicDB.Artist: associated schema MusicDB.Note does not have field artist_id
lib/music_db/artist.ex:9: MusicDB.Artist (module)

warning: invalid association `notes` in schema MusicDB.Track: associated schema MusicDB.Note does not have field `track_id`
  lib/music_db/track.ex:9: MusicDB.Track (module)

warning: invalid association `notes` in schema MusicDB.Album: associated schema MusicDB.Note does not have field `album_id`
  lib/music_db/album.ex:9: MusicDB.Album (module)

Is that something to worry about?

Cheers

It shouldn’t block you from working with the code, and we’ll get rid of the warning in the next beta :slightly_smiling_face:

1 Like

Thanks Darin,

So far so good! Book is awesome - I am very glad I bought it.

Cheers

1 Like

2018-09-05

3 Likes

Does anyone know whether this book will be updated to reflect the changes in Ecto 3.0?

Thanks in advance!

Posted on July 1st above :slight_smile: .

4 Likes

2018-11-02

1 Like

2019-01-24

This is just to let you know that Programming Ecto (eBook) has recently been updated. You own an electronic version of this book, and so you’ll be able to download this latest version.

Changes in This Release

  • Added chapter 17, Tuning For Performance
  • Made several edits and additions throughout to book to reflect the changes for Ecto 3.0
  • Fixed many errata
4 Likes

Aaaaand that’s a wrap - the book is officially completed and released!

Thanks to all who participated in the beta - the early feedback was super helpful :heart:

13 Likes

Interim-review (continued in post below)

It’s summer here and as I try not being glued to my desk all day, I needed a book to read where it wasn’t strictly necessary to follow along with the examples. I chose this book after what @peerreynders said in his post above :smiley:

I’m only 60% of the way through this book (chapters 1-6 as per Peer’s recommendation, and will be going back to Programming Phoenix now - edit - I actually ended up finishing the book as I was enjoying it so much! See the rest of my review in the post below…) but I just wanted to post a mini-review to say I’m really really really loving this book! It’s been one of the most enjoyable Elixir books I’ve read so far. Why? Because it’s sooooooooooooooooooooooooooooo easy to read!! :lol:

There are a number of reasons for this, in part it is because of the pace and well thought out progression of topics, but the thing I really appreciated, is that it seems to have been produced with those of us who use e-readers in mind; there’s absolutely no having to flick back through pages to review code that is spoken about. Code is either just before or just after it’s covered or (and I really appreciate this…) repeated if it is spoken about again. The output is also included, eradicating the need to actually follow along as the results are there in front of you. On top of that, since everything is well explained and at an even pace, the cognitive load required to progress through the book is relatively low if you already know the basics of SQL. All this makes reading on e-readers frustration free and a real joy.

The other thing I really like about this book is that it acts as a bit of a SQL refresher (in fact so far my only suggestion would have been to run with this and cover more SQL and how to do it with Ecto - though I’m guessing the remainder of the book may do some of that anyway).

The other reason I like this book is because it (perhaps obviously) showcases what’s so great about Ecto. Ecto has been put together very nicely and I like that it follows the Elixir theme of keeping magic to a minimum. The query syntax is pretty close to SQL query syntax, and I think this will make converting SQL to Ecto a lot easier than trying to do the same in other DB libraries or ORMs such as Active Record. It will also mean if you ever need to drop into raw SQL it should be a bit easier.

So my thoughts so far are… get this book!! It’s so incredibly easy to read that I’m pretty sure you’ll enjoy it just as much as I am!

Well done Eric and Darin! :023:

5 Likes

My review - continued…

I’ve now finished this book! So here’s the rest of my review :003:

I’ll start by saying that I wasn’t really sure what I was expecting from this book - I mean, how deep can a database library be? Or how involved might it be using one? Of course that was based on my experience with ActiveRecord. Well it turns out a DB library can be more than what you may be used to or expect, and I was very pleasantly surprised by this book as well as Ecto itself.

One of the things I’ve said numerous times about Elixir - that it and the community show us a ‘better way’ - is most definitely echoed in this book! Some of the things I learned that I didn’t know before are that I can create custom Ecto types, or use table-less schemas, or that polymorphism can be handled in several different ways - all with different pros and cons.

I also learned about upserts and transactions and as I mentioned in the book club thread, there was a very welcomed chapter on application design which covers separating the Pure from the Impure, (Phoenix) Contexts and Umbrellas! I really wasn’t expecting to see that in a book about Ecto and it was definitely very much appreciated.

I also enjoyed the chapter and details regarding Phoenix, and I agree with @peerreynders that it may actually be beneficial to read this book before Programming Phoenix (or as I did, after the initial part of it) - it is so easy to read and doesn’t require much cognitive load if you are already familiar with the basics of SQL and something like ActiveRecord, so you will finish it in no time!

Miss this book at your peril! It will show you why Ecto is different… and why Ecto guides you down a ‘better’ path. Isn’t that in part why we came to Elixir?

Well done @darinwilson and @ericmj (and for all your work on Ecto!) :023:

5 Likes

Nice review; now I’m even more excited to dig into this book after I revisit Programming Elixir >= 1.6 and work through Elixir in Action. :tada:

Thanks for reiterating this point; I’m going to follow suit. :spades: :diamonds: :clubs: :purple_heart:

2 Likes

Nice one @Ted! Let us know how you get on :003:

You have selected great books so you can’t go wrong no matter which order you may end up reading them :023:

I started a related thread here so as to not go off-topic in this thread :smiley:

I started reading the book and I agree with @AstonJ the book is very easy to follow :point_up: (I am far from being a DB expert) knowing how important Ecto in the elixir ecosystem reading it is a no brainer for anyone learning elixir or phoenix

This is my favorite elixir book so far :heart:

Thank you to the authors @darinwilson @ericmj and to everyone involved in this project!

4 Likes

This is my next book.

2 Likes