forked from supabase/pg-toolbelt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterialized-view.diff.test.ts
More file actions
265 lines (246 loc) · 7.85 KB
/
Copy pathmaterialized-view.diff.test.ts
File metadata and controls
265 lines (246 loc) · 7.85 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
import { describe, expect, test } from "bun:test";
import { DefaultPrivilegeState } from "../base.default-privileges.ts";
import {
AlterMaterializedViewChangeOwner,
AlterMaterializedViewSetStorageParams,
} from "./changes/materialized-view.alter.ts";
import {
CreateCommentOnMaterializedView,
CreateCommentOnMaterializedViewColumn,
DropCommentOnMaterializedView,
DropCommentOnMaterializedViewColumn,
} from "./changes/materialized-view.comment.ts";
import { CreateMaterializedView } from "./changes/materialized-view.create.ts";
import { DropMaterializedView } from "./changes/materialized-view.drop.ts";
import {
GrantMaterializedViewPrivileges,
RevokeGrantOptionMaterializedViewPrivileges,
RevokeMaterializedViewPrivileges,
} from "./changes/materialized-view.privilege.ts";
import { diffMaterializedViews } from "./materialized-view.diff.ts";
import {
MaterializedView,
type MaterializedViewProps,
} from "./materialized-view.model.ts";
const base: MaterializedViewProps = {
schema: "public",
name: "mv1",
definition: "select 1",
row_security: false,
force_row_security: false,
has_indexes: false,
has_rules: false,
has_triggers: false,
has_subclasses: false,
is_populated: true,
replica_identity: "d",
is_partition: false,
options: null,
partition_bound: null,
owner: "o1",
comment: null,
columns: [],
privileges: [],
};
const baseColumn = {
name: "id",
position: 1,
data_type: "integer",
data_type_str: "integer",
is_custom_type: false,
custom_type_type: null,
custom_type_category: null,
custom_type_schema: null,
custom_type_name: null,
not_null: false,
is_identity: false,
is_identity_always: false,
is_generated: false,
collation: null,
default: null,
comment: null,
};
const makeMaterializedView = (override: Partial<MaterializedViewProps> = {}) =>
new MaterializedView({
...base,
...override,
privileges: override.privileges ?? [...base.privileges],
columns: override.columns ?? [...base.columns],
});
const testContext = {
version: 170000,
currentUser: "postgres",
defaultPrivilegeState: new DefaultPrivilegeState({}),
mainRoles: {},
};
describe.concurrent("materialized-view.diff", () => {
test("create and drop", () => {
const mv = new MaterializedView(base);
const created = diffMaterializedViews(
testContext,
{},
{ [mv.stableId]: mv },
);
expect(created[0]).toBeInstanceOf(CreateMaterializedView);
const dropped = diffMaterializedViews(
testContext,
{ [mv.stableId]: mv },
{},
);
expect(dropped[0]).toBeInstanceOf(DropMaterializedView);
});
test("alter owner", () => {
const main = new MaterializedView(base);
const branch = new MaterializedView({ ...base, owner: "o2" });
const changes = diffMaterializedViews(
testContext,
{ [main.stableId]: main },
{ [branch.stableId]: branch },
);
expect(changes[0]).toBeInstanceOf(AlterMaterializedViewChangeOwner);
});
test("drop + create with metadata on non-alterable change", () => {
const main = new MaterializedView(base);
const branch = new MaterializedView({ ...base, definition: "select 2" });
const changes = diffMaterializedViews(
testContext,
{ [main.stableId]: main },
{ [branch.stableId]: branch },
);
expect(changes).toHaveLength(3);
expect(changes[0]).toBeInstanceOf(DropMaterializedView);
expect(changes[1]).toBeInstanceOf(CreateMaterializedView);
expect(changes[2]).toBeInstanceOf(AlterMaterializedViewChangeOwner);
});
test("alter storage parameters: set and reset", () => {
const main = new MaterializedView({
...base,
options: ["fillfactor=90", "autovacuum_enabled=false"],
});
const branch = new MaterializedView({
...base,
options: ["fillfactor=70", "user_catalog_table=true"],
});
const changes = diffMaterializedViews(
testContext,
{ [main.stableId]: main },
{ [branch.stableId]: branch },
);
expect(
changes.some((c) => c instanceof AlterMaterializedViewSetStorageParams),
).toBe(true);
});
test("create with comment and column comments", () => {
const mv = makeMaterializedView({
comment: "my matview",
columns: [
{ ...baseColumn, comment: "id column" },
{ ...baseColumn, name: "val", position: 2, comment: null },
],
});
const changes = diffMaterializedViews(
testContext,
{},
{ [mv.stableId]: mv },
);
expect(changes[0]).toBeInstanceOf(CreateMaterializedView);
expect(
changes.some((c) => c instanceof CreateCommentOnMaterializedView),
).toBe(true);
expect(
changes.some((c) => c instanceof CreateCommentOnMaterializedViewColumn),
).toBe(true);
});
test("create with privileges emits grant changes", () => {
const mv = makeMaterializedView({
privileges: [
{ grantee: "role_select", privilege: "SELECT", grantable: false },
],
});
const changes = diffMaterializedViews(
testContext,
{},
{ [mv.stableId]: mv },
);
expect(changes[0]).toBeInstanceOf(CreateMaterializedView);
expect(
changes.some((c) => c instanceof GrantMaterializedViewPrivileges),
).toBe(true);
});
test("comment changes emit create/drop comment statements", () => {
const main = makeMaterializedView();
const withComment = makeMaterializedView({ comment: "matview comment" });
const addComment = diffMaterializedViews(
testContext,
{ [main.stableId]: main },
{ [withComment.stableId]: withComment },
);
expect(addComment[0]).toBeInstanceOf(CreateCommentOnMaterializedView);
const dropComment = diffMaterializedViews(
testContext,
{ [withComment.stableId]: withComment },
{ [main.stableId]: main },
);
expect(dropComment[0]).toBeInstanceOf(DropCommentOnMaterializedView);
});
test("column comment changes emit create/drop column comment statements", () => {
const main = makeMaterializedView({
columns: [{ ...baseColumn, comment: null }],
});
const withColComment = makeMaterializedView({
columns: [{ ...baseColumn, comment: "id column" }],
});
const addColComment = diffMaterializedViews(
testContext,
{ [main.stableId]: main },
{ [withColComment.stableId]: withColComment },
);
expect(
addColComment.some(
(c) => c instanceof CreateCommentOnMaterializedViewColumn,
),
).toBe(true);
const dropColComment = diffMaterializedViews(
testContext,
{ [withColComment.stableId]: withColComment },
{ [main.stableId]: main },
);
expect(
dropColComment.some(
(c) => c instanceof DropCommentOnMaterializedViewColumn,
),
).toBe(true);
});
test("privilege diffs emit grant, revoke, and revoke grant option statements", () => {
const main = makeMaterializedView({
privileges: [
{ grantee: "role_select", privilege: "SELECT", grantable: false },
{ grantee: "role_with_option", privilege: "SELECT", grantable: true },
{ grantee: "role_removed", privilege: "SELECT", grantable: false },
],
});
const branch = makeMaterializedView({
privileges: [
{ grantee: "role_select", privilege: "SELECT", grantable: true },
{ grantee: "role_with_option", privilege: "SELECT", grantable: false },
{ grantee: "role_new", privilege: "SELECT", grantable: false },
],
});
const changes = diffMaterializedViews(
testContext,
{ [main.stableId]: main },
{ [branch.stableId]: branch },
);
expect(
changes.some((c) => c instanceof GrantMaterializedViewPrivileges),
).toBe(true);
expect(
changes.some((c) => c instanceof RevokeMaterializedViewPrivileges),
).toBe(true);
expect(
changes.some(
(c) => c instanceof RevokeGrantOptionMaterializedViewPrivileges,
),
).toBe(true);
});
});