1
1
2
2
import re
3
3
import json
4
+ import logging
4
5
5
6
from typing import Union , Callable , Dict , List
6
7
14
15
from .revisions import CommandAdapter , Drop
15
16
from odoo .upgrade .util .misc import version_gte
16
17
18
+ _logger = logging .getLogger (__name__ )
19
+
17
20
18
21
def read_spreadsheet_attachments (cr , like_pattern = "%" ):
19
22
yield from read_spreadsheet_initial_data (cr , like_pattern )
@@ -25,15 +28,16 @@ def read_spreadsheet_snapshots(cr, like_pattern="%"):
25
28
"""
26
29
SELECT id, res_model, res_id, db_datas
27
30
FROM ir_attachment
28
- WHERE res_model IN [ 'spreadsheet.dashboard', 'documents.document']
31
+ WHERE res_model IN ( 'spreadsheet.dashboard', 'documents.document')
29
32
AND res_field = 'spreadsheet_snapshot'
30
33
AND db_datas LIKE %s
31
34
""" ,
32
35
[like_pattern ],
33
36
)
37
+ # TODO LIKE probably doesn't work because the field is of type bytea
34
38
for attachment_id , res_model , res_id , db_datas in cr .fetchall ():
35
39
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 () )
37
41
38
42
39
43
def read_spreadsheet_initial_data (cr , like_pattern = "%" ):
@@ -50,31 +54,33 @@ def read_spreadsheet_initial_data(cr, like_pattern="%"):
50
54
# TODO there are excel files in there!
51
55
for document_id , attachment_id , db_datas in cr .fetchall ():
52
56
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 ())
54
59
data_field = "spreadsheet_binary_data" if version_gte ("saas~16.3" ) else "data"
55
60
cr .execute (
56
61
"""
57
62
SELECT id, res_model, res_id, db_datas
58
63
FROM ir_attachment
59
- WHERE res_model IN = 'spreadsheet.dashboard'
64
+ WHERE res_model = 'spreadsheet.dashboard'
60
65
AND res_field = %s
61
66
AND db_datas LIKE %s
62
67
""" ,
63
68
[data_field , like_pattern ],
64
69
)
65
70
for attachment_id , res_model , res_id , db_datas in cr .fetchall ():
66
71
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 () )
68
73
69
74
def apply_in_all_spreadsheets (cr , like_pattern , callback ):
75
+ _logger .info ("upgrading initial data and revisions" )
70
76
# upgrade the initial data and all revisions based on it
71
77
for attachment_id , res_model , res_id , db_datas in read_spreadsheet_initial_data (cr ):
72
78
revisions_data = []
73
79
revisions_ids = []
74
80
for revision_id , commands in get_revisions (cr , res_model , res_id , like_pattern ):
75
81
revisions_data .append (json .loads (commands ))
76
82
revisions_ids .append (revision_id )
77
- data , revisions = callback (cr , db_datas , revisions_data )
83
+ data , revisions = callback (db_datas , revisions_data )
78
84
write_attachment (cr , attachment_id , data )
79
85
for revision_id , revision in zip (revisions_ids , revisions ):
80
86
cr .execute (
@@ -85,22 +91,25 @@ def apply_in_all_spreadsheets(cr, like_pattern, callback):
85
91
""" ,
86
92
[json .dumps (revision ), revision_id ],
87
93
)
94
+ _logger .info ("upgrading snapshots" )
88
95
# upgrade snapshots
89
96
for attachment_id , _res_model , _res_id , db_datas in read_spreadsheet_snapshots (cr ):
90
97
data , revisions = callback (cr , db_datas , [])
91
98
write_attachment (cr , attachment_id , data )
92
99
93
100
94
101
def write_attachment (cr , attachment_id , data ):
102
+ _logger .info ("replacing attachment %s" , attachment_id )
95
103
cr .execute (
96
104
"""
97
105
UPDATE ir_attachment
98
106
SET db_datas=%s
99
107
WHERE id=%s
100
108
""" ,
101
- [json .dumps (data ), attachment_id ]
109
+ [json .dumps (data ). encode () , attachment_id ]
102
110
)
103
111
112
+
104
113
def get_revisions (cr , res_model , res_id , like_pattern ):
105
114
if version_gte ("16.0" ):
106
115
cr .execute (
@@ -134,8 +143,9 @@ def upgrade_data(cr, upgrade_callback):
134
143
SET db_datas=%s
135
144
WHERE id=%s
136
145
""" ,
137
- [json .dumps (upgraded_data ), attachment_id ],
146
+ [json .dumps (upgraded_data ). encode () , attachment_id ],
138
147
)
148
+ _logger .info ("spreadsheet json data upgraded" )
139
149
140
150
141
151
def transform_data_source_functions (content , data_source_ids , functions , adapter ):
0 commit comments