Skip to content

Commit

Permalink
[优化]alter_tablename
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Mar 4, 2024
1 parent f5810da commit 145e547
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pydbapi/api/trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
35 changes: 18 additions & 17 deletions pydbapi/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 145e547

Please sign in to comment.