Repository navigation
Expand file tree
/
Copy pathsqlite-import.feature
More file actions
312 lines (287 loc) · 12.4 KB
/
Copy pathsqlite-import.feature
File metadata and controls
312 lines (287 loc) · 12.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
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
Feature: WP-CLI SQLite Import Command
In order to populate a WordPress SQLite database
As a website administrator
I need to be able to import data into SQLite databases using WP-CLI
Background:
Given a WP installation
@require-sqlite
Scenario: Successfully import a SQLite database
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO test_table (name) VALUES ('Test Name');
"""
When I run `wp sqlite import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
And the "test_table" should contain a row with name "Test Name"
@require-sqlite
Scenario: Attempt to import without specifying a file
When I try `wp sqlite import`
Then STDOUT should contain:
"""
usage: wp sqlite import <file>
"""
And the return code should be 1
@require-sqlite
Scenario: Import from STDIN
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO test_table (name) VALUES ('Test Name');
"""
When I run `cat test_import.sql | wp sqlite import -`
Then STDOUT should contain:
"""
Success: Imported from 'STDIN'.
"""
And the SQLite database should contain the imported data
@require-sqlite
Scenario: Import a file with MySQL backslash escape sequences
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
INSERT INTO test_table (name) VALUES ('Test that escaping a backslash \\ works');
INSERT INTO test_table (name) VALUES ('Test that escaping multiple backslashes \\\\\\ works');
INSERT INTO test_table (name) VALUES ('Test that escaping a character \a works');
INSERT INTO test_table (name) VALUES ('Test that escaping a backslash followed by a character \\a works');
INSERT INTO test_table (name) VALUES ('Test that escaping a backslash and a character \\\a works');
"""
When I run `wp sqlite --enable-ast-driver import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
And the "test_table" should contain a row with name "Test that escaping a backslash \ works"
And the "test_table" should contain a row with name "Test that escaping multiple backslashes \\\ works"
And the "test_table" should contain a row with name "Test that escaping a character a works"
And the "test_table" should contain a row with name "Test that escaping a backslash followed by a character \a works"
And the "test_table" should contain a row with name "Test that escaping a backslash and a character \a works"
@require-sqlite
Scenario: Import a file with newlines in strings
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
INSERT INTO test_table (name) VALUES ('Test that a string containing
a newline character and some whitespace works');
"""
When I run `wp sqlite --enable-ast-driver import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
And the "test_table" should contain a row with name:
"""
Test that a string containing
a newline character and some whitespace works
"""
@require-sqlite
Scenario: Import a file with comments
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
-- This is an inline comment.
# This is an inline comment.
INSERT INTO test_table (name) VALUES ('one'); -- This is an inline comment.
/* This is a block comment */
INSERT INTO test_table (name) VALUES ('two'); /* This
is a block comment
on multiple lines */ INSERT INTO test_table (name) VALUES ('three');
INSERT INTO test_table (name) VALUES ('fo -- this looks like a comment ur');
INSERT INTO test_table (name) VALUES ('fi/* this looks like a comment */ve');
"""
When I run `wp sqlite --enable-ast-driver import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
And the "test_table" should contain a row with name "one"
And the "test_table" should contain a row with name "two"
And the "test_table" should contain a row with name "three"
And the "test_table" should contain a row with name "fo -- this looks like a comment ur"
And the "test_table" should contain a row with name "fi/* this looks like a comment */ve"
@require-sqlite
Scenario: Import a file quoted strings
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
INSERT INTO test_table (name) VALUES ('a single-quoted string with \' '' some " tricky ` chars');
INSERT INTO test_table (name) VALUES ("a double-quoted string with ' some \" "" tricky ` chars");
"""
When I run `wp sqlite --enable-ast-driver import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
And the "test_table" should contain a row with name:
"""
a single-quoted string with ' ' some " tricky ` chars
"""
And the "test_table" should contain a row with name:
"""
a double-quoted string with ' some " " tricky ` chars
"""
@require-sqlite
Scenario: Import a file backtick-quoted identifiers
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE `a'strange``identifier\\name` (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
"""
When I run `wp sqlite --enable-ast-driver import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "a'strange`identifier\\name"
@require-sqlite
Scenario: Import a file with whitespace and empty lines
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
INSERT INTO test_table (name) VALUES ('Test Name');
"""
When I run `wp sqlite --enable-ast-driver import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
And the "test_table" should contain a row with name "Test Name"
@require-sqlite
Scenario: Import data that expect non-strict SQL mode to be set
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (
id INTEGER,
name TEXT NOT NULL,
created_at DATETIME DEFAULT '0000-00-00 00:00:00'
);
INSERT INTO test_table (id) VALUES (1);
"""
When I run `wp sqlite --enable-ast-driver import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
And the "test_table" should contain a row with name ""
@require-sqlite
Scenario: Import tables with foreign key constraints when child table is imported before parent
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE wp_aa_child (
id INTEGER PRIMARY KEY,
parent_id INTEGER NOT NULL,
CONSTRAINT fk_parent FOREIGN KEY (parent_id) REFERENCES wp_zz_parent(id)
);
INSERT INTO wp_aa_child (id, parent_id) VALUES (1, 1);
CREATE TABLE wp_zz_parent (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
INSERT INTO wp_zz_parent (id, name) VALUES (1, 'Parent Row');
"""
When I run `wp sqlite --enable-ast-driver import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "wp_zz_parent"
And the SQLite database should contain a table named "wp_aa_child"
@require-sqlite
Scenario: Refuse to overwrite an existing database
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO test_table (name) VALUES ('Test Name');
"""
When I run `wp sqlite import test_import.sql`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
When I try `wp sqlite import test_import.sql --refuse-overwrite`
Then the return code should be 1
And STDERR should contain:
"""
already contains tables
"""
And the SQLite database should contain a table named "test_table"
@require-sqlite
Scenario: Allow refuse-overwrite into a fresh database file
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO test_table (name) VALUES ('Test Name');
"""
When I run `wp config set DB_PATH '{RUN_DIR}/fresh.sqlite'`
And I run `wp sqlite import test_import.sql --refuse-overwrite`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
@require-sqlite
Scenario: Failed import into a new database removes the new file
Given a SQL dump file named "test_import_bad.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO missing_table (name) VALUES ('Test Name');
"""
When I run `wp config set DB_PATH '{RUN_DIR}/atomic.sqlite'`
And I try `wp sqlite import test_import_bad.sql`
Then the return code should be 1
And STDERR should contain:
"""
Could not execute statement
"""
And the atomic.sqlite file should not exist
And the atomic.sqlite-wal file should not exist
And the atomic.sqlite-shm file should not exist
And the atomic.sqlite-journal file should not exist
@require-sqlite
Scenario: Fail verification when the old host remains
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE verify_host (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO verify_host (name) VALUES ('https://oldhost.invalid/page');
"""
When I try `wp sqlite import test_import.sql --verify-old-host=oldhost.invalid`
Then the return code should be 1
And STDERR should contain:
"""
still contain 'oldhost.invalid'
"""
And the SQLite database should not contain a table named "verify_host"
@require-sqlite
Scenario: Pass verification when the old host is gone
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE verify_host (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO verify_host (name) VALUES ('https://newhost.invalid/page');
"""
When I run `wp sqlite import test_import.sql --verify-old-host=oldhost.invalid`
Then STDOUT should contain:
"""
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "verify_host"
And the "verify_host" should contain a row with name "https://newhost.invalid/page"
@require-sqlite
Scenario: Fail verification on corrupt serialized data
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE verify_serialized (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO verify_serialized (name) VALUES ('a:1:{s:3:"foo";s:4:"broken}');
"""
When I try `wp sqlite import test_import.sql --verify-old-host=never-present.invalid`
Then the return code should be 1
And STDERR should contain:
"""
invalid serialized data
"""
And the SQLite database should not contain a table named "verify_serialized"