Skip to content

Commit

Permalink
[优化]alter tabecol
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Mar 4, 2024
1 parent 145e547 commit e77fe4d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
20 changes: 10 additions & 10 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-01 13:34:58
# @Last Modified time: 2024-03-04 18:03:47
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -173,14 +173,14 @@ def alter_tablecol(self, tablename: str, colname: str, newname: str = None, newt
partition: str = None, distribution: str = None, conditions: list[str] = None,
verbose: int = 0):
alter_columns = self.alter_column(tablename, colname, newname, newtype)
if isinstance(alter_columns, ColumnModel):
mysqllogger.info(f"{alter_columns} same, not needed to alter ~")
return

# create middle table
mtablename = f"{tablename}_tmp"
self.create(mtablename, alter_columns, indexes=indexes, index_part=index_part, ismultiple_index=ismultiple_index,
partition=partition, distribution=distribution, verbose=verbose)
if alter_columns:
# create tmp table
mtablename = f"{tablename}_tmp"
self.create(mtablename, alter_columns, indexes=indexes, index_part=index_part, ismultiple_index=ismultiple_index,
partition=partition, distribution=distribution, verbose=verbose)

# alter table
self.alter_table_base(tablename, mtablename, alter_columns, conditions=conditions, verbose=verbose)
# 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 ~")
18 changes: 9 additions & 9 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 11:31:02
# @Last Modified time: 2024-03-04 18:03:07
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -154,13 +154,13 @@ def insert(self, tablename, columns, inserttype='value', values=None, chunksize=
def alter_tablecol(self, tablename: str, colname: str, newname: str = None, newtype: str = None,
partition: str = 'part_date', conditions: list[str] = None, verbose: int = 0):
alter_columns = self.alter_column(tablename, colname, newname, newtype)
if isinstance(alter_columns, ColumnModel):
mytrinologger.info(f"{alter_columns} same, not needed to alter ~")
return

# create middle table
mtablename = f"{tablename}_tmp"
self.create(mtablename, alter_columns, partition=partition, verbose=verbose)
if alter_columns:
# create tmp table
mtablename = f"{tablename}_tmp"
self.create(mtablename, alter_columns, partition=partition, verbose=verbose)

# alter table
self.alter_table_base(tablename, mtablename, alter_columns, conditions=conditions, verbose=verbose)
# alter table
self.alter_tablecol_base(tablename, mtablename, alter_columns, conditions=conditions, verbose=verbose)
else:
mytrinologger.info(f"{colname} not need change ~")
8 changes: 4 additions & 4 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:41:24
# @Last Modified time: 2024-03-04 17:56:27
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -291,14 +291,14 @@ def alter_column(self, tablename: str, colname: str, newname: str = None, newtyp
newcol = ColumnModel(newname, newtype, sqlexpr=sqlexpr)

if alter_col.newname == newcol.newname and alter_col.coltype == newcol.coltype:
return newcol
return

alter_columns = old_columns.alter(colname, newcol)

return alter_columns

def alter_table_base(self, ftablename: str, mtablename: str, alter_columns: ColumnsModel,
conditions: list[str] = None, verbose: int = 0):
def alter_tablecol_base(self, ftablename: str, mtablename: str, alter_columns: ColumnsModel,
conditions: list[str] = None, verbose: int = 0):
# tablename
today = date.today()
today_str = today.strftime('%Y%m%d')
Expand Down

0 comments on commit e77fe4d

Please sign in to comment.