forked from gridgain/python-thin-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_binary.py
More file actions
193 lines (166 loc) · 5.4 KB
/
Copy pathmigrate_binary.py
File metadata and controls
193 lines (166 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#
# Copyright 2022 GridGain Systems, Inc. and Contributors.
#
# Licensed under the GridGain Community Edition License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import date
from decimal import Decimal
from pprint import pprint
from helpers.converters import obj_to_dict
from pygridgain import Client, GenericObjectMeta
from pygridgain.datatypes import BoolObject, DateObject, DecimalObject, LongObject, String
# prepare old data
old_schema = {'date': DateObject,
'reported': BoolObject,
'purpose': String,
'sum': DecimalObject,
'recipient': String,
'cashier_id': LongObject
}
old_data = {
1: {
'date': date(2017, 9, 21),
'reported': True,
'purpose': 'Praesent eget fermentum massa',
'sum': Decimal('666.67'),
'recipient': 'John Doe',
'cashier_id': 8,
},
2: {
'date': date(2017, 10, 11),
'reported': True,
'purpose': 'Proin in bibendum nulla',
'sum': Decimal('333.33'),
'recipient': 'Jane Roe',
'cashier_id': 9,
},
3: {
'date': date(2017, 10, 11),
'reported': True,
'purpose': 'Suspendisse nec dolor auctor, scelerisque ex eu, iaculis odio',
'sum': Decimal('400.0'),
'recipient': 'Jane Roe',
'cashier_id': 8,
},
4: {
'date': date(2017, 10, 24),
'reported': False,
'purpose': 'Quisque ut leo ligula',
'sum': Decimal('1234.5'),
'recipient': 'Joe Bloggs',
'cashier_id': 10,
},
5: {
'date': date(2017, 12, 1),
'reported': True,
'purpose': 'Quisque ut leo ligula',
'sum': Decimal('800.0'),
'recipient': 'Richard Public',
'cashier_id': 12,
},
6: {
'date': date(2017, 12, 1),
'reported': True,
'purpose': 'Aenean eget bibendum lorem, a luctus libero',
'sum': Decimal('135.79'),
'recipient': 'Joe Bloggs',
'cashier_id': 10,
}
}
# - add `report_date`
# - set `report_date` to the current date if `reported` is True, None if False
# - delete `reported`
#
# new_schema = {
# 'date': DateObject,
# 'report_date': DateObject,
# 'purpose': String,
# 'sum': DecimalObject,
# 'recipient': String,
# 'cashier_id': LongObject,
# }
class ExpenseVoucher(
metaclass=GenericObjectMeta,
schema=old_schema,
):
pass
client = Client()
with client.connect('127.0.0.1', 10800):
accounting = client.get_or_create_cache('accounting')
for item, value in old_data.items():
print(item)
accounting.put(item, ExpenseVoucher(**value))
data_classes = client.query_binary_type('ExpenseVoucher')
print(data_classes)
# {
# {547629991: <class 'pygridgain.binary.ExpenseVoucher'>, -231598180: <class '__main__.ExpenseVoucher'>}
# }
s_id, data_class = data_classes.popitem()
schema = data_class.schema
schema['expense_date'] = schema['date']
del schema['date']
schema['report_date'] = DateObject
del schema['reported']
schema['sum'] = DecimalObject
# define new data class
class ExpenseVoucherV2(
metaclass=GenericObjectMeta,
type_name='ExpenseVoucher',
schema=schema,
):
pass
def migrate(cache, data, new_class):
""" Migrate given data pages. """
for key, old_value in data:
# read data
print('Old value:')
pprint(obj_to_dict(old_value))
# Old value:
# {'cashier_id': 10,
# 'date': datetime.datetime(2017, 12, 1, 0, 0),
# 'purpose': 'Aenean eget bibendum lorem, a luctus libero',
# 'recipient': 'Joe Bloggs',
# 'reported': True,
# 'sum': Decimal('135.79'),
# 'type_name': 'ExpenseVoucher'}
# create new binary object
new_value = new_class()
# process data
new_value.sum = old_value.sum
new_value.purpose = old_value.purpose
new_value.recipient = old_value.recipient
new_value.cashier_id = old_value.cashier_id
new_value.expense_date = old_value.date
new_value.report_date = date.today() if old_value.reported else None
# replace data
cache.put(key, new_value)
# verify data
verify = cache.get(key)
print('New value:')
pprint(obj_to_dict(verify))
# New value:
# {'cashier_id': 10,
# 'expense_date': datetime.datetime(2017, 12, 1, 0, 0),
# 'purpose': 'Aenean eget bibendum lorem, a luctus libero',
# 'recipient': 'Joe Bloggs',
# 'report_date': datetime.datetime(2022, 5, 6, 0, 0),
# 'sum': Decimal('135.79'),
# 'type_name': 'ExpenseVoucher'}
print('-' * 20)
# migrate data
with client.connect('127.0.0.1', 10800):
accounting = client.get_or_create_cache('accounting')
with accounting.scan() as cursor:
migrate(accounting, cursor, ExpenseVoucherV2)
# cleanup
accounting.destroy()