11
22import re
33import json
4+ import logging
45
56from typing import Union , Callable , Dict , List
67
1415from .revisions import CommandAdapter , Drop
1516from odoo .upgrade .util .misc import version_gte
1617
18+ _logger = logging .getLogger (__name__ )
19+
1720
1821def read_spreadsheet_attachments (cr , like_pattern = "%" ):
1922 yield from read_spreadsheet_initial_data (cr , like_pattern )
@@ -25,15 +28,16 @@ def read_spreadsheet_snapshots(cr, like_pattern="%"):
2528 """
2629 SELECT id, res_model, res_id, db_datas
2730 FROM ir_attachment
28- WHERE res_model IN [ 'spreadsheet.dashboard', 'documents.document']
31+ WHERE res_model IN ( 'spreadsheet.dashboard', 'documents.document')
2932 AND res_field = 'spreadsheet_snapshot'
3033 AND db_datas LIKE %s
3134 """ ,
3235 [like_pattern ],
3336 )
37+ # TODO LIKE probably doesn't work because the field is of type bytea
3438 for attachment_id , res_model , res_id , db_datas in cr .fetchall ():
3539 if db_datas :
36- yield attachment_id , res_model , res_id , json .loads (db_datas )
40+ yield attachment_id , res_model , res_id , json .loads (db_datas . tobytes () )
3741
3842
3943def read_spreadsheet_initial_data (cr , like_pattern = "%" ):
@@ -50,31 +54,33 @@ def read_spreadsheet_initial_data(cr, like_pattern="%"):
5054 # TODO there are excel files in there!
5155 for document_id , attachment_id , db_datas in cr .fetchall ():
5256 if db_datas :
53- yield attachment_id , "documents.document" , document_id , json .loads (db_datas )
57+ # print(db_datas)
58+ yield attachment_id , "documents.document" , document_id , json .loads (db_datas .tobytes ())
5459 data_field = "spreadsheet_binary_data" if version_gte ("saas~16.3" ) else "data"
5560 cr .execute (
5661 """
5762 SELECT id, res_model, res_id, db_datas
5863 FROM ir_attachment
59- WHERE res_model IN = 'spreadsheet.dashboard'
64+ WHERE res_model = 'spreadsheet.dashboard'
6065 AND res_field = %s
6166 AND db_datas LIKE %s
6267 """ ,
6368 [data_field , like_pattern ],
6469 )
6570 for attachment_id , res_model , res_id , db_datas in cr .fetchall ():
6671 if db_datas :
67- yield attachment_id , res_model , res_id , json .loads (db_datas )
72+ yield attachment_id , res_model , res_id , json .loads (db_datas . tobytes () )
6873
6974def apply_in_all_spreadsheets (cr , like_pattern , callback ):
75+ _logger .info ("upgrading initial data and revisions" )
7076 # upgrade the initial data and all revisions based on it
7177 for attachment_id , res_model , res_id , db_datas in read_spreadsheet_initial_data (cr ):
7278 revisions_data = []
7379 revisions_ids = []
7480 for revision_id , commands in get_revisions (cr , res_model , res_id , like_pattern ):
7581 revisions_data .append (json .loads (commands ))
7682 revisions_ids .append (revision_id )
77- data , revisions = callback (cr , db_datas , revisions_data )
83+ data , revisions = callback (db_datas , revisions_data )
7884 write_attachment (cr , attachment_id , data )
7985 for revision_id , revision in zip (revisions_ids , revisions ):
8086 cr .execute (
@@ -85,22 +91,25 @@ def apply_in_all_spreadsheets(cr, like_pattern, callback):
8591 """ ,
8692 [json .dumps (revision ), revision_id ],
8793 )
94+ _logger .info ("upgrading snapshots" )
8895 # upgrade snapshots
8996 for attachment_id , _res_model , _res_id , db_datas in read_spreadsheet_snapshots (cr ):
9097 data , revisions = callback (cr , db_datas , [])
9198 write_attachment (cr , attachment_id , data )
9299
93100
94101def write_attachment (cr , attachment_id , data ):
102+ _logger .info ("replacing attachment %s" , attachment_id )
95103 cr .execute (
96104 """
97105 UPDATE ir_attachment
98106 SET db_datas=%s
99107 WHERE id=%s
100108 """ ,
101- [json .dumps (data ), attachment_id ]
109+ [json .dumps (data ). encode () , attachment_id ]
102110 )
103111
112+
104113def get_revisions (cr , res_model , res_id , like_pattern ):
105114 if version_gte ("16.0" ):
106115 cr .execute (
@@ -134,8 +143,9 @@ def upgrade_data(cr, upgrade_callback):
134143 SET db_datas=%s
135144 WHERE id=%s
136145 """ ,
137- [json .dumps (upgraded_data ), attachment_id ],
146+ [json .dumps (upgraded_data ). encode () , attachment_id ],
138147 )
148+ _logger .info ("spreadsheet json data upgraded" )
139149
140150
141151def transform_data_source_functions (content , data_source_ids , functions , adapter ):
0 commit comments