fix: make tenant workbench initialization idempotent
This commit is contained in:
@@ -25,6 +25,7 @@ func (s *Store) InitializeTenantDefaults(ctx context.Context) (*TenantInitializa
|
|||||||
defer func() { _ = tx.Rollback() }()
|
defer func() { _ = tx.Rollback() }()
|
||||||
|
|
||||||
now := time.Now().UTC().Round(time.Millisecond)
|
now := time.Now().UTC().Round(time.Millisecond)
|
||||||
|
tenantID := s.tenant()
|
||||||
result := &TenantInitializationResult{}
|
result := &TenantInitializationResult{}
|
||||||
|
|
||||||
apps := []struct {
|
apps := []struct {
|
||||||
@@ -42,23 +43,27 @@ func (s *Store) InitializeTenantDefaults(ctx context.Context) (*TenantInitializa
|
|||||||
for _, app := range apps {
|
for _, app := range apps {
|
||||||
res, execErr := tx.ExecContext(ctx, `INSERT INTO im_workbench_apps
|
res, execErr := tx.ExecContext(ctx, `INSERT INTO im_workbench_apps
|
||||||
(tenant_id,code,name,icon_file_id,route,sort_order,unread_key,enabled,permission,created_at,updated_at)
|
(tenant_id,code,name,icon_file_id,route,sort_order,unread_key,enabled,permission,created_at,updated_at)
|
||||||
VALUES(?,?,?,?,?,?,?,?,?,?,?)
|
SELECT ?,?,?,?,?,?,?,?,?,?,?
|
||||||
ON DUPLICATE KEY UPDATE tenant_id=VALUES(tenant_id)`,
|
WHERE NOT EXISTS (
|
||||||
s.tenant(), app.code, app.name, "", app.route, app.sort, "", 1, "enabled", now, now)
|
SELECT 1 FROM im_workbench_apps WHERE tenant_id=? AND code=?
|
||||||
|
)`,
|
||||||
|
tenantID, app.code, app.name, "", app.route, app.sort, "", 1, "enabled", now, now,
|
||||||
|
tenantID, app.code)
|
||||||
if execErr != nil {
|
if execErr != nil {
|
||||||
return nil, execErr
|
return nil, execErr
|
||||||
}
|
}
|
||||||
rows, _ := res.RowsAffected()
|
rows, _ := res.RowsAffected()
|
||||||
if rows == 1 {
|
result.WorkbenchApps += rows
|
||||||
result.WorkbenchApps++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := tx.ExecContext(ctx, `INSERT INTO im_attendance_rules
|
res, err := tx.ExecContext(ctx, `INSERT INTO im_attendance_rules
|
||||||
(tenant_id,rule_id,name,workday,location_required,wifi_required,allow_outside_checkin,appeal_allowed,effective_from,effective_to,state,created_at,updated_at)
|
(tenant_id,rule_id,name,workday,location_required,wifi_required,allow_outside_checkin,appeal_allowed,effective_from,effective_to,state,created_at,updated_at)
|
||||||
VALUES(?,?,?,?,?,?,?,?,CURDATE(),NULL,'active',?,?)
|
SELECT ?,?,?,?,?,?,?,?,CURDATE(),NULL,'active',?,?
|
||||||
ON DUPLICATE KEY UPDATE tenant_id=VALUES(tenant_id)`,
|
WHERE NOT EXISTS (
|
||||||
s.tenant(), "att_rule_default", "默认考勤规则", "1,2,3,4,5", 0, 0, 1, 1, now, now)
|
SELECT 1 FROM im_attendance_rules WHERE tenant_id=? AND rule_id=?
|
||||||
|
)`,
|
||||||
|
tenantID, "att_rule_default", "默认考勤规则", "1,2,3,4,5", 0, 0, 1, 1, now, now,
|
||||||
|
tenantID, "att_rule_default")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -66,9 +71,12 @@ func (s *Store) InitializeTenantDefaults(ctx context.Context) (*TenantInitializa
|
|||||||
|
|
||||||
res, err = tx.ExecContext(ctx, `INSERT INTO im_attendance_shifts
|
res, err = tx.ExecContext(ctx, `INSERT INTO im_attendance_shifts
|
||||||
(tenant_id,shift_id,rule_id,name,work_start,work_end,checkin_start,checkout_end,state,sort_order,created_at,updated_at)
|
(tenant_id,shift_id,rule_id,name,work_start,work_end,checkin_start,checkout_end,state,sort_order,created_at,updated_at)
|
||||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?)
|
SELECT ?,?,?,?,?,?,?,?,?,?,?,?
|
||||||
ON DUPLICATE KEY UPDATE tenant_id=VALUES(tenant_id)`,
|
WHERE NOT EXISTS (
|
||||||
s.tenant(), "shift_default", "att_rule_default", "默认班次", "09:00:00", "18:00:00", "08:00:00", "19:00:00", "active", 10, now, now)
|
SELECT 1 FROM im_attendance_shifts WHERE tenant_id=? AND shift_id=?
|
||||||
|
)`,
|
||||||
|
tenantID, "shift_default", "att_rule_default", "默认班次", "09:00:00", "18:00:00", "08:00:00", "19:00:00", "active", 10, now, now,
|
||||||
|
tenantID, "shift_default")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -76,9 +84,12 @@ func (s *Store) InitializeTenantDefaults(ctx context.Context) (*TenantInitializa
|
|||||||
|
|
||||||
res, err = tx.ExecContext(ctx, `INSERT INTO im_mailbox_config
|
res, err = tx.ExecContext(ctx, `INSERT INTO im_mailbox_config
|
||||||
(tenant_id,enabled,allow_anonymous,required_fields,max_length,attachment_policy,notice_title,notice_content,notice_updated_at,created_at,updated_at)
|
(tenant_id,enabled,allow_anonymous,required_fields,max_length,attachment_policy,notice_title,notice_content,notice_updated_at,created_at,updated_at)
|
||||||
VALUES(?,1,1,?,5000,?,'总经理信箱','请如实填写诉求。',?,?,?)
|
SELECT ?,1,1,?,5000,?,'总经理信箱','请如实填写诉求。',?,?,?
|
||||||
ON DUPLICATE KEY UPDATE tenant_id=VALUES(tenant_id)`,
|
WHERE NOT EXISTS (
|
||||||
s.tenant(), `["subject","content"]`, `{"max_files":5,"purpose":"executive_mailbox"}`, now, now, now)
|
SELECT 1 FROM im_mailbox_config WHERE tenant_id=?
|
||||||
|
)`,
|
||||||
|
tenantID, `["subject","content"]`, `{"max_files":5,"purpose":"executive_mailbox"}`, now, now, now,
|
||||||
|
tenantID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -86,9 +97,12 @@ func (s *Store) InitializeTenantDefaults(ctx context.Context) (*TenantInitializa
|
|||||||
|
|
||||||
res, err = tx.ExecContext(ctx, `INSERT INTO im_feedback_config
|
res, err = tx.ExecContext(ctx, `INSERT INTO im_feedback_config
|
||||||
(tenant_id,categories,max_length,attachment_policy,allow_contact,diagnostics_notice,created_at,updated_at)
|
(tenant_id,categories,max_length,attachment_policy,allow_contact,diagnostics_notice,created_at,updated_at)
|
||||||
VALUES(?,?,2000,?,1,'',?,?)
|
SELECT ?,?,2000,?,1,'',?,?
|
||||||
ON DUPLICATE KEY UPDATE tenant_id=VALUES(tenant_id)`,
|
WHERE NOT EXISTS (
|
||||||
s.tenant(), `["bug","suggestion","complaint","other"]`, `{"max_files":5,"purpose":"feedback"}`, now, now)
|
SELECT 1 FROM im_feedback_config WHERE tenant_id=?
|
||||||
|
)`,
|
||||||
|
tenantID, `["bug","suggestion","complaint","other"]`, `{"max_files":5,"purpose":"feedback"}`, now, now,
|
||||||
|
tenantID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user