{{ message }}
Repository navigation
Expand file tree
/
Copy pathDestructiveLinterTest.php
More file actions
302 lines (245 loc) · 12.7 KB
/
Copy pathDestructiveLinterTest.php
File metadata and controls
302 lines (245 loc) · 12.7 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
<?php
namespace DBDiff\Tests\Unit\Linter;
use PHPUnit\Framework\TestCase;
use DBDiff\Linter\DestructiveLinter;
use DBDiff\Linter\LintResult;
use DBDiff\Linter\LintViolation;
use DBDiff\Diff\DropTable;
use DBDiff\Diff\AlterTableDropColumn;
use DBDiff\Diff\AlterTableAddColumn;
use DBDiff\Diff\DropEnum;
use DBDiff\Diff\DropRoutine;
use DBDiff\Diff\DropTrigger;
use DBDiff\Diff\DropView;
use DBDiff\Diff\DropMatView;
use DBDiff\Diff\DropSequence;
use DBDiff\Diff\DropCompositeType;
use DBDiff\Diff\DropDomain;
use DBDiff\Diff\DropPolicy;
use DBDiff\Diff\AddTable;
class DestructiveLinterTest extends TestCase
{
private DestructiveLinter $linter;
protected function setUp(): void {
$this->linter = new DestructiveLinter();
}
// ── DropTable ───────────────────────────────────────────────────────────
public function testDropTableProducesError(): void {
$diff = ['schema' => [new DropTable('orders', null, 'source')]];
$result = $this->linter->lint($diff);
$this->assertTrue($result->hasErrors());
$errors = $result->getErrors();
$this->assertCount(1, $errors);
$this->assertSame('error', $errors[0]->level);
$this->assertSame('drop-table', $errors[0]->type);
$this->assertStringContainsString('orders', $errors[0]->object);
$this->assertStringContainsString('allow-destructive', $errors[0]->suggestion);
}
public function testMultipleDropTablesAllReportedAsErrors(): void {
$diff = [
'schema' => [
new DropTable('orders', null, 'source'),
new DropTable('payments', null, 'source'),
]
];
$result = $this->linter->lint($diff);
$this->assertCount(2, $result->getErrors());
}
// ── AlterTableDropColumn ────────────────────────────────────────────────
public function testDropColumnProducesError(): void {
$colDiff = (object)['Type' => 'varchar(255)'];
$diff = ['schema' => [new AlterTableDropColumn('users', 'email', $colDiff)]];
$result = $this->linter->lint($diff);
$this->assertTrue($result->hasErrors());
$errors = $result->getErrors();
$this->assertCount(1, $errors);
$this->assertSame('drop-column', $errors[0]->type);
$this->assertStringContainsString('users', $errors[0]->object);
$this->assertStringContainsString('email', $errors[0]->object);
}
// ── Rename heuristic ────────────────────────────────────────────────────
public function testDropPlusAddSameTypeSameTableIsWarnedAsRename(): void {
$colDiff = (object)['Type' => 'varchar(255)'];
$diff = [
'schema' => [
new AlterTableDropColumn('users', 'email', $colDiff),
new AlterTableAddColumn('users', 'email_address', $colDiff),
]
];
$result = $this->linter->lint($diff);
// Should be warning (possible-rename), not error (drop-column)
$this->assertFalse($result->hasErrors());
$this->assertTrue($result->hasWarnings());
$warnings = $result->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('possible-rename', $warnings[0]->type);
}
public function testDropPlusAddDifferentTypeIsStillError(): void {
$intDiff = (object)['Type' => 'int'];
$varcharDiff = (object)['Type' => 'varchar(100)'];
$diff = [
'schema' => [
new AlterTableDropColumn('users', 'age', $intDiff),
new AlterTableAddColumn('users', 'nickname', $varcharDiff),
]
];
$result = $this->linter->lint($diff);
$this->assertTrue($result->hasErrors());
$errors = $result->getErrors();
$this->assertSame('drop-column', $errors[0]->type);
}
public function testDropPlusAddOnDifferentTablesIsError(): void {
$colDiff = (object)['Type' => 'int'];
$diff = [
'schema' => [
new AlterTableDropColumn('users', 'score', $colDiff),
new AlterTableAddColumn('posts', 'score', $colDiff),
]
];
$result = $this->linter->lint($diff);
// Tables differ — no rename heuristic should fire
$this->assertTrue($result->hasErrors());
$this->assertSame('drop-column', $result->getErrors()[0]->type);
}
public function testRenameHeuristicWorkWithArrayDiffFormat(): void {
$colDiff = ['Type' => 'text'];
$diff = [
'schema' => [
new AlterTableDropColumn('articles', 'body', $colDiff),
new AlterTableAddColumn('articles', 'content', $colDiff),
]
];
$result = $this->linter->lint($diff);
$this->assertFalse($result->hasErrors());
$this->assertSame('possible-rename', $result->getWarnings()[0]->type);
}
// ── DropEnum ────────────────────────────────────────────────────────────
public function testDropEnumProducesWarning(): void {
$diff = ['schema' => [new DropEnum('user_status', 'CREATE TYPE "user_status" AS ENUM (\'active\')')]];
$result = $this->linter->lint($diff);
$this->assertFalse($result->hasErrors());
$this->assertTrue($result->hasWarnings());
$this->assertSame('drop-enum', $result->getWarnings()[0]->type);
$this->assertStringContainsString('user_status', $result->getWarnings()[0]->object);
}
// ── DropRoutine ─────────────────────────────────────────────────────────
public function testDropRoutineProducesWarning(): void {
$diff = ['schema' => [new DropRoutine('calculate_tax', 'CREATE FUNCTION calculate_tax()')]];
$result = $this->linter->lint($diff);
$this->assertFalse($result->hasErrors());
$warnings = $result->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('drop-routine', $warnings[0]->type);
$this->assertStringContainsString('calculate_tax', $warnings[0]->object);
}
// ── DropTrigger ─────────────────────────────────────────────────────────
public function testDropTriggerProducesWarning(): void {
$diff = ['schema' => [new DropTrigger('audit_log', 'users', 'CREATE TRIGGER audit_log ...')]];
$result = $this->linter->lint($diff);
$warnings = $result->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('drop-trigger', $warnings[0]->type);
$this->assertStringContainsString('audit_log', $warnings[0]->object);
$this->assertStringContainsString('users', $warnings[0]->object);
}
// ── DropView ─────────────────────────────────────────────────────────────
public function testDropViewProducesWarning(): void {
$diff = ['schema' => [new DropView('active_users', 'CREATE VIEW active_users AS ...')]];
$result = $this->linter->lint($diff);
$warnings = $result->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('drop-view', $warnings[0]->type);
}
// ── Newly modelled object kinds ─────────────────────────────────────────
/**
* These became possible to emit only once DBDiff modelled the object kinds,
* so the linter had no rule for them. An unguarded DROP MATERIALIZED VIEW
* or DROP SEQUENCE discards data or a counter.
*/
public function testDropMatViewProducesWarning(): void {
$diff = ['schema' => [new DropMatView('daily_totals', 'CREATE MATERIALIZED VIEW ...')]];
$warnings = $this->linter->lint($diff)->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('drop-matview', $warnings[0]->type);
$this->assertStringContainsString('daily_totals', $warnings[0]->object);
}
public function testDropSequenceProducesWarning(): void {
$diff = ['schema' => [new DropSequence('invoice_no', 'CREATE SEQUENCE ...')]];
$warnings = $this->linter->lint($diff)->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('drop-sequence', $warnings[0]->type);
}
public function testDropCompositeTypeProducesWarning(): void {
$diff = ['schema' => [new DropCompositeType('address', 'CREATE TYPE ...')]];
$warnings = $this->linter->lint($diff)->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('drop-composite-type', $warnings[0]->type);
}
public function testDropDomainProducesWarning(): void {
$diff = ['schema' => [new DropDomain('positive_int', 'CREATE DOMAIN ...')]];
$warnings = $this->linter->lint($diff)->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('drop-domain', $warnings[0]->type);
}
/**
* Dropping a policy widens access rather than removing data, which is why
* it is reported at all.
*/
public function testDropPolicyProducesWarningNamingItsTable(): void {
$diff = ['schema' => [new DropPolicy('tenant_isolation', 'documents', 'CREATE POLICY ...')]];
$warnings = $this->linter->lint($diff)->getWarnings();
$this->assertCount(1, $warnings);
$this->assertSame('drop-policy', $warnings[0]->type);
$this->assertStringContainsString('tenant_isolation', $warnings[0]->object);
$this->assertStringContainsString('documents', $warnings[0]->object);
}
// ── Clean diffs ──────────────────────────────────────────────────────────
public function testAddTableProducesNoViolations(): void {
$diff = ['schema' => [new AddTable('new_feature', null, 'source')]];
$result = $this->linter->lint($diff);
$this->assertTrue($result->isEmpty());
$this->assertFalse($result->hasErrors());
$this->assertFalse($result->hasWarnings());
}
public function testEmptyDiffProducesNoViolations(): void {
$result = $this->linter->lint([]);
$this->assertTrue($result->isEmpty());
}
public function testDataOnlyDiffProducesNoViolations(): void {
$result = $this->linter->lint(['data' => ['some data diff item']]);
$this->assertTrue($result->isEmpty());
}
// ── Mixed diff ───────────────────────────────────────────────────────────
public function testMixedDestructiveAndSafeChangesReportsAll(): void {
$colDiff = (object)['Type' => 'int'];
$diff = [
'schema' => [
new AddTable('feature_flags', null, 'source'),
new DropTable('legacy_data', null, 'source'),
new AlterTableDropColumn('users', 'phone', $colDiff),
new DropView('old_view', 'CREATE VIEW old_view ...'),
]
];
$result = $this->linter->lint($diff);
$this->assertCount(2, $result->getErrors());
$this->assertCount(1, $result->getWarnings());
$this->assertCount(3, $result->getViolations());
}
// ── LintResult API ───────────────────────────────────────────────────────
public function testLintResultGetErrorsFiltersOnlyErrors(): void {
$colDiff = (object)['Type' => 'int'];
$diff = [
'schema' => [
new DropTable('tbl', null, 'source'),
new DropView('v', 'CREATE VIEW v ...'),
]
];
$result = $this->linter->lint($diff);
$errors = $result->getErrors();
$warnings = $result->getWarnings();
$this->assertCount(1, $errors);
$this->assertSame('error', $errors[0]->level);
$this->assertCount(1, $warnings);
$this->assertSame('warning', $warnings[0]->level);
}
}
You can’t perform that action at this time.
