-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.test.ts
221 lines (199 loc) · 7.42 KB
/
main.test.ts
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
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
import { knex } from "knex";
import { PassThrough } from "stream";
import { updateTypes } from "./main";
const db = knex({ client: "pg", connection: { database: "update_types" } });
beforeAll(async function setup() {
await createDatabase();
await db.raw(`CREATE DOMAIN short_id AS TEXT CHECK(VALUE ~ '^[0-9a-z]{6}$')`);
await db.raw(`CREATE TYPE identity_provider AS ENUM ('google', 'facebook', 'linkedin')`); // prettier-ignore
await db.schema.createTable("user", (table) => {
table.increments("int").notNullable().primary();
table.specificType("provider", "identity_provider").notNullable();
table.specificType("provider_null", "identity_provider");
table.specificType("provider_array", "identity_provider[]").notNullable();
table.specificType("int_array", "integer[]").notNullable();
table.specificType("short_id", "short_id").notNullable();
table.decimal("decimal").notNullable();
table.specificType("decimal_array", "decimal[]").notNullable();
table.double("double").notNullable();
table.specificType("double_array", "float8[]").notNullable();
table.float("float").notNullable();
table.specificType("float_array", "float4[]").notNullable();
table.specificType("money", "money").notNullable();
table.bigInteger("bigint").notNullable();
table.smallint("smallint").notNullable();
table.specificType("int2", "int2").notNullable();
table.specificType("int4", "int4").notNullable();
table.specificType("int8", "int8").notNullable();
table.binary("binary").notNullable();
table.binary("binary_null");
table.specificType("binary_array", "bytea[]").notNullable();
table.uuid("uuid").notNullable();
table.uuid("uuid_null");
table.specificType("uuid_array", "uuid[]").notNullable();
table.text("text").notNullable();
table.text("text_null");
table.specificType("text_array", "text[]").notNullable();
table.specificType("citext", "citext").notNullable();
table.specificType("citext_null", "citext");
table.specificType("citext_array", "citext[]").notNullable();
table.specificType("char", "char(2)").notNullable();
table.string("varchar", 10).notNullable();
table.boolean("bool").notNullable();
table.boolean("bool_null");
table.specificType("bool_array", "bool[]").notNullable();
table.jsonb("jsonb_object").notNullable().defaultTo("{}");
table.jsonb("jsonb_object_null").defaultTo("{}");
table.jsonb("jsonb_array").notNullable().defaultTo("[]");
table.jsonb("jsonb_array_null").defaultTo("[]");
table.timestamp("timestamp").notNullable();
table.timestamp("timestamp_null");
table.time("time").notNullable();
table.time("time_null");
table.specificType("time_array", "time[]").notNullable();
table.specificType("interval", "interval").notNullable();
table.text("display name");
table.text("1invalidIdentifierName");
table.text(`name with a "`);
});
await db.schema.createTable("login", (table) => {
table.increments("secret").notNullable().primary();
});
await db.schema.withSchema("log").createTable("messages", (table) => {
table.increments("int").notNullable().primary();
table.text("notes");
table.timestamp("timestamp").notNullable();
});
await db.schema.withSchema("secret").createTable("secret", (table) => {
table.increments("int").notNullable().primary();
table.text("notes");
table.timestamp("timestamp").notNullable();
});
});
afterAll(async function teardown() {
await db.destroy();
});
test("updateTypes", async function () {
const output = new PassThrough();
const overrides = {
"identity_provider.linkedin": "LinkedIn",
};
await updateTypes(db, {
output,
overrides,
prefix: 'import { PostgresInterval} from "postgres-interval";',
suffix: "// user supplied suffix",
schema: ["public", "log", "!secret"],
exclude: ["login"],
});
expect(await toString(output)).toMatchInlineSnapshot(`
"// The TypeScript definitions below are automatically generated.
// Do not touch them, or risk, your modifications being lost.
import { PostgresInterval} from "postgres-interval";
export enum IdentityProvider {
Google = "google",
Facebook = "facebook",
LinkedIn = "linkedin",
}
export enum Table {
LogMessages = "log.messages",
User = "user",
}
export type Tables = {
"log.messages": LogMessages,
"user": User,
};
export type LogMessages = {
int: number;
notes: string | null;
timestamp: Date;
};
export type User = {
int: number;
provider: IdentityProvider;
provider_null: IdentityProvider | null;
provider_array: IdentityProvider[];
int_array: number[];
short_id: string;
decimal: string;
decimal_array: string[];
double: number;
double_array: number[];
float: number;
float_array: number[];
money: string;
bigint: string;
smallint: number;
int2: number;
int4: number;
int8: string;
binary: Buffer;
binary_null: Buffer | null;
binary_array: Buffer[];
uuid: string;
uuid_null: string | null;
uuid_array: string[];
text: string;
text_null: string | null;
text_array: string[];
citext: string;
citext_null: string | null;
citext_array: string[];
char: string;
varchar: string;
bool: boolean;
bool_null: boolean | null;
bool_array: boolean[];
jsonb_object: Record<string, unknown>;
jsonb_object_null: Record<string, unknown> | null;
jsonb_array: unknown[];
jsonb_array_null: unknown[] | null;
timestamp: Date;
timestamp_null: Date | null;
time: string;
time_null: string | null;
time_array: string[];
interval: PostgresInterval;
"display name": string | null;
"1invalidIdentifierName": string | null;
"name with a \\"": string | null;
};
// user supplied suffix
"
`);
});
async function createDatabase(): Promise<void> {
try {
await db.select(db.raw("version()")).first();
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (err instanceof Error && (err as any).code !== "3D000") throw err;
// Create a new test database if it doesn't exist
const tmp = knex({ client: "pg", connection: { database: "template1" } });
try {
const dbName = db.client.config.connection.database;
await tmp.raw("create database ?", [dbName]);
} finally {
await tmp.destroy();
}
}
await db.schema.raw("DROP SCHEMA IF EXISTS public CASCADE");
await db.schema.raw("DROP SCHEMA IF EXISTS log CASCADE");
await db.schema.raw("DROP SCHEMA IF EXISTS secret CASCADE");
await db.schema.raw("CREATE SCHEMA public");
await db.schema.raw("CREATE SCHEMA log");
await db.schema.raw("CREATE SCHEMA secret");
await db.raw(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
await db.raw(`CREATE EXTENSION IF NOT EXISTS "hstore"`);
await db.raw(`CREATE EXTENSION IF NOT EXISTS "citext"`);
}
function toString(stream: PassThrough): Promise<string> {
const chunks: Buffer[] = [];
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
stream.on("error", (err) => reject(err));
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
}