-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.py
285 lines (258 loc) · 8.09 KB
/
migration.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
Command-line interface for the script
"""
import click
import datetime
import customers, orders, products
@click.group()
def cli():
"""
A command-line tool to migrate orders and customers from WooCommerce
to MongoDB database.
"""
pass
@click.command("orders")
@click.option(
"--id",
"-i",
type=click.INT,
help="ID of specific order to be imported.",
)
@click.option(
"--sort",
"-s",
help="Order sort attribute ascending (asc) or descending (desc).",
default="desc",
)
@click.option("--after", "-a", help="ISO datetime to import orders after (FROM)")
@click.option("--before", "-b", help="ISO datetime to import orders before (TO)")
@click.option(
"--days",
"-d",
type=click.INT,
help="Import orders created in the past X days (default=0 today)",
default=0,
)
@click.option(
"--hours",
"-h",
type=click.INT,
help="Import orders created in the past X hours (default=1 hour)",
default=1,
)
@click.option(
"--sync",
is_flag=True,
help="Sync records (insert ones that are not in the Database)",
default=False,
)
def import_orders(id, sort, after, before, days, hours, sync):
"""
Import all orders created between a datetime range or specific order
"""
if id:
print(f"Importing specific order with ID {id}")
orders.get_order(id)
return
if sort:
if sort.startswith("asc"):
sort = "asc"
else:
sort = "desc"
if after and before:
print(
f"Importing all orders created after '{after}' and before '{before}' sorted '{sort}'...\n"
)
orders.import_all_orders(sort, after, before)
else:
current_time = datetime.datetime.now()
today = datetime.date.today()
if days > 0:
# user has provided days argument
start_day = today - datetime.timedelta(days=days)
else:
# default is today
start_day = today
if start_day != today:
after = f'{str(start_day)}T{current_time.strftime("%H:%M:%S")}.000'
else:
if hours > 1:
# hours argument provided
start_time = current_time - datetime.timedelta(seconds=hours * 3600)
else:
# last 1 hour
start_time = current_time - datetime.timedelta(seconds=3600)
after = f'{start_time.strftime("%Y-%m-%dT%H:%M:%S")}.000'
before = f'{current_time.strftime("%Y-%m-%dT%H:%M:%S")}.000'
print(
f"Importing all orders created after '{after}' and before '{before}' sorted '{sort}'...\n"
)
if sync:
orders.import_all_orders(sort, after, before, sync=True)
else:
orders.import_all_orders(sort, after, before, sync=False)
@click.command("customers")
@click.option(
"--id",
"-i",
type=click.INT,
help="ID of specific customer to be imported.",
)
@click.option(
"--sort",
"-s",
help="Customer sort attribute ascending (asc) or descending (desc).",
default="desc",
)
@click.option("--after", "-a", help="ISO datetime to import customers after (FROM)")
@click.option("--before", "-b", help="ISO datetime to import customers before (TO)")
@click.option(
"--days",
"-d",
type=click.INT,
help="Import customers created in the past X days (default=0 today)",
default=0,
)
@click.option(
"--hours",
"-h",
type=click.INT,
help="Import customers created in the past X hours (default=1 hour)",
default=1,
)
@click.option(
"--sync",
is_flag=True,
help="Sync records (insert ones that are not in the Database)",
default=False,
)
def import_customers(id, sort, after, before, days, hours, sync):
"""
Import all customers created between a datetime range or specific customer
"""
if id:
print(f"Importing specific customer with ID {id}...\n")
customers.get_customer(id)
return
if sort:
if sort.startswith("asc"):
sort = "asc"
else:
sort = "desc"
if after and before:
print(
f"Importing all customers created after '{after}' and before '{before}' sorted '{sort}'...\n"
)
customers.import_all_customers(sort, after, before)
else:
current_time = datetime.datetime.now()
today = datetime.date.today()
if days > 0:
# user has provided days argument
start_day = today - datetime.timedelta(days=days)
else:
# default is today
start_day = today
if start_day != today:
after = f'{str(start_day)}T{current_time.strftime("%H:%M:%S")}.000'
else:
if hours > 1:
# hours argument provided
start_time = current_time - datetime.timedelta(seconds=hours * 3600)
else:
# last 1 hour
start_time = current_time - datetime.timedelta(seconds=3600)
after = f'{start_time.strftime("%Y-%m-%dT%H:%M:%S")}.000'
before = f'{current_time.strftime("%Y-%m-%dT%H:%M:%S")}.000'
print(
f"Importing all customers created after '{after}' and before '{before}' sorted '{sort}'...\n"
)
if sync == True:
customers.import_all_customers(sort, after, before, sync=True)
else:
customers.import_all_customers(sort, after, before, sync=False)
@click.command("products")
@click.option(
"--id",
"-i",
type=click.INT,
help="ID of specific product to be imported.",
)
@click.option(
"--sort",
"-s",
help="Product sort attribute ascending (asc) or descending (desc).",
default="desc",
)
@click.option("--after", "-a", help="ISO datetime to import products after (FROM)")
@click.option("--before", "-b", help="ISO datetime to import products before (TO)")
@click.option(
"--days",
"-d",
type=click.INT,
help="Import products created in the past X days (default=0 today)",
default=0,
)
@click.option(
"--hours",
"-h",
type=click.INT,
help="Import products created in the past X hours (default=1 hour)",
default=1,
)
@click.option(
"--sync",
is_flag=True,
help="Sync records (insert ones that are not in the Database)",
default=False,
)
def import_products(id, sort, after, before, days, hours, sync):
"""
Import all products created between a datetime range or specific product
"""
if id:
print(f"Importing specific product with ID {id}")
products.get_product(id)
return
if sort:
if sort.startswith("asc"):
sort = "asc"
else:
sort = "desc"
if after and before:
print(
f"Importing all products created after '{after}' and before '{before}' sorted '{sort}'...\n"
)
products.import_all_products(sort, after, before)
else:
current_time = datetime.datetime.now()
today = datetime.date.today()
if days > 0:
# user has provided days argument
start_day = today - datetime.timedelta(days=days)
else:
# default is today
start_day = today
if start_day != today:
after = f'{str(start_day)}T{current_time.strftime("%H:%M:%S")}.000'
else:
if hours > 1:
# hours argument provided
start_time = current_time - datetime.timedelta(seconds=hours * 3600)
else:
# last 1 hour
start_time = current_time - datetime.timedelta(seconds=3600)
after = f'{start_time.strftime("%Y-%m-%dT%H:%M:%S")}.000'
before = f'{current_time.strftime("%Y-%m-%dT%H:%M:%S")}.000'
print(
f"Importing all products created after '{after}' and before '{before}' sorted '{sort}'...\n"
)
if sync:
products.import_all_products(sort, after, before, sync=True)
else:
products.import_all_products(sort, after, before, sync=False)
cli.add_command(import_orders)
cli.add_command(import_products)
cli.add_command(import_customers)
if __name__ == "__main__":
cli()