Created
November 16, 2018 01:30
-
-
Save niski84/01f02707d1ef25e8e80f9b52f2c87cc4 to your computer and use it in GitHub Desktop.
update tenant config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UpdateTenantConfig | |
func InsertOrUpdateTenantConfig(instance *confighelper.Instance, dbcfgM *dbhelper.DbConfig, dbcfgTenant *dbhelper.DbConfig, dbcfgOlap *dbhelper.DbConfig) (err error) { | |
// 1. update the connection strings where id_ = tenant | |
// 2. if records updated, return. if no record updated, continue | |
// 3. run stored procedure to insert a new tenantconfig record | |
// logging for instance | |
log := instance.Logger | |
tenantSQL := fmt.Sprintf(`update tenantconfig set jsondoc_ = jsondoc_ || | |
'{"DbConnectionString": "%v", | |
"TenantId": "%v", | |
"OlapDBConnectionString": "%v"}' | |
where id_ = '%[2]v' RETURNING id_, jsondoc_;`, | |
GetDbConnectionString(dbcfgTenant), instance.Config.Tenant.Id, GetDbConnectionString(dbcfgOlap)) | |
log.Println("Executing SQL: ", tenantSQL) | |
type Record struct { | |
TableName struct{} `pg:",discard_unknown_columns"` | |
Id_ string `sql:"id_"` | |
JSONdoc string `sql:"jsondoc_"` | |
} | |
var Records []Record | |
_, err = dbhelper.PgSql(instance.Logger, dbcfgM, &Records, tenantSQL) | |
if err != nil { | |
return | |
} | |
// if no record updated, assume this is a fresh install | |
if len(Records) > 0 { | |
log.Printf("Updated database value: %v", Records[0].Id_) | |
return | |
} else { | |
log.Warnf("No record found in 'tenantconfig' table for : %v", instance.Config.Tenant.Id) | |
} | |
tenantSQL = fmt.Sprintf(`select * from save_document_with_ignore('tenantconfig', '{"t_": "TenantConfiguration", | |
"id_": "%[2]v", "IsDeleted_": false, "IsActive": true, "TenantId": "%[2]v", "CreatedBy_": "dbscripts", | |
"DbProvider": "PostgreSql", "Description": "Default Tenant", | |
"DbConnectionString": "%[1]v", | |
"OlapDBConnectionString": "%[3]v"}', | |
'S', 'TenantId', false);`, | |
GetDbConnectionString(dbcfgTenant), instance.Config.Tenant.Id, GetDbConnectionString(dbcfgOlap)) | |
log.Println("Executing SQL: ", tenantSQL) | |
_, err = dbhelper.PgSql(instance.Logger, dbcfgM, &Records, tenantSQL) | |
if err != nil { | |
return | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment