Skip to content

Commit

Permalink
[优化]alter_column
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Mar 4, 2024
1 parent e77fe4d commit 3d5c9c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 1 addition & 3 deletions pydbapi/api/mysql.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-04 18:03:47
# @Last Modified time: 2024-03-04 18:15:30
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -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 ~")
4 changes: 1 addition & 3 deletions 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-04 18:03:07
# @Last Modified time: 2024-03-04 18:15:24
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -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 ~")
14 changes: 9 additions & 5 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-04 17:56:27
# @Last Modified time: 2024-03-04 18:22:37
# @github: https://github.com/longfengpili


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

0 comments on commit 3d5c9c5

Please sign in to comment.