refactor(go): simplify identity table keys

This commit is contained in:
lingniu
2026-07-03 07:22:18 +08:00
parent 04bafb4cd6
commit bb0d001d72
4 changed files with 26 additions and 8 deletions

View File

@@ -71,17 +71,14 @@ func identityBindingTableSQL(table string) string {
table = "vehicle_identity_binding"
}
return `CREATE TABLE IF NOT EXISTS ` + table + ` (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
vin VARCHAR(32) NOT NULL,
vin VARCHAR(32) PRIMARY KEY,
plate VARCHAR(32) NULL,
phone VARCHAR(32) NULL,
device_id VARCHAR(64) NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
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),
KEY idx_identity_vin (vin)
UNIQUE KEY uk_identity_device (device_id)
)`
}
@@ -103,7 +100,6 @@ const jt808RegistrationTableSQL = `CREATE TABLE IF NOT EXISTS jt808_registration
latest_registered_at DATETIME NULL,
latest_authenticated_at DATETIME NULL,
latest_seen_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
KEY idx_jt808_registration_device (device_id),
KEY idx_jt808_registration_plate (plate),

View File

@@ -3,6 +3,7 @@ package identity
import (
"context"
"database/sql"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
@@ -202,6 +203,24 @@ func TestMySQLResolverEnsuresMinimalIdentitySchema(t *testing.T) {
}
}
func TestIdentitySchemaUsesBusinessKeysOnly(t *testing.T) {
binding := identityBindingTableSQL("vehicle_identity_binding")
registration := jt808RegistrationTableSQL
for _, sqlText := range []string{binding, registration} {
for _, column := range []string{"id BIGINT", "AUTO_INCREMENT", "created_at"} {
if strings.Contains(sqlText, column) {
t.Fatalf("identity schema should not contain %s:\n%s", column, sqlText)
}
}
}
if !strings.Contains(binding, "vin VARCHAR(32) PRIMARY KEY") {
t.Fatalf("binding table should key by vin:\n%s", binding)
}
if !strings.Contains(registration, "phone VARCHAR(32) PRIMARY KEY") {
t.Fatalf("registration table should key by phone:\n%s", registration)
}
}
func newMockDB(t *testing.T) (*sql.DB, sqlmock.Sqlmock) {
t.Helper()
db, mock, err := sqlmock.New()