Skip to content

Commit 87dd2bb

Browse files
committed
[ADD] util/pg.py: add helper to create backup column
- we need to have backup of the column sometime in script to update later - or on requirement of customer we need to have backup of the deprecated stored field - this function will help to have backup of values having the column name appended '_upg_copy' closes #351 Signed-off-by: Alvaro Fuentes Suarez (afu) <afu@odoo.com>
1 parent 92efe24 commit 87dd2bb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/util/pg.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,37 @@ def create_column(cr, table, column, definition, **kwargs):
698698
return True
699699

700700

701+
def copy_column(cr, table, column, new_name=AUTO):
702+
"""
703+
Copy a column.
704+
705+
This function copies a column if it exists. It raises an error otherwise.
706+
707+
:param str table: table of the column
708+
:param str column: name of the column
709+
:param str new_name: name of the new column, if not passed the original column name
710+
will be used with suffix ``_upg_copy``.
711+
:return: new column name
712+
:rtype: str
713+
"""
714+
if not column_exists(cr, table, column):
715+
raise MigrationError("column {} doesn't exists".format(column))
716+
if new_name is AUTO:
717+
new_name = column + "_upg_copy"
718+
if column_exists(cr, table, new_name):
719+
raise MigrationError("column {} already exists".format(new_name))
720+
create_column(cr, table, new_name, column_type(cr, table, column))
721+
query = format_query(
722+
cr,
723+
"UPDATE {table} SET {new_name} = {column} WHERE {column} IS NOT NULL",
724+
table=table,
725+
new_name=new_name,
726+
column=column,
727+
)
728+
explode_execute(cr, query, table=table)
729+
return new_name
730+
731+
701732
def create_fk(cr, table, column, fk_table, on_delete_action="NO ACTION"):
702733
assert on_delete_action in ON_DELETE_ACTIONS
703734
current_target = target_of(cr, table, column)

0 commit comments

Comments
 (0)