From 145e547bfd6a372603f61f18858c5fa1ba100386 Mon Sep 17 00:00:00 2001 From: longfengpili <398745129@qq.com> Date: Mon, 4 Mar 2024 17:47:03 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]alter=5Ftablename?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pydbapi/api/trino.py | 2 +- pydbapi/db/base.py | 35 ++++++++++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pydbapi/api/trino.py b/pydbapi/api/trino.py index f4323f1..359320f 100644 --- a/pydbapi/api/trino.py +++ b/pydbapi/api/trino.py @@ -2,7 +2,7 @@ # @Author: longfengpili # @Date: 2023-06-02 15:27:41 # @Last Modified by: longfengpili -# @Last Modified time: 2024-03-01 13:34:40 +# @Last Modified time: 2024-03-04 11:31:02 # @github: https://github.com/longfengpili diff --git a/pydbapi/db/base.py b/pydbapi/db/base.py index d058aa7..fde7737 100644 --- a/pydbapi/db/base.py +++ b/pydbapi/db/base.py @@ -2,7 +2,7 @@ # @Author: longfengpili # @Date: 2023-06-02 15:27:41 # @Last Modified by: longfengpili -# @Last Modified time: 2024-03-01 13:21:20 +# @Last Modified time: 2024-03-04 17:41:24 # @github: https://github.com/longfengpili @@ -134,12 +134,10 @@ def execute(self, sql, count=None, ehandling='raise', verbose=0): columns, results = self.fetch_query_results(action, cur, count, verbose) conn.commit() - except Exception as e: - dblogger.error(e) - if ehandling == 'raise': - if self.dbtype not in ('trino',): - conn.rollback() - raise + except Exception: + if self.dbtype not in ('trino',): + conn.rollback() + raise finally: if self.dbtype not in ('trino',): cur.close() @@ -268,17 +266,20 @@ def alter_tablename(self, ftablename: str, ttablename: str, retries: int = 3, ve while attempt < retries: try: self.execute(altersql, verbose=verbose) - break - except: # noqa: E722 + except Exception as e: + dblogger.error(f"Attempt {attempt + 1} failed: {e}") + time.sleep(5) # 在重试之前等待 attempt += 1 - time.sleep(5) - - try: - self.get_columns(ttablename) - dblogger.info(f"alter table {ftablename} to {ttablename} succeeded ~") - break - except: # noqa: E722 - pass + + try: + self.get_columns(ttablename) + dblogger.info(f"alter table {ftablename} to {ttablename} succeeded ~") + break + except Exception: + pass + + if attempt == retries: + dblogger.error(f"All {retries} attempts to rename table {ftablename} to {ttablename} failed.") def alter_column(self, tablename: str, colname: str, newname: str = None, newtype: str = None): old_columns = self.get_columns(tablename)