Delete the 1st pair in a map

In the beginning I was feeding my query through Enum.group_by so it’s a map, but my logic changed(what a mess I created) now actually I don’t use it anymore and I agreed that enum query returning a list

  • On what exactly?

  • And what defines the ‘first’? First registered matching the constraint? (for example: first user registered at an address)

Ps. My guess now is that we will end up writing the proper query or migration :slight_smile:

Or: you have multiple entries that should be detected as the same (needs logic) and have to be ‘merged’ into one.

Yep, your 2nd thought…
Old methods allows them to have different accounts with same email and we treated them based on a verified column, if they’re verified ?
Now seems like new logic users are all verified and old logic made bunches of duplicates in the past and the past users can’t register now because register method now got piped through email check
So I cleaned old data and put unique constraint on email

I’m lost.

But for archeological reasons:

were you looking for the “first” two identical values of the map?

  1. I guess you made the unique constraint only in the Ecto schema. Is that right?

Otherwise there are no duplicated email addresses in the database possible due to the constraint…so no merging needed.

  1. Or: you mean you want to detect the duplicates, merge them and THEN put the unique constraint.

Yeah I need to make a unique constraint for it, it’s not there since the beginning
But after I run a mix task to clean all the old data

We are getting there :slight_smile:

So, I guess the email addresses are some sort of ‘group by’ field, which creates groups with multiple accounts. Then:

A. All records should merge their fields
B. Just pick one (first based on insert date?), remove the rest
C. Other…

Pick one.

Once we have only one record per email address, the unique constraint can be set.

1 Like

Yep
A. All records should merge themselves
And luckily they are lists
So I can iterate through each bunch of records
Then I can Enum.reduce to get accumulated records of same kind in one bunch of duplicate based on statuses, delete the rest and except the 1st one in that acc records
Or I can just Enum.each if they’re the same status
Or if all of them not verified then I delete all except first one

Just for the record badum tishh: How many record are there in total?

ps. Gotta sleep. Will see if someone takes it from here :slight_smile:

1 Like

There are 300-ish :sweat_smile:

Then we better get to the solution quickly otherwise there might be more posts in this thread then records…and doing it manually would have been faster :sweat_smile:

It won’t haha, there are 10 user statuses, so the logic is like crazy
I probably close as I can’t edit the post or title, that’s sad

It would be nice if the post is editable when it’s still under 5 replies

For pseudo code:

Get all accounts

accounts
|> Enum.group_by(&(&1.email))
|> IO.inspect(label: :groups)
|> Enum.map(fn {email, records} = group ->
       IO.inspect(records, label: :do_merge)
       
        records
        |> Enum.reduce(%{}, fn 
            record, acc -> 
                   something
                   %{acc | email: foo, other: bla}
       end)
       |> IO.inspect(label: :merge_result)

   end)

—- at this moment we have a list of ‘unique by email’ maps —-

Remove all records from table
Insert all unique structs into table
Run migration to add constraint

For during ‘merging’, I will delete everything until there is one optimised left that I want to keep. I wouldn’t be neccessarily merging them

Added fake indentation to make the nested Enum.map distinguishable

See some dummy code written at mobile in post above.

That makes so much more sense! But it’s been an entertaining thread :slight_smile:

Oddly I had maybe a similar mental blip last week. I said to my colleague: “We can use Map.delete/1 to delete the key and get the value!” to which he was like, “Uh, what?” I was like, “Crap right, we’re not in Ruby.” Of course, that is not even how Ruby’s delete works either :sweat_smile: We all have our moments.

1 Like

I’ll admit I was having more fun when we didn’t know :laughing:

2 Likes

did you end up finding a solution for your problem?

Yes I did, which is using head and tail pattern matching is all I need lol. And I ended up using list instead of map

3 Likes