feat(go): make identity binding read only

This commit is contained in:
lingniu
2026-07-03 15:45:47 +08:00
parent 1d79dc25e0
commit f292d4c0d6
5 changed files with 51 additions and 67 deletions

View File

@@ -90,7 +90,7 @@ func (r *MySQLResolver) EnsureSchema(ctx context.Context) error {
}
for _, statement := range identityBindingAlterSQL(r.table) {
if _, err := r.db.ExecContext(ctx, statement); err != nil {
if !isDuplicateColumnError(err) {
if !isIgnoredIdentityBindingAlterError(err) {
return err
}
}
@@ -107,12 +107,10 @@ func identityBindingTableSQL(table string) string {
vin VARCHAR(32) PRIMARY KEY,
plate VARCHAR(32) NULL,
phone VARCHAR(32) NULL,
device_id VARCHAR(64) NULL,
oem VARCHAR(64) NULL,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uk_identity_plate (plate),
UNIQUE KEY uk_identity_phone (phone),
UNIQUE KEY uk_identity_device (device_id)
UNIQUE KEY uk_identity_phone (phone)
)`
}
@@ -121,13 +119,19 @@ func identityBindingAlterSQL(table string) []string {
table = "vehicle_identity_binding"
}
return []string{
`ALTER TABLE ` + table + ` ADD COLUMN oem VARCHAR(64) NULL AFTER device_id`,
`ALTER TABLE ` + table + ` ADD COLUMN oem VARCHAR(64) NULL AFTER phone`,
`ALTER TABLE ` + table + ` DROP INDEX uk_identity_device`,
`ALTER TABLE ` + table + ` DROP COLUMN device_id`,
}
}
func isDuplicateColumnError(err error) bool {
func isIgnoredIdentityBindingAlterError(err error) bool {
text := strings.ToLower(strings.TrimSpace(fmt.Sprint(err)))
return strings.Contains(text, "duplicate column") || strings.Contains(text, "error 1060")
return strings.Contains(text, "duplicate column") ||
strings.Contains(text, "error 1060") ||
strings.Contains(text, "can't drop") ||
strings.Contains(text, "check that column/key exists") ||
strings.Contains(text, "error 1091")
}
const jt808RegistrationTableSQL = `CREATE TABLE IF NOT EXISTS jt808_registration (
@@ -238,30 +242,24 @@ func (r *MySQLResolver) resolveFromJT808Registration(ctx context.Context, env en
addIdentityMetadata(env.Parsed, "jt808_registration.vin", env.Phone)
return env, true, nil
}
for _, candidate := range []CandidateKey{
{Column: "device_id", Value: env.DeviceID},
{Column: "plate", Value: env.Plate},
} {
value := strings.TrimSpace(candidate.Value)
if value == "" {
continue
}
vin, err := r.lookup(ctx, candidate.Column, value)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
continue
}
return env, false, err
}
if strings.TrimSpace(vin) == "" {
continue
}
env.VIN = strings.TrimSpace(vin)
env.EventID = env.StableEventID()
addIdentityMetadata(env.Parsed, "jt808_registration."+candidate.Column, value)
return env, true, nil
value := strings.TrimSpace(env.Plate)
if value == "" {
return env, false, nil
}
return env, false, nil
vin, err = r.lookup(ctx, "plate", value)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return env, false, nil
}
return env, false, err
}
if strings.TrimSpace(vin) == "" {
return env, false, nil
}
env.VIN = strings.TrimSpace(vin)
env.EventID = env.StableEventID()
addIdentityMetadata(env.Parsed, "jt808_registration.plate", value)
return env, true, nil
}
func (r *MySQLResolver) lookupJT808Registration(ctx context.Context, phone string) (vin string, deviceID string, plate string, err error) {
@@ -407,16 +405,7 @@ ON DUPLICATE KEY UPDATE
if err != nil {
return err
}
if vin == "unknown" {
return nil
}
_, err = r.db.ExecContext(ctx, `UPDATE IGNORE `+r.table+`
SET
phone = CASE WHEN (phone IS NULL OR phone = '') AND ? <> '' THEN ? ELSE phone END,
device_id = CASE WHEN (device_id IS NULL OR device_id = '') AND ? <> '' THEN ? ELSE device_id END,
plate = CASE WHEN (plate IS NULL OR plate = '') AND ? <> '' THEN ? ELSE plate END
WHERE vin = ?`, env.Phone, env.Phone, deviceID, deviceID, plate, plate, vin)
return err
return nil
}
func (r *MySQLResolver) shouldTouchJT808Location(env envelope.FrameEnvelope) bool {
@@ -458,7 +447,6 @@ func CandidateKeys(env envelope.FrameEnvelope) []CandidateKey {
out = append(out, CandidateKey{Column: column, Value: value})
}
add("phone", normalizePhone(env.Phone))
add("device_id", env.DeviceID)
add("plate", env.Plate)
add("vin", env.VehicleKeyHint)
return out