Skip to content

Commit 154b612

Browse files
committed
replace LIKE in bytea field
1 parent fa13b5d commit 154b612

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/util/spreadsheet/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from .fields import *
1313
from .models import *
14+
from .misc import *
1415
# try:
1516
# from .fields import *
1617
# from .models import *

src/util/spreadsheet/fields.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ def rename_field(cr, model, old, new, data, revisions = ()):
2929
return spreadsheet.data, transform_revisions_data(revisions, *adapters)
3030

3131
def remove_field_in_all_spreadsheets(cr, model, field):
32-
if field == "__last_update":
33-
return # concurrent updates
3432
apply_in_all_spreadsheets(cr, model, (lambda data, revisions_data: remove_field(cr, model, field, data, revisions_data)))
3533

3634
def remove_field(cr, model, field, data, revisions=()):

src/util/spreadsheet/misc.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,36 @@
1818
_logger = logging.getLogger(__name__)
1919

2020

21-
def read_spreadsheet_attachments(cr, like_pattern="%"):
21+
def read_spreadsheet_attachments(cr, like_pattern=""):
2222
yield from read_spreadsheet_initial_data(cr, like_pattern)
2323
yield from read_spreadsheet_snapshots(cr, like_pattern)
2424

2525

26-
def read_spreadsheet_snapshots(cr, like_pattern="%"):
26+
def read_spreadsheet_snapshots(cr, like_pattern=""):
2727
cr.execute(
2828
"""
2929
SELECT id, res_model, res_id, db_datas
3030
FROM ir_attachment
3131
WHERE res_model IN ('spreadsheet.dashboard', 'documents.document')
3232
AND res_field = 'spreadsheet_snapshot'
33-
AND db_datas LIKE %s
33+
AND position(%s::bytea in db_datas) > 0
3434
""",
3535
[like_pattern],
3636
)
37-
# TODO LIKE probably doesn't work because the field is of type bytea
37+
# TODO rename 'like_pattern', it's not LIKE because LIKE doesn't work because the field is of type bytea
3838
for attachment_id, res_model, res_id, db_datas in cr.fetchall():
3939
if db_datas:
4040
yield attachment_id, res_model, res_id, json.loads(db_datas.tobytes())
4141

4242

43-
def read_spreadsheet_initial_data(cr, like_pattern="%"):
43+
def read_spreadsheet_initial_data(cr, like_pattern=""):
4444
cr.execute(
4545
"""
4646
SELECT doc.id AS document_id, a.id AS attachment_id, a.db_datas
4747
FROM documents_document doc
4848
LEFT JOIN ir_attachment a ON a.id = doc.attachment_id
4949
WHERE doc.handler='spreadsheet'
50-
AND a.db_datas LIKE %s
50+
AND position(%s::bytea in db_datas) > 0
5151
""",
5252
[like_pattern],
5353
)
@@ -63,7 +63,7 @@ def read_spreadsheet_initial_data(cr, like_pattern="%"):
6363
FROM ir_attachment
6464
WHERE res_model = 'spreadsheet.dashboard'
6565
AND res_field = %s
66-
AND db_datas LIKE %s
66+
AND position(%s::bytea in db_datas) > 0
6767
""",
6868
[data_field, like_pattern],
6969
)
@@ -74,7 +74,7 @@ def read_spreadsheet_initial_data(cr, like_pattern="%"):
7474
def apply_in_all_spreadsheets(cr, like_pattern, callback):
7575
_logger.info("upgrading initial data and revisions")
7676
# upgrade the initial data and all revisions based on it
77-
for attachment_id, res_model, res_id, db_datas in read_spreadsheet_initial_data(cr):
77+
for attachment_id, res_model, res_id, db_datas in read_spreadsheet_initial_data(cr, like_pattern):
7878
revisions_data = []
7979
revisions_ids = []
8080
for revision_id, commands in get_revisions(cr, res_model, res_id, like_pattern):
@@ -93,7 +93,7 @@ def apply_in_all_spreadsheets(cr, like_pattern, callback):
9393
)
9494
_logger.info("upgrading snapshots")
9595
# upgrade snapshots
96-
for attachment_id, _res_model, _res_id, db_datas in read_spreadsheet_snapshots(cr):
96+
for attachment_id, _res_model, _res_id, db_datas in read_spreadsheet_snapshots(cr, like_pattern):
9797
data, revisions = callback(db_datas, [])
9898
write_attachment(cr, attachment_id, data)
9999

0 commit comments

Comments
 (0)