-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathsearch-replace-export.feature
More file actions
410 lines (356 loc) · 12 KB
/
search-replace-export.feature
File metadata and controls
410 lines (356 loc) · 12 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
Feature: Search / replace with file export
@require-mysql
Scenario: Search / replace export to STDOUT
Given a WP install
And I run `echo ' '`
And save STDOUT as {SPACE}
When I run `wp search-replace example.com example.net --export`
Then STDOUT should contain:
"""
DROP TABLE IF EXISTS `wp_commentmeta`;
CREATE TABLE `wp_commentmeta`
"""
And STDOUT should contain:
"""
'siteurl', 'https://example.net',
"""
When I run `wp option get home`
Then STDOUT should be:
"""
https://example.com
"""
When I run `wp search-replace example.com example.net --skip-tables=wp_options --export`
Then STDOUT should not contain:
"""
INSERT INTO `wp_options`
"""
When I run `wp search-replace example.com example.net --skip-tables=wp_opt\?ons,wp_post\* --export`
Then STDOUT should not contain:
"""
wp_posts
"""
And STDOUT should not contain:
"""
wp_postmeta
"""
And STDOUT should not contain:
"""
wp_options
"""
And STDOUT should contain:
"""
wp_users
"""
When I run `wp search-replace example.com example.net --skip-columns=option_value --export`
Then STDOUT should contain:
"""
INSERT INTO `wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES{SPACE}
"""
And STDOUT should contain:
"""
'siteurl', 'https://example.com'
"""
When I run `wp search-replace example.com example.net --skip-columns=option_value --export --export_insert_size=1`
Then STDOUT should contain:
"""
'siteurl', 'https://example.com'
"""
And STDOUT should contain:
"""
INSERT INTO `wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES{SPACE}
"""
When I run `wp search-replace foo bar --export | tail -n 1`
Then STDOUT should not contain:
"""
Success: Made
"""
When I run `wp search-replace example.com example.net --export > wordpress.sql`
And I run `wp db import wordpress.sql`
Then STDOUT should not be empty
When I run `wp option get home`
Then STDOUT should be:
"""
https://example.net
"""
@require-mysql
Scenario: Search / replace export to file
Given a WP install
And I run `wp post generate --count=100`
And I run `wp option add example_url https://example.com`
When I run `wp search-replace example.com example.net --export=wordpress.sql`
Then STDOUT should contain:
"""
Success: Made
"""
# Skip exact number as it changes in trunk due to https://core.trac.wordpress.org/changeset/42981
And STDOUT should contain:
"""
replacements and exported to wordpress.sql
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 6 | PHP |
When I run `wp option get home`
Then STDOUT should be:
"""
https://example.com
"""
When I run `wp site empty --yes`
And I run `wp post list --format=count`
Then STDOUT should be:
"""
0
"""
When I run `wp db import wordpress.sql`
Then STDOUT should not be empty
When I run `wp option get home`
Then STDOUT should be:
"""
https://example.net
"""
When I run `wp option get example_url`
Then STDOUT should be:
"""
https://example.net
"""
When I run `wp post list --format=count`
Then STDOUT should be:
"""
101
"""
@require-mysql
Scenario: Search / replace export to file with verbosity
Given a WP install
When I run `wp search-replace example.com example.net --export=wordpress.sql --verbose`
Then STDOUT should contain:
"""
Checking: wp_posts
"""
And STDOUT should contain:
"""
Checking: wp_options
"""
Scenario: Search / replace export with dry-run
Given a WP install
When I try `wp search-replace example.com example.net --export --dry-run`
Then STDERR should be:
"""
Error: You cannot supply --dry-run and --export at the same time.
"""
@require-mysql
Scenario: Search / replace shouldn't affect primary key
Given a WP install
And I run `wp post create --post_title=foo --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp option update {POST_ID} foo`
And I run `wp option get {POST_ID}`
Then STDOUT should be:
"""
foo
"""
When I run `wp search-replace {POST_ID} 99999999 --export=wordpress.sql`
And I run `wp db import wordpress.sql`
Then STDOUT should not be empty
When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
foo
"""
When I try `wp option get {POST_ID}`
Then STDOUT should be empty
When I run `wp option get 99999999`
Then STDOUT should be:
"""
foo
"""
Scenario: Search / replace export invalid file
Given a WP install
When I try `wp search-replace example.com example.net --export=foo/bar.sql`
Then STDERR should contain:
"""
Error: Unable to open export file "foo/bar.sql" for writing:
"""
@require-mysql
Scenario: Search / replace specific table
Given a WP install
When I run `wp post create --post_title=foo --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp option update bar foo`
Then STDOUT should not be empty
When I run `wp search-replace foo burrito wp_posts --export=wordpress.sql --verbose`
Then STDOUT should contain:
"""
Checking: wp_posts
"""
And STDOUT should contain:
"""
Success: Made 1 replacement and exported to wordpress.sql.
"""
When I run `wp db import wordpress.sql`
Then STDOUT should not be empty
When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
burrito
"""
When I run `wp option get bar`
Then STDOUT should be:
"""
foo
"""
@require-mysql
Scenario: Search / replace export should cater for field/table names that use reserved words or unusual characters
Given a WP install
# Unlike search-replace.features version, don't use `back``tick` column name as WP_CLI\Iterators\Table::build_fields() can't handle it.
And a esc_sql_ident.sql file:
"""
CREATE TABLE `TABLE` (`KEY` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `VALUES` TEXT, `single'double"quote` TEXT, PRIMARY KEY (`KEY`) );
INSERT INTO `TABLE` (`VALUES`, `single'double"quote`) VALUES ('v"vvvv_v1', 'v"vvvv_v1' );
INSERT INTO `TABLE` (`VALUES`, `single'double"quote`) VALUES ('v"vvvv_v2', 'v"vvvv_v2' );
"""
When I run `wp db query "SOURCE esc_sql_ident.sql;"`
Then STDERR should be empty
When I run `wp search-replace 'v"vvvv_v' 'w"wwww_w' TABLE --export --all-tables`
Then STDOUT should contain:
"""
INSERT INTO `TABLE` (`KEY`, `VALUES`, `single'double"quote`) VALUES
"""
And STDOUT should contain:
"""
('1', 'w\"wwww_w1', 'w\"wwww_w1')
"""
And STDOUT should contain:
"""
('2', 'w\"wwww_w2', 'w\"wwww_w2')
"""
And STDERR should be empty
When I run `wp search-replace 'v"vvvv_v2' 'w"wwww_w2' TABLE --export --regex --all-tables`
Then STDOUT should contain:
"""
INSERT INTO `TABLE` (`KEY`, `VALUES`, `single'double"quote`) VALUES
"""
And STDOUT should contain:
"""
('1', 'v\"vvvv_v1', 'v\"vvvv_v1')
"""
And STDOUT should contain:
"""
('2', 'w\"wwww_w2', 'w\"wwww_w2')
"""
And STDERR should be empty
@require-mysql
Scenario: Suppress report or only report changes on export to file
Given a WP install
When I run `wp option set foo baz`
And I run `wp option get foo`
Then STDOUT should be:
"""
baz
"""
When I run `wp post create --post_title=baz --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post meta add {POST_ID} foo baz`
Then STDOUT should not be empty
When I run `wp search-replace baz bar --export=wordpress.sql`
Then STDOUT should contain:
"""
Success: Made 3 replacements and exported to wordpress.sql.
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_commentmeta | meta_id | 0 | PHP |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_value | 1 | PHP |
| wp_posts | post_title | 1 | PHP |
| wp_users | display_name | 0 | PHP |
And STDERR should be empty
When I run `wp search-replace baz bar --report --export=wordpress.sql`
Then STDOUT should contain:
"""
Success: Made 3 replacements and exported to wordpress.sql.
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_commentmeta | meta_id | 0 | PHP |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_value | 1 | PHP |
| wp_posts | post_title | 1 | PHP |
| wp_users | display_name | 0 | PHP |
And STDERR should be empty
When I run `wp search-replace baz bar --no-report --export=wordpress.sql`
Then STDOUT should contain:
"""
Success: Made 3 replacements and exported to wordpress.sql.
"""
And STDOUT should not contain:
"""
Table Column Replacements Type
"""
And STDOUT should not contain:
"""
wp_commentmeta meta_id 0 PHP
"""
And STDOUT should not contain:
"""
wp_options option_value 1 PHP
"""
And STDERR should be empty
When I run `wp search-replace baz bar --no-report-changed-only --export=wordpress.sql`
Then STDOUT should contain:
"""
Success: Made 3 replacements and exported to wordpress.sql.
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_commentmeta | meta_id | 0 | PHP |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_value | 1 | PHP |
| wp_posts | post_title | 1 | PHP |
| wp_users | display_name | 0 | PHP |
And STDERR should be empty
When I run `wp search-replace baz bar --report-changed-only --export=wordpress.sql`
Then STDOUT should contain:
"""
Success: Made 3 replacements and exported to wordpress.sql.
"""
And STDOUT should end with a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_value | 1 | PHP |
| wp_posts | post_title | 1 | PHP |
And STDOUT should not contain:
"""
wp_commentmeta meta_id 0 PHP
"""
And STDOUT should not contain:
"""
wp_users display_name 0 PHP
"""
And STDERR should be empty
@require-mysql
Scenario: Search / replace should remove placeholder escape on export
Given a WP install
And I run `wp post create --post_title=test-remove-placeholder-escape% --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp search-replace baz bar --export | grep test-remove-placeholder-escape`
Then STDOUT should contain:
"""
'test-remove-placeholder-escape%'
"""
And STDOUT should not contain:
"""
'test-remove-placeholder-escape{'
"""
@require-mysql
Scenario: NULLs exported as NULL and not null string
Given a WP install
And I run `wp db query "INSERT INTO wp_postmeta VALUES (9999, 9999, NULL, 'foo')"`
When I run `wp search-replace bar replaced wp_postmeta --export`
Then STDOUT should contain:
"""
('9999', '9999', NULL, 'foo')
"""
And STDOUT should not contain:
"""
('9999', '9999', '', 'foo')
"""