{{ message }}
Repository navigation
Expand file tree
/
Copy pathDiffSorterProgrammableTest.php
More file actions
344 lines (289 loc) · 13.2 KB
/
Copy pathDiffSorterProgrammableTest.php
File metadata and controls
344 lines (289 loc) · 13.2 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
<?php
namespace DBDiff\Tests\Unit;
use PHPUnit\Framework\TestCase;
use DBDiff\SQLGen\DiffSorter;
use DBDiff\Diff\AddTable;
use DBDiff\Diff\DropTable;
use DBDiff\Diff\CreateView;
use DBDiff\Diff\DropView;
use DBDiff\Diff\AlterView;
use DBDiff\Diff\CreateTrigger;
use DBDiff\Diff\DropTrigger;
use DBDiff\Diff\CreateRoutine;
use DBDiff\Diff\DropRoutine;
use DBDiff\Diff\CreateEnum;
use DBDiff\Diff\DropEnum;
use DBDiff\Diff\AlterEnum;
use DBDiff\Diff\CreateDomain;
use DBDiff\Diff\CreateSequence;
use DBDiff\Diff\DropDomain;
use DBDiff\Diff\DropSequence;
use DBDiff\Diff\CreateCompositeType;
class DiffSorterProgrammableTest extends TestCase
{
private DiffSorter $sorter;
protected function setUp(): void
{
$this->sorter = new DiffSorter();
}
private function className($obj): string
{
return (new \ReflectionClass($obj))->getShortName();
}
/**
* UP order: views and triggers the source no longer has come off before
* any table is touched; routines it no longer has are dropped last, once
* the triggers, views, defaults and tables calling them are gone (issue
* #238). New views and triggers come after the tables.
*/
public function testUpOrderDropsProgrammableBeforeTables(): void
{
$stub = $this->createMock(\DBDiff\DB\DBManager::class);
$diffs = [
new CreateView('v1', 'CREATE VIEW ...'),
new AddTable('t1', $stub, 'source'),
new DropView('v2', 'CREATE VIEW ...'),
new CreateTrigger('trg1', 'products', 'CREATE TRIGGER ...'),
new DropTrigger('trg2', 'products', 'CREATE TRIGGER ...'),
new CreateRoutine('fn1', 'CREATE FUNCTION ...'),
new DropRoutine('fn2', 'CREATE FUNCTION ...'),
];
$names = array_map([$this, 'className'], $this->sorter->sort($diffs, 'up'));
$addTableIdx = array_search('AddTable', $names);
$this->assertLessThan($addTableIdx, array_search('DropView', $names));
$this->assertLessThan($addTableIdx, array_search('DropTrigger', $names));
$this->assertSame('DropRoutine', end($names));
$this->assertGreaterThan($addTableIdx, array_search('CreateView', $names));
$this->assertGreaterThan($addTableIdx, array_search('CreateTrigger', $names));
$this->assertGreaterThan($addTableIdx, array_search('CreateRoutine', $names));
}
/**
* DOWN order: views the UP created or changed are taken back before the
* tables are reverted; a view the UP dropped is recreated after them,
* since it may read a table or column the DOWN puts back (issue #238).
*/
public function testDownOrderProgrammableBeforeTables(): void
{
$stub = $this->createMock(\DBDiff\DB\DBManager::class);
$diffs = [
new AddTable('t1', $stub, 'source'),
new DropTable('t2', $stub, 'target'),
new CreateView('v1', 'CREATE VIEW ...'),
new DropView('v2', 'CREATE VIEW ...'),
new AlterView('v3', 'src def', 'tgt def'),
];
$names = array_map([$this, 'className'], $this->sorter->sort($diffs, 'down'));
$firstTableIdx = min(array_search('AddTable', $names), array_search('DropTable', $names));
$lastTableIdx = max(array_search('AddTable', $names), array_search('DropTable', $names));
$this->assertLessThan($firstTableIdx, array_search('CreateView', $names));
$this->assertLessThan($firstTableIdx, array_search('AlterView', $names));
$this->assertGreaterThan($lastTableIdx, array_search('DropView', $names));
}
/**
* Views, triggers, and routines sort by name within their type.
*/
public function testProgrammableObjectsSortByName(): void
{
$diffs = [
new CreateView('z_view', 'CREATE VIEW ...'),
new CreateView('a_view', 'CREATE VIEW ...'),
new CreateView('m_view', 'CREATE VIEW ...'),
];
$sorted = $this->sorter->sort($diffs, 'up');
$names = array_map(fn($d) => $d->name, $sorted);
$this->assertSame(['a_view', 'm_view', 'z_view'], $names);
}
/**
* UP order: an enum the source no longer has is dropped after the views
* that may use it — the other way round, DROP TYPE failed with "cannot
* drop type ... because other objects depend on it" (issue #238).
*/
public function testUpOrderDropEnumAfterDropView(): void
{
$diffs = [
new DropEnum('status', 'CREATE TYPE "status" AS ENUM (\'a\')'),
new DropView('v1', 'CREATE VIEW ...'),
];
$names = array_map([$this, 'className'], $this->sorter->sort($diffs, 'up'));
$this->assertSame(['DropView', 'DropEnum'], $names);
}
/**
* UP order: CreateEnum after data ops but before CreateView.
*/
public function testUpOrderCreateEnumBeforeCreateView(): void
{
$diffs = [
new CreateView('v1', 'CREATE VIEW ...'),
new CreateEnum('status', 'CREATE TYPE "status" AS ENUM (\'a\')'),
];
$sorted = $this->sorter->sort($diffs, 'up');
$names = array_map([$this, 'className'], $sorted);
$this->assertSame(['CreateEnum', 'CreateView'], $names);
}
/**
* DOWN order: an enum the UP dropped is recreated before a view the UP
* dropped, which may use it (issue #238).
*/
public function testDownOrderRecreatesEnumBeforeViews(): void
{
$diffs = [
new DropView('v1', 'CREATE VIEW ...'),
new DropEnum('e1', 'CREATE TYPE "e1" AS ENUM (\'x\')'),
];
$names = array_map([$this, 'className'], $this->sorter->sort($diffs, 'down'));
$this->assertSame(['DropEnum', 'DropView'], $names);
}
/**
* UP order: an enum, domain, composite or sequence the source no longer
* has is dropped after the tables — a dropped table or column may still
* be typed by it, or default to it (issue #238).
*/
public function testUpOrderDroppedTypesAfterTables(): void
{
$stub = $this->createMock(\DBDiff\DB\DBManager::class);
$diffs = [
new DropEnum('old_type', 'CREATE TYPE "old_type" AS ENUM (\'x\')'),
new DropDomain('d', 'CREATE DOMAIN "d" AS int'),
new DropSequence('s', 'CREATE SEQUENCE "s"'),
new DropTable('t1', $stub, 'target'),
new AddTable('t2', $stub, 'source'),
];
$names = array_map([$this, 'className'], $this->sorter->sort($diffs, 'up'));
$this->assertSame(['AddTable', 'DropTable', 'DropSequence', 'DropDomain', 'DropEnum'], $names);
}
/**
* A function must exist before anything that calls it.
*
* CreateRoutine used to sort last, after CreateView and CreateTrigger, so a
* trigger whose function was also new was emitted before the function:
*
* CREATE TRIGGER tg ... EXECUTE FUNCTION f();
* CREATE OR REPLACE FUNCTION public.f() ...
* ERROR: function f() does not exist
*
* The fix shipped in 3.0.0-rc.9 but had no test of its own — it was only
* covered incidentally by a partition-trigger test that happens to need a
* function, so a regression would have pointed at triggers rather than at
* ordering. Views have the same dependency and are asserted here too.
*/
public function testUpOrderCreateRoutineBeforeItsCallers(): void
{
$diffs = [
new CreateTrigger('trg1', 't1', 'CREATE TRIGGER ...'),
new CreateView('v1', 'CREATE VIEW ...'),
new CreateRoutine('fn1', 'CREATE FUNCTION ...'),
];
$names = array_map([$this, 'className'], $this->sorter->sort($diffs, 'up'));
$routineIdx = array_search('CreateRoutine', $names);
$viewIdx = array_search('CreateView', $names);
$triggerIdx = array_search('CreateTrigger', $names);
$this->assertLessThan($viewIdx, $routineIdx, 'CreateRoutine before CreateView');
$this->assertLessThan($triggerIdx, $routineIdx, 'CreateRoutine before CreateTrigger');
}
/**
* UP order: CreateEnum must come BEFORE AddTable. A table with an enum
* column cannot be created until the type exists — PostgreSQL rejects it
* with 'type "x" does not exist'. Enums are Postgres-only here (the MySQL
* and SQLite adapters both report no enums), so ordering them ahead of
* tables affects nothing else.
*/
public function testUpOrderCreateEnumBeforeTableAndView(): void
{
$stub = $this->createMock(\DBDiff\DB\DBManager::class);
$diffs = [
new CreateView('v1', 'CREATE VIEW ...'),
new AddTable('t1', $stub, 'source'),
new CreateEnum('status', 'CREATE TYPE "status" AS ENUM (\'a\')'),
];
$sorted = $this->sorter->sort($diffs, 'up');
$names = array_map([$this, 'className'], $sorted);
$addIdx = array_search('AddTable', $names);
$enumIdx = array_search('CreateEnum', $names);
$viewIdx = array_search('CreateView', $names);
$this->assertLessThan($addIdx, $enumIdx, 'CreateEnum before AddTable');
$this->assertLessThan($viewIdx, $enumIdx, 'CreateEnum before CreateView');
}
/**
* Full lifecycle: enum + view + trigger + table in a single UP sort.
*/
public function testUpFullLifecycleSort(): void
{
$stub = $this->createMock(\DBDiff\DB\DBManager::class);
$diffs = [
new CreateView('v1', 'CREATE VIEW ...'),
new CreateEnum('e1', 'CREATE TYPE "e1" AS ENUM (\'a\')'),
new AddTable('t1', $stub, 'source'),
new DropView('v2', 'CREATE VIEW ...'),
new DropEnum('e2', 'CREATE TYPE "e2" AS ENUM (\'b\')'),
new CreateTrigger('trg1', 't1', 'CREATE TRIGGER ...'),
new DropTrigger('trg2', 't1', 'CREATE TRIGGER ...'),
];
$sorted = $this->sorter->sort($diffs, 'up');
$names = array_map([$this, 'className'], $sorted);
// Views and triggers come off first; the enum only once nothing can
// use it.
$dropEnumIdx = array_search('DropEnum', $names);
$dropViewIdx = array_search('DropView', $names);
$dropTrigIdx = array_search('DropTrigger', $names);
$addTableIdx = array_search('AddTable', $names);
$createEnumIdx = array_search('CreateEnum', $names);
$createViewIdx = array_search('CreateView', $names);
$createTrigIdx = array_search('CreateTrigger', $names);
$this->assertSame(count($names) - 1, $dropEnumIdx);
$this->assertLessThan($addTableIdx, $dropViewIdx);
$this->assertLessThan($addTableIdx, $dropTrigIdx);
// Enums precede the tables that may reference them
$this->assertLessThan($addTableIdx, $createEnumIdx);
// Views and triggers still come after the tables they depend on
$this->assertGreaterThan($addTableIdx, $createViewIdx);
$this->assertGreaterThan($addTableIdx, $createTrigIdx);
// CreateEnum before CreateView
$this->assertLessThan($createViewIdx, $createEnumIdx);
}
/**
* A routine a table's default, check or index calls is created before
* the tables (issue #238); any other stays after them.
*/
public function testUpOrderAnEarlyRoutineGoesBeforeTheTables(): void
{
$stub = $this->createMock(\DBDiff\DB\DBManager::class);
$early = new CreateRoutine('f', 'CREATE FUNCTION f() ...');
$early->early = true;
$diffs = [new AddTable('t1', $stub, 'source'), new CreateRoutine('g', 'CREATE FUNCTION g() ...'), $early];
$sorted = $this->sorter->sort($diffs, 'up');
$this->assertSame([$early, $diffs[0], $diffs[1]], $sorted);
}
/**
* The DOWN drops what the UP created only once nothing uses it: after the
* tables and columns that may be typed by it or call it (issue #238).
*/
public function testDownOrderDropsCreatedTypesAndRoutinesLast(): void
{
$stub = $this->createMock(\DBDiff\DB\DBManager::class);
$diffs = [
new CreateEnum('e', "CREATE TYPE \"e\" AS ENUM ('a')"),
new CreateDomain('d', 'CREATE DOMAIN "d" AS int'),
new CreateSequence('s', 'CREATE SEQUENCE "s"'),
new CreateCompositeType('c', 'CREATE TYPE "c" AS (a int)'),
new CreateRoutine('f', 'CREATE FUNCTION f() ...'),
new AddTable('t1', $stub, 'source'),
];
$names = array_map([$this, 'className'], $this->sorter->sort($diffs, 'down'));
$this->assertSame('AddTable', $names[0]);
$this->assertSame(['CreateRoutine', 'CreateCompositeType', 'CreateDomain', 'CreateEnum', 'CreateSequence'], array_slice($names, 1));
}
/**
* The DOWN changes an enum's labels before it puts back the routines,
* views and policies that may name them (issue #237).
*/
public function testDownOrderAlterEnumBeforeProgrammableObjects(): void
{
$diffs = [
new AlterView('v', 'CREATE VIEW ...', 'CREATE VIEW ...'),
new DropRoutine('f', 'CREATE FUNCTION ...'),
new AlterEnum('e', "CREATE TYPE \"e\" AS ENUM ('a')", "CREATE TYPE \"e\" AS ENUM ('a', 'b')"),
];
$names = array_map([$this, 'className'], $this->sorter->sort($diffs, 'down'));
$this->assertSame('AlterEnum', $names[0]);
}
}
You can’t perform that action at this time.
