feat(go): add oem to identity binding schema
This commit is contained in:
@@ -88,6 +88,13 @@ func (r *MySQLResolver) EnsureSchema(ctx context.Context) error {
|
||||
if _, err := r.db.ExecContext(ctx, identityBindingTableSQL(r.table)); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, statement := range identityBindingAlterSQL(r.table) {
|
||||
if _, err := r.db.ExecContext(ctx, statement); err != nil {
|
||||
if !isDuplicateColumnError(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err := r.db.ExecContext(ctx, jt808RegistrationTableSQL)
|
||||
return err
|
||||
}
|
||||
@@ -101,6 +108,7 @@ func identityBindingTableSQL(table string) string {
|
||||
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),
|
||||
@@ -108,6 +116,20 @@ func identityBindingTableSQL(table string) string {
|
||||
)`
|
||||
}
|
||||
|
||||
func identityBindingAlterSQL(table string) []string {
|
||||
if table == "" || !safeIdentifier(table) {
|
||||
table = "vehicle_identity_binding"
|
||||
}
|
||||
return []string{
|
||||
`ALTER TABLE ` + table + ` ADD COLUMN oem VARCHAR(64) NULL AFTER device_id`,
|
||||
}
|
||||
}
|
||||
|
||||
func isDuplicateColumnError(err error) bool {
|
||||
text := strings.ToLower(strings.TrimSpace(fmt.Sprint(err)))
|
||||
return strings.Contains(text, "duplicate column") || strings.Contains(text, "error 1060")
|
||||
}
|
||||
|
||||
const jt808RegistrationTableSQL = `CREATE TABLE IF NOT EXISTS jt808_registration (
|
||||
phone VARCHAR(32) PRIMARY KEY,
|
||||
device_id VARCHAR(64) NULL,
|
||||
|
||||
Reference in New Issue
Block a user