From 3d5c9c5729de5711de87251ec9db90a3b845804a Mon Sep 17 00:00:00 2001 From: longfengpili <398745129@qq.com> Date: Mon, 4 Mar 2024 18:26:58 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]alter=5Fcolumn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pydbapi/api/mysql.py | 4 +--- pydbapi/api/trino.py | 4 +--- pydbapi/db/base.py | 14 +++++++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pydbapi/api/mysql.py b/pydbapi/api/mysql.py index 7591afb..4d16695 100644 --- a/pydbapi/api/mysql.py +++ b/pydbapi/api/mysql.py @@ -2,7 +2,7 @@ # @Author: longfengpili # @Date: 2023-06-02 15:27:41 # @Last Modified by: longfengpili -# @Last Modified time: 2024-03-04 18:03:47 +# @Last Modified time: 2024-03-04 18:15:30 # @github: https://github.com/longfengpili @@ -182,5 +182,3 @@ def alter_tablecol(self, tablename: str, colname: str, newname: str = None, newt # alter table self.alter_tablecol_base(tablename, mtablename, alter_columns, conditions=conditions, verbose=verbose) - else: - mysqllogger.info(f"{alter_columns} same, not needed to alter ~") diff --git a/pydbapi/api/trino.py b/pydbapi/api/trino.py index 5536410..da04a70 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-04 18:03:07 +# @Last Modified time: 2024-03-04 18:15:24 # @github: https://github.com/longfengpili @@ -162,5 +162,3 @@ def alter_tablecol(self, tablename: str, colname: str, newname: str = None, newt # alter table self.alter_tablecol_base(tablename, mtablename, alter_columns, conditions=conditions, verbose=verbose) - else: - mytrinologger.info(f"{colname} not need change ~") diff --git a/pydbapi/db/base.py b/pydbapi/db/base.py index a36b4b6..4745d02 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-04 17:56:27 +# @Last Modified time: 2024-03-04 18:22:37 # @github: https://github.com/longfengpili @@ -281,16 +281,20 @@ def alter_tablename(self, ftablename: str, ttablename: str, retries: int = 3, ve 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): + def alter_column(self, tablename: str, colname: str, newname: str = None, newtype: str = None, sqlexpr: str = None): old_columns = self.get_columns(tablename) alter_col = old_columns.get_column_by_name(colname) + if not alter_col: + dblogger.error(f"{colname} not in {tablename} !!!") + return + newname = newname or alter_col.newname newtype = newtype or alter_col.coltype - sqlexpr = f"cast({colname} as {newtype})" + sqlexpr = sqlexpr or f"cast({colname} as {newtype})" if newtype != alter_col.coltype else None newcol = ColumnModel(newname, newtype, sqlexpr=sqlexpr) - - if alter_col.newname == newcol.newname and alter_col.coltype == newcol.coltype: + if newcol == alter_col: + dblogger.info(f"{newcol} same, not need to change ~") return alter_columns = old_columns.alter(colname, newcol)