#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2023/5/9 14:29 # @Author : old tom # @File : metadata_version_dao.py # @Project : futool-tiny-datahub # @Desc : from common.fudb.connectors.connector_factory import ConnFactory from common.fudb.dbapis.fu_dao import BaseDao class IncreaseVersionError(Exception): """ 递增版本号异常 """ def __init__(self, msg): Exception.__init__(self, msg) class MetadataVersionDao(BaseDao): def __init__(self, connector: ConnFactory): super().__init__(connector) def init_version(self, source_id): """ 初始化版本号 :param source_id: :return: """ return self.execute_update(f"insert into metadata_object_version_record (source_id) values ('{source_id}')") def query_latest_version(self, source_id): """ 查询最新版本号 :param source_id: :return: """ result = self.query_one( f"select version_code from metadata_object_version_record where source_id='{source_id}' " f"order by version_code desc limit 1") return result[0] if result else None def add_version(self, source_id): """ 版本号加1并返回最新 :param source_id: :return: """ last_version = self.query_latest_version(source_id) latest_version = last_version + 1 flag = self.execute_update( f"insert into metadata_object_version_record (source_id,version_code) values ('{source_id}',{latest_version})") if flag == 1: return latest_version else: raise IncreaseVersionError(f'[{source_id}] 更新版本号失败')