github.com/polarismesh/polaris@v1.17.8/store/mysql/config_file_release_history.go (about)

     1  /*
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package sqldb
    19  
    20  import (
    21  	"database/sql"
    22  	"encoding/json"
    23  	"strconv"
    24  	"time"
    25  
    26  	"github.com/polarismesh/polaris/common/model"
    27  	"github.com/polarismesh/polaris/common/utils"
    28  	"github.com/polarismesh/polaris/store"
    29  )
    30  
    31  type configFileReleaseHistoryStore struct {
    32  	master *BaseDB
    33  	slave  *BaseDB
    34  }
    35  
    36  // CreateConfigFileReleaseHistory 创建配置文件发布历史记录
    37  func (rh *configFileReleaseHistoryStore) CreateConfigFileReleaseHistory(
    38  	history *model.ConfigFileReleaseHistory) error {
    39  
    40  	s := "INSERT INTO config_file_release_history(" +
    41  		" name, namespace, `group`, file_name, content, comment, md5, type, status, format, tags, " +
    42  		"create_time, create_by, modify_time, modify_by, version, reason, description) " +
    43  		" VALUES " +
    44  		"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate(), ?, sysdate(), ?, ?, ?, ?)"
    45  	_, err := rh.master.Exec(s, history.Name, history.Namespace,
    46  		history.Group, history.FileName, history.Content,
    47  		history.Comment, history.Md5,
    48  		history.Type, history.Status, history.Format, utils.MustJson(history.Metadata),
    49  		history.CreateBy, history.ModifyBy, history.Version, history.Reason, history.ReleaseDescription)
    50  	if err != nil {
    51  		return store.Error(err)
    52  	}
    53  	return nil
    54  }
    55  
    56  // QueryConfigFileReleaseHistories 获取配置文件的发布历史记录
    57  func (rh *configFileReleaseHistoryStore) QueryConfigFileReleaseHistories(filter map[string]string,
    58  	offset, limit uint32) (uint32, []*model.ConfigFileReleaseHistory, error) {
    59  	countSql := "SELECT COUNT(*) FROM config_file_release_history WHERE "
    60  	querySql := rh.genSelectSql() + " WHERE "
    61  
    62  	namespace := filter["namespace"]
    63  	group := filter["group"]
    64  	fileName := filter["name"]
    65  	endId, _ := strconv.ParseUint(filter["endId"], 10, 64)
    66  
    67  	var queryParams []interface{}
    68  	if namespace != "" {
    69  		countSql += " namespace = ? AND "
    70  		querySql += " namespace = ? AND "
    71  		queryParams = append(queryParams, namespace)
    72  	}
    73  	if endId > 0 {
    74  		countSql += " id < ? AND "
    75  		querySql += " id < ? AND "
    76  		queryParams = append(queryParams, endId)
    77  	}
    78  
    79  	countSql += " `group` LIKE ? AND file_name LIKE ? "
    80  	querySql += " `group` LIKE ? AND file_name LIKE ? ORDER BY id DESC LIMIT ?, ? "
    81  	queryParams = append(queryParams, "%"+group+"%")
    82  	queryParams = append(queryParams, "%"+fileName+"%")
    83  
    84  	var count uint32
    85  	err := rh.master.QueryRow(countSql, queryParams...).Scan(&count)
    86  	if err != nil {
    87  		return 0, nil, err
    88  	}
    89  
    90  	queryParams = append(queryParams, offset)
    91  	queryParams = append(queryParams, limit)
    92  	rows, err := rh.master.Query(querySql, queryParams...)
    93  	if err != nil {
    94  		return 0, nil, err
    95  	}
    96  
    97  	fileReleaseHistories, err := rh.transferRows(rows)
    98  	if err != nil {
    99  		return 0, nil, err
   100  	}
   101  
   102  	return count, fileReleaseHistories, nil
   103  }
   104  
   105  // CleanConfigFileReleaseHistory 清理配置发布历史
   106  func (rh *configFileReleaseHistoryStore) CleanConfigFileReleaseHistory(endTime time.Time, limit uint64) error {
   107  	delSql := "DELETE FROM config_file_release_history WHERE create_time < ? LIMIT ?"
   108  	_, err := rh.master.Exec(delSql, endTime, limit)
   109  	return err
   110  }
   111  
   112  func (rh *configFileReleaseHistoryStore) genSelectSql() string {
   113  	return "SELECT id, name, namespace, `group`, file_name, content, IFNULL(comment, ''), " +
   114  		" md5, format, tags, type, status, UNIX_TIMESTAMP(create_time), IFNULL(create_by, ''), " +
   115  		" UNIX_TIMESTAMP(modify_time), IFNULL(modify_by, ''), IFNULL(reason, ''), " +
   116  		" IFNULL(description, ''), IFNULL(version, 0) FROM config_file_release_history "
   117  }
   118  
   119  func (rh *configFileReleaseHistoryStore) transferRows(rows *sql.Rows) ([]*model.ConfigFileReleaseHistory, error) {
   120  	if rows == nil {
   121  		return nil, nil
   122  	}
   123  	defer rows.Close()
   124  
   125  	records := make([]*model.ConfigFileReleaseHistory, 0, 16)
   126  
   127  	for rows.Next() {
   128  		item := &model.ConfigFileReleaseHistory{}
   129  		var (
   130  			ctime, mtime int64
   131  			tags         string
   132  		)
   133  		err := rows.Scan(&item.Id, &item.Name, &item.Namespace, &item.Group, &item.FileName, &item.Content,
   134  			&item.Comment, &item.Md5, &item.Format, &tags, &item.Type, &item.Status, &ctime, &item.CreateBy,
   135  			&mtime, &item.ModifyBy, &item.Reason, &item.ReleaseDescription, &item.Version)
   136  		if err != nil {
   137  			return nil, err
   138  		}
   139  		item.CreateTime = time.Unix(ctime, 0)
   140  		item.ModifyTime = time.Unix(mtime, 0)
   141  		item.Metadata = map[string]string{}
   142  		_ = json.Unmarshal([]byte(tags), &item.Metadata)
   143  
   144  		records = append(records, item)
   145  	}
   146  
   147  	if err := rows.Err(); err != nil {
   148  		return nil, err
   149  	}
   150  	return records, nil
   151  }