插入 或者 更新 工作区的数据
(workspaceInfo workspace.WorkspaceInfo)
| 69 | |
| 70 | // 插入 或者 更新 工作区的数据 |
| 71 | func InsertOrUpdateWorkspace(workspaceInfo workspace.WorkspaceInfo) (affectId int64, err error) { |
| 72 | //1. init |
| 73 | db := getDb() |
| 74 | defer db.Close() |
| 75 | |
| 76 | //2. 是否数据已经存在 |
| 77 | isExit := false |
| 78 | if workspaceInfo.ID != "" { //2.1. 用户录入workspaceid的情况 |
| 79 | i, err := strconv.Atoi(workspaceInfo.ID) |
| 80 | common.CheckError(err) |
| 81 | affectId = int64(i) |
| 82 | isExit = true |
| 83 | } |
| 84 | |
| 85 | //3. insert or update |
| 86 | jsonBytes, err := json.Marshal(workspaceInfo.Extend) // 扩展字段序列化 |
| 87 | common.CheckError(err) |
| 88 | if workspaceInfo.Mode != workspace.WorkingMode_K8s && workspaceInfo.ConfigYaml.IsNil() { |
| 89 | return -1, errors.New("配置文件数据为空!") //TODO |
| 90 | } |
| 91 | if workspaceInfo.TempDockerCompose.IsNil() && workspaceInfo.Mode != workspace.WorkingMode_K8s { |
| 92 | return -1, errors.New("生成docker-compose数据为空!") //TODO |
| 93 | } |
| 94 | |
| 95 | //4. 配置文件 及 关联配置 |
| 96 | configStr := "" |
| 97 | linkComposeStr := "" |
| 98 | tempComposeStr := "" |
| 99 | //4.1. k8s 时关联文件格式单独指定 |
| 100 | if workspaceInfo.Mode == workspace.WorkingMode_K8s { |
| 101 | configStr, _ = workspaceInfo.K8sInfo.OriginK8sYaml.ConvertToConfigYaml() |
| 102 | linkComposeStr, _ = workspaceInfo.K8sInfo.OriginK8sYaml.ConvertToK8sYaml() |
| 103 | tempComposeStr, _ = workspaceInfo.K8sInfo.TempK8sConfig.ConvertToK8sYaml() |
| 104 | } else { |
| 105 | configStr, _ = workspaceInfo.ConfigYaml.ToYaml() |
| 106 | linkComposeStr, _ = workspaceInfo.ConfigYaml.Workspace.LinkCompose.ToYaml() |
| 107 | tempComposeStr, _ = workspaceInfo.TempDockerCompose.ToYaml() |
| 108 | } |
| 109 | //4.2. 校验 |
| 110 | /* if strings.TrimSpace(configStr) == "" { |
| 111 | return -1, errors.New("配置文件数据为空!") |
| 112 | } |
| 113 | if workspaceInfo.Mode == workspace.WorkingMode_K8s && strings.TrimSpace(linkComposeStr) == "" { |
| 114 | return -1, errors.New("链接K8S yaml文件为空!") |
| 115 | } |
| 116 | if strings.TrimSpace(tempComposeStr) == "" { |
| 117 | return -1, errors.New("生成临时文件为空!") |
| 118 | } */ |
| 119 | |
| 120 | //5. insert or update //TODO: 使用事务,确保成功 |
| 121 | //5.1. 更新关联信息 |
| 122 | remoteId := sql.NullInt32{} // 可能是个空值 |
| 123 | k8sId := sql.NullInt32{} // 可能是个空值 |
| 124 | if workspaceInfo.Mode != workspace.WorkingMode_K8s { // 插入到 remote 表中 |
| 125 | if (workspaceInfo.Remote != workspace.RemoteInfo{}) { |
| 126 | tmpId, err := InsertOrUpdateRemote(workspaceInfo.Remote) |
| 127 | common.CheckError(err) |
| 128 | if tmpId > 0 { |
nothing calls this directly
no test coverage detected