Migrate contact_email and contact_phone to allow dupes

The key item here is that comms.phone and comms.email are meant to
represent a real global namespace, but comms.contact is meant to
represent an organization-specific namespace. This means the mapping,
comms.contact_phone and comms.contact_email can't key off the global
namespace. Otherwise the contact namespace would implicitly be global.
This commit is contained in:
Eli Ribble 2026-05-22 15:13:03 +00:00
parent a72f228e4e
commit f957dc6982
No known key found for this signature in database
14 changed files with 250 additions and 44 deletions

42
db/query/comms/phone.go Normal file
View file

@ -0,0 +1,42 @@
package comms
import (
"context"
"github.com/Gleipnir-Technology/jet/postgres"
"source.gleipnir.technology/Gleipnir/nidus-sync/db"
"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/model"
"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/table"
)
func PhoneInsertIfNotExists(ctx context.Context, txn db.Ex, m model.Phone) (model.Phone, error) {
inserted := postgres.CTE("inserted")
insert := table.Phone.INSERT(
table.Phone.AllColumns,
).MODEL(m).
ON_CONFLICT(table.Phone.E164).DO_NOTHING().
RETURNING(table.Phone.AllColumns)
statement := postgres.WITH(inserted.AS(insert))(
postgres.SELECT(inserted.AllColumns()).
FROM(inserted).
UNION_ALL(
postgres.SELECT(table.Phone.AllColumns).
FROM(table.Phone).
WHERE(postgres.AND(
table.Phone.E164.EQ(postgres.String(m.E164)),
postgres.NOT(postgres.EXISTS(
postgres.SELECT(postgres.STAR).FROM(inserted),
)),
)),
),
)
return db.ExecuteOneTx[model.Phone](ctx, txn, statement)
}
func PhoneFromE164(ctx context.Context, txn db.Ex, e164 string) (model.Phone, error) {
statement := table.Phone.SELECT(
table.Phone.AllColumns,
).FROM(table.Phone).
WHERE(table.Phone.E164.EQ(postgres.String(e164)))
return db.ExecuteOneTx[model.Phone](ctx, txn, statement)
}