Skip to content

Commit f1a99cf

Browse files
committed
[ADD] util/convert_m2o_to_m2m
TODO
1 parent 3e4f017 commit f1a99cf

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/base/tests/test_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,10 @@ def test_invert_boolean_field(self):
11191119
initial_repartition[False] += initial_repartition.pop(None, 0)
11201120
self.assertEqual(back_repartition, initial_repartition)
11211121

1122+
def test_convert_m2o_to_m2m(self):
1123+
# TODO
1124+
return
1125+
11221126

11231127
class TestHelpers(UnitTestCase):
11241128
def test_model_table_conversion(self):

src/util/fields.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,3 +1585,59 @@ def update_server_actions_fields(cr, src_model, dst_model=None, fields_mapping=N
15851585
) % {"src_model": src_model, "dst_model": dst_model, "actions": ", ".join(action_names)}
15861586

15871587
add_to_migration_reports(message=msg, category="Server Actions")
1588+
1589+
1590+
def convert_m2o_to_m2m(cr, origin_model, m2o_field, m2m_field):
1591+
"""
1592+
TODO
1593+
"""
1594+
_validate_model(origin_model)
1595+
1596+
if not table_exists(cr, m2m_field):
1597+
raise SleepyDeveloperError("m2m table should already exist warning")
1598+
1599+
origin_table = table_of_model(cr, origin_model)
1600+
1601+
cr.execute(
1602+
"""
1603+
SELECT kcu.column_name,
1604+
ccu.table_name AS foreign_table_name,
1605+
ccu.column_name AS foreign_column_name
1606+
FROM information_schema.table_constraints tc
1607+
JOIN information_schema.key_column_usage kcu
1608+
ON kcu.constraint_name = tc.constraint_name
1609+
AND kcu.table_schema = tc.table_schema
1610+
JOIN information_schema.constraint_column_usage ccu
1611+
ON ccu.constraint_name = tc.constraint_name
1612+
WHERE tc.constraint_type = 'FOREIGN KEY'
1613+
AND tc.table_name = %s
1614+
""",
1615+
[origin_table],
1616+
)
1617+
for column_name, foreign_table_name, foreign_column_name in cr.fetchall():
1618+
if foreign_table_name == origin_table:
1619+
origin_table_column = column_name
1620+
origin_table_fk_column = foreign_column_name
1621+
else:
1622+
other_table_column = column_name
1623+
1624+
cr.execute(
1625+
format_query(
1626+
cr,
1627+
"""
1628+
INSERT INTO {m2m_field} ({origin_table_column}, {other_table_column})
1629+
SELECT {origin_table_fk_column}, {m2o_field}
1630+
FROM {origin_table}
1631+
WHERE {m2o_field} IS NOT NULL
1632+
""",
1633+
m2m_field=m2m_field,
1634+
m2o_field=m2o_field,
1635+
origin_table_fk_column=origin_table_fk_column,
1636+
origin_table_column=origin_table_column,
1637+
other_table_column=other_table_column,
1638+
origin_table=origin_table,
1639+
)
1640+
)
1641+
1642+
remove_column(cr, origin_table, m2o_field)
1643+
rename_field(cr, origin_model, m2o_field, m2m_field)

0 commit comments

Comments
 (0)