Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Cancel the unsubscribe table foreign key constraint

lotk0d06e53ec595
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go b/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go
index f8a2a118..02f97e8a 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go
@@ -74,6 +74,7 @@ func (c *ControllerV1) Unsubscribe(ctx context.Context, req *v1.UnsubscribeReq)
g.Log().Error(ctx, "Failed to record unsubscribe: %v", err)
return err
}
+
}
return nil
diff --git a/core/internal/service/database_initialization/batch_mail.go b/core/internal/service/database_initialization/batch_mail.go
index be046121..c98ba6a7 100644
--- a/core/internal/service/database_initialization/batch_mail.go
+++ b/core/internal/service/database_initialization/batch_mail.go
@@ -100,10 +100,7 @@ func init() {
group_id INTEGER,
template_id INTEGER,
task_id INTEGER,
- unsubscribe_time INTEGER NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
- FOREIGN KEY (group_id) REFERENCES bm_contact_groups(id) ON DELETE SET NULL,
- FOREIGN KEY (template_id) REFERENCES email_templates(id) ON DELETE SET NULL,
- FOREIGN KEY (task_id) REFERENCES email_tasks(id) ON DELETE SET NULL
+ unsubscribe_time INTEGER NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW())
)`,
`CREATE TABLE IF NOT EXISTS abnormal_recipient (
@@ -200,6 +197,11 @@ func init() {
_ = AddColumnIfNotExists("bm_contact_groups", "welcome_subject", "TEXT", "''", false)
_ = AddColumnIfNotExists("bm_contact_groups", "send_welcome_email", "SMALLINT", "0", true)
+ // unsubscribe_records
+ _ = DropForeignKeyIfExists("unsubscribe_records", "group_id")
+ _ = DropForeignKeyIfExists("unsubscribe_records", "template_id")
+ _ = DropForeignKeyIfExists("unsubscribe_records", "task_id")
+
g.Log().Info(context.Background(), "Batch mail tables initialized successfully")
})
}
diff --git a/core/internal/service/database_initialization/utils.go b/core/internal/service/database_initialization/utils.go
index d9433abf..e4b28d1f 100644
--- a/core/internal/service/database_initialization/utils.go
+++ b/core/internal/service/database_initialization/utils.go
@@ -32,3 +32,32 @@ END $$;`, table, column, table, column, columnType, notNullStr, defaultStr)
}
return err
}
+
+func DropForeignKeyIfExists(table, column string) error {
+ sql := fmt.Sprintf(`
+DO $$
+DECLARE
+ constraint_name text;
+BEGIN
+ SELECT tc.constraint_name INTO constraint_name
+ FROM information_schema.table_constraints tc
+ JOIN information_schema.key_column_usage kcu
+ ON tc.constraint_name = kcu.constraint_name
+ AND tc.table_schema = kcu.table_schema
+ WHERE tc.constraint_type = 'FOREIGN KEY'
+ AND tc.table_name = '%s'
+ AND kcu.column_name = '%s'
+ LIMIT 1;
+
+ IF constraint_name IS NOT NULL THEN
+ EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %%I;', constraint_name);
+ END IF;
+END $$;
+`, table, column, table)
+
+ _, err := g.DB().Exec(context.Background(), sql)
+ if err != nil {
+ g.Log().Error(context.Background(), "Failed to drop foreign key:", err, sql)
+ }
+ return err
+}