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

table name [contacts] changed to [bm_contacts] table name [contact_groups] changed to [bm_contact_groups]

lotk39203958cd81
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_get_user_groups.go b/core/internal/controller/batch_mail/batch_mail_v1_get_user_groups.go
index 8da3c967..e69fba67 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_get_user_groups.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_get_user_groups.go
@@ -13,7 +13,7 @@ func (c *ControllerV1) GetUserGroups(ctx context.Context, req *v1.GetUserGroupsR
var groups []*v1.GroupInfo
err = g.DB().Model("contacts c").
- LeftJoin("contact_groups cg", "c.group_id = cg.id").
+ LeftJoin("bm_contact_groups cg", "c.group_id = cg.id").
Fields("cg.id, cg.name").
Where("c.email", req.Email).
Where("c.active", 1).
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 a4144f31..f8a2a118 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go
@@ -43,7 +43,7 @@ func (c *ControllerV1) Unsubscribe(ctx context.Context, req *v1.UnsubscribeReq)
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// 1. Update contact status to inactive (unsubscribed)
for _, groupId := range req.GroupId {
- result, err := tx.Model("contacts").
+ result, err := tx.Model("bm_contacts").
Where("email", claims.Email).
Where("group_id", groupId).
Data(g.Map{"active": 0}).
diff --git a/core/internal/controller/contact/contact_v1_delete_contacts.go b/core/internal/controller/contact/contact_v1_delete_contacts.go
index 48a4ee15..5377458a 100644
--- a/core/internal/controller/contact/contact_v1_delete_contacts.go
+++ b/core/internal/controller/contact/contact_v1_delete_contacts.go
@@ -28,7 +28,7 @@ func (c *ControllerV1) DeleteContacts(ctx context.Context, req *v1.DeleteContact
// Start a transaction
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
- result, err := g.DB().Model("contacts").
+ result, err := g.DB().Model("bm_contacts").
Where("email IN(?)", req.Emails).
Where("active", req.Status).
Delete()
diff --git a/core/internal/controller/contact/contact_v1_merge_contacts_groups.go b/core/internal/controller/contact/contact_v1_merge_contacts_groups.go
index c0c176ab..f568ea29 100644
--- a/core/internal/controller/contact/contact_v1_merge_contacts_groups.go
+++ b/core/internal/controller/contact/contact_v1_merge_contacts_groups.go
@@ -38,7 +38,7 @@ func (c *ControllerV1) MergeContactsGroups(ctx context.Context, req *v1.MergeCon
for {
// 1. Batch get source group contacts
var contacts []*entity.Contact
- err = g.DB().Model("contacts").
+ err = g.DB().Model("bm_contacts").
Where("group_id IN(?)", req.SourceGroups).
Limit(batchSize).
Offset(offset).
@@ -76,7 +76,7 @@ func (c *ControllerV1) MergeContactsGroups(ctx context.Context, req *v1.MergeCon
// 3. Batch check existing contacts in the target group
var existingEmails []string
- err = g.DB().Model("contacts").
+ err = g.DB().Model("bm_contacts").
Fields("email").
Where("group_id = ?", req.TargetGroup).
Where("email IN (?)", getMapKeys(emailStatusMap)).
@@ -115,7 +115,7 @@ func (c *ControllerV1) MergeContactsGroups(ctx context.Context, req *v1.MergeCon
end = len(insertData)
}
- _, err = g.DB().Model("contacts").
+ _, err = g.DB().Model("bm_contacts").
Data(insertData[i:end]).
Insert()
diff --git a/core/internal/service/batch_mail/batch_mail.go b/core/internal/service/batch_mail/batch_mail.go
index e350c664..1d226300 100644
--- a/core/internal/service/batch_mail/batch_mail.go
+++ b/core/internal/service/batch_mail/batch_mail.go
@@ -191,7 +191,7 @@ func GetGroupInfo(ctx context.Context, groupId int) (*v1.GroupInfo, error) {
Count int `json:"count"`
}
- err := g.DB().Model("contact_groups cg").
+ err := g.DB().Model("bm_contact_groups cg").
LeftJoin("contacts c", "cg.id = c.group_id").
Fields("cg.id, cg.name, COUNT(CASE WHEN c.active = 1 THEN 1 END) as count").
Where("cg.id", groupId).
@@ -212,7 +212,7 @@ func GetGroupInfo(ctx context.Context, groupId int) (*v1.GroupInfo, error) {
// GetActiveContacts get active contacts in group
func GetActiveContacts(ctx context.Context, groupId int) ([]*entity.Contact, error) {
var contacts []*entity.Contact
- err := g.DB().Model("contacts").
+ err := g.DB().Model("bm_contacts").
Where("group_id", groupId).
Where("active", 1).
Scan(&contacts)
diff --git a/core/internal/service/contact/contact.go b/core/internal/service/contact/contact.go
index cfa2a5ab..08fbf405 100644
--- a/core/internal/service/contact/contact.go
+++ b/core/internal/service/contact/contact.go
@@ -17,18 +17,18 @@ func CreateGroup(ctx context.Context, name, description string) (int, error) {
"create_time": int(now),
"update_time": int(now),
}
- lastInsertId, err := g.DB().Model("contact_groups").Ctx(ctx).Data(data).InsertAndGetId()
+ lastInsertId, err := g.DB().Model("bm_contact_groups").Ctx(ctx).Data(data).InsertAndGetId()
return int(lastInsertId), err
}
func GetGroup(ctx context.Context, id int) (*entity.ContactGroup, error) {
var group entity.ContactGroup
- err := g.DB().Model("contact_groups").Ctx(ctx).Where("id", id).Scan(&group)
+ err := g.DB().Model("bm_contact_groups").Ctx(ctx).Where("id", id).Scan(&group)
return &group, err
}
func GetAllGroups(ctx context.Context, keyword string) ([]*v1.ContactGroup, error) {
- model := g.DB().Model("contact_groups").
+ model := g.DB().Model("bm_contact_groups").
Fields("id, name, description, create_time, update_time").
Order("create_time desc")
@@ -49,17 +49,17 @@ func GetAllGroups(ctx context.Context, keyword string) ([]*v1.ContactGroup, erro
func UpdateGroup(ctx context.Context, id int, data g.Map) error {
data["update_time"] = time.Now().Unix()
- _, err := g.DB().Model("contact_groups").Ctx(ctx).Data(data).Where("id", id).Update()
+ _, err := g.DB().Model("bm_contact_groups").Ctx(ctx).Data(data).Where("id", id).Update()
return err
}
func DeleteContactsByGroupId(ctx context.Context, groupId int) error {
- _, err := g.DB().Model("contacts").Ctx(ctx).Where("group_id", groupId).Delete()
+ _, err := g.DB().Model("bm_contacts").Ctx(ctx).Where("group_id", groupId).Delete()
return err
}
func DeleteGroup(ctx context.Context, id int) error {
- _, err := g.DB().Model("contact_groups").Ctx(ctx).Where("id", id).Delete()
+ _, err := g.DB().Model("bm_contact_groups").Ctx(ctx).Where("id", id).Delete()
return err
}
@@ -104,7 +104,7 @@ func BatchCreateContactsIgnoreDuplicate(ctx context.Context, contacts []*entity.
}
// Insert current batch
- result, err := g.DB().Model("contacts").Ctx(ctx).Data(data).InsertIgnore()
+ result, err := g.DB().Model("bm_contacts").Ctx(ctx).Data(data).InsertIgnore()
if err != nil {
g.Log().Error(ctx, "Failed to insert batch %d/%d: %v", i+1, totalBatches, err)
return totalAffected, err
@@ -132,12 +132,12 @@ func BatchCreateContacts(ctx context.Context, contacts []*entity.Contact) error
func GetContactsByGroup(ctx context.Context, groupId int) ([]*entity.Contact, error) {
var contacts []*entity.Contact
- err := g.DB().Model("contacts").Ctx(ctx).Where("group_id", groupId).Scan(&contacts)
+ err := g.DB().Model("bm_contacts").Ctx(ctx).Where("group_id", groupId).Scan(&contacts)
return contacts, err
}
func CountContactsByGroup(ctx context.Context, groupId int, active int) (int, error) {
- model := g.DB().Model("contacts").Ctx(ctx).Where("group_id", groupId)
+ model := g.DB().Model("bm_contacts").Ctx(ctx).Where("group_id", groupId)
if active >= 0 {
model = model.Where("active", active)
}
@@ -145,7 +145,7 @@ func CountContactsByGroup(ctx context.Context, groupId int, active int) (int, er
}
func CheckGroupNameExists(ctx context.Context, name string) (bool, error) {
- count, err := g.DB().Model("contact_groups").Ctx(ctx).Where("name", name).Count()
+ count, err := g.DB().Model("bm_contact_groups").Ctx(ctx).Where("name", name).Count()
if err != nil {
return false, err
}
@@ -163,7 +163,7 @@ func ContactsGroupWithPage(ctx context.Context, page, pageSize int, keyword stri
keyword = strings.TrimSpace(keyword)
- model := g.DB().Model("contact_groups").Ctx(ctx)
+ model := g.DB().Model("bm_contact_groups").Ctx(ctx)
if keyword != "" {
model = model.WhereLike("name", "%"+keyword+"%").
@@ -185,7 +185,7 @@ func ContactsGroupWithPage(ctx context.Context, page, pageSize int, keyword stri
}
func GetContactsWithPage(ctx context.Context, page, pageSize int, groupId int, keyword string, status int) (total int, list []*entity.Contact, err error) {
- model := g.DB().Model("contacts").Safe()
+ model := g.DB().Model("bm_contacts").Safe()
// Add query conditions
if groupId > 0 {
@@ -222,7 +222,7 @@ func GetContactsWithPage(ctx context.Context, page, pageSize int, groupId int, k
conditions["active"] = status
}
- subQuery := g.DB().Model("contacts").
+ subQuery := g.DB().Model("bm_contacts").
Fields("DISTINCT ON (email) id")
if len(conditions) > 0 {
@@ -253,7 +253,7 @@ func GetContactGroupsInfo(ctx context.Context, email string, status int) ([]*ent
var groups []*entity.ContactGroup
err := g.DB().Model("contacts c").
- LeftJoin("contact_groups g", "c.group_id = g.id").
+ LeftJoin("bm_contact_groups g", "c.group_id = g.id").
Fields("DISTINCT g.id, g.name, g.description, g.create_time, g.update_time").
Where("c.email", email).
Where("c.active", status).
@@ -277,7 +277,7 @@ func GetContactsTrend(ctx context.Context, startTime, endTime time.Time) ([]*str
UnsubscribeCount int `json:"unsubscribe_count"`
}
- err := g.DB().Model("contacts").
+ err := g.DB().Model("bm_contacts").
Fields(
"to_char(to_timestamp(create_time), 'YYYY-MM') as month",
"SUM(CASE WHEN active = 1 THEN 1 ELSE 0 END) as subscribe_count",
@@ -293,7 +293,7 @@ func GetContactsTrend(ctx context.Context, startTime, endTime time.Time) ([]*str
func UpdateContactsGroups(ctx context.Context, emails []string, status int, newGroupIds []int) (int, error) {
// 1. delete old group relations
- _, err := g.DB().Model("contacts").
+ _, err := g.DB().Model("bm_contacts").
Where("email IN(?)", emails).
Where("active", status).
Delete()
@@ -319,7 +319,7 @@ func UpdateContactsGroups(ctx context.Context, emails []string, status int, newG
return 0, nil
}
- result, err := g.DB().Model("contacts").
+ result, err := g.DB().Model("bm_contacts").
Data(data).
Insert()
if err != nil {
@@ -341,7 +341,7 @@ func GetGroupContactCount(ctx context.Context, groupIds []int) (int, error) {
Count int `json:"count"`
}
- err := g.DB().Model("contacts").
+ err := g.DB().Model("bm_contacts").
Fields("COUNT(DISTINCT email) as count").
WhereIn("group_id", groupIds).
Where("active", 1). // Only count active contacts
diff --git a/core/internal/service/database_initialization/batch_mail.go b/core/internal/service/database_initialization/batch_mail.go
index 219e09fa..8e6ecc2c 100644
--- a/core/internal/service/database_initialization/batch_mail.go
+++ b/core/internal/service/database_initialization/batch_mail.go
@@ -9,7 +9,7 @@ func init() {
registerHandler(func() {
batchMailSQLList := []string{
- `CREATE TABLE IF NOT EXISTS contact_groups (
+ `CREATE TABLE IF NOT EXISTS bm_contact_groups (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
@@ -18,14 +18,14 @@ func init() {
UNIQUE(name)
)`,
- `CREATE TABLE IF NOT EXISTS contacts (
+ `CREATE TABLE IF NOT EXISTS bm_contacts (
id SERIAL PRIMARY KEY,
email VARCHAR(320) NOT NULL,
group_id INTEGER,
active INTEGER DEFAULT 1,
task_id INTEGER DEFAULT 0,
create_time INTEGER NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
- FOREIGN KEY (group_id) REFERENCES contact_groups(id) ON DELETE SET NULL,
+ FOREIGN KEY (group_id) REFERENCES bm_contact_groups(id) ON DELETE SET NULL,
UNIQUE(group_id, email)
)`,
@@ -84,15 +84,15 @@ func init() {
template_id INTEGER,
task_id INTEGER,
unsubscribe_time INTEGER NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
- FOREIGN KEY (group_id) REFERENCES contact_groups(id) ON DELETE SET NULL,
+ 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
)`,
`CREATE INDEX IF NOT EXISTS idx_unsubscribe_email ON unsubscribe_records (email)`,
`CREATE INDEX IF NOT EXISTS idx_unsubscribe_time ON unsubscribe_records (unsubscribe_time)`,
- `CREATE INDEX IF NOT EXISTS idx_contacts_email ON contacts (email)`,
- `CREATE INDEX IF NOT EXISTS idx_contacts_active ON contacts (active)`,
+ `CREATE INDEX IF NOT EXISTS idx_bm_contacts_email ON bm_contacts (email)`,
+ `CREATE INDEX IF NOT EXISTS idx_bm_contacts_active ON bm_contacts (active)`,
}
for _, sql := range batchMailSQLList {