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:
parent
a72f228e4e
commit
f957dc6982
14 changed files with 250 additions and 44 deletions
|
|
@ -8,8 +8,9 @@
|
|||
package model
|
||||
|
||||
type ContactEmail struct {
|
||||
Address string `sql:"primary_key"`
|
||||
Address string
|
||||
Confirmed bool
|
||||
ContactID int32
|
||||
IsSubscribed bool
|
||||
ID int32 `sql:"primary_key"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ type ContactPhone struct {
|
|||
CanSms bool
|
||||
ConfirmedMessageID *int32
|
||||
ContactID int32
|
||||
E164 string `sql:"primary_key"`
|
||||
E164 string
|
||||
IsSubscribed bool
|
||||
StopMessageID *int32
|
||||
ID int32 `sql:"primary_key"`
|
||||
}
|
||||
|
|
|
|||
13
db/gen/nidus-sync/comms/model/email.go
Normal file
13
db/gen/nidus-sync/comms/model/email.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
//
|
||||
// Code generated by go-jet DO NOT EDIT.
|
||||
//
|
||||
// WARNING: Changes to this file may cause incorrect behavior
|
||||
// and will be lost if the code is regenerated
|
||||
//
|
||||
|
||||
package model
|
||||
|
||||
type Email struct {
|
||||
Address string `sql:"primary_key"`
|
||||
IsSubscribed bool
|
||||
}
|
||||
|
|
@ -21,6 +21,7 @@ type contactEmailTable struct {
|
|||
Confirmed postgres.ColumnBool
|
||||
ContactID postgres.ColumnInteger
|
||||
IsSubscribed postgres.ColumnBool
|
||||
ID postgres.ColumnInteger
|
||||
|
||||
AllColumns postgres.ColumnList
|
||||
MutableColumns postgres.ColumnList
|
||||
|
|
@ -66,9 +67,10 @@ func newContactEmailTableImpl(schemaName, tableName, alias string) contactEmailT
|
|||
ConfirmedColumn = postgres.BoolColumn("confirmed")
|
||||
ContactIDColumn = postgres.IntegerColumn("contact_id")
|
||||
IsSubscribedColumn = postgres.BoolColumn("is_subscribed")
|
||||
allColumns = postgres.ColumnList{AddressColumn, ConfirmedColumn, ContactIDColumn, IsSubscribedColumn}
|
||||
mutableColumns = postgres.ColumnList{ConfirmedColumn, ContactIDColumn, IsSubscribedColumn}
|
||||
defaultColumns = postgres.ColumnList{}
|
||||
IDColumn = postgres.IntegerColumn("id")
|
||||
allColumns = postgres.ColumnList{AddressColumn, ConfirmedColumn, ContactIDColumn, IsSubscribedColumn, IDColumn}
|
||||
mutableColumns = postgres.ColumnList{AddressColumn, ConfirmedColumn, ContactIDColumn, IsSubscribedColumn}
|
||||
defaultColumns = postgres.ColumnList{IDColumn}
|
||||
)
|
||||
|
||||
return contactEmailTable{
|
||||
|
|
@ -79,6 +81,7 @@ func newContactEmailTableImpl(schemaName, tableName, alias string) contactEmailT
|
|||
Confirmed: ConfirmedColumn,
|
||||
ContactID: ContactIDColumn,
|
||||
IsSubscribed: IsSubscribedColumn,
|
||||
ID: IDColumn,
|
||||
|
||||
AllColumns: allColumns,
|
||||
MutableColumns: mutableColumns,
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ type contactPhoneTable struct {
|
|||
E164 postgres.ColumnString
|
||||
IsSubscribed postgres.ColumnBool
|
||||
StopMessageID postgres.ColumnInteger
|
||||
ID postgres.ColumnInteger
|
||||
|
||||
AllColumns postgres.ColumnList
|
||||
MutableColumns postgres.ColumnList
|
||||
|
|
@ -70,9 +71,10 @@ func newContactPhoneTableImpl(schemaName, tableName, alias string) contactPhoneT
|
|||
E164Column = postgres.StringColumn("e164")
|
||||
IsSubscribedColumn = postgres.BoolColumn("is_subscribed")
|
||||
StopMessageIDColumn = postgres.IntegerColumn("stop_message_id")
|
||||
allColumns = postgres.ColumnList{CanSmsColumn, ConfirmedMessageIDColumn, ContactIDColumn, E164Column, IsSubscribedColumn, StopMessageIDColumn}
|
||||
mutableColumns = postgres.ColumnList{CanSmsColumn, ConfirmedMessageIDColumn, ContactIDColumn, IsSubscribedColumn, StopMessageIDColumn}
|
||||
defaultColumns = postgres.ColumnList{}
|
||||
IDColumn = postgres.IntegerColumn("id")
|
||||
allColumns = postgres.ColumnList{CanSmsColumn, ConfirmedMessageIDColumn, ContactIDColumn, E164Column, IsSubscribedColumn, StopMessageIDColumn, IDColumn}
|
||||
mutableColumns = postgres.ColumnList{CanSmsColumn, ConfirmedMessageIDColumn, ContactIDColumn, E164Column, IsSubscribedColumn, StopMessageIDColumn}
|
||||
defaultColumns = postgres.ColumnList{IDColumn}
|
||||
)
|
||||
|
||||
return contactPhoneTable{
|
||||
|
|
@ -85,6 +87,7 @@ func newContactPhoneTableImpl(schemaName, tableName, alias string) contactPhoneT
|
|||
E164: E164Column,
|
||||
IsSubscribed: IsSubscribedColumn,
|
||||
StopMessageID: StopMessageIDColumn,
|
||||
ID: IDColumn,
|
||||
|
||||
AllColumns: allColumns,
|
||||
MutableColumns: mutableColumns,
|
||||
|
|
|
|||
81
db/gen/nidus-sync/comms/table/email.go
Normal file
81
db/gen/nidus-sync/comms/table/email.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
//
|
||||
// Code generated by go-jet DO NOT EDIT.
|
||||
//
|
||||
// WARNING: Changes to this file may cause incorrect behavior
|
||||
// and will be lost if the code is regenerated
|
||||
//
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
"github.com/Gleipnir-Technology/jet/postgres"
|
||||
)
|
||||
|
||||
var Email = newEmailTable("comms", "email", "")
|
||||
|
||||
type emailTable struct {
|
||||
postgres.Table
|
||||
|
||||
// Columns
|
||||
Address postgres.ColumnString
|
||||
IsSubscribed postgres.ColumnBool
|
||||
|
||||
AllColumns postgres.ColumnList
|
||||
MutableColumns postgres.ColumnList
|
||||
DefaultColumns postgres.ColumnList
|
||||
}
|
||||
|
||||
type EmailTable struct {
|
||||
emailTable
|
||||
|
||||
EXCLUDED emailTable
|
||||
}
|
||||
|
||||
// AS creates new EmailTable with assigned alias
|
||||
func (a EmailTable) AS(alias string) *EmailTable {
|
||||
return newEmailTable(a.SchemaName(), a.TableName(), alias)
|
||||
}
|
||||
|
||||
// Schema creates new EmailTable with assigned schema name
|
||||
func (a EmailTable) FromSchema(schemaName string) *EmailTable {
|
||||
return newEmailTable(schemaName, a.TableName(), a.Alias())
|
||||
}
|
||||
|
||||
// WithPrefix creates new EmailTable with assigned table prefix
|
||||
func (a EmailTable) WithPrefix(prefix string) *EmailTable {
|
||||
return newEmailTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
|
||||
}
|
||||
|
||||
// WithSuffix creates new EmailTable with assigned table suffix
|
||||
func (a EmailTable) WithSuffix(suffix string) *EmailTable {
|
||||
return newEmailTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
|
||||
}
|
||||
|
||||
func newEmailTable(schemaName, tableName, alias string) *EmailTable {
|
||||
return &EmailTable{
|
||||
emailTable: newEmailTableImpl(schemaName, tableName, alias),
|
||||
EXCLUDED: newEmailTableImpl("", "excluded", ""),
|
||||
}
|
||||
}
|
||||
|
||||
func newEmailTableImpl(schemaName, tableName, alias string) emailTable {
|
||||
var (
|
||||
AddressColumn = postgres.StringColumn("address")
|
||||
IsSubscribedColumn = postgres.BoolColumn("is_subscribed")
|
||||
allColumns = postgres.ColumnList{AddressColumn, IsSubscribedColumn}
|
||||
mutableColumns = postgres.ColumnList{IsSubscribedColumn}
|
||||
defaultColumns = postgres.ColumnList{}
|
||||
)
|
||||
|
||||
return emailTable{
|
||||
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
|
||||
|
||||
//Columns
|
||||
Address: AddressColumn,
|
||||
IsSubscribed: IsSubscribedColumn,
|
||||
|
||||
AllColumns: allColumns,
|
||||
MutableColumns: mutableColumns,
|
||||
DefaultColumns: defaultColumns,
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ func UseSchema(schema string) {
|
|||
Contact = Contact.FromSchema(schema)
|
||||
ContactEmail = ContactEmail.FromSchema(schema)
|
||||
ContactPhone = ContactPhone.FromSchema(schema)
|
||||
Email = Email.FromSchema(schema)
|
||||
EmailContact = EmailContact.FromSchema(schema)
|
||||
EmailLog = EmailLog.FromSchema(schema)
|
||||
EmailTemplate = EmailTemplate.FromSchema(schema)
|
||||
|
|
|
|||
65
db/migrations/00153_comms_phone_log_contact.sql
Normal file
65
db/migrations/00153_comms_phone_log_contact.sql
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
-- +goose Up
|
||||
-- We need to make it possible to have multiple relationships relating phone numbers, a global asset,
|
||||
-- to cantacts, an organization-specific asset.
|
||||
|
||||
-- Step 1: Drop the existing primary key constraint on e164
|
||||
ALTER TABLE comms.contact_phone
|
||||
DROP CONSTRAINT contact_phone_pkey;
|
||||
|
||||
-- Step 2: Add new ID column with SERIAL type (auto-generates sequence)
|
||||
ALTER TABLE comms.contact_phone
|
||||
ADD COLUMN id SERIAL;
|
||||
|
||||
-- Step 3: Set the new ID column as the primary key
|
||||
ALTER TABLE comms.contact_phone
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
-- Step 4: Add foreign key constraint on e164 referencing comms.phone
|
||||
ALTER TABLE comms.contact_phone
|
||||
ADD CONSTRAINT contact_phone_e164_fkey
|
||||
FOREIGN KEY (e164) REFERENCES comms.phone(e164);
|
||||
|
||||
-- Step 5 (Optional but recommended): Add an index on e164 for query performance
|
||||
CREATE INDEX idx_contact_phone_e164 ON comms.contact_phone(e164);
|
||||
|
||||
CREATE TABLE comms.email (
|
||||
address TEXT NOT NULL,
|
||||
is_subscribed BOOLEAN NOT NULL,
|
||||
PRIMARY KEY(address)
|
||||
);
|
||||
INSERT INTO comms.email (
|
||||
address,
|
||||
is_subscribed
|
||||
) SELECT
|
||||
address,
|
||||
FALSE
|
||||
FROM comms.contact_email;
|
||||
INSERT INTO comms.email (
|
||||
address,
|
||||
is_subscribed
|
||||
) SELECT
|
||||
source,
|
||||
FALSE
|
||||
FROM comms.email_log
|
||||
ON CONFLICT (address) DO NOTHING;
|
||||
|
||||
-- Do the same thing to email, for exactly the same reasons
|
||||
-- Step 1: Drop the existing primary key constraint on address
|
||||
ALTER TABLE comms.contact_email
|
||||
DROP CONSTRAINT contact_email_pkey;
|
||||
|
||||
-- Step 2: Add new ID column with SERIAL type (auto-generates sequence)
|
||||
ALTER TABLE comms.contact_email
|
||||
ADD COLUMN id SERIAL;
|
||||
|
||||
-- Step 3: Set the new ID column as the primary key
|
||||
ALTER TABLE comms.contact_email
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
-- Step 4: Add foreign key constraint on address referencing comms.email
|
||||
ALTER TABLE comms.contact_email
|
||||
ADD CONSTRAINT contact_email_address_fkey
|
||||
FOREIGN KEY (address) REFERENCES comms.email(address);
|
||||
|
||||
-- Step 5 (Optional but recommended): Add an index on address for query performance
|
||||
CREATE INDEX idx_contact_email_address ON comms.contact_email(address);
|
||||
42
db/query/comms/phone.go
Normal file
42
db/query/comms/phone.go
Normal 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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue