| 332 | } |
| 333 | |
| 334 | func UpdateExchanges(initConfig *CmsConfig, transaction *sqlx.Tx) { |
| 335 | |
| 336 | log.Printf("We have %d data exchange updates", len(initConfig.ExchangeContracts)) |
| 337 | |
| 338 | adminId, _ := GetAdminUserIdAndUserGroupId(transaction) |
| 339 | |
| 340 | for _, exchange := range initConfig.ExchangeContracts { |
| 341 | |
| 342 | s, v, err := statementbuilder.Squirrel. |
| 343 | Select("reference_id").Prepared(true). |
| 344 | From("data_exchange"). |
| 345 | Where(goqu.Ex{"name": exchange.Name}).ToSQL() |
| 346 | |
| 347 | if err != nil { |
| 348 | log.Errorf("Failed to build query existing data exchange: %v", err) |
| 349 | continue |
| 350 | } |
| 351 | |
| 352 | var referenceId string |
| 353 | stmt1, err := transaction.Preparex(s) |
| 354 | if err != nil { |
| 355 | log.Errorf("[361] failed to prepare statment: %v", err) |
| 356 | continue |
| 357 | } |
| 358 | |
| 359 | errScan := stmt1.QueryRowx(v...).Scan(&referenceId) |
| 360 | |
| 361 | if errScan != nil { |
| 362 | log.Printf("no existing data exchange for [%v]", exchange.Name) |
| 363 | } |
| 364 | err = stmt1.Close() |
| 365 | if err != nil { |
| 366 | log.Errorf("failed to close prepared statement: %v", err) |
| 367 | } |
| 368 | |
| 369 | if errScan == nil { |
| 370 | |
| 371 | optionsJson, err := json.Marshal(exchange.Options) |
| 372 | |
| 373 | CheckErr(err, "Failed to marshal options to json: %v") |
| 374 | sourceAttrsJson, err := json.Marshal(exchange.SourceAttributes) |
| 375 | CheckErr(err, "Failed to marshal source attrs to json") |
| 376 | targetAttrsJson, err := json.Marshal(exchange.TargetAttributes) |
| 377 | CheckErr(err, "Failed to marshal target attrs to json") |
| 378 | attrsJson, err := json.Marshal(exchange.Attributes) |
| 379 | CheckErr(err, "Failed to marshal target attrs to json") |
| 380 | |
| 381 | s, v, err = statementbuilder.Squirrel. |
| 382 | Update("data_exchange").Prepared(true). |
| 383 | Set(goqu.Record{ |
| 384 | "source_attributes": sourceAttrsJson, |
| 385 | "source_type": exchange.SourceType, |
| 386 | "target_attributes": targetAttrsJson, |
| 387 | "attributes": attrsJson, |
| 388 | "target_type": exchange.TargetType, |
| 389 | "options": optionsJson, |
| 390 | "updated_at": time.Now(), |
| 391 | USER_ACCOUNT_ID_COLUMN: adminId, |