cgrothaus

cgrothaus

Patterns for making seeds idempotent?

Every freshly generated Phoenix project sports a seeds file priv/repo/seeds.exs and an accompanying mix alias ecto.setup to apply those seeds. AFAIU the seeds can only be applied once to a database.

Are there any established patterns to make the seeds idempotent? Is this a good idea at all?

Marked As Solved

cgrothaus

cgrothaus

FTR: I came up with this solution to mark the seeds as already applied from within seeds.exs, so that running that script is idempotent.

defmodule SeedsIdempotencyHandling do
  @magic_marker_value 2000_00_00_00_00_00
  import Ecto.Query, only: [from: 2]

  def seeds_already_applied? do
    query = from m in Ecto.Migration.SchemaMigration, where: m.version == @magic_marker_value
    Repo.exists?(query)
  end

  def mark_seeds_as_applied do
    Repo.insert(%Ecto.Migration.SchemaMigration{
      version: @magic_marker_value,
      inserted_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
    })
  end
end

if SeedsIdempotencyHandling.seeds_already_applied?() do
  IO.puts("Seeds already applied, skipping...")
else
  # put your seeds here
  SeedsIdempotencyHandling.mark_seeds_as_applied()
end

Also Liked

al2o3cr

al2o3cr

It’s likely too much machinery for a single seed, but if you have an ongoing need for idempotent seeding there’s phil_columns:

LostKobrakai

LostKobrakai

I personally use seeds only for dummy data for development. If it changes I just drop and reapply. Everything else should imo be a (data) migration.

rcothren

rcothren

That’s the best project name ever.

krasenyp

krasenyp

The mixture of snake and kebab case is making me uncomfortable.

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement