@anthonyalayo/drizzle-kit 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.d.mts +2891 -0
- package/api.d.ts +2891 -0
- package/api.js +47534 -0
- package/api.mjs +47521 -0
- package/bin.cjs +80043 -0
- package/{src/index.ts → index-Z-1TKnbX.d.mts} +123 -141
- package/index-Z-1TKnbX.d.ts +308 -0
- package/index.d.mts +2 -0
- package/index.d.ts +2 -0
- package/index.js +32 -0
- package/index.mjs +7 -0
- package/package.json +1 -1
- package/utils.js +6056 -0
- package/utils.mjs +6032 -0
- package/.gitignore +0 -25
- package/build.dev.ts +0 -51
- package/build.ts +0 -112
- package/imports-checker/analyze.ts +0 -78
- package/imports-checker/checker.ts +0 -296
- package/imports-checker/grammar/grammar.ohm +0 -121
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +0 -164
- package/imports-checker/grammar/grammar.ohm-bundle.js +0 -753
- package/imports-checker/index.ts +0 -48
- package/patches/difflib@0.2.4.patch +0 -31
- package/src/@types/utils.ts +0 -58
- package/src/api.ts +0 -504
- package/src/cli/commands/_es5.ts +0 -2
- package/src/cli/commands/check.ts +0 -52
- package/src/cli/commands/drop.ts +0 -60
- package/src/cli/commands/introspect.ts +0 -772
- package/src/cli/commands/libSqlPushUtils.ts +0 -355
- package/src/cli/commands/migrate.ts +0 -1526
- package/src/cli/commands/mysqlIntrospect.ts +0 -53
- package/src/cli/commands/mysqlPushUtils.ts +0 -352
- package/src/cli/commands/mysqlUp.ts +0 -102
- package/src/cli/commands/pgIntrospect.ts +0 -55
- package/src/cli/commands/pgPushUtils.ts +0 -269
- package/src/cli/commands/pgUp.ts +0 -217
- package/src/cli/commands/push.ts +0 -637
- package/src/cli/commands/singlestoreIntrospect.ts +0 -53
- package/src/cli/commands/singlestorePushUtils.ts +0 -456
- package/src/cli/commands/singlestoreUp.ts +0 -1
- package/src/cli/commands/sqliteIntrospect.ts +0 -96
- package/src/cli/commands/sqlitePushUtils.ts +0 -322
- package/src/cli/commands/sqliteUp.ts +0 -52
- package/src/cli/commands/utils.ts +0 -858
- package/src/cli/connections.ts +0 -914
- package/src/cli/index.ts +0 -48
- package/src/cli/schema.ts +0 -794
- package/src/cli/selector-ui.ts +0 -38
- package/src/cli/utils.ts +0 -112
- package/src/cli/validations/cli.ts +0 -79
- package/src/cli/validations/common.ts +0 -193
- package/src/cli/validations/libsql.ts +0 -27
- package/src/cli/validations/mysql.ts +0 -61
- package/src/cli/validations/outputs.ts +0 -91
- package/src/cli/validations/postgres.ts +0 -83
- package/src/cli/validations/singlestore.ts +0 -61
- package/src/cli/validations/sqlite.ts +0 -104
- package/src/cli/validations/studio.ts +0 -24
- package/src/cli/views.ts +0 -658
- package/src/extensions/getTablesFilterByExtensions.ts +0 -16
- package/src/extensions/vector.ts +0 -10
- package/src/global.ts +0 -61
- package/src/introspect-mysql.ts +0 -1030
- package/src/introspect-pg.ts +0 -1435
- package/src/introspect-singlestore.ts +0 -913
- package/src/introspect-sqlite.ts +0 -533
- package/src/jsonDiffer.js +0 -882
- package/src/jsonStatements.ts +0 -3590
- package/src/loader.mjs +0 -57
- package/src/migrationPreparator.ts +0 -217
- package/src/schemaValidator.ts +0 -23
- package/src/serializer/index.ts +0 -142
- package/src/serializer/mysqlImports.ts +0 -38
- package/src/serializer/mysqlSchema.ts +0 -423
- package/src/serializer/mysqlSerializer.ts +0 -999
- package/src/serializer/pgImports.ts +0 -106
- package/src/serializer/pgSchema.ts +0 -937
- package/src/serializer/pgSerializer.ts +0 -2223
- package/src/serializer/singlestoreImports.ts +0 -38
- package/src/serializer/singlestoreSchema.ts +0 -257
- package/src/serializer/singlestoreSerializer.ts +0 -767
- package/src/serializer/sqliteImports.ts +0 -41
- package/src/serializer/sqliteSchema.ts +0 -352
- package/src/serializer/sqliteSerializer.ts +0 -931
- package/src/serializer/studio.ts +0 -694
- package/src/serializer/utils.ts +0 -45
- package/src/simulator.ts +0 -157
- package/src/snapshotsDiffer.ts +0 -4398
- package/src/sqlgenerator.ts +0 -4212
- package/src/statementCombiner.ts +0 -597
- package/src/utils/certs.ts +0 -44
- package/src/utils/words.ts +0 -1333
- package/src/utils.ts +0 -370
- package/tests/bin.test.ts +0 -58
- package/tests/cli/d1http.config.ts +0 -12
- package/tests/cli/drizzle.config.ts +0 -9
- package/tests/cli/durable-sqlite.config.ts +0 -7
- package/tests/cli/expo.config.ts +0 -7
- package/tests/cli/postgres.config.ts +0 -13
- package/tests/cli/postgres2.config.ts +0 -17
- package/tests/cli/schema.ts +0 -1
- package/tests/cli/turso.config.ts +0 -10
- package/tests/cli-export.test.ts +0 -79
- package/tests/cli-generate.test.ts +0 -259
- package/tests/cli-migrate.test.ts +0 -103
- package/tests/cli-push.test.ts +0 -126
- package/tests/common.ts +0 -15
- package/tests/indexes/common.ts +0 -21
- package/tests/indexes/pg.test.ts +0 -245
- package/tests/introspect/libsql.test.ts +0 -35
- package/tests/introspect/mysql.test.ts +0 -319
- package/tests/introspect/pg.test.ts +0 -894
- package/tests/introspect/singlestore.test.ts +0 -275
- package/tests/introspect/sqlite.test.ts +0 -122
- package/tests/libsql-checks.test.ts +0 -308
- package/tests/libsql-statements.test.ts +0 -989
- package/tests/libsql-views.test.ts +0 -218
- package/tests/migrate/libsq-schema.ts +0 -6
- package/tests/migrate/libsql-migrate.test.ts +0 -58
- package/tests/migrate/migrations/0000_little_blizzard.sql +0 -4
- package/tests/migrate/migrations/0001_nebulous_storm.sql +0 -10
- package/tests/migrate/migrations/meta/0000_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/0001_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/_journal.json +0 -20
- package/tests/mysql-checks.test.ts +0 -291
- package/tests/mysql-generated.test.ts +0 -1290
- package/tests/mysql-schemas.test.ts +0 -156
- package/tests/mysql-views.test.ts +0 -553
- package/tests/mysql.test.ts +0 -863
- package/tests/pg-array.test.ts +0 -368
- package/tests/pg-checks.test.ts +0 -329
- package/tests/pg-columns.test.ts +0 -484
- package/tests/pg-domains.test.ts +0 -306
- package/tests/pg-enums.test.ts +0 -751
- package/tests/pg-generated.test.ts +0 -529
- package/tests/pg-identity.test.ts +0 -447
- package/tests/pg-schemas.test.ts +0 -105
- package/tests/pg-sequences.test.ts +0 -298
- package/tests/pg-tables.test.ts +0 -1021
- package/tests/pg-views.test.ts +0 -1929
- package/tests/push/common.ts +0 -58
- package/tests/push/libsql.test.ts +0 -1400
- package/tests/push/mysql-push.test.ts +0 -345
- package/tests/push/mysql.test.ts +0 -788
- package/tests/push/pg.test.ts +0 -4341
- package/tests/push/singlestore-push.test.ts +0 -894
- package/tests/push/singlestore.test.ts +0 -444
- package/tests/push/sqlite.test.ts +0 -1613
- package/tests/rls/pg-policy.test.ts +0 -1656
- package/tests/rls/pg-role.test.ts +0 -234
- package/tests/schemaDiffer.ts +0 -2852
- package/tests/singlestore-generated.test.ts +0 -1290
- package/tests/singlestore-schemas.test.ts +0 -155
- package/tests/singlestore.test.ts +0 -978
- package/tests/sqlite-checks.test.ts +0 -308
- package/tests/sqlite-columns.test.ts +0 -1049
- package/tests/sqlite-generated.test.ts +0 -1726
- package/tests/sqlite-tables.test.ts +0 -637
- package/tests/sqlite-views.test.ts +0 -218
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +0 -1812
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +0 -882
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +0 -1211
- package/tests/test/sqlite.test.ts +0 -39
- package/tests/testsinglestore.ts +0 -29
- package/tests/validations.test.ts +0 -869
- package/tests/wrap-param.test.ts +0 -16
- package/tsconfig.build.json +0 -4
- package/tsconfig.cli-types.json +0 -9
- package/tsconfig.json +0 -28
- package/vitest.config.ts +0 -34
package/tests/push/pg.test.ts
DELETED
|
@@ -1,4341 +0,0 @@
|
|
|
1
|
-
import { PGlite } from '@electric-sql/pglite';
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
import {
|
|
4
|
-
bigint,
|
|
5
|
-
bigserial,
|
|
6
|
-
boolean,
|
|
7
|
-
char,
|
|
8
|
-
check,
|
|
9
|
-
date,
|
|
10
|
-
doublePrecision,
|
|
11
|
-
index,
|
|
12
|
-
integer,
|
|
13
|
-
interval,
|
|
14
|
-
json,
|
|
15
|
-
jsonb,
|
|
16
|
-
numeric,
|
|
17
|
-
pgEnum,
|
|
18
|
-
pgMaterializedView,
|
|
19
|
-
pgPolicy,
|
|
20
|
-
pgRole,
|
|
21
|
-
pgSchema,
|
|
22
|
-
pgSequence,
|
|
23
|
-
pgTable,
|
|
24
|
-
pgView,
|
|
25
|
-
primaryKey,
|
|
26
|
-
real,
|
|
27
|
-
serial,
|
|
28
|
-
smallint,
|
|
29
|
-
text,
|
|
30
|
-
time,
|
|
31
|
-
timestamp,
|
|
32
|
-
uniqueIndex,
|
|
33
|
-
uuid,
|
|
34
|
-
varchar,
|
|
35
|
-
} from 'drizzle-orm/pg-core';
|
|
36
|
-
import { drizzle } from 'drizzle-orm/pglite';
|
|
37
|
-
import { eq, SQL, sql } from 'drizzle-orm/sql';
|
|
38
|
-
import { pgSuggestions } from 'src/cli/commands/pgPushUtils';
|
|
39
|
-
import { diffTestSchemas, diffTestSchemasPush } from 'tests/schemaDiffer';
|
|
40
|
-
import { expect, test } from 'vitest';
|
|
41
|
-
import { DialectSuite, run } from './common';
|
|
42
|
-
|
|
43
|
-
const pgSuite: DialectSuite = {
|
|
44
|
-
async allTypes() {
|
|
45
|
-
const client = new PGlite();
|
|
46
|
-
|
|
47
|
-
const customSchema = pgSchema('schemass');
|
|
48
|
-
|
|
49
|
-
const transactionStatusEnum = customSchema.enum('TransactionStatusEnum', ['PENDING', 'FAILED', 'SUCCESS']);
|
|
50
|
-
|
|
51
|
-
const enumname = pgEnum('enumname', ['three', 'two', 'one']);
|
|
52
|
-
|
|
53
|
-
const schema1 = {
|
|
54
|
-
test: pgEnum('test', ['ds']),
|
|
55
|
-
testHello: pgEnum('test_hello', ['ds']),
|
|
56
|
-
enumname: pgEnum('enumname', ['three', 'two', 'one']),
|
|
57
|
-
|
|
58
|
-
customSchema: customSchema,
|
|
59
|
-
transactionStatusEnum: customSchema.enum('TransactionStatusEnum', ['PENDING', 'FAILED', 'SUCCESS']),
|
|
60
|
-
|
|
61
|
-
allSmallSerials: pgTable('schema_test', {
|
|
62
|
-
columnAll: uuid('column_all').defaultRandom(),
|
|
63
|
-
column: transactionStatusEnum('column').notNull(),
|
|
64
|
-
}),
|
|
65
|
-
|
|
66
|
-
allSmallInts: customSchema.table(
|
|
67
|
-
'schema_test2',
|
|
68
|
-
{
|
|
69
|
-
columnAll: smallint('column_all').default(124).notNull(),
|
|
70
|
-
column: smallint('columns').array(),
|
|
71
|
-
column1: smallint('column1').array().array(),
|
|
72
|
-
column2: smallint('column2').array().array(),
|
|
73
|
-
column3: smallint('column3').array(),
|
|
74
|
-
},
|
|
75
|
-
(t) => ({
|
|
76
|
-
cd: uniqueIndex('testdfds').on(t.column),
|
|
77
|
-
}),
|
|
78
|
-
),
|
|
79
|
-
|
|
80
|
-
allEnums: customSchema.table(
|
|
81
|
-
'all_enums',
|
|
82
|
-
{
|
|
83
|
-
columnAll: enumname('column_all').default('three').notNull(),
|
|
84
|
-
column: enumname('columns'),
|
|
85
|
-
},
|
|
86
|
-
(t) => ({
|
|
87
|
-
d: index('ds').on(t.column),
|
|
88
|
-
}),
|
|
89
|
-
),
|
|
90
|
-
|
|
91
|
-
allTimestamps: customSchema.table('all_timestamps', {
|
|
92
|
-
columnDateNow: timestamp('column_date_now', {
|
|
93
|
-
precision: 1,
|
|
94
|
-
withTimezone: true,
|
|
95
|
-
mode: 'string',
|
|
96
|
-
}).defaultNow(),
|
|
97
|
-
columnAll: timestamp('column_all', { mode: 'string' }).default('2023-03-01 12:47:29.792'),
|
|
98
|
-
column: timestamp('column', { mode: 'string' }).default(sql`'2023-02-28 16:18:31.18'`),
|
|
99
|
-
column2: timestamp('column2', { mode: 'string', precision: 3 }).default(sql`'2023-02-28 16:18:31.18'`),
|
|
100
|
-
}),
|
|
101
|
-
|
|
102
|
-
allUuids: customSchema.table('all_uuids', {
|
|
103
|
-
columnAll: uuid('column_all').defaultRandom().notNull(),
|
|
104
|
-
column: uuid('column'),
|
|
105
|
-
}),
|
|
106
|
-
|
|
107
|
-
allDates: customSchema.table('all_dates', {
|
|
108
|
-
column_date_now: date('column_date_now').defaultNow(),
|
|
109
|
-
column_all: date('column_all', { mode: 'date' }).default(new Date()).notNull(),
|
|
110
|
-
column: date('column'),
|
|
111
|
-
}),
|
|
112
|
-
|
|
113
|
-
allReals: customSchema.table('all_reals', {
|
|
114
|
-
columnAll: real('column_all').default(32).notNull(),
|
|
115
|
-
column: real('column'),
|
|
116
|
-
columnPrimary: real('column_primary').primaryKey().notNull(),
|
|
117
|
-
}),
|
|
118
|
-
|
|
119
|
-
allBigints: pgTable('all_bigints', {
|
|
120
|
-
columnAll: bigint('column_all', { mode: 'number' }).default(124).notNull(),
|
|
121
|
-
column: bigint('column', { mode: 'number' }),
|
|
122
|
-
}),
|
|
123
|
-
|
|
124
|
-
allBigserials: customSchema.table('all_bigserials', {
|
|
125
|
-
columnAll: bigserial('column_all', { mode: 'bigint' }).notNull(),
|
|
126
|
-
column: bigserial('column', { mode: 'bigint' }).notNull(),
|
|
127
|
-
}),
|
|
128
|
-
|
|
129
|
-
allIntervals: customSchema.table('all_intervals', {
|
|
130
|
-
columnAllConstrains: interval('column_all_constrains', {
|
|
131
|
-
fields: 'month',
|
|
132
|
-
})
|
|
133
|
-
.default('1 mon')
|
|
134
|
-
.notNull(),
|
|
135
|
-
columnMinToSec: interval('column_min_to_sec', {
|
|
136
|
-
fields: 'minute to second',
|
|
137
|
-
}),
|
|
138
|
-
columnWithoutFields: interval('column_without_fields').default('00:00:01').notNull(),
|
|
139
|
-
column: interval('column'),
|
|
140
|
-
column5: interval('column5', {
|
|
141
|
-
fields: 'minute to second',
|
|
142
|
-
precision: 3,
|
|
143
|
-
}),
|
|
144
|
-
column6: interval('column6'),
|
|
145
|
-
}),
|
|
146
|
-
|
|
147
|
-
allSerials: customSchema.table('all_serials', {
|
|
148
|
-
columnAll: serial('column_all').notNull(),
|
|
149
|
-
column: serial('column').notNull(),
|
|
150
|
-
}),
|
|
151
|
-
|
|
152
|
-
allTexts: customSchema.table(
|
|
153
|
-
'all_texts',
|
|
154
|
-
{
|
|
155
|
-
columnAll: text('column_all').default('text').notNull(),
|
|
156
|
-
column: text('columns').primaryKey(),
|
|
157
|
-
},
|
|
158
|
-
(t) => ({
|
|
159
|
-
cd: index('test').on(t.column),
|
|
160
|
-
}),
|
|
161
|
-
),
|
|
162
|
-
|
|
163
|
-
allBools: customSchema.table('all_bools', {
|
|
164
|
-
columnAll: boolean('column_all').default(true).notNull(),
|
|
165
|
-
column: boolean('column'),
|
|
166
|
-
}),
|
|
167
|
-
|
|
168
|
-
allVarchars: customSchema.table('all_varchars', {
|
|
169
|
-
columnAll: varchar('column_all').default('text').notNull(),
|
|
170
|
-
column: varchar('column', { length: 200 }),
|
|
171
|
-
}),
|
|
172
|
-
|
|
173
|
-
allTimes: customSchema.table('all_times', {
|
|
174
|
-
columnDateNow: time('column_date_now').defaultNow(),
|
|
175
|
-
columnAll: time('column_all').default('22:12:12').notNull(),
|
|
176
|
-
column: time('column'),
|
|
177
|
-
}),
|
|
178
|
-
|
|
179
|
-
allChars: customSchema.table('all_chars', {
|
|
180
|
-
columnAll: char('column_all', { length: 1 }).default('text').notNull(),
|
|
181
|
-
column: char('column', { length: 1 }),
|
|
182
|
-
}),
|
|
183
|
-
|
|
184
|
-
allDoublePrecision: customSchema.table('all_double_precision', {
|
|
185
|
-
columnAll: doublePrecision('column_all').default(33.2).notNull(),
|
|
186
|
-
column: doublePrecision('column'),
|
|
187
|
-
}),
|
|
188
|
-
|
|
189
|
-
allJsonb: customSchema.table('all_jsonb', {
|
|
190
|
-
columnDefaultObject: jsonb('column_default_object').default({ hello: 'world world' }).notNull(),
|
|
191
|
-
columnDefaultArray: jsonb('column_default_array').default({
|
|
192
|
-
hello: { 'world world': ['foo', 'bar'] },
|
|
193
|
-
}),
|
|
194
|
-
column: jsonb('column'),
|
|
195
|
-
}),
|
|
196
|
-
|
|
197
|
-
allJson: customSchema.table('all_json', {
|
|
198
|
-
columnDefaultObject: json('column_default_object').default({ hello: 'world world' }).notNull(),
|
|
199
|
-
columnDefaultArray: json('column_default_array').default({
|
|
200
|
-
hello: { 'world world': ['foo', 'bar'] },
|
|
201
|
-
foo: 'bar',
|
|
202
|
-
fe: 23,
|
|
203
|
-
}),
|
|
204
|
-
column: json('column'),
|
|
205
|
-
}),
|
|
206
|
-
|
|
207
|
-
allIntegers: customSchema.table('all_integers', {
|
|
208
|
-
columnAll: integer('column_all').primaryKey(),
|
|
209
|
-
column: integer('column'),
|
|
210
|
-
columnPrimary: integer('column_primary'),
|
|
211
|
-
}),
|
|
212
|
-
|
|
213
|
-
allNumerics: customSchema.table('all_numerics', {
|
|
214
|
-
columnAll: numeric('column_all', { precision: 1, scale: 1 }).default('32').notNull(),
|
|
215
|
-
column: numeric('column'),
|
|
216
|
-
columnPrimary: numeric('column_primary').primaryKey().notNull(),
|
|
217
|
-
}),
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema1, [], false, [
|
|
221
|
-
'public',
|
|
222
|
-
'schemass',
|
|
223
|
-
]);
|
|
224
|
-
expect(statements.length).toBe(0);
|
|
225
|
-
},
|
|
226
|
-
|
|
227
|
-
async addBasicIndexes() {
|
|
228
|
-
const client = new PGlite();
|
|
229
|
-
|
|
230
|
-
const schema1 = {
|
|
231
|
-
users: pgTable('users', {
|
|
232
|
-
id: serial('id').primaryKey(),
|
|
233
|
-
name: text('name'),
|
|
234
|
-
}),
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
const schema2 = {
|
|
238
|
-
users: pgTable(
|
|
239
|
-
'users',
|
|
240
|
-
{
|
|
241
|
-
id: serial('id').primaryKey(),
|
|
242
|
-
name: text('name'),
|
|
243
|
-
},
|
|
244
|
-
(t) => ({
|
|
245
|
-
indx: index()
|
|
246
|
-
.on(t.name.desc(), t.id.asc().nullsLast())
|
|
247
|
-
.with({ fillfactor: 70 })
|
|
248
|
-
.where(sql`select 1`),
|
|
249
|
-
indx1: index('indx1')
|
|
250
|
-
.using('hash', t.name.desc(), sql`${t.name}`)
|
|
251
|
-
.with({ fillfactor: 70 }),
|
|
252
|
-
}),
|
|
253
|
-
),
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
257
|
-
expect(statements.length).toBe(2);
|
|
258
|
-
expect(statements[0]).toStrictEqual({
|
|
259
|
-
schema: '',
|
|
260
|
-
tableName: 'users',
|
|
261
|
-
type: 'create_index_pg',
|
|
262
|
-
data: {
|
|
263
|
-
columns: [
|
|
264
|
-
{
|
|
265
|
-
asc: false,
|
|
266
|
-
expression: 'name',
|
|
267
|
-
isExpression: false,
|
|
268
|
-
nulls: 'last',
|
|
269
|
-
opclass: undefined,
|
|
270
|
-
},
|
|
271
|
-
{
|
|
272
|
-
asc: true,
|
|
273
|
-
expression: 'id',
|
|
274
|
-
isExpression: false,
|
|
275
|
-
nulls: 'last',
|
|
276
|
-
opclass: undefined,
|
|
277
|
-
},
|
|
278
|
-
],
|
|
279
|
-
concurrently: false,
|
|
280
|
-
isUnique: false,
|
|
281
|
-
method: 'btree',
|
|
282
|
-
name: 'users_name_id_index',
|
|
283
|
-
where: 'select 1',
|
|
284
|
-
with: {
|
|
285
|
-
fillfactor: 70,
|
|
286
|
-
},
|
|
287
|
-
},
|
|
288
|
-
});
|
|
289
|
-
expect(statements[1]).toStrictEqual({
|
|
290
|
-
schema: '',
|
|
291
|
-
tableName: 'users',
|
|
292
|
-
type: 'create_index_pg',
|
|
293
|
-
data: {
|
|
294
|
-
columns: [
|
|
295
|
-
{
|
|
296
|
-
asc: false,
|
|
297
|
-
expression: 'name',
|
|
298
|
-
isExpression: false,
|
|
299
|
-
nulls: 'last',
|
|
300
|
-
opclass: undefined,
|
|
301
|
-
},
|
|
302
|
-
{
|
|
303
|
-
asc: true,
|
|
304
|
-
expression: '"name"',
|
|
305
|
-
isExpression: true,
|
|
306
|
-
nulls: 'last',
|
|
307
|
-
},
|
|
308
|
-
],
|
|
309
|
-
concurrently: false,
|
|
310
|
-
isUnique: false,
|
|
311
|
-
method: 'hash',
|
|
312
|
-
name: 'indx1',
|
|
313
|
-
where: undefined,
|
|
314
|
-
with: {
|
|
315
|
-
fillfactor: 70,
|
|
316
|
-
},
|
|
317
|
-
},
|
|
318
|
-
});
|
|
319
|
-
expect(sqlStatements.length).toBe(2);
|
|
320
|
-
expect(sqlStatements[0]).toBe(
|
|
321
|
-
`CREATE INDEX "users_name_id_index" ON "users" USING btree ("name" DESC NULLS LAST,"id") WITH (fillfactor=70) WHERE select 1;`,
|
|
322
|
-
);
|
|
323
|
-
expect(sqlStatements[1]).toBe(
|
|
324
|
-
`CREATE INDEX "indx1" ON "users" USING hash ("name" DESC NULLS LAST,"name") WITH (fillfactor=70);`,
|
|
325
|
-
);
|
|
326
|
-
},
|
|
327
|
-
|
|
328
|
-
async addGeneratedColumn() {
|
|
329
|
-
const client = new PGlite();
|
|
330
|
-
|
|
331
|
-
const schema1 = {
|
|
332
|
-
users: pgTable('users', {
|
|
333
|
-
id: integer('id'),
|
|
334
|
-
id2: integer('id2'),
|
|
335
|
-
name: text('name'),
|
|
336
|
-
}),
|
|
337
|
-
};
|
|
338
|
-
const schema2 = {
|
|
339
|
-
users: pgTable('users', {
|
|
340
|
-
id: integer('id'),
|
|
341
|
-
id2: integer('id2'),
|
|
342
|
-
name: text('name'),
|
|
343
|
-
generatedName: text('gen_name').generatedAlwaysAs((): SQL => sql`${schema2.users.name}`),
|
|
344
|
-
}),
|
|
345
|
-
};
|
|
346
|
-
|
|
347
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
348
|
-
|
|
349
|
-
expect(statements).toStrictEqual([
|
|
350
|
-
{
|
|
351
|
-
column: {
|
|
352
|
-
generated: {
|
|
353
|
-
as: '"users"."name"',
|
|
354
|
-
type: 'stored',
|
|
355
|
-
},
|
|
356
|
-
name: 'gen_name',
|
|
357
|
-
notNull: false,
|
|
358
|
-
primaryKey: false,
|
|
359
|
-
type: 'text',
|
|
360
|
-
},
|
|
361
|
-
schema: '',
|
|
362
|
-
tableName: 'users',
|
|
363
|
-
type: 'alter_table_add_column',
|
|
364
|
-
},
|
|
365
|
-
]);
|
|
366
|
-
expect(sqlStatements).toStrictEqual([
|
|
367
|
-
'ALTER TABLE "users" ADD COLUMN "gen_name" text GENERATED ALWAYS AS ("users"."name") STORED;',
|
|
368
|
-
]);
|
|
369
|
-
|
|
370
|
-
// for (const st of sqlStatements) {
|
|
371
|
-
// await client.query(st);
|
|
372
|
-
// }
|
|
373
|
-
},
|
|
374
|
-
|
|
375
|
-
async addGeneratedToColumn() {
|
|
376
|
-
const client = new PGlite();
|
|
377
|
-
|
|
378
|
-
const schema1 = {
|
|
379
|
-
users: pgTable('users', {
|
|
380
|
-
id: integer('id'),
|
|
381
|
-
id2: integer('id2'),
|
|
382
|
-
name: text('name'),
|
|
383
|
-
generatedName: text('gen_name'),
|
|
384
|
-
}),
|
|
385
|
-
};
|
|
386
|
-
const schema2 = {
|
|
387
|
-
users: pgTable('users', {
|
|
388
|
-
id: integer('id'),
|
|
389
|
-
id2: integer('id2'),
|
|
390
|
-
name: text('name'),
|
|
391
|
-
generatedName: text('gen_name').generatedAlwaysAs((): SQL => sql`${schema2.users.name}`),
|
|
392
|
-
}),
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
396
|
-
|
|
397
|
-
expect(statements).toStrictEqual([
|
|
398
|
-
{
|
|
399
|
-
columnAutoIncrement: undefined,
|
|
400
|
-
columnDefault: undefined,
|
|
401
|
-
columnGenerated: {
|
|
402
|
-
as: '"users"."name"',
|
|
403
|
-
type: 'stored',
|
|
404
|
-
},
|
|
405
|
-
columnName: 'gen_name',
|
|
406
|
-
columnNotNull: false,
|
|
407
|
-
columnOnUpdate: undefined,
|
|
408
|
-
columnPk: false,
|
|
409
|
-
newDataType: 'text',
|
|
410
|
-
schema: '',
|
|
411
|
-
tableName: 'users',
|
|
412
|
-
type: 'alter_table_alter_column_set_generated',
|
|
413
|
-
},
|
|
414
|
-
]);
|
|
415
|
-
expect(sqlStatements).toStrictEqual([
|
|
416
|
-
'ALTER TABLE "users" drop column "gen_name";',
|
|
417
|
-
'ALTER TABLE "users" ADD COLUMN "gen_name" text GENERATED ALWAYS AS ("users"."name") STORED;',
|
|
418
|
-
]);
|
|
419
|
-
|
|
420
|
-
// for (const st of sqlStatements) {
|
|
421
|
-
// await client.query(st);
|
|
422
|
-
// }
|
|
423
|
-
},
|
|
424
|
-
|
|
425
|
-
async dropGeneratedConstraint() {
|
|
426
|
-
const client = new PGlite();
|
|
427
|
-
|
|
428
|
-
const schema1 = {
|
|
429
|
-
users: pgTable('users', {
|
|
430
|
-
id: integer('id'),
|
|
431
|
-
id2: integer('id2'),
|
|
432
|
-
name: text('name'),
|
|
433
|
-
generatedName: text('gen_name').generatedAlwaysAs((): SQL => sql`${schema1.users.name}`),
|
|
434
|
-
}),
|
|
435
|
-
};
|
|
436
|
-
const schema2 = {
|
|
437
|
-
users: pgTable('users', {
|
|
438
|
-
id: integer('id'),
|
|
439
|
-
id2: integer('id2'),
|
|
440
|
-
name: text('name'),
|
|
441
|
-
generatedName: text('gen_name'),
|
|
442
|
-
}),
|
|
443
|
-
};
|
|
444
|
-
|
|
445
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
446
|
-
|
|
447
|
-
expect(statements).toStrictEqual([
|
|
448
|
-
{
|
|
449
|
-
columnAutoIncrement: undefined,
|
|
450
|
-
columnDefault: undefined,
|
|
451
|
-
columnGenerated: undefined,
|
|
452
|
-
columnName: 'gen_name',
|
|
453
|
-
columnNotNull: false,
|
|
454
|
-
columnOnUpdate: undefined,
|
|
455
|
-
columnPk: false,
|
|
456
|
-
newDataType: 'text',
|
|
457
|
-
schema: '',
|
|
458
|
-
tableName: 'users',
|
|
459
|
-
type: 'alter_table_alter_column_drop_generated',
|
|
460
|
-
},
|
|
461
|
-
]);
|
|
462
|
-
expect(sqlStatements).toStrictEqual(['ALTER TABLE "users" ALTER COLUMN "gen_name" DROP EXPRESSION;']);
|
|
463
|
-
},
|
|
464
|
-
|
|
465
|
-
async alterGeneratedConstraint() {
|
|
466
|
-
const client = new PGlite();
|
|
467
|
-
|
|
468
|
-
const schema1 = {
|
|
469
|
-
users: pgTable('users', {
|
|
470
|
-
id: integer('id'),
|
|
471
|
-
id2: integer('id2'),
|
|
472
|
-
name: text('name'),
|
|
473
|
-
generatedName: text('gen_name').generatedAlwaysAs((): SQL => sql`${schema1.users.name}`),
|
|
474
|
-
}),
|
|
475
|
-
};
|
|
476
|
-
const schema2 = {
|
|
477
|
-
users: pgTable('users', {
|
|
478
|
-
id: integer('id'),
|
|
479
|
-
id2: integer('id2'),
|
|
480
|
-
name: text('name'),
|
|
481
|
-
generatedName: text('gen_name').generatedAlwaysAs((): SQL => sql`${schema2.users.name} || 'hello'`),
|
|
482
|
-
}),
|
|
483
|
-
};
|
|
484
|
-
|
|
485
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
486
|
-
|
|
487
|
-
expect(statements).toStrictEqual([]);
|
|
488
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
489
|
-
},
|
|
490
|
-
|
|
491
|
-
async createTableWithGeneratedConstraint() {
|
|
492
|
-
const client = new PGlite();
|
|
493
|
-
|
|
494
|
-
const schema1 = {};
|
|
495
|
-
const schema2 = {
|
|
496
|
-
users: pgTable('users', {
|
|
497
|
-
id: integer('id'),
|
|
498
|
-
id2: integer('id2'),
|
|
499
|
-
name: text('name'),
|
|
500
|
-
generatedName: text('gen_name').generatedAlwaysAs((): SQL => sql`${schema2.users.name} || 'hello'`),
|
|
501
|
-
}),
|
|
502
|
-
};
|
|
503
|
-
|
|
504
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
505
|
-
|
|
506
|
-
expect(statements).toStrictEqual([
|
|
507
|
-
{
|
|
508
|
-
columns: [
|
|
509
|
-
{
|
|
510
|
-
name: 'id',
|
|
511
|
-
notNull: false,
|
|
512
|
-
primaryKey: false,
|
|
513
|
-
type: 'integer',
|
|
514
|
-
},
|
|
515
|
-
{
|
|
516
|
-
name: 'id2',
|
|
517
|
-
notNull: false,
|
|
518
|
-
primaryKey: false,
|
|
519
|
-
type: 'integer',
|
|
520
|
-
},
|
|
521
|
-
{
|
|
522
|
-
name: 'name',
|
|
523
|
-
notNull: false,
|
|
524
|
-
primaryKey: false,
|
|
525
|
-
type: 'text',
|
|
526
|
-
},
|
|
527
|
-
{
|
|
528
|
-
generated: {
|
|
529
|
-
as: '"users"."name" || \'hello\'',
|
|
530
|
-
type: 'stored',
|
|
531
|
-
},
|
|
532
|
-
name: 'gen_name',
|
|
533
|
-
notNull: false,
|
|
534
|
-
primaryKey: false,
|
|
535
|
-
type: 'text',
|
|
536
|
-
},
|
|
537
|
-
],
|
|
538
|
-
compositePKs: [],
|
|
539
|
-
compositePkName: '',
|
|
540
|
-
isRLSEnabled: false,
|
|
541
|
-
schema: '',
|
|
542
|
-
tableName: 'users',
|
|
543
|
-
policies: [],
|
|
544
|
-
type: 'create_table',
|
|
545
|
-
uniqueConstraints: [],
|
|
546
|
-
checkConstraints: [],
|
|
547
|
-
},
|
|
548
|
-
]);
|
|
549
|
-
expect(sqlStatements).toStrictEqual([
|
|
550
|
-
'CREATE TABLE "users" (\n\t"id" integer,\n\t"id2" integer,\n\t"name" text,\n\t"gen_name" text GENERATED ALWAYS AS ("users"."name" || \'hello\') STORED\n);\n',
|
|
551
|
-
]);
|
|
552
|
-
},
|
|
553
|
-
|
|
554
|
-
async addBasicSequences() {
|
|
555
|
-
const client = new PGlite();
|
|
556
|
-
|
|
557
|
-
const schema1 = {
|
|
558
|
-
seq: pgSequence('my_seq', { startWith: 100 }),
|
|
559
|
-
};
|
|
560
|
-
|
|
561
|
-
const schema2 = {
|
|
562
|
-
seq: pgSequence('my_seq', { startWith: 100 }),
|
|
563
|
-
};
|
|
564
|
-
|
|
565
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
566
|
-
expect(statements.length).toBe(0);
|
|
567
|
-
},
|
|
568
|
-
|
|
569
|
-
async changeIndexFields() {
|
|
570
|
-
const client = new PGlite();
|
|
571
|
-
|
|
572
|
-
const schema1 = {
|
|
573
|
-
users: pgTable(
|
|
574
|
-
'users',
|
|
575
|
-
{
|
|
576
|
-
id: serial('id').primaryKey(),
|
|
577
|
-
name: text('name'),
|
|
578
|
-
},
|
|
579
|
-
(t) => ({
|
|
580
|
-
removeColumn: index('removeColumn').on(t.name, t.id),
|
|
581
|
-
addColumn: index('addColumn').on(t.name.desc()).with({ fillfactor: 70 }),
|
|
582
|
-
removeExpression: index('removeExpression')
|
|
583
|
-
.on(t.name.desc(), sql`name`)
|
|
584
|
-
.concurrently(),
|
|
585
|
-
addExpression: index('addExpression').on(t.id.desc()),
|
|
586
|
-
changeExpression: index('changeExpression').on(t.id.desc(), sql`name`),
|
|
587
|
-
changeName: index('changeName').on(t.name.desc(), t.id.asc().nullsLast()).with({ fillfactor: 70 }),
|
|
588
|
-
changeWith: index('changeWith').on(t.name).with({ fillfactor: 70 }),
|
|
589
|
-
changeUsing: index('changeUsing').on(t.name),
|
|
590
|
-
}),
|
|
591
|
-
),
|
|
592
|
-
};
|
|
593
|
-
|
|
594
|
-
const schema2 = {
|
|
595
|
-
users: pgTable(
|
|
596
|
-
'users',
|
|
597
|
-
{
|
|
598
|
-
id: serial('id').primaryKey(),
|
|
599
|
-
name: text('name'),
|
|
600
|
-
},
|
|
601
|
-
(t) => ({
|
|
602
|
-
removeColumn: index('removeColumn').on(t.name),
|
|
603
|
-
addColumn: index('addColumn').on(t.name.desc(), t.id.nullsLast()).with({ fillfactor: 70 }),
|
|
604
|
-
removeExpression: index('removeExpression').on(t.name.desc()).concurrently(),
|
|
605
|
-
addExpression: index('addExpression').on(t.id.desc()),
|
|
606
|
-
changeExpression: index('changeExpression').on(t.id.desc(), sql`name desc`),
|
|
607
|
-
changeName: index('newName')
|
|
608
|
-
.on(t.name.desc(), sql`name`)
|
|
609
|
-
.with({ fillfactor: 70 }),
|
|
610
|
-
changeWith: index('changeWith').on(t.name).with({ fillfactor: 90 }),
|
|
611
|
-
changeUsing: index('changeUsing').using('hash', t.name),
|
|
612
|
-
}),
|
|
613
|
-
),
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
617
|
-
|
|
618
|
-
expect(sqlStatements).toStrictEqual([
|
|
619
|
-
'DROP INDEX "changeName";',
|
|
620
|
-
'DROP INDEX "addColumn";',
|
|
621
|
-
'DROP INDEX "changeExpression";',
|
|
622
|
-
'DROP INDEX "changeUsing";',
|
|
623
|
-
'DROP INDEX "changeWith";',
|
|
624
|
-
'DROP INDEX "removeColumn";',
|
|
625
|
-
'DROP INDEX "removeExpression";',
|
|
626
|
-
'CREATE INDEX "newName" ON "users" USING btree ("name" DESC NULLS LAST,name) WITH (fillfactor=70);',
|
|
627
|
-
'CREATE INDEX "addColumn" ON "users" USING btree ("name" DESC NULLS LAST,"id") WITH (fillfactor=70);',
|
|
628
|
-
'CREATE INDEX "changeExpression" ON "users" USING btree ("id" DESC NULLS LAST,name desc);',
|
|
629
|
-
'CREATE INDEX "changeUsing" ON "users" USING hash ("name");',
|
|
630
|
-
'CREATE INDEX "changeWith" ON "users" USING btree ("name") WITH (fillfactor=90);',
|
|
631
|
-
'CREATE INDEX "removeColumn" ON "users" USING btree ("name");',
|
|
632
|
-
'CREATE INDEX CONCURRENTLY "removeExpression" ON "users" USING btree ("name" DESC NULLS LAST);',
|
|
633
|
-
]);
|
|
634
|
-
},
|
|
635
|
-
|
|
636
|
-
async dropIndex() {
|
|
637
|
-
const client = new PGlite();
|
|
638
|
-
|
|
639
|
-
const schema1 = {
|
|
640
|
-
users: pgTable(
|
|
641
|
-
'users',
|
|
642
|
-
{
|
|
643
|
-
id: serial('id').primaryKey(),
|
|
644
|
-
name: text('name'),
|
|
645
|
-
},
|
|
646
|
-
(t) => ({
|
|
647
|
-
indx: index().on(t.name.desc(), t.id.asc().nullsLast()).with({ fillfactor: 70 }),
|
|
648
|
-
}),
|
|
649
|
-
),
|
|
650
|
-
};
|
|
651
|
-
|
|
652
|
-
const schema2 = {
|
|
653
|
-
users: pgTable('users', {
|
|
654
|
-
id: serial('id').primaryKey(),
|
|
655
|
-
name: text('name'),
|
|
656
|
-
}),
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
660
|
-
|
|
661
|
-
expect(statements.length).toBe(1);
|
|
662
|
-
expect(statements[0]).toStrictEqual({
|
|
663
|
-
schema: '',
|
|
664
|
-
tableName: 'users',
|
|
665
|
-
type: 'drop_index',
|
|
666
|
-
data: 'users_name_id_index;name--false--last,,id--true--last;false;btree;{"fillfactor":"70"}',
|
|
667
|
-
});
|
|
668
|
-
|
|
669
|
-
expect(sqlStatements.length).toBe(1);
|
|
670
|
-
expect(sqlStatements[0]).toBe(`DROP INDEX "users_name_id_index";`);
|
|
671
|
-
},
|
|
672
|
-
|
|
673
|
-
async indexesToBeNotTriggered() {
|
|
674
|
-
const client = new PGlite();
|
|
675
|
-
|
|
676
|
-
const schema1 = {
|
|
677
|
-
users: pgTable(
|
|
678
|
-
'users',
|
|
679
|
-
{
|
|
680
|
-
id: serial('id').primaryKey(),
|
|
681
|
-
name: text('name'),
|
|
682
|
-
},
|
|
683
|
-
(t) => ({
|
|
684
|
-
indx: index('indx').on(t.name.desc()).concurrently(),
|
|
685
|
-
indx1: index('indx1')
|
|
686
|
-
.on(t.name.desc())
|
|
687
|
-
.where(sql`true`),
|
|
688
|
-
indx2: index('indx2')
|
|
689
|
-
.on(t.name.op('text_ops'))
|
|
690
|
-
.where(sql`true`),
|
|
691
|
-
indx3: index('indx3')
|
|
692
|
-
.on(sql`lower(name)`)
|
|
693
|
-
.where(sql`true`),
|
|
694
|
-
}),
|
|
695
|
-
),
|
|
696
|
-
};
|
|
697
|
-
|
|
698
|
-
const schema2 = {
|
|
699
|
-
users: pgTable(
|
|
700
|
-
'users',
|
|
701
|
-
{
|
|
702
|
-
id: serial('id').primaryKey(),
|
|
703
|
-
name: text('name'),
|
|
704
|
-
},
|
|
705
|
-
(t) => ({
|
|
706
|
-
indx: index('indx').on(t.name.desc()),
|
|
707
|
-
indx1: index('indx1')
|
|
708
|
-
.on(t.name.desc())
|
|
709
|
-
.where(sql`false`),
|
|
710
|
-
indx2: index('indx2')
|
|
711
|
-
.on(t.name.op('test'))
|
|
712
|
-
.where(sql`true`),
|
|
713
|
-
indx3: index('indx3')
|
|
714
|
-
.on(sql`lower(id)`)
|
|
715
|
-
.where(sql`true`),
|
|
716
|
-
}),
|
|
717
|
-
),
|
|
718
|
-
};
|
|
719
|
-
|
|
720
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
721
|
-
|
|
722
|
-
expect(statements.length).toBe(0);
|
|
723
|
-
},
|
|
724
|
-
|
|
725
|
-
async indexesTestCase1() {
|
|
726
|
-
const client = new PGlite();
|
|
727
|
-
|
|
728
|
-
const schema1 = {
|
|
729
|
-
users: pgTable(
|
|
730
|
-
'users',
|
|
731
|
-
{
|
|
732
|
-
id: uuid('id').defaultRandom().primaryKey(),
|
|
733
|
-
name: text('name').notNull(),
|
|
734
|
-
description: text('description'),
|
|
735
|
-
imageUrl: text('image_url'),
|
|
736
|
-
inStock: boolean('in_stock').default(true),
|
|
737
|
-
},
|
|
738
|
-
(t) => ({
|
|
739
|
-
indx: index().on(t.id.desc().nullsFirst()),
|
|
740
|
-
indx1: index('indx1').on(t.id, t.imageUrl),
|
|
741
|
-
indx2: index('indx4').on(t.id),
|
|
742
|
-
}),
|
|
743
|
-
),
|
|
744
|
-
};
|
|
745
|
-
|
|
746
|
-
const schema2 = {
|
|
747
|
-
users: pgTable(
|
|
748
|
-
'users',
|
|
749
|
-
{
|
|
750
|
-
id: uuid('id').defaultRandom().primaryKey(),
|
|
751
|
-
name: text('name').notNull(),
|
|
752
|
-
description: text('description'),
|
|
753
|
-
imageUrl: text('image_url'),
|
|
754
|
-
inStock: boolean('in_stock').default(true),
|
|
755
|
-
},
|
|
756
|
-
(t) => ({
|
|
757
|
-
indx: index().on(t.id.desc().nullsFirst()),
|
|
758
|
-
indx1: index('indx1').on(t.id, t.imageUrl),
|
|
759
|
-
indx2: index('indx4').on(t.id),
|
|
760
|
-
}),
|
|
761
|
-
),
|
|
762
|
-
};
|
|
763
|
-
|
|
764
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
765
|
-
|
|
766
|
-
expect(statements.length).toBe(0);
|
|
767
|
-
},
|
|
768
|
-
|
|
769
|
-
async addNotNull() {
|
|
770
|
-
const client = new PGlite();
|
|
771
|
-
|
|
772
|
-
const schema1 = {
|
|
773
|
-
users: pgTable(
|
|
774
|
-
'User',
|
|
775
|
-
{
|
|
776
|
-
id: text('id').primaryKey().notNull(),
|
|
777
|
-
name: text('name'),
|
|
778
|
-
username: text('username'),
|
|
779
|
-
gh_username: text('gh_username'),
|
|
780
|
-
email: text('email'),
|
|
781
|
-
emailVerified: timestamp('emailVerified', {
|
|
782
|
-
precision: 3,
|
|
783
|
-
mode: 'date',
|
|
784
|
-
}),
|
|
785
|
-
image: text('image'),
|
|
786
|
-
createdAt: timestamp('createdAt', { precision: 3, mode: 'date' })
|
|
787
|
-
.default(sql`CURRENT_TIMESTAMP`)
|
|
788
|
-
.notNull(),
|
|
789
|
-
updatedAt: timestamp('updatedAt', { precision: 3, mode: 'date' })
|
|
790
|
-
.notNull()
|
|
791
|
-
.$onUpdate(() => new Date()),
|
|
792
|
-
},
|
|
793
|
-
(table) => {
|
|
794
|
-
return {
|
|
795
|
-
emailKey: uniqueIndex('User_email_key').on(table.email),
|
|
796
|
-
};
|
|
797
|
-
},
|
|
798
|
-
),
|
|
799
|
-
};
|
|
800
|
-
|
|
801
|
-
const schema2 = {
|
|
802
|
-
users: pgTable(
|
|
803
|
-
'User',
|
|
804
|
-
{
|
|
805
|
-
id: text('id').primaryKey().notNull(),
|
|
806
|
-
name: text('name'),
|
|
807
|
-
username: text('username'),
|
|
808
|
-
gh_username: text('gh_username'),
|
|
809
|
-
email: text('email').notNull(),
|
|
810
|
-
emailVerified: timestamp('emailVerified', {
|
|
811
|
-
precision: 3,
|
|
812
|
-
mode: 'date',
|
|
813
|
-
}),
|
|
814
|
-
image: text('image'),
|
|
815
|
-
createdAt: timestamp('createdAt', { precision: 3, mode: 'date' })
|
|
816
|
-
.default(sql`CURRENT_TIMESTAMP`)
|
|
817
|
-
.notNull(),
|
|
818
|
-
updatedAt: timestamp('updatedAt', { precision: 3, mode: 'date' })
|
|
819
|
-
.notNull()
|
|
820
|
-
.$onUpdate(() => new Date()),
|
|
821
|
-
},
|
|
822
|
-
(table) => {
|
|
823
|
-
return {
|
|
824
|
-
emailKey: uniqueIndex('User_email_key').on(table.email),
|
|
825
|
-
};
|
|
826
|
-
},
|
|
827
|
-
),
|
|
828
|
-
};
|
|
829
|
-
|
|
830
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
831
|
-
const query = async (sql: string, params?: any[]) => {
|
|
832
|
-
const result = await client.query(sql, params ?? []);
|
|
833
|
-
return result.rows as any[];
|
|
834
|
-
};
|
|
835
|
-
|
|
836
|
-
const { statementsToExecute } = await pgSuggestions({ query }, statements);
|
|
837
|
-
|
|
838
|
-
expect(statementsToExecute).toStrictEqual(['ALTER TABLE "User" ALTER COLUMN "email" SET NOT NULL;']);
|
|
839
|
-
},
|
|
840
|
-
|
|
841
|
-
async addNotNullWithDataNoRollback() {
|
|
842
|
-
const client = new PGlite();
|
|
843
|
-
const db = drizzle(client);
|
|
844
|
-
|
|
845
|
-
const schema1 = {
|
|
846
|
-
users: pgTable(
|
|
847
|
-
'User',
|
|
848
|
-
{
|
|
849
|
-
id: text('id').primaryKey().notNull(),
|
|
850
|
-
name: text('name'),
|
|
851
|
-
username: text('username'),
|
|
852
|
-
gh_username: text('gh_username'),
|
|
853
|
-
email: text('email'),
|
|
854
|
-
emailVerified: timestamp('emailVerified', {
|
|
855
|
-
precision: 3,
|
|
856
|
-
mode: 'date',
|
|
857
|
-
}),
|
|
858
|
-
image: text('image'),
|
|
859
|
-
createdAt: timestamp('createdAt', { precision: 3, mode: 'date' })
|
|
860
|
-
.default(sql`CURRENT_TIMESTAMP`)
|
|
861
|
-
.notNull(),
|
|
862
|
-
updatedAt: timestamp('updatedAt', { precision: 3, mode: 'date' })
|
|
863
|
-
.notNull()
|
|
864
|
-
.$onUpdate(() => new Date()),
|
|
865
|
-
},
|
|
866
|
-
(table) => {
|
|
867
|
-
return {
|
|
868
|
-
emailKey: uniqueIndex('User_email_key').on(table.email),
|
|
869
|
-
};
|
|
870
|
-
},
|
|
871
|
-
),
|
|
872
|
-
};
|
|
873
|
-
|
|
874
|
-
const schema2 = {
|
|
875
|
-
users: pgTable(
|
|
876
|
-
'User',
|
|
877
|
-
{
|
|
878
|
-
id: text('id').primaryKey().notNull(),
|
|
879
|
-
name: text('name'),
|
|
880
|
-
username: text('username'),
|
|
881
|
-
gh_username: text('gh_username'),
|
|
882
|
-
email: text('email').notNull(),
|
|
883
|
-
emailVerified: timestamp('emailVerified', {
|
|
884
|
-
precision: 3,
|
|
885
|
-
mode: 'date',
|
|
886
|
-
}),
|
|
887
|
-
image: text('image'),
|
|
888
|
-
createdAt: timestamp('createdAt', { precision: 3, mode: 'date' })
|
|
889
|
-
.default(sql`CURRENT_TIMESTAMP`)
|
|
890
|
-
.notNull(),
|
|
891
|
-
updatedAt: timestamp('updatedAt', { precision: 3, mode: 'date' })
|
|
892
|
-
.notNull()
|
|
893
|
-
.$onUpdate(() => new Date()),
|
|
894
|
-
},
|
|
895
|
-
(table) => {
|
|
896
|
-
return {
|
|
897
|
-
emailKey: uniqueIndex('User_email_key').on(table.email),
|
|
898
|
-
};
|
|
899
|
-
},
|
|
900
|
-
),
|
|
901
|
-
};
|
|
902
|
-
|
|
903
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
904
|
-
const query = async (sql: string, params?: any[]) => {
|
|
905
|
-
const result = await client.query(sql, params ?? []);
|
|
906
|
-
return result.rows as any[];
|
|
907
|
-
};
|
|
908
|
-
|
|
909
|
-
await db.insert(schema1.users).values({ id: 'str', email: 'email@gmail' });
|
|
910
|
-
|
|
911
|
-
const { statementsToExecute, shouldAskForApprove } = await pgSuggestions({ query }, statements);
|
|
912
|
-
|
|
913
|
-
expect(statementsToExecute).toStrictEqual(['ALTER TABLE "User" ALTER COLUMN "email" SET NOT NULL;']);
|
|
914
|
-
|
|
915
|
-
expect(shouldAskForApprove).toBeFalsy();
|
|
916
|
-
},
|
|
917
|
-
|
|
918
|
-
async createCompositePrimaryKey() {
|
|
919
|
-
const client = new PGlite();
|
|
920
|
-
|
|
921
|
-
const schema1 = {};
|
|
922
|
-
|
|
923
|
-
const schema2 = {
|
|
924
|
-
table: pgTable('table', {
|
|
925
|
-
col1: integer('col1').notNull(),
|
|
926
|
-
col2: integer('col2').notNull(),
|
|
927
|
-
}, (t) => ({
|
|
928
|
-
pk: primaryKey({
|
|
929
|
-
columns: [t.col1, t.col2],
|
|
930
|
-
}),
|
|
931
|
-
})),
|
|
932
|
-
};
|
|
933
|
-
|
|
934
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
935
|
-
client,
|
|
936
|
-
schema1,
|
|
937
|
-
schema2,
|
|
938
|
-
[],
|
|
939
|
-
false,
|
|
940
|
-
['public'],
|
|
941
|
-
);
|
|
942
|
-
|
|
943
|
-
expect(statements).toStrictEqual([
|
|
944
|
-
{
|
|
945
|
-
type: 'create_table',
|
|
946
|
-
tableName: 'table',
|
|
947
|
-
schema: '',
|
|
948
|
-
compositePKs: ['col1,col2;table_col1_col2_pk'],
|
|
949
|
-
compositePkName: 'table_col1_col2_pk',
|
|
950
|
-
isRLSEnabled: false,
|
|
951
|
-
policies: [],
|
|
952
|
-
uniqueConstraints: [],
|
|
953
|
-
checkConstraints: [],
|
|
954
|
-
columns: [
|
|
955
|
-
{ name: 'col1', type: 'integer', primaryKey: false, notNull: true },
|
|
956
|
-
{ name: 'col2', type: 'integer', primaryKey: false, notNull: true },
|
|
957
|
-
],
|
|
958
|
-
},
|
|
959
|
-
]);
|
|
960
|
-
expect(sqlStatements).toStrictEqual([
|
|
961
|
-
'CREATE TABLE "table" (\n\t"col1" integer NOT NULL,\n\t"col2" integer NOT NULL,\n\tCONSTRAINT "table_col1_col2_pk" PRIMARY KEY("col1","col2")\n);\n',
|
|
962
|
-
]);
|
|
963
|
-
},
|
|
964
|
-
|
|
965
|
-
async renameTableWithCompositePrimaryKey() {
|
|
966
|
-
const client = new PGlite();
|
|
967
|
-
|
|
968
|
-
const productsCategoriesTable = (tableName: string) => {
|
|
969
|
-
return pgTable(tableName, {
|
|
970
|
-
productId: text('product_id').notNull(),
|
|
971
|
-
categoryId: text('category_id').notNull(),
|
|
972
|
-
}, (t) => ({
|
|
973
|
-
pk: primaryKey({
|
|
974
|
-
columns: [t.productId, t.categoryId],
|
|
975
|
-
}),
|
|
976
|
-
}));
|
|
977
|
-
};
|
|
978
|
-
|
|
979
|
-
const schema1 = {
|
|
980
|
-
table: productsCategoriesTable('products_categories'),
|
|
981
|
-
};
|
|
982
|
-
const schema2 = {
|
|
983
|
-
test: productsCategoriesTable('products_to_categories'),
|
|
984
|
-
};
|
|
985
|
-
|
|
986
|
-
const { sqlStatements } = await diffTestSchemasPush(
|
|
987
|
-
client,
|
|
988
|
-
schema1,
|
|
989
|
-
schema2,
|
|
990
|
-
['public.products_categories->public.products_to_categories'],
|
|
991
|
-
false,
|
|
992
|
-
['public'],
|
|
993
|
-
);
|
|
994
|
-
expect(sqlStatements).toStrictEqual([
|
|
995
|
-
'ALTER TABLE "products_categories" RENAME TO "products_to_categories";',
|
|
996
|
-
'ALTER TABLE "products_to_categories" DROP CONSTRAINT "products_categories_product_id_category_id_pk";',
|
|
997
|
-
'ALTER TABLE "products_to_categories" ADD CONSTRAINT "products_to_categories_product_id_category_id_pk" PRIMARY KEY("product_id","category_id");',
|
|
998
|
-
]);
|
|
999
|
-
},
|
|
1000
|
-
|
|
1001
|
-
// async addVectorIndexes() {
|
|
1002
|
-
// const client = new PGlite();
|
|
1003
|
-
|
|
1004
|
-
// const schema1 = {
|
|
1005
|
-
// users: pgTable("users", {
|
|
1006
|
-
// id: serial("id").primaryKey(),
|
|
1007
|
-
// name: vector("name", { dimensions: 3 }),
|
|
1008
|
-
// }),
|
|
1009
|
-
// };
|
|
1010
|
-
|
|
1011
|
-
// const schema2 = {
|
|
1012
|
-
// users: pgTable(
|
|
1013
|
-
// "users",
|
|
1014
|
-
// {
|
|
1015
|
-
// id: serial("id").primaryKey(),
|
|
1016
|
-
// embedding: vector("name", { dimensions: 3 }),
|
|
1017
|
-
// },
|
|
1018
|
-
// (t) => ({
|
|
1019
|
-
// indx2: index("vector_embedding_idx")
|
|
1020
|
-
// .using("hnsw", t.embedding.op("vector_ip_ops"))
|
|
1021
|
-
// .with({ m: 16, ef_construction: 64 }),
|
|
1022
|
-
// })
|
|
1023
|
-
// ),
|
|
1024
|
-
// };
|
|
1025
|
-
|
|
1026
|
-
// const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
1027
|
-
// client,
|
|
1028
|
-
// schema1,
|
|
1029
|
-
// schema2,
|
|
1030
|
-
// [],
|
|
1031
|
-
// false,
|
|
1032
|
-
// ["public"]
|
|
1033
|
-
// );
|
|
1034
|
-
// expect(statements.length).toBe(1);
|
|
1035
|
-
// expect(statements[0]).toStrictEqual({
|
|
1036
|
-
// schema: "",
|
|
1037
|
-
// tableName: "users",
|
|
1038
|
-
// type: "create_index",
|
|
1039
|
-
// data: 'vector_embedding_idx;name,true,last,vector_ip_ops;false;false;hnsw;undefined;{"m":16,"ef_construction":64}',
|
|
1040
|
-
// });
|
|
1041
|
-
// expect(sqlStatements.length).toBe(1);
|
|
1042
|
-
// expect(sqlStatements[0]).toBe(
|
|
1043
|
-
// `CREATE INDEX IF NOT EXISTS "vector_embedding_idx" ON "users" USING hnsw (name vector_ip_ops) WITH (m=16,ef_construction=64);`
|
|
1044
|
-
// );
|
|
1045
|
-
// },
|
|
1046
|
-
async case1() {
|
|
1047
|
-
// TODO: implement if needed
|
|
1048
|
-
expect(true).toBe(true);
|
|
1049
|
-
},
|
|
1050
|
-
};
|
|
1051
|
-
|
|
1052
|
-
run(pgSuite);
|
|
1053
|
-
|
|
1054
|
-
test('full sequence: no changes', async () => {
|
|
1055
|
-
const client = new PGlite();
|
|
1056
|
-
|
|
1057
|
-
const schema1 = {
|
|
1058
|
-
seq: pgSequence('my_seq', {
|
|
1059
|
-
startWith: 100,
|
|
1060
|
-
maxValue: 10000,
|
|
1061
|
-
minValue: 100,
|
|
1062
|
-
cycle: true,
|
|
1063
|
-
cache: 10,
|
|
1064
|
-
increment: 2,
|
|
1065
|
-
}),
|
|
1066
|
-
};
|
|
1067
|
-
|
|
1068
|
-
const schema2 = {
|
|
1069
|
-
seq: pgSequence('my_seq', {
|
|
1070
|
-
startWith: 100,
|
|
1071
|
-
maxValue: 10000,
|
|
1072
|
-
minValue: 100,
|
|
1073
|
-
cycle: true,
|
|
1074
|
-
cache: 10,
|
|
1075
|
-
increment: 2,
|
|
1076
|
-
}),
|
|
1077
|
-
};
|
|
1078
|
-
|
|
1079
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1080
|
-
|
|
1081
|
-
expect(statements.length).toBe(0);
|
|
1082
|
-
expect(sqlStatements.length).toBe(0);
|
|
1083
|
-
|
|
1084
|
-
for (const st of sqlStatements) {
|
|
1085
|
-
await client.query(st);
|
|
1086
|
-
}
|
|
1087
|
-
});
|
|
1088
|
-
|
|
1089
|
-
test('basic sequence: change fields', async () => {
|
|
1090
|
-
const client = new PGlite();
|
|
1091
|
-
|
|
1092
|
-
const schema1 = {
|
|
1093
|
-
seq: pgSequence('my_seq', {
|
|
1094
|
-
startWith: 100,
|
|
1095
|
-
maxValue: 10000,
|
|
1096
|
-
minValue: 100,
|
|
1097
|
-
cycle: true,
|
|
1098
|
-
cache: 10,
|
|
1099
|
-
increment: 2,
|
|
1100
|
-
}),
|
|
1101
|
-
};
|
|
1102
|
-
|
|
1103
|
-
const schema2 = {
|
|
1104
|
-
seq: pgSequence('my_seq', {
|
|
1105
|
-
startWith: 100,
|
|
1106
|
-
maxValue: 100000,
|
|
1107
|
-
minValue: 100,
|
|
1108
|
-
cycle: true,
|
|
1109
|
-
cache: 10,
|
|
1110
|
-
increment: 4,
|
|
1111
|
-
}),
|
|
1112
|
-
};
|
|
1113
|
-
|
|
1114
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1115
|
-
|
|
1116
|
-
expect(statements).toStrictEqual([
|
|
1117
|
-
{
|
|
1118
|
-
type: 'alter_sequence',
|
|
1119
|
-
schema: 'public',
|
|
1120
|
-
name: 'my_seq',
|
|
1121
|
-
values: {
|
|
1122
|
-
minValue: '100',
|
|
1123
|
-
maxValue: '100000',
|
|
1124
|
-
increment: '4',
|
|
1125
|
-
startWith: '100',
|
|
1126
|
-
cache: '10',
|
|
1127
|
-
cycle: true,
|
|
1128
|
-
},
|
|
1129
|
-
},
|
|
1130
|
-
]);
|
|
1131
|
-
expect(sqlStatements).toStrictEqual([
|
|
1132
|
-
'ALTER SEQUENCE "public"."my_seq" INCREMENT BY 4 MINVALUE 100 MAXVALUE 100000 START WITH 100 CACHE 10 CYCLE;',
|
|
1133
|
-
]);
|
|
1134
|
-
|
|
1135
|
-
for (const st of sqlStatements) {
|
|
1136
|
-
await client.query(st);
|
|
1137
|
-
}
|
|
1138
|
-
});
|
|
1139
|
-
|
|
1140
|
-
test('basic sequence: change name', async () => {
|
|
1141
|
-
const client = new PGlite();
|
|
1142
|
-
|
|
1143
|
-
const schema1 = {
|
|
1144
|
-
seq: pgSequence('my_seq', {
|
|
1145
|
-
startWith: 100,
|
|
1146
|
-
maxValue: 10000,
|
|
1147
|
-
minValue: 100,
|
|
1148
|
-
cycle: true,
|
|
1149
|
-
cache: 10,
|
|
1150
|
-
increment: 2,
|
|
1151
|
-
}),
|
|
1152
|
-
};
|
|
1153
|
-
|
|
1154
|
-
const schema2 = {
|
|
1155
|
-
seq: pgSequence('my_seq2', {
|
|
1156
|
-
startWith: 100,
|
|
1157
|
-
maxValue: 10000,
|
|
1158
|
-
minValue: 100,
|
|
1159
|
-
cycle: true,
|
|
1160
|
-
cache: 10,
|
|
1161
|
-
increment: 2,
|
|
1162
|
-
}),
|
|
1163
|
-
};
|
|
1164
|
-
|
|
1165
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
1166
|
-
client,
|
|
1167
|
-
schema1,
|
|
1168
|
-
schema2,
|
|
1169
|
-
['public.my_seq->public.my_seq2'],
|
|
1170
|
-
false,
|
|
1171
|
-
['public'],
|
|
1172
|
-
);
|
|
1173
|
-
|
|
1174
|
-
expect(statements).toStrictEqual([
|
|
1175
|
-
{
|
|
1176
|
-
nameFrom: 'my_seq',
|
|
1177
|
-
nameTo: 'my_seq2',
|
|
1178
|
-
schema: 'public',
|
|
1179
|
-
type: 'rename_sequence',
|
|
1180
|
-
},
|
|
1181
|
-
]);
|
|
1182
|
-
expect(sqlStatements).toStrictEqual(['ALTER SEQUENCE "public"."my_seq" RENAME TO "my_seq2";']);
|
|
1183
|
-
|
|
1184
|
-
for (const st of sqlStatements) {
|
|
1185
|
-
await client.query(st);
|
|
1186
|
-
}
|
|
1187
|
-
});
|
|
1188
|
-
|
|
1189
|
-
test('basic sequence: change name and fields', async () => {
|
|
1190
|
-
const client = new PGlite();
|
|
1191
|
-
|
|
1192
|
-
const schema1 = {
|
|
1193
|
-
seq: pgSequence('my_seq', {
|
|
1194
|
-
startWith: 100,
|
|
1195
|
-
maxValue: 10000,
|
|
1196
|
-
minValue: 100,
|
|
1197
|
-
cycle: true,
|
|
1198
|
-
cache: 10,
|
|
1199
|
-
increment: 2,
|
|
1200
|
-
}),
|
|
1201
|
-
};
|
|
1202
|
-
|
|
1203
|
-
const schema2 = {
|
|
1204
|
-
seq: pgSequence('my_seq2', {
|
|
1205
|
-
startWith: 100,
|
|
1206
|
-
maxValue: 10000,
|
|
1207
|
-
minValue: 100,
|
|
1208
|
-
cycle: true,
|
|
1209
|
-
cache: 10,
|
|
1210
|
-
increment: 4,
|
|
1211
|
-
}),
|
|
1212
|
-
};
|
|
1213
|
-
|
|
1214
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
1215
|
-
client,
|
|
1216
|
-
schema1,
|
|
1217
|
-
schema2,
|
|
1218
|
-
['public.my_seq->public.my_seq2'],
|
|
1219
|
-
false,
|
|
1220
|
-
['public'],
|
|
1221
|
-
);
|
|
1222
|
-
|
|
1223
|
-
expect(statements).toStrictEqual([
|
|
1224
|
-
{
|
|
1225
|
-
nameFrom: 'my_seq',
|
|
1226
|
-
nameTo: 'my_seq2',
|
|
1227
|
-
schema: 'public',
|
|
1228
|
-
type: 'rename_sequence',
|
|
1229
|
-
},
|
|
1230
|
-
{
|
|
1231
|
-
name: 'my_seq2',
|
|
1232
|
-
schema: 'public',
|
|
1233
|
-
type: 'alter_sequence',
|
|
1234
|
-
values: {
|
|
1235
|
-
cache: '10',
|
|
1236
|
-
cycle: true,
|
|
1237
|
-
increment: '4',
|
|
1238
|
-
maxValue: '10000',
|
|
1239
|
-
minValue: '100',
|
|
1240
|
-
startWith: '100',
|
|
1241
|
-
},
|
|
1242
|
-
},
|
|
1243
|
-
]);
|
|
1244
|
-
expect(sqlStatements).toStrictEqual([
|
|
1245
|
-
'ALTER SEQUENCE "public"."my_seq" RENAME TO "my_seq2";',
|
|
1246
|
-
'ALTER SEQUENCE "public"."my_seq2" INCREMENT BY 4 MINVALUE 100 MAXVALUE 10000 START WITH 100 CACHE 10 CYCLE;',
|
|
1247
|
-
]);
|
|
1248
|
-
|
|
1249
|
-
for (const st of sqlStatements) {
|
|
1250
|
-
await client.query(st);
|
|
1251
|
-
}
|
|
1252
|
-
});
|
|
1253
|
-
|
|
1254
|
-
// identity push tests
|
|
1255
|
-
test('create table: identity always/by default - no params', async () => {
|
|
1256
|
-
const client = new PGlite();
|
|
1257
|
-
|
|
1258
|
-
const schema1 = {};
|
|
1259
|
-
|
|
1260
|
-
const schema2 = {
|
|
1261
|
-
users: pgTable('users', {
|
|
1262
|
-
id: integer('id').generatedByDefaultAsIdentity(),
|
|
1263
|
-
id1: bigint('id1', { mode: 'number' }).generatedByDefaultAsIdentity(),
|
|
1264
|
-
id2: smallint('id2').generatedByDefaultAsIdentity(),
|
|
1265
|
-
}),
|
|
1266
|
-
};
|
|
1267
|
-
|
|
1268
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1269
|
-
|
|
1270
|
-
expect(statements).toStrictEqual([
|
|
1271
|
-
{
|
|
1272
|
-
columns: [
|
|
1273
|
-
{
|
|
1274
|
-
identity: 'users_id_seq;byDefault;1;2147483647;1;1;1;false',
|
|
1275
|
-
name: 'id',
|
|
1276
|
-
notNull: true,
|
|
1277
|
-
primaryKey: false,
|
|
1278
|
-
type: 'integer',
|
|
1279
|
-
},
|
|
1280
|
-
{
|
|
1281
|
-
identity: 'users_id1_seq;byDefault;1;9223372036854775807;1;1;1;false',
|
|
1282
|
-
name: 'id1',
|
|
1283
|
-
notNull: true,
|
|
1284
|
-
primaryKey: false,
|
|
1285
|
-
type: 'bigint',
|
|
1286
|
-
},
|
|
1287
|
-
{
|
|
1288
|
-
identity: 'users_id2_seq;byDefault;1;32767;1;1;1;false',
|
|
1289
|
-
name: 'id2',
|
|
1290
|
-
notNull: true,
|
|
1291
|
-
primaryKey: false,
|
|
1292
|
-
type: 'smallint',
|
|
1293
|
-
},
|
|
1294
|
-
],
|
|
1295
|
-
compositePKs: [],
|
|
1296
|
-
compositePkName: '',
|
|
1297
|
-
schema: '',
|
|
1298
|
-
tableName: 'users',
|
|
1299
|
-
policies: [],
|
|
1300
|
-
type: 'create_table',
|
|
1301
|
-
uniqueConstraints: [],
|
|
1302
|
-
isRLSEnabled: false,
|
|
1303
|
-
checkConstraints: [],
|
|
1304
|
-
},
|
|
1305
|
-
]);
|
|
1306
|
-
expect(sqlStatements).toStrictEqual([
|
|
1307
|
-
'CREATE TABLE "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),\n\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 START WITH 1 CACHE 1)\n);\n',
|
|
1308
|
-
]);
|
|
1309
|
-
|
|
1310
|
-
for (const st of sqlStatements) {
|
|
1311
|
-
await client.query(st);
|
|
1312
|
-
}
|
|
1313
|
-
});
|
|
1314
|
-
|
|
1315
|
-
test('create table: identity always/by default - few params', async () => {
|
|
1316
|
-
const client = new PGlite();
|
|
1317
|
-
|
|
1318
|
-
const schema1 = {};
|
|
1319
|
-
|
|
1320
|
-
const schema2 = {
|
|
1321
|
-
users: pgTable('users', {
|
|
1322
|
-
id: integer('id').generatedByDefaultAsIdentity({ increment: 4 }),
|
|
1323
|
-
id1: bigint('id1', { mode: 'number' }).generatedByDefaultAsIdentity({
|
|
1324
|
-
startWith: 120,
|
|
1325
|
-
maxValue: 17000,
|
|
1326
|
-
}),
|
|
1327
|
-
id2: smallint('id2').generatedByDefaultAsIdentity({ cycle: true }),
|
|
1328
|
-
}),
|
|
1329
|
-
};
|
|
1330
|
-
|
|
1331
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1332
|
-
|
|
1333
|
-
expect(statements).toStrictEqual([
|
|
1334
|
-
{
|
|
1335
|
-
columns: [
|
|
1336
|
-
{
|
|
1337
|
-
identity: 'users_id_seq;byDefault;1;2147483647;4;1;1;false',
|
|
1338
|
-
name: 'id',
|
|
1339
|
-
notNull: true,
|
|
1340
|
-
primaryKey: false,
|
|
1341
|
-
type: 'integer',
|
|
1342
|
-
},
|
|
1343
|
-
{
|
|
1344
|
-
identity: 'users_id1_seq;byDefault;1;17000;1;120;1;false',
|
|
1345
|
-
name: 'id1',
|
|
1346
|
-
notNull: true,
|
|
1347
|
-
primaryKey: false,
|
|
1348
|
-
type: 'bigint',
|
|
1349
|
-
},
|
|
1350
|
-
{
|
|
1351
|
-
identity: 'users_id2_seq;byDefault;1;32767;1;1;1;true',
|
|
1352
|
-
name: 'id2',
|
|
1353
|
-
notNull: true,
|
|
1354
|
-
primaryKey: false,
|
|
1355
|
-
type: 'smallint',
|
|
1356
|
-
},
|
|
1357
|
-
],
|
|
1358
|
-
compositePKs: [],
|
|
1359
|
-
compositePkName: '',
|
|
1360
|
-
policies: [],
|
|
1361
|
-
schema: '',
|
|
1362
|
-
tableName: 'users',
|
|
1363
|
-
type: 'create_table',
|
|
1364
|
-
isRLSEnabled: false,
|
|
1365
|
-
uniqueConstraints: [],
|
|
1366
|
-
checkConstraints: [],
|
|
1367
|
-
},
|
|
1368
|
-
]);
|
|
1369
|
-
expect(sqlStatements).toStrictEqual([
|
|
1370
|
-
'CREATE TABLE "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 4 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),\n\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 17000 START WITH 120 CACHE 1),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 START WITH 1 CACHE 1 CYCLE)\n);\n',
|
|
1371
|
-
]);
|
|
1372
|
-
|
|
1373
|
-
for (const st of sqlStatements) {
|
|
1374
|
-
await client.query(st);
|
|
1375
|
-
}
|
|
1376
|
-
});
|
|
1377
|
-
|
|
1378
|
-
test('create table: identity always/by default - all params', async () => {
|
|
1379
|
-
const client = new PGlite();
|
|
1380
|
-
|
|
1381
|
-
const schema1 = {};
|
|
1382
|
-
|
|
1383
|
-
const schema2 = {
|
|
1384
|
-
users: pgTable('users', {
|
|
1385
|
-
id: integer('id').generatedByDefaultAsIdentity({
|
|
1386
|
-
increment: 4,
|
|
1387
|
-
minValue: 100,
|
|
1388
|
-
}),
|
|
1389
|
-
id1: bigint('id1', { mode: 'number' }).generatedByDefaultAsIdentity({
|
|
1390
|
-
startWith: 120,
|
|
1391
|
-
maxValue: 17000,
|
|
1392
|
-
increment: 3,
|
|
1393
|
-
cycle: true,
|
|
1394
|
-
cache: 100,
|
|
1395
|
-
}),
|
|
1396
|
-
id2: smallint('id2').generatedByDefaultAsIdentity({ cycle: true }),
|
|
1397
|
-
}),
|
|
1398
|
-
};
|
|
1399
|
-
|
|
1400
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1401
|
-
|
|
1402
|
-
expect(statements).toStrictEqual([
|
|
1403
|
-
{
|
|
1404
|
-
columns: [
|
|
1405
|
-
{
|
|
1406
|
-
identity: 'users_id_seq;byDefault;100;2147483647;4;100;1;false',
|
|
1407
|
-
name: 'id',
|
|
1408
|
-
notNull: true,
|
|
1409
|
-
primaryKey: false,
|
|
1410
|
-
type: 'integer',
|
|
1411
|
-
},
|
|
1412
|
-
{
|
|
1413
|
-
identity: 'users_id1_seq;byDefault;1;17000;3;120;100;true',
|
|
1414
|
-
name: 'id1',
|
|
1415
|
-
notNull: true,
|
|
1416
|
-
primaryKey: false,
|
|
1417
|
-
type: 'bigint',
|
|
1418
|
-
},
|
|
1419
|
-
{
|
|
1420
|
-
identity: 'users_id2_seq;byDefault;1;32767;1;1;1;true',
|
|
1421
|
-
name: 'id2',
|
|
1422
|
-
notNull: true,
|
|
1423
|
-
primaryKey: false,
|
|
1424
|
-
type: 'smallint',
|
|
1425
|
-
},
|
|
1426
|
-
],
|
|
1427
|
-
compositePKs: [],
|
|
1428
|
-
compositePkName: '',
|
|
1429
|
-
schema: '',
|
|
1430
|
-
tableName: 'users',
|
|
1431
|
-
type: 'create_table',
|
|
1432
|
-
policies: [],
|
|
1433
|
-
isRLSEnabled: false,
|
|
1434
|
-
uniqueConstraints: [],
|
|
1435
|
-
checkConstraints: [],
|
|
1436
|
-
},
|
|
1437
|
-
]);
|
|
1438
|
-
expect(sqlStatements).toStrictEqual([
|
|
1439
|
-
'CREATE TABLE "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 4 MINVALUE 100 MAXVALUE 2147483647 START WITH 100 CACHE 1),\n\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 3 MINVALUE 1 MAXVALUE 17000 START WITH 120 CACHE 100 CYCLE),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 START WITH 1 CACHE 1 CYCLE)\n);\n',
|
|
1440
|
-
]);
|
|
1441
|
-
|
|
1442
|
-
for (const st of sqlStatements) {
|
|
1443
|
-
await client.query(st);
|
|
1444
|
-
}
|
|
1445
|
-
});
|
|
1446
|
-
|
|
1447
|
-
test('no diff: identity always/by default - no params', async () => {
|
|
1448
|
-
const client = new PGlite();
|
|
1449
|
-
|
|
1450
|
-
const schema1 = {
|
|
1451
|
-
users: pgTable('users', {
|
|
1452
|
-
id: integer('id').generatedByDefaultAsIdentity(),
|
|
1453
|
-
id2: integer('id2').generatedAlwaysAsIdentity(),
|
|
1454
|
-
}),
|
|
1455
|
-
};
|
|
1456
|
-
|
|
1457
|
-
const schema2 = {
|
|
1458
|
-
users: pgTable('users', {
|
|
1459
|
-
id: integer('id').generatedByDefaultAsIdentity(),
|
|
1460
|
-
id2: integer('id2').generatedAlwaysAsIdentity(),
|
|
1461
|
-
}),
|
|
1462
|
-
};
|
|
1463
|
-
|
|
1464
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1465
|
-
|
|
1466
|
-
expect(statements).toStrictEqual([]);
|
|
1467
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
1468
|
-
});
|
|
1469
|
-
|
|
1470
|
-
test('no diff: identity always/by default - few params', async () => {
|
|
1471
|
-
const client = new PGlite();
|
|
1472
|
-
|
|
1473
|
-
const schema1 = {
|
|
1474
|
-
users: pgTable('users', {
|
|
1475
|
-
id: integer('id').generatedByDefaultAsIdentity({
|
|
1476
|
-
name: 'custom_name',
|
|
1477
|
-
}),
|
|
1478
|
-
id2: integer('id2').generatedAlwaysAsIdentity({
|
|
1479
|
-
increment: 1,
|
|
1480
|
-
startWith: 3,
|
|
1481
|
-
}),
|
|
1482
|
-
}),
|
|
1483
|
-
};
|
|
1484
|
-
|
|
1485
|
-
const schema2 = {
|
|
1486
|
-
users: pgTable('users', {
|
|
1487
|
-
id: integer('id').generatedByDefaultAsIdentity({
|
|
1488
|
-
name: 'custom_name',
|
|
1489
|
-
}),
|
|
1490
|
-
id2: integer('id2').generatedAlwaysAsIdentity({
|
|
1491
|
-
increment: 1,
|
|
1492
|
-
startWith: 3,
|
|
1493
|
-
}),
|
|
1494
|
-
}),
|
|
1495
|
-
};
|
|
1496
|
-
|
|
1497
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1498
|
-
|
|
1499
|
-
expect(statements).toStrictEqual([]);
|
|
1500
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
1501
|
-
});
|
|
1502
|
-
|
|
1503
|
-
test('no diff: identity always/by default - all params', async () => {
|
|
1504
|
-
const client = new PGlite();
|
|
1505
|
-
|
|
1506
|
-
const schema1 = {
|
|
1507
|
-
users: pgTable('users', {
|
|
1508
|
-
id: integer('id').generatedByDefaultAsIdentity({
|
|
1509
|
-
name: 'custom_name',
|
|
1510
|
-
startWith: 10,
|
|
1511
|
-
minValue: 10,
|
|
1512
|
-
maxValue: 1000,
|
|
1513
|
-
cycle: true,
|
|
1514
|
-
cache: 10,
|
|
1515
|
-
increment: 2,
|
|
1516
|
-
}),
|
|
1517
|
-
id2: integer('id2').generatedAlwaysAsIdentity({
|
|
1518
|
-
startWith: 10,
|
|
1519
|
-
minValue: 10,
|
|
1520
|
-
maxValue: 1000,
|
|
1521
|
-
cycle: true,
|
|
1522
|
-
cache: 10,
|
|
1523
|
-
increment: 2,
|
|
1524
|
-
}),
|
|
1525
|
-
}),
|
|
1526
|
-
};
|
|
1527
|
-
|
|
1528
|
-
const schema2 = {
|
|
1529
|
-
users: pgTable('users', {
|
|
1530
|
-
id: integer('id').generatedByDefaultAsIdentity({
|
|
1531
|
-
name: 'custom_name',
|
|
1532
|
-
startWith: 10,
|
|
1533
|
-
minValue: 10,
|
|
1534
|
-
maxValue: 1000,
|
|
1535
|
-
cycle: true,
|
|
1536
|
-
cache: 10,
|
|
1537
|
-
increment: 2,
|
|
1538
|
-
}),
|
|
1539
|
-
id2: integer('id2').generatedAlwaysAsIdentity({
|
|
1540
|
-
startWith: 10,
|
|
1541
|
-
minValue: 10,
|
|
1542
|
-
maxValue: 1000,
|
|
1543
|
-
cycle: true,
|
|
1544
|
-
cache: 10,
|
|
1545
|
-
increment: 2,
|
|
1546
|
-
}),
|
|
1547
|
-
}),
|
|
1548
|
-
};
|
|
1549
|
-
|
|
1550
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1551
|
-
|
|
1552
|
-
expect(statements).toStrictEqual([]);
|
|
1553
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
1554
|
-
});
|
|
1555
|
-
|
|
1556
|
-
test('drop identity from a column - no params', async () => {
|
|
1557
|
-
const client = new PGlite();
|
|
1558
|
-
|
|
1559
|
-
const schema1 = {
|
|
1560
|
-
users: pgTable('users', {
|
|
1561
|
-
id: integer('id').generatedByDefaultAsIdentity(),
|
|
1562
|
-
}),
|
|
1563
|
-
};
|
|
1564
|
-
|
|
1565
|
-
const schema2 = {
|
|
1566
|
-
users: pgTable('users', {
|
|
1567
|
-
id: integer('id'),
|
|
1568
|
-
}),
|
|
1569
|
-
};
|
|
1570
|
-
|
|
1571
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1572
|
-
|
|
1573
|
-
expect(statements).toStrictEqual([
|
|
1574
|
-
{
|
|
1575
|
-
columnName: 'id',
|
|
1576
|
-
schema: '',
|
|
1577
|
-
tableName: 'users',
|
|
1578
|
-
type: 'alter_table_alter_column_drop_identity',
|
|
1579
|
-
},
|
|
1580
|
-
]);
|
|
1581
|
-
expect(sqlStatements).toStrictEqual([`ALTER TABLE \"users\" ALTER COLUMN \"id\" DROP IDENTITY;`]);
|
|
1582
|
-
|
|
1583
|
-
for (const st of sqlStatements) {
|
|
1584
|
-
await client.query(st);
|
|
1585
|
-
}
|
|
1586
|
-
});
|
|
1587
|
-
|
|
1588
|
-
test('drop identity from a column - few params', async () => {
|
|
1589
|
-
const client = new PGlite();
|
|
1590
|
-
|
|
1591
|
-
const schema1 = {
|
|
1592
|
-
users: pgTable('users', {
|
|
1593
|
-
id: integer('id').generatedByDefaultAsIdentity({ name: 'custom_name' }),
|
|
1594
|
-
id1: integer('id1').generatedByDefaultAsIdentity({
|
|
1595
|
-
name: 'custom_name1',
|
|
1596
|
-
increment: 4,
|
|
1597
|
-
}),
|
|
1598
|
-
id2: integer('id2').generatedAlwaysAsIdentity({
|
|
1599
|
-
name: 'custom_name2',
|
|
1600
|
-
increment: 4,
|
|
1601
|
-
}),
|
|
1602
|
-
}),
|
|
1603
|
-
};
|
|
1604
|
-
|
|
1605
|
-
const schema2 = {
|
|
1606
|
-
users: pgTable('users', {
|
|
1607
|
-
id: integer('id'),
|
|
1608
|
-
id1: integer('id1'),
|
|
1609
|
-
id2: integer('id2'),
|
|
1610
|
-
}),
|
|
1611
|
-
};
|
|
1612
|
-
|
|
1613
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1614
|
-
|
|
1615
|
-
expect(statements).toStrictEqual([
|
|
1616
|
-
{
|
|
1617
|
-
columnName: 'id',
|
|
1618
|
-
schema: '',
|
|
1619
|
-
tableName: 'users',
|
|
1620
|
-
type: 'alter_table_alter_column_drop_identity',
|
|
1621
|
-
},
|
|
1622
|
-
{
|
|
1623
|
-
columnName: 'id1',
|
|
1624
|
-
schema: '',
|
|
1625
|
-
tableName: 'users',
|
|
1626
|
-
type: 'alter_table_alter_column_drop_identity',
|
|
1627
|
-
},
|
|
1628
|
-
{
|
|
1629
|
-
columnName: 'id2',
|
|
1630
|
-
schema: '',
|
|
1631
|
-
tableName: 'users',
|
|
1632
|
-
type: 'alter_table_alter_column_drop_identity',
|
|
1633
|
-
},
|
|
1634
|
-
]);
|
|
1635
|
-
expect(sqlStatements).toStrictEqual([
|
|
1636
|
-
`ALTER TABLE \"users\" ALTER COLUMN \"id\" DROP IDENTITY;`,
|
|
1637
|
-
'ALTER TABLE "users" ALTER COLUMN "id1" DROP IDENTITY;',
|
|
1638
|
-
'ALTER TABLE "users" ALTER COLUMN "id2" DROP IDENTITY;',
|
|
1639
|
-
]);
|
|
1640
|
-
|
|
1641
|
-
for (const st of sqlStatements) {
|
|
1642
|
-
await client.query(st);
|
|
1643
|
-
}
|
|
1644
|
-
});
|
|
1645
|
-
|
|
1646
|
-
test('drop identity from a column - all params', async () => {
|
|
1647
|
-
const client = new PGlite();
|
|
1648
|
-
|
|
1649
|
-
const schema1 = {
|
|
1650
|
-
users: pgTable('users', {
|
|
1651
|
-
id: integer('id').generatedByDefaultAsIdentity(),
|
|
1652
|
-
id1: integer('id1').generatedByDefaultAsIdentity({
|
|
1653
|
-
name: 'custom_name1',
|
|
1654
|
-
startWith: 10,
|
|
1655
|
-
minValue: 10,
|
|
1656
|
-
maxValue: 1000,
|
|
1657
|
-
cycle: true,
|
|
1658
|
-
cache: 10,
|
|
1659
|
-
increment: 2,
|
|
1660
|
-
}),
|
|
1661
|
-
id2: integer('id2').generatedAlwaysAsIdentity({
|
|
1662
|
-
name: 'custom_name2',
|
|
1663
|
-
startWith: 10,
|
|
1664
|
-
minValue: 10,
|
|
1665
|
-
maxValue: 1000,
|
|
1666
|
-
cycle: true,
|
|
1667
|
-
cache: 10,
|
|
1668
|
-
increment: 2,
|
|
1669
|
-
}),
|
|
1670
|
-
}),
|
|
1671
|
-
};
|
|
1672
|
-
|
|
1673
|
-
const schema2 = {
|
|
1674
|
-
users: pgTable('users', {
|
|
1675
|
-
id: integer('id'),
|
|
1676
|
-
id1: integer('id1'),
|
|
1677
|
-
id2: integer('id2'),
|
|
1678
|
-
}),
|
|
1679
|
-
};
|
|
1680
|
-
|
|
1681
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1682
|
-
|
|
1683
|
-
expect(statements).toStrictEqual([
|
|
1684
|
-
{
|
|
1685
|
-
columnName: 'id',
|
|
1686
|
-
schema: '',
|
|
1687
|
-
tableName: 'users',
|
|
1688
|
-
type: 'alter_table_alter_column_drop_identity',
|
|
1689
|
-
},
|
|
1690
|
-
{
|
|
1691
|
-
columnName: 'id1',
|
|
1692
|
-
schema: '',
|
|
1693
|
-
tableName: 'users',
|
|
1694
|
-
type: 'alter_table_alter_column_drop_identity',
|
|
1695
|
-
},
|
|
1696
|
-
{
|
|
1697
|
-
columnName: 'id2',
|
|
1698
|
-
schema: '',
|
|
1699
|
-
tableName: 'users',
|
|
1700
|
-
type: 'alter_table_alter_column_drop_identity',
|
|
1701
|
-
},
|
|
1702
|
-
]);
|
|
1703
|
-
expect(sqlStatements).toStrictEqual([
|
|
1704
|
-
`ALTER TABLE \"users\" ALTER COLUMN \"id\" DROP IDENTITY;`,
|
|
1705
|
-
'ALTER TABLE "users" ALTER COLUMN "id1" DROP IDENTITY;',
|
|
1706
|
-
'ALTER TABLE "users" ALTER COLUMN "id2" DROP IDENTITY;',
|
|
1707
|
-
]);
|
|
1708
|
-
|
|
1709
|
-
for (const st of sqlStatements) {
|
|
1710
|
-
await client.query(st);
|
|
1711
|
-
}
|
|
1712
|
-
});
|
|
1713
|
-
|
|
1714
|
-
test('alter identity from a column - no params', async () => {
|
|
1715
|
-
const client = new PGlite();
|
|
1716
|
-
|
|
1717
|
-
const schema1 = {
|
|
1718
|
-
users: pgTable('users', {
|
|
1719
|
-
id: integer('id').generatedByDefaultAsIdentity(),
|
|
1720
|
-
}),
|
|
1721
|
-
};
|
|
1722
|
-
|
|
1723
|
-
const schema2 = {
|
|
1724
|
-
users: pgTable('users', {
|
|
1725
|
-
id: integer('id').generatedByDefaultAsIdentity({ startWith: 100 }),
|
|
1726
|
-
}),
|
|
1727
|
-
};
|
|
1728
|
-
|
|
1729
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1730
|
-
|
|
1731
|
-
expect(statements).toStrictEqual([
|
|
1732
|
-
{
|
|
1733
|
-
columnName: 'id',
|
|
1734
|
-
identity: 'users_id_seq;byDefault;1;2147483647;1;100;1;false',
|
|
1735
|
-
oldIdentity: 'users_id_seq;byDefault;1;2147483647;1;1;1;false',
|
|
1736
|
-
schema: '',
|
|
1737
|
-
tableName: 'users',
|
|
1738
|
-
type: 'alter_table_alter_column_change_identity',
|
|
1739
|
-
},
|
|
1740
|
-
]);
|
|
1741
|
-
expect(sqlStatements).toStrictEqual(['ALTER TABLE "users" ALTER COLUMN "id" SET START WITH 100;']);
|
|
1742
|
-
|
|
1743
|
-
for (const st of sqlStatements) {
|
|
1744
|
-
await client.query(st);
|
|
1745
|
-
}
|
|
1746
|
-
});
|
|
1747
|
-
|
|
1748
|
-
test('alter identity from a column - few params', async () => {
|
|
1749
|
-
const client = new PGlite();
|
|
1750
|
-
|
|
1751
|
-
const schema1 = {
|
|
1752
|
-
users: pgTable('users', {
|
|
1753
|
-
id: integer('id').generatedByDefaultAsIdentity({ startWith: 100 }),
|
|
1754
|
-
}),
|
|
1755
|
-
};
|
|
1756
|
-
|
|
1757
|
-
const schema2 = {
|
|
1758
|
-
users: pgTable('users', {
|
|
1759
|
-
id: integer('id').generatedByDefaultAsIdentity({
|
|
1760
|
-
startWith: 100,
|
|
1761
|
-
increment: 4,
|
|
1762
|
-
maxValue: 10000,
|
|
1763
|
-
}),
|
|
1764
|
-
}),
|
|
1765
|
-
};
|
|
1766
|
-
|
|
1767
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1768
|
-
|
|
1769
|
-
expect(statements).toStrictEqual([
|
|
1770
|
-
{
|
|
1771
|
-
columnName: 'id',
|
|
1772
|
-
identity: 'users_id_seq;byDefault;1;10000;4;100;1;false',
|
|
1773
|
-
oldIdentity: 'users_id_seq;byDefault;1;2147483647;1;100;1;false',
|
|
1774
|
-
schema: '',
|
|
1775
|
-
tableName: 'users',
|
|
1776
|
-
type: 'alter_table_alter_column_change_identity',
|
|
1777
|
-
},
|
|
1778
|
-
]);
|
|
1779
|
-
expect(sqlStatements).toStrictEqual([
|
|
1780
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET MAXVALUE 10000;',
|
|
1781
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET INCREMENT BY 4;',
|
|
1782
|
-
]);
|
|
1783
|
-
|
|
1784
|
-
for (const st of sqlStatements) {
|
|
1785
|
-
await client.query(st);
|
|
1786
|
-
}
|
|
1787
|
-
});
|
|
1788
|
-
|
|
1789
|
-
test('alter identity from a column - by default to always', async () => {
|
|
1790
|
-
const client = new PGlite();
|
|
1791
|
-
|
|
1792
|
-
const schema1 = {
|
|
1793
|
-
users: pgTable('users', {
|
|
1794
|
-
id: integer('id').generatedByDefaultAsIdentity({ startWith: 100 }),
|
|
1795
|
-
}),
|
|
1796
|
-
};
|
|
1797
|
-
|
|
1798
|
-
const schema2 = {
|
|
1799
|
-
users: pgTable('users', {
|
|
1800
|
-
id: integer('id').generatedAlwaysAsIdentity({
|
|
1801
|
-
startWith: 100,
|
|
1802
|
-
increment: 4,
|
|
1803
|
-
maxValue: 10000,
|
|
1804
|
-
}),
|
|
1805
|
-
}),
|
|
1806
|
-
};
|
|
1807
|
-
|
|
1808
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1809
|
-
|
|
1810
|
-
expect(statements).toStrictEqual([
|
|
1811
|
-
{
|
|
1812
|
-
columnName: 'id',
|
|
1813
|
-
identity: 'users_id_seq;always;1;10000;4;100;1;false',
|
|
1814
|
-
oldIdentity: 'users_id_seq;byDefault;1;2147483647;1;100;1;false',
|
|
1815
|
-
schema: '',
|
|
1816
|
-
tableName: 'users',
|
|
1817
|
-
type: 'alter_table_alter_column_change_identity',
|
|
1818
|
-
},
|
|
1819
|
-
]);
|
|
1820
|
-
expect(sqlStatements).toStrictEqual([
|
|
1821
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET GENERATED ALWAYS;',
|
|
1822
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET MAXVALUE 10000;',
|
|
1823
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET INCREMENT BY 4;',
|
|
1824
|
-
]);
|
|
1825
|
-
|
|
1826
|
-
for (const st of sqlStatements) {
|
|
1827
|
-
await client.query(st);
|
|
1828
|
-
}
|
|
1829
|
-
});
|
|
1830
|
-
|
|
1831
|
-
test('alter identity from a column - always to by default', async () => {
|
|
1832
|
-
const client = new PGlite();
|
|
1833
|
-
|
|
1834
|
-
const schema1 = {
|
|
1835
|
-
users: pgTable('users', {
|
|
1836
|
-
id: integer('id').generatedAlwaysAsIdentity({ startWith: 100 }),
|
|
1837
|
-
}),
|
|
1838
|
-
};
|
|
1839
|
-
|
|
1840
|
-
const schema2 = {
|
|
1841
|
-
users: pgTable('users', {
|
|
1842
|
-
id: integer('id').generatedByDefaultAsIdentity({
|
|
1843
|
-
startWith: 100,
|
|
1844
|
-
increment: 4,
|
|
1845
|
-
maxValue: 10000,
|
|
1846
|
-
cycle: true,
|
|
1847
|
-
cache: 100,
|
|
1848
|
-
}),
|
|
1849
|
-
}),
|
|
1850
|
-
};
|
|
1851
|
-
|
|
1852
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1853
|
-
|
|
1854
|
-
expect(statements).toStrictEqual([
|
|
1855
|
-
{
|
|
1856
|
-
columnName: 'id',
|
|
1857
|
-
identity: 'users_id_seq;byDefault;1;10000;4;100;100;true',
|
|
1858
|
-
oldIdentity: 'users_id_seq;always;1;2147483647;1;100;1;false',
|
|
1859
|
-
schema: '',
|
|
1860
|
-
tableName: 'users',
|
|
1861
|
-
type: 'alter_table_alter_column_change_identity',
|
|
1862
|
-
},
|
|
1863
|
-
]);
|
|
1864
|
-
expect(sqlStatements).toStrictEqual([
|
|
1865
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET GENERATED BY DEFAULT;',
|
|
1866
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET MAXVALUE 10000;',
|
|
1867
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET INCREMENT BY 4;',
|
|
1868
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET CACHE 100;',
|
|
1869
|
-
'ALTER TABLE "users" ALTER COLUMN "id" SET CYCLE;',
|
|
1870
|
-
]);
|
|
1871
|
-
|
|
1872
|
-
for (const st of sqlStatements) {
|
|
1873
|
-
await client.query(st);
|
|
1874
|
-
}
|
|
1875
|
-
});
|
|
1876
|
-
|
|
1877
|
-
test('add column with identity - few params', async () => {
|
|
1878
|
-
const client = new PGlite();
|
|
1879
|
-
|
|
1880
|
-
const schema1 = {
|
|
1881
|
-
users: pgTable('users', {
|
|
1882
|
-
email: text('email'),
|
|
1883
|
-
}),
|
|
1884
|
-
};
|
|
1885
|
-
|
|
1886
|
-
const schema2 = {
|
|
1887
|
-
users: pgTable('users', {
|
|
1888
|
-
email: text('email'),
|
|
1889
|
-
id: integer('id').generatedByDefaultAsIdentity({ name: 'custom_name' }),
|
|
1890
|
-
id1: integer('id1').generatedAlwaysAsIdentity({
|
|
1891
|
-
name: 'custom_name1',
|
|
1892
|
-
increment: 4,
|
|
1893
|
-
}),
|
|
1894
|
-
}),
|
|
1895
|
-
};
|
|
1896
|
-
|
|
1897
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1898
|
-
|
|
1899
|
-
expect(statements).toStrictEqual([
|
|
1900
|
-
{
|
|
1901
|
-
column: {
|
|
1902
|
-
identity: 'custom_name;byDefault;1;2147483647;1;1;1;false',
|
|
1903
|
-
name: 'id',
|
|
1904
|
-
notNull: true,
|
|
1905
|
-
primaryKey: false,
|
|
1906
|
-
type: 'integer',
|
|
1907
|
-
},
|
|
1908
|
-
schema: '',
|
|
1909
|
-
tableName: 'users',
|
|
1910
|
-
type: 'alter_table_add_column',
|
|
1911
|
-
},
|
|
1912
|
-
{
|
|
1913
|
-
column: {
|
|
1914
|
-
identity: 'custom_name1;always;1;2147483647;4;1;1;false',
|
|
1915
|
-
name: 'id1',
|
|
1916
|
-
notNull: true,
|
|
1917
|
-
primaryKey: false,
|
|
1918
|
-
type: 'integer',
|
|
1919
|
-
},
|
|
1920
|
-
schema: '',
|
|
1921
|
-
tableName: 'users',
|
|
1922
|
-
type: 'alter_table_add_column',
|
|
1923
|
-
},
|
|
1924
|
-
]);
|
|
1925
|
-
expect(sqlStatements).toStrictEqual([
|
|
1926
|
-
'ALTER TABLE "users" ADD COLUMN "id" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY (sequence name "custom_name" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1);',
|
|
1927
|
-
'ALTER TABLE "users" ADD COLUMN "id1" integer NOT NULL GENERATED ALWAYS AS IDENTITY (sequence name "custom_name1" INCREMENT BY 4 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1);',
|
|
1928
|
-
]);
|
|
1929
|
-
|
|
1930
|
-
// for (const st of sqlStatements) {
|
|
1931
|
-
// await client.query(st);
|
|
1932
|
-
// }
|
|
1933
|
-
});
|
|
1934
|
-
|
|
1935
|
-
test('add identity to column - few params', async () => {
|
|
1936
|
-
const client = new PGlite();
|
|
1937
|
-
|
|
1938
|
-
const schema1 = {
|
|
1939
|
-
users: pgTable('users', {
|
|
1940
|
-
id: integer('id'),
|
|
1941
|
-
id1: integer('id1'),
|
|
1942
|
-
}),
|
|
1943
|
-
};
|
|
1944
|
-
|
|
1945
|
-
const schema2 = {
|
|
1946
|
-
users: pgTable('users', {
|
|
1947
|
-
id: integer('id').generatedByDefaultAsIdentity({ name: 'custom_name' }),
|
|
1948
|
-
id1: integer('id1').generatedAlwaysAsIdentity({
|
|
1949
|
-
name: 'custom_name1',
|
|
1950
|
-
increment: 4,
|
|
1951
|
-
}),
|
|
1952
|
-
}),
|
|
1953
|
-
};
|
|
1954
|
-
|
|
1955
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1956
|
-
|
|
1957
|
-
expect(statements).toStrictEqual([
|
|
1958
|
-
{
|
|
1959
|
-
columnName: 'id',
|
|
1960
|
-
identity: 'custom_name;byDefault;1;2147483647;1;1;1;false',
|
|
1961
|
-
schema: '',
|
|
1962
|
-
tableName: 'users',
|
|
1963
|
-
type: 'alter_table_alter_column_set_identity',
|
|
1964
|
-
},
|
|
1965
|
-
{
|
|
1966
|
-
columnName: 'id1',
|
|
1967
|
-
identity: 'custom_name1;always;1;2147483647;4;1;1;false',
|
|
1968
|
-
schema: '',
|
|
1969
|
-
tableName: 'users',
|
|
1970
|
-
type: 'alter_table_alter_column_set_identity',
|
|
1971
|
-
},
|
|
1972
|
-
]);
|
|
1973
|
-
expect(sqlStatements).toStrictEqual([
|
|
1974
|
-
'ALTER TABLE "users" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (sequence name "custom_name" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1);',
|
|
1975
|
-
'ALTER TABLE "users" ALTER COLUMN "id1" ADD GENERATED ALWAYS AS IDENTITY (sequence name "custom_name1" INCREMENT BY 4 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1);',
|
|
1976
|
-
]);
|
|
1977
|
-
|
|
1978
|
-
// for (const st of sqlStatements) {
|
|
1979
|
-
// await client.query(st);
|
|
1980
|
-
// }
|
|
1981
|
-
});
|
|
1982
|
-
|
|
1983
|
-
test('add array column - empty array default', async () => {
|
|
1984
|
-
const client = new PGlite();
|
|
1985
|
-
|
|
1986
|
-
const schema1 = {
|
|
1987
|
-
test: pgTable('test', {
|
|
1988
|
-
id: serial('id').primaryKey(),
|
|
1989
|
-
}),
|
|
1990
|
-
};
|
|
1991
|
-
const schema2 = {
|
|
1992
|
-
test: pgTable('test', {
|
|
1993
|
-
id: serial('id').primaryKey(),
|
|
1994
|
-
values: integer('values').array().default([]),
|
|
1995
|
-
}),
|
|
1996
|
-
};
|
|
1997
|
-
|
|
1998
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
1999
|
-
|
|
2000
|
-
expect(statements).toStrictEqual([
|
|
2001
|
-
{
|
|
2002
|
-
type: 'alter_table_add_column',
|
|
2003
|
-
tableName: 'test',
|
|
2004
|
-
schema: '',
|
|
2005
|
-
column: { name: 'values', type: 'integer[]', primaryKey: false, notNull: false, default: "'{}'" },
|
|
2006
|
-
},
|
|
2007
|
-
]);
|
|
2008
|
-
expect(sqlStatements).toStrictEqual(['ALTER TABLE "test" ADD COLUMN "values" integer[] DEFAULT \'{}\';']);
|
|
2009
|
-
});
|
|
2010
|
-
|
|
2011
|
-
test('add array column - default', async () => {
|
|
2012
|
-
const client = new PGlite();
|
|
2013
|
-
|
|
2014
|
-
const schema1 = {
|
|
2015
|
-
test: pgTable('test', {
|
|
2016
|
-
id: serial('id').primaryKey(),
|
|
2017
|
-
}),
|
|
2018
|
-
};
|
|
2019
|
-
const schema2 = {
|
|
2020
|
-
test: pgTable('test', {
|
|
2021
|
-
id: serial('id').primaryKey(),
|
|
2022
|
-
values: integer('values').array().default([1, 2, 3]),
|
|
2023
|
-
}),
|
|
2024
|
-
};
|
|
2025
|
-
|
|
2026
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2027
|
-
|
|
2028
|
-
expect(statements).toStrictEqual([
|
|
2029
|
-
{
|
|
2030
|
-
type: 'alter_table_add_column',
|
|
2031
|
-
tableName: 'test',
|
|
2032
|
-
schema: '',
|
|
2033
|
-
column: { name: 'values', type: 'integer[]', primaryKey: false, notNull: false, default: "'{1,2,3}'" },
|
|
2034
|
-
},
|
|
2035
|
-
]);
|
|
2036
|
-
expect(sqlStatements).toStrictEqual(['ALTER TABLE "test" ADD COLUMN "values" integer[] DEFAULT \'{1,2,3}\';']);
|
|
2037
|
-
});
|
|
2038
|
-
|
|
2039
|
-
test('create view', async () => {
|
|
2040
|
-
const client = new PGlite();
|
|
2041
|
-
|
|
2042
|
-
const table = pgTable('test', {
|
|
2043
|
-
id: serial('id').primaryKey(),
|
|
2044
|
-
});
|
|
2045
|
-
const schema1 = {
|
|
2046
|
-
test: table,
|
|
2047
|
-
};
|
|
2048
|
-
|
|
2049
|
-
const schema2 = {
|
|
2050
|
-
test: table,
|
|
2051
|
-
view: pgView('view').as((qb) => qb.selectDistinct().from(table)),
|
|
2052
|
-
};
|
|
2053
|
-
|
|
2054
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2055
|
-
|
|
2056
|
-
expect(statements).toStrictEqual([
|
|
2057
|
-
{
|
|
2058
|
-
definition: 'select distinct "id" from "test"',
|
|
2059
|
-
name: 'view',
|
|
2060
|
-
schema: 'public',
|
|
2061
|
-
type: 'create_view',
|
|
2062
|
-
with: undefined,
|
|
2063
|
-
materialized: false,
|
|
2064
|
-
tablespace: undefined,
|
|
2065
|
-
using: undefined,
|
|
2066
|
-
withNoData: false,
|
|
2067
|
-
},
|
|
2068
|
-
]);
|
|
2069
|
-
expect(sqlStatements).toStrictEqual(['CREATE VIEW "public"."view" AS (select distinct "id" from "test");']);
|
|
2070
|
-
});
|
|
2071
|
-
|
|
2072
|
-
test('add check constraint to table', async () => {
|
|
2073
|
-
const client = new PGlite();
|
|
2074
|
-
|
|
2075
|
-
const schema1 = {
|
|
2076
|
-
test: pgTable('test', {
|
|
2077
|
-
id: serial('id').primaryKey(),
|
|
2078
|
-
values: integer('values').array().default([1, 2, 3]),
|
|
2079
|
-
}),
|
|
2080
|
-
};
|
|
2081
|
-
const schema2 = {
|
|
2082
|
-
test: pgTable('test', {
|
|
2083
|
-
id: serial('id').primaryKey(),
|
|
2084
|
-
values: integer('values').array().default([1, 2, 3]),
|
|
2085
|
-
}, (table) => ({
|
|
2086
|
-
checkConstraint1: check('some_check1', sql`${table.values} < 100`),
|
|
2087
|
-
checkConstraint2: check('some_check2', sql`'test' < 100`),
|
|
2088
|
-
})),
|
|
2089
|
-
};
|
|
2090
|
-
|
|
2091
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2092
|
-
|
|
2093
|
-
expect(statements).toStrictEqual([
|
|
2094
|
-
{
|
|
2095
|
-
type: 'create_check_constraint',
|
|
2096
|
-
tableName: 'test',
|
|
2097
|
-
schema: '',
|
|
2098
|
-
data: 'some_check1;"test"."values" < 100',
|
|
2099
|
-
},
|
|
2100
|
-
{
|
|
2101
|
-
data: "some_check2;'test' < 100",
|
|
2102
|
-
schema: '',
|
|
2103
|
-
tableName: 'test',
|
|
2104
|
-
type: 'create_check_constraint',
|
|
2105
|
-
},
|
|
2106
|
-
]);
|
|
2107
|
-
expect(sqlStatements).toStrictEqual([
|
|
2108
|
-
'ALTER TABLE "test" ADD CONSTRAINT "some_check1" CHECK ("test"."values" < 100);',
|
|
2109
|
-
`ALTER TABLE "test" ADD CONSTRAINT "some_check2" CHECK ('test' < 100);`,
|
|
2110
|
-
]);
|
|
2111
|
-
});
|
|
2112
|
-
|
|
2113
|
-
test('create materialized view', async () => {
|
|
2114
|
-
const client = new PGlite();
|
|
2115
|
-
|
|
2116
|
-
const table = pgTable('test', {
|
|
2117
|
-
id: serial('id').primaryKey(),
|
|
2118
|
-
});
|
|
2119
|
-
const schema1 = {
|
|
2120
|
-
test: table,
|
|
2121
|
-
};
|
|
2122
|
-
|
|
2123
|
-
const schema2 = {
|
|
2124
|
-
test: table,
|
|
2125
|
-
view: pgMaterializedView('view')
|
|
2126
|
-
.withNoData()
|
|
2127
|
-
.using('heap')
|
|
2128
|
-
.as((qb) => qb.selectDistinct().from(table)),
|
|
2129
|
-
};
|
|
2130
|
-
|
|
2131
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2132
|
-
|
|
2133
|
-
expect(statements).toStrictEqual([
|
|
2134
|
-
{
|
|
2135
|
-
definition: 'select distinct "id" from "test"',
|
|
2136
|
-
name: 'view',
|
|
2137
|
-
schema: 'public',
|
|
2138
|
-
type: 'create_view',
|
|
2139
|
-
with: undefined,
|
|
2140
|
-
materialized: true,
|
|
2141
|
-
tablespace: undefined,
|
|
2142
|
-
using: 'heap',
|
|
2143
|
-
withNoData: true,
|
|
2144
|
-
},
|
|
2145
|
-
]);
|
|
2146
|
-
expect(sqlStatements).toStrictEqual([
|
|
2147
|
-
'CREATE MATERIALIZED VIEW "public"."view" USING "heap" AS (select distinct "id" from "test") WITH NO DATA;',
|
|
2148
|
-
]);
|
|
2149
|
-
});
|
|
2150
|
-
|
|
2151
|
-
test('drop check constraint', async () => {
|
|
2152
|
-
const client = new PGlite();
|
|
2153
|
-
|
|
2154
|
-
const schema1 = {
|
|
2155
|
-
test: pgTable('test', {
|
|
2156
|
-
id: serial('id').primaryKey(),
|
|
2157
|
-
values: integer('values').default(1),
|
|
2158
|
-
}, (table) => ({
|
|
2159
|
-
checkConstraint: check('some_check', sql`${table.values} < 100`),
|
|
2160
|
-
})),
|
|
2161
|
-
};
|
|
2162
|
-
const schema2 = {
|
|
2163
|
-
test: pgTable('test', {
|
|
2164
|
-
id: serial('id').primaryKey(),
|
|
2165
|
-
values: integer('values').default(1),
|
|
2166
|
-
}),
|
|
2167
|
-
};
|
|
2168
|
-
|
|
2169
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
2170
|
-
client,
|
|
2171
|
-
schema1,
|
|
2172
|
-
schema2,
|
|
2173
|
-
[],
|
|
2174
|
-
false,
|
|
2175
|
-
['public'],
|
|
2176
|
-
);
|
|
2177
|
-
|
|
2178
|
-
expect(statements).toStrictEqual([
|
|
2179
|
-
{
|
|
2180
|
-
type: 'delete_check_constraint',
|
|
2181
|
-
tableName: 'test',
|
|
2182
|
-
schema: '',
|
|
2183
|
-
constraintName: 'some_check',
|
|
2184
|
-
},
|
|
2185
|
-
]);
|
|
2186
|
-
expect(sqlStatements).toStrictEqual([
|
|
2187
|
-
'ALTER TABLE "test" DROP CONSTRAINT "some_check";',
|
|
2188
|
-
]);
|
|
2189
|
-
});
|
|
2190
|
-
|
|
2191
|
-
test('Column with same name as enum', async () => {
|
|
2192
|
-
const client = new PGlite();
|
|
2193
|
-
const statusEnum = pgEnum('status', ['inactive', 'active', 'banned']);
|
|
2194
|
-
|
|
2195
|
-
const schema1 = {
|
|
2196
|
-
statusEnum,
|
|
2197
|
-
table1: pgTable('table1', {
|
|
2198
|
-
id: serial('id').primaryKey(),
|
|
2199
|
-
}),
|
|
2200
|
-
};
|
|
2201
|
-
|
|
2202
|
-
const schema2 = {
|
|
2203
|
-
statusEnum,
|
|
2204
|
-
table1: pgTable('table1', {
|
|
2205
|
-
id: serial('id').primaryKey(),
|
|
2206
|
-
status: statusEnum('status').default('inactive'),
|
|
2207
|
-
}),
|
|
2208
|
-
table2: pgTable('table2', {
|
|
2209
|
-
id: serial('id').primaryKey(),
|
|
2210
|
-
status: statusEnum('status').default('inactive'),
|
|
2211
|
-
}),
|
|
2212
|
-
};
|
|
2213
|
-
|
|
2214
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
2215
|
-
client,
|
|
2216
|
-
schema1,
|
|
2217
|
-
schema2,
|
|
2218
|
-
[],
|
|
2219
|
-
false,
|
|
2220
|
-
['public'],
|
|
2221
|
-
);
|
|
2222
|
-
|
|
2223
|
-
expect(statements).toStrictEqual([
|
|
2224
|
-
{
|
|
2225
|
-
type: 'create_table',
|
|
2226
|
-
tableName: 'table2',
|
|
2227
|
-
schema: '',
|
|
2228
|
-
compositePKs: [],
|
|
2229
|
-
compositePkName: '',
|
|
2230
|
-
isRLSEnabled: false,
|
|
2231
|
-
policies: [],
|
|
2232
|
-
uniqueConstraints: [],
|
|
2233
|
-
checkConstraints: [],
|
|
2234
|
-
columns: [
|
|
2235
|
-
{ name: 'id', type: 'serial', primaryKey: true, notNull: true },
|
|
2236
|
-
{
|
|
2237
|
-
name: 'status',
|
|
2238
|
-
type: 'status',
|
|
2239
|
-
typeSchema: 'public',
|
|
2240
|
-
primaryKey: false,
|
|
2241
|
-
notNull: false,
|
|
2242
|
-
default: "'inactive'",
|
|
2243
|
-
},
|
|
2244
|
-
],
|
|
2245
|
-
},
|
|
2246
|
-
{
|
|
2247
|
-
type: 'alter_table_add_column',
|
|
2248
|
-
tableName: 'table1',
|
|
2249
|
-
schema: '',
|
|
2250
|
-
column: {
|
|
2251
|
-
name: 'status',
|
|
2252
|
-
type: 'status',
|
|
2253
|
-
typeSchema: 'public',
|
|
2254
|
-
primaryKey: false,
|
|
2255
|
-
notNull: false,
|
|
2256
|
-
default: "'inactive'",
|
|
2257
|
-
},
|
|
2258
|
-
},
|
|
2259
|
-
]);
|
|
2260
|
-
expect(sqlStatements).toStrictEqual([
|
|
2261
|
-
'CREATE TABLE "table2" (\n\t"id" serial PRIMARY KEY NOT NULL,\n\t"status" "status" DEFAULT \'inactive\'\n);\n',
|
|
2262
|
-
'ALTER TABLE "table1" ADD COLUMN "status" "status" DEFAULT \'inactive\';',
|
|
2263
|
-
]);
|
|
2264
|
-
});
|
|
2265
|
-
|
|
2266
|
-
test('db has checks. Push with same names', async () => {
|
|
2267
|
-
const client = new PGlite();
|
|
2268
|
-
|
|
2269
|
-
const schema1 = {
|
|
2270
|
-
test: pgTable('test', {
|
|
2271
|
-
id: serial('id').primaryKey(),
|
|
2272
|
-
values: integer('values').default(1),
|
|
2273
|
-
}, (table) => ({
|
|
2274
|
-
checkConstraint: check('some_check', sql`${table.values} < 100`),
|
|
2275
|
-
})),
|
|
2276
|
-
};
|
|
2277
|
-
const schema2 = {
|
|
2278
|
-
test: pgTable('test', {
|
|
2279
|
-
id: serial('id').primaryKey(),
|
|
2280
|
-
values: integer('values').default(1),
|
|
2281
|
-
}, (table) => ({
|
|
2282
|
-
checkConstraint: check('some_check', sql`some new value`),
|
|
2283
|
-
})),
|
|
2284
|
-
};
|
|
2285
|
-
|
|
2286
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
2287
|
-
client,
|
|
2288
|
-
schema1,
|
|
2289
|
-
schema2,
|
|
2290
|
-
[],
|
|
2291
|
-
false,
|
|
2292
|
-
['public'],
|
|
2293
|
-
);
|
|
2294
|
-
|
|
2295
|
-
expect(statements).toStrictEqual([]);
|
|
2296
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
2297
|
-
});
|
|
2298
|
-
|
|
2299
|
-
test('drop view', async () => {
|
|
2300
|
-
const client = new PGlite();
|
|
2301
|
-
|
|
2302
|
-
const table = pgTable('test', {
|
|
2303
|
-
id: serial('id').primaryKey(),
|
|
2304
|
-
});
|
|
2305
|
-
const schema1 = {
|
|
2306
|
-
test: table,
|
|
2307
|
-
view: pgView('view').as((qb) => qb.selectDistinct().from(table)),
|
|
2308
|
-
};
|
|
2309
|
-
|
|
2310
|
-
const schema2 = {
|
|
2311
|
-
test: table,
|
|
2312
|
-
};
|
|
2313
|
-
|
|
2314
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2315
|
-
|
|
2316
|
-
expect(statements).toStrictEqual([
|
|
2317
|
-
{
|
|
2318
|
-
name: 'view',
|
|
2319
|
-
schema: 'public',
|
|
2320
|
-
type: 'drop_view',
|
|
2321
|
-
},
|
|
2322
|
-
]);
|
|
2323
|
-
expect(sqlStatements).toStrictEqual(['DROP VIEW "public"."view";']);
|
|
2324
|
-
});
|
|
2325
|
-
|
|
2326
|
-
test('drop materialized view', async () => {
|
|
2327
|
-
const client = new PGlite();
|
|
2328
|
-
|
|
2329
|
-
const table = pgTable('test', {
|
|
2330
|
-
id: serial('id').primaryKey(),
|
|
2331
|
-
});
|
|
2332
|
-
const schema1 = {
|
|
2333
|
-
test: table,
|
|
2334
|
-
view: pgMaterializedView('view').as((qb) => qb.selectDistinct().from(table)),
|
|
2335
|
-
};
|
|
2336
|
-
|
|
2337
|
-
const schema2 = {
|
|
2338
|
-
test: table,
|
|
2339
|
-
};
|
|
2340
|
-
|
|
2341
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2342
|
-
|
|
2343
|
-
expect(statements).toStrictEqual([
|
|
2344
|
-
{
|
|
2345
|
-
name: 'view',
|
|
2346
|
-
schema: 'public',
|
|
2347
|
-
type: 'drop_view',
|
|
2348
|
-
materialized: true,
|
|
2349
|
-
},
|
|
2350
|
-
]);
|
|
2351
|
-
expect(sqlStatements).toStrictEqual(['DROP MATERIALIZED VIEW "public"."view";']);
|
|
2352
|
-
});
|
|
2353
|
-
|
|
2354
|
-
test('push view with same name', async () => {
|
|
2355
|
-
const client = new PGlite();
|
|
2356
|
-
|
|
2357
|
-
const table = pgTable('test', {
|
|
2358
|
-
id: serial('id').primaryKey(),
|
|
2359
|
-
});
|
|
2360
|
-
const schema1 = {
|
|
2361
|
-
test: table,
|
|
2362
|
-
view: pgView('view').as((qb) => qb.selectDistinct().from(table)),
|
|
2363
|
-
};
|
|
2364
|
-
|
|
2365
|
-
const schema2 = {
|
|
2366
|
-
test: table,
|
|
2367
|
-
view: pgView('view').as((qb) => qb.selectDistinct().from(table).where(eq(table.id, 1))),
|
|
2368
|
-
};
|
|
2369
|
-
|
|
2370
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2371
|
-
|
|
2372
|
-
expect(statements).toStrictEqual([]);
|
|
2373
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
2374
|
-
});
|
|
2375
|
-
|
|
2376
|
-
test('push materialized view with same name', async () => {
|
|
2377
|
-
const client = new PGlite();
|
|
2378
|
-
|
|
2379
|
-
const table = pgTable('test', {
|
|
2380
|
-
id: serial('id').primaryKey(),
|
|
2381
|
-
});
|
|
2382
|
-
const schema1 = {
|
|
2383
|
-
test: table,
|
|
2384
|
-
view: pgMaterializedView('view').as((qb) => qb.selectDistinct().from(table)),
|
|
2385
|
-
};
|
|
2386
|
-
|
|
2387
|
-
const schema2 = {
|
|
2388
|
-
test: table,
|
|
2389
|
-
view: pgMaterializedView('view').as((qb) => qb.selectDistinct().from(table).where(eq(table.id, 1))),
|
|
2390
|
-
};
|
|
2391
|
-
|
|
2392
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2393
|
-
|
|
2394
|
-
expect(statements).toStrictEqual([]);
|
|
2395
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
2396
|
-
});
|
|
2397
|
-
|
|
2398
|
-
test('add with options for materialized view', async () => {
|
|
2399
|
-
const client = new PGlite();
|
|
2400
|
-
|
|
2401
|
-
const table = pgTable('test', {
|
|
2402
|
-
id: serial('id').primaryKey(),
|
|
2403
|
-
});
|
|
2404
|
-
const schema1 = {
|
|
2405
|
-
test: table,
|
|
2406
|
-
view: pgMaterializedView('view').as((qb) => qb.selectDistinct().from(table)),
|
|
2407
|
-
};
|
|
2408
|
-
|
|
2409
|
-
const schema2 = {
|
|
2410
|
-
test: table,
|
|
2411
|
-
view: pgMaterializedView('view')
|
|
2412
|
-
.with({ autovacuumFreezeTableAge: 1, autovacuumEnabled: false })
|
|
2413
|
-
.as((qb) => qb.selectDistinct().from(table)),
|
|
2414
|
-
};
|
|
2415
|
-
|
|
2416
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2417
|
-
|
|
2418
|
-
expect(statements.length).toBe(1);
|
|
2419
|
-
expect(statements[0]).toStrictEqual({
|
|
2420
|
-
name: 'view',
|
|
2421
|
-
schema: 'public',
|
|
2422
|
-
type: 'alter_view_add_with_option',
|
|
2423
|
-
with: {
|
|
2424
|
-
autovacuumFreezeTableAge: 1,
|
|
2425
|
-
autovacuumEnabled: false,
|
|
2426
|
-
},
|
|
2427
|
-
materialized: true,
|
|
2428
|
-
});
|
|
2429
|
-
expect(sqlStatements.length).toBe(1);
|
|
2430
|
-
expect(sqlStatements[0]).toBe(
|
|
2431
|
-
`ALTER MATERIALIZED VIEW "public"."view" SET (autovacuum_enabled = false, autovacuum_freeze_table_age = 1);`,
|
|
2432
|
-
);
|
|
2433
|
-
});
|
|
2434
|
-
|
|
2435
|
-
test('add with options to materialized', async () => {
|
|
2436
|
-
const client = new PGlite();
|
|
2437
|
-
|
|
2438
|
-
const table = pgTable('test', {
|
|
2439
|
-
id: serial('id').primaryKey(),
|
|
2440
|
-
});
|
|
2441
|
-
const schema1 = {
|
|
2442
|
-
test: table,
|
|
2443
|
-
view: pgMaterializedView('view').as((qb) => qb.selectDistinct().from(table)),
|
|
2444
|
-
};
|
|
2445
|
-
|
|
2446
|
-
const schema2 = {
|
|
2447
|
-
test: table,
|
|
2448
|
-
view: pgMaterializedView('view')
|
|
2449
|
-
.with({ autovacuumVacuumCostDelay: 100, vacuumTruncate: false })
|
|
2450
|
-
.as((qb) => qb.selectDistinct().from(table)),
|
|
2451
|
-
};
|
|
2452
|
-
|
|
2453
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2454
|
-
|
|
2455
|
-
expect(statements.length).toBe(1);
|
|
2456
|
-
expect(statements[0]).toStrictEqual({
|
|
2457
|
-
name: 'view',
|
|
2458
|
-
schema: 'public',
|
|
2459
|
-
type: 'alter_view_add_with_option',
|
|
2460
|
-
with: {
|
|
2461
|
-
autovacuumVacuumCostDelay: 100,
|
|
2462
|
-
vacuumTruncate: false,
|
|
2463
|
-
},
|
|
2464
|
-
materialized: true,
|
|
2465
|
-
});
|
|
2466
|
-
expect(sqlStatements.length).toBe(1);
|
|
2467
|
-
expect(sqlStatements[0]).toBe(
|
|
2468
|
-
`ALTER MATERIALIZED VIEW "public"."view" SET (vacuum_truncate = false, autovacuum_vacuum_cost_delay = 100);`,
|
|
2469
|
-
);
|
|
2470
|
-
});
|
|
2471
|
-
|
|
2472
|
-
test('add with options to materialized with existing flag', async () => {
|
|
2473
|
-
const client = new PGlite();
|
|
2474
|
-
|
|
2475
|
-
const table = pgTable('test', {
|
|
2476
|
-
id: serial('id').primaryKey(),
|
|
2477
|
-
});
|
|
2478
|
-
const schema1 = {
|
|
2479
|
-
test: table,
|
|
2480
|
-
view: pgMaterializedView('view', {}).as(sql`SELECT id FROM "test"`),
|
|
2481
|
-
};
|
|
2482
|
-
|
|
2483
|
-
const schema2 = {
|
|
2484
|
-
test: table,
|
|
2485
|
-
view: pgMaterializedView('view', {}).with({ autovacuumVacuumCostDelay: 100, vacuumTruncate: false }).existing(),
|
|
2486
|
-
};
|
|
2487
|
-
|
|
2488
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']);
|
|
2489
|
-
|
|
2490
|
-
expect(statements.length).toBe(0);
|
|
2491
|
-
expect(sqlStatements.length).toBe(0);
|
|
2492
|
-
});
|
|
2493
|
-
|
|
2494
|
-
test('drop mat view with data', async () => {
|
|
2495
|
-
const client = new PGlite();
|
|
2496
|
-
|
|
2497
|
-
const table = pgTable('table', {
|
|
2498
|
-
id: serial('id').primaryKey(),
|
|
2499
|
-
});
|
|
2500
|
-
const schema1 = {
|
|
2501
|
-
test: table,
|
|
2502
|
-
view: pgMaterializedView('view', {}).as(sql`SELECT * FROM ${table}`),
|
|
2503
|
-
};
|
|
2504
|
-
|
|
2505
|
-
const schema2 = {
|
|
2506
|
-
test: table,
|
|
2507
|
-
};
|
|
2508
|
-
|
|
2509
|
-
const seedStatements = [`INSERT INTO "public"."table" ("id") VALUES (1), (2), (3)`];
|
|
2510
|
-
|
|
2511
|
-
const {
|
|
2512
|
-
statements,
|
|
2513
|
-
sqlStatements,
|
|
2514
|
-
columnsToRemove,
|
|
2515
|
-
infoToPrint,
|
|
2516
|
-
schemasToRemove,
|
|
2517
|
-
shouldAskForApprove,
|
|
2518
|
-
tablesToRemove,
|
|
2519
|
-
tablesToTruncate,
|
|
2520
|
-
matViewsToRemove,
|
|
2521
|
-
} = await diffTestSchemasPush(
|
|
2522
|
-
client,
|
|
2523
|
-
schema1,
|
|
2524
|
-
schema2,
|
|
2525
|
-
[],
|
|
2526
|
-
false,
|
|
2527
|
-
['public'],
|
|
2528
|
-
undefined,
|
|
2529
|
-
undefined,
|
|
2530
|
-
{ after: seedStatements },
|
|
2531
|
-
);
|
|
2532
|
-
|
|
2533
|
-
expect(statements.length).toBe(1);
|
|
2534
|
-
expect(statements[0]).toStrictEqual({
|
|
2535
|
-
materialized: true,
|
|
2536
|
-
name: 'view',
|
|
2537
|
-
schema: 'public',
|
|
2538
|
-
type: 'drop_view',
|
|
2539
|
-
});
|
|
2540
|
-
expect(sqlStatements.length).toBe(1);
|
|
2541
|
-
expect(sqlStatements[0]).toBe(`DROP MATERIALIZED VIEW "public"."view";`);
|
|
2542
|
-
expect(infoToPrint!.length).toBe(1);
|
|
2543
|
-
expect(infoToPrint![0]).toBe(`· You're about to delete "${chalk.underline('view')}" materialized view with 3 items`);
|
|
2544
|
-
expect(columnsToRemove!.length).toBe(0);
|
|
2545
|
-
expect(schemasToRemove!.length).toBe(0);
|
|
2546
|
-
expect(shouldAskForApprove).toBe(true);
|
|
2547
|
-
expect(tablesToRemove!.length).toBe(0);
|
|
2548
|
-
expect(matViewsToRemove!.length).toBe(1);
|
|
2549
|
-
});
|
|
2550
|
-
|
|
2551
|
-
test('drop mat view without data', async () => {
|
|
2552
|
-
const client = new PGlite();
|
|
2553
|
-
|
|
2554
|
-
const table = pgTable('table', {
|
|
2555
|
-
id: serial('id').primaryKey(),
|
|
2556
|
-
});
|
|
2557
|
-
const schema1 = {
|
|
2558
|
-
test: table,
|
|
2559
|
-
view: pgMaterializedView('view', {}).as(sql`SELECT * FROM ${table}`),
|
|
2560
|
-
};
|
|
2561
|
-
|
|
2562
|
-
const schema2 = {
|
|
2563
|
-
test: table,
|
|
2564
|
-
};
|
|
2565
|
-
|
|
2566
|
-
const {
|
|
2567
|
-
statements,
|
|
2568
|
-
sqlStatements,
|
|
2569
|
-
columnsToRemove,
|
|
2570
|
-
infoToPrint,
|
|
2571
|
-
schemasToRemove,
|
|
2572
|
-
shouldAskForApprove,
|
|
2573
|
-
tablesToRemove,
|
|
2574
|
-
tablesToTruncate,
|
|
2575
|
-
matViewsToRemove,
|
|
2576
|
-
} = await diffTestSchemasPush(
|
|
2577
|
-
client,
|
|
2578
|
-
schema1,
|
|
2579
|
-
schema2,
|
|
2580
|
-
[],
|
|
2581
|
-
false,
|
|
2582
|
-
['public'],
|
|
2583
|
-
);
|
|
2584
|
-
|
|
2585
|
-
expect(statements.length).toBe(1);
|
|
2586
|
-
expect(statements[0]).toStrictEqual({
|
|
2587
|
-
materialized: true,
|
|
2588
|
-
name: 'view',
|
|
2589
|
-
schema: 'public',
|
|
2590
|
-
type: 'drop_view',
|
|
2591
|
-
});
|
|
2592
|
-
expect(sqlStatements.length).toBe(1);
|
|
2593
|
-
expect(sqlStatements[0]).toBe(`DROP MATERIALIZED VIEW "public"."view";`);
|
|
2594
|
-
expect(infoToPrint!.length).toBe(0);
|
|
2595
|
-
expect(columnsToRemove!.length).toBe(0);
|
|
2596
|
-
expect(schemasToRemove!.length).toBe(0);
|
|
2597
|
-
expect(shouldAskForApprove).toBe(false);
|
|
2598
|
-
expect(tablesToRemove!.length).toBe(0);
|
|
2599
|
-
expect(matViewsToRemove!.length).toBe(0);
|
|
2600
|
-
});
|
|
2601
|
-
|
|
2602
|
-
test('drop view with data', async () => {
|
|
2603
|
-
const client = new PGlite();
|
|
2604
|
-
|
|
2605
|
-
const table = pgTable('table', {
|
|
2606
|
-
id: serial('id').primaryKey(),
|
|
2607
|
-
});
|
|
2608
|
-
const schema1 = {
|
|
2609
|
-
test: table,
|
|
2610
|
-
view: pgView('view', {}).as(sql`SELECT * FROM ${table}`),
|
|
2611
|
-
};
|
|
2612
|
-
|
|
2613
|
-
const schema2 = {
|
|
2614
|
-
test: table,
|
|
2615
|
-
};
|
|
2616
|
-
|
|
2617
|
-
const seedStatements = [`INSERT INTO "public"."table" ("id") VALUES (1), (2), (3)`];
|
|
2618
|
-
|
|
2619
|
-
const {
|
|
2620
|
-
statements,
|
|
2621
|
-
sqlStatements,
|
|
2622
|
-
columnsToRemove,
|
|
2623
|
-
infoToPrint,
|
|
2624
|
-
schemasToRemove,
|
|
2625
|
-
shouldAskForApprove,
|
|
2626
|
-
tablesToRemove,
|
|
2627
|
-
tablesToTruncate,
|
|
2628
|
-
matViewsToRemove,
|
|
2629
|
-
} = await diffTestSchemasPush(
|
|
2630
|
-
client,
|
|
2631
|
-
schema1,
|
|
2632
|
-
schema2,
|
|
2633
|
-
[],
|
|
2634
|
-
false,
|
|
2635
|
-
['public'],
|
|
2636
|
-
undefined,
|
|
2637
|
-
undefined,
|
|
2638
|
-
{ after: seedStatements },
|
|
2639
|
-
);
|
|
2640
|
-
|
|
2641
|
-
expect(statements.length).toBe(1);
|
|
2642
|
-
expect(statements[0]).toStrictEqual({
|
|
2643
|
-
name: 'view',
|
|
2644
|
-
schema: 'public',
|
|
2645
|
-
type: 'drop_view',
|
|
2646
|
-
});
|
|
2647
|
-
expect(sqlStatements.length).toBe(1);
|
|
2648
|
-
expect(sqlStatements[0]).toBe(`DROP VIEW "public"."view";`);
|
|
2649
|
-
expect(infoToPrint!.length).toBe(0);
|
|
2650
|
-
expect(columnsToRemove!.length).toBe(0);
|
|
2651
|
-
expect(schemasToRemove!.length).toBe(0);
|
|
2652
|
-
expect(shouldAskForApprove).toBe(false);
|
|
2653
|
-
expect(tablesToRemove!.length).toBe(0);
|
|
2654
|
-
expect(matViewsToRemove!.length).toBe(0);
|
|
2655
|
-
});
|
|
2656
|
-
|
|
2657
|
-
test('enums ordering', async () => {
|
|
2658
|
-
const enum1 = pgEnum('enum_users_customer_and_ship_to_settings_roles', [
|
|
2659
|
-
'custAll',
|
|
2660
|
-
'custAdmin',
|
|
2661
|
-
'custClerk',
|
|
2662
|
-
'custInvoiceManager',
|
|
2663
|
-
'custMgf',
|
|
2664
|
-
'custApprover',
|
|
2665
|
-
'custOrderWriter',
|
|
2666
|
-
'custBuyer',
|
|
2667
|
-
]);
|
|
2668
|
-
const schema1 = {};
|
|
2669
|
-
|
|
2670
|
-
const schema2 = {
|
|
2671
|
-
enum1,
|
|
2672
|
-
};
|
|
2673
|
-
|
|
2674
|
-
const { sqlStatements: createEnum } = await diffTestSchemas(schema1, schema2, []);
|
|
2675
|
-
|
|
2676
|
-
const enum2 = pgEnum('enum_users_customer_and_ship_to_settings_roles', [
|
|
2677
|
-
'addedToTop',
|
|
2678
|
-
'custAll',
|
|
2679
|
-
'custAdmin',
|
|
2680
|
-
'custClerk',
|
|
2681
|
-
'custInvoiceManager',
|
|
2682
|
-
'custMgf',
|
|
2683
|
-
'custApprover',
|
|
2684
|
-
'custOrderWriter',
|
|
2685
|
-
'custBuyer',
|
|
2686
|
-
]);
|
|
2687
|
-
const schema3 = {
|
|
2688
|
-
enum2,
|
|
2689
|
-
};
|
|
2690
|
-
|
|
2691
|
-
const { sqlStatements: addedValueSql } = await diffTestSchemas(schema2, schema3, []);
|
|
2692
|
-
|
|
2693
|
-
const enum3 = pgEnum('enum_users_customer_and_ship_to_settings_roles', [
|
|
2694
|
-
'addedToTop',
|
|
2695
|
-
'custAll',
|
|
2696
|
-
'custAdmin',
|
|
2697
|
-
'custClerk',
|
|
2698
|
-
'custInvoiceManager',
|
|
2699
|
-
'addedToMiddle',
|
|
2700
|
-
'custMgf',
|
|
2701
|
-
'custApprover',
|
|
2702
|
-
'custOrderWriter',
|
|
2703
|
-
'custBuyer',
|
|
2704
|
-
]);
|
|
2705
|
-
const schema4 = {
|
|
2706
|
-
enum3,
|
|
2707
|
-
};
|
|
2708
|
-
|
|
2709
|
-
const client = new PGlite();
|
|
2710
|
-
|
|
2711
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
2712
|
-
client,
|
|
2713
|
-
schema3,
|
|
2714
|
-
schema4,
|
|
2715
|
-
[],
|
|
2716
|
-
false,
|
|
2717
|
-
['public'],
|
|
2718
|
-
undefined,
|
|
2719
|
-
undefined,
|
|
2720
|
-
{ before: [...createEnum, ...addedValueSql], runApply: false },
|
|
2721
|
-
);
|
|
2722
|
-
|
|
2723
|
-
expect(statements.length).toBe(1);
|
|
2724
|
-
expect(statements[0]).toStrictEqual({
|
|
2725
|
-
before: 'custMgf',
|
|
2726
|
-
name: 'enum_users_customer_and_ship_to_settings_roles',
|
|
2727
|
-
schema: 'public',
|
|
2728
|
-
type: 'alter_type_add_value',
|
|
2729
|
-
value: 'addedToMiddle',
|
|
2730
|
-
});
|
|
2731
|
-
|
|
2732
|
-
expect(sqlStatements.length).toBe(1);
|
|
2733
|
-
expect(sqlStatements[0]).toBe(
|
|
2734
|
-
`ALTER TYPE "public"."enum_users_customer_and_ship_to_settings_roles" ADD VALUE 'addedToMiddle' BEFORE 'custMgf';`,
|
|
2735
|
-
);
|
|
2736
|
-
});
|
|
2737
|
-
|
|
2738
|
-
test('drop enum values', async () => {
|
|
2739
|
-
const newSchema = pgSchema('mySchema');
|
|
2740
|
-
const enum3 = pgEnum('enum_users_customer_and_ship_to_settings_roles', [
|
|
2741
|
-
'addedToTop',
|
|
2742
|
-
'custAll',
|
|
2743
|
-
'custAdmin',
|
|
2744
|
-
'custClerk',
|
|
2745
|
-
'custInvoiceManager',
|
|
2746
|
-
'addedToMiddle',
|
|
2747
|
-
'custMgf',
|
|
2748
|
-
'custApprover',
|
|
2749
|
-
'custOrderWriter',
|
|
2750
|
-
'custBuyer',
|
|
2751
|
-
]);
|
|
2752
|
-
const schema1 = {
|
|
2753
|
-
enum3,
|
|
2754
|
-
table: pgTable('enum_table', {
|
|
2755
|
-
id: enum3(),
|
|
2756
|
-
}),
|
|
2757
|
-
newSchema,
|
|
2758
|
-
table1: newSchema.table('enum_table', {
|
|
2759
|
-
id: enum3(),
|
|
2760
|
-
}),
|
|
2761
|
-
};
|
|
2762
|
-
|
|
2763
|
-
const enum4 = pgEnum('enum_users_customer_and_ship_to_settings_roles', [
|
|
2764
|
-
'addedToTop',
|
|
2765
|
-
'custAll',
|
|
2766
|
-
'custAdmin',
|
|
2767
|
-
'custClerk',
|
|
2768
|
-
'custInvoiceManager',
|
|
2769
|
-
'custApprover',
|
|
2770
|
-
'custOrderWriter',
|
|
2771
|
-
'custBuyer',
|
|
2772
|
-
]);
|
|
2773
|
-
const schema2 = {
|
|
2774
|
-
enum4,
|
|
2775
|
-
table: pgTable('enum_table', {
|
|
2776
|
-
id: enum4(),
|
|
2777
|
-
}),
|
|
2778
|
-
newSchema,
|
|
2779
|
-
table1: newSchema.table('enum_table', {
|
|
2780
|
-
id: enum4(),
|
|
2781
|
-
}),
|
|
2782
|
-
};
|
|
2783
|
-
|
|
2784
|
-
const client = new PGlite();
|
|
2785
|
-
|
|
2786
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
2787
|
-
client,
|
|
2788
|
-
schema1,
|
|
2789
|
-
schema2,
|
|
2790
|
-
[],
|
|
2791
|
-
false,
|
|
2792
|
-
['public', 'mySchema'],
|
|
2793
|
-
undefined,
|
|
2794
|
-
);
|
|
2795
|
-
|
|
2796
|
-
expect(statements.length).toBe(1);
|
|
2797
|
-
expect(statements[0]).toStrictEqual({
|
|
2798
|
-
name: 'enum_users_customer_and_ship_to_settings_roles',
|
|
2799
|
-
schema: 'public',
|
|
2800
|
-
type: 'alter_type_drop_value',
|
|
2801
|
-
newValues: [
|
|
2802
|
-
'addedToTop',
|
|
2803
|
-
'custAll',
|
|
2804
|
-
'custAdmin',
|
|
2805
|
-
'custClerk',
|
|
2806
|
-
'custInvoiceManager',
|
|
2807
|
-
'custApprover',
|
|
2808
|
-
'custOrderWriter',
|
|
2809
|
-
'custBuyer',
|
|
2810
|
-
],
|
|
2811
|
-
deletedValues: ['addedToMiddle', 'custMgf'],
|
|
2812
|
-
columnsWithEnum: [{
|
|
2813
|
-
column: 'id',
|
|
2814
|
-
schema: 'public',
|
|
2815
|
-
table: 'enum_table',
|
|
2816
|
-
}, {
|
|
2817
|
-
column: 'id',
|
|
2818
|
-
schema: 'mySchema',
|
|
2819
|
-
table: 'enum_table',
|
|
2820
|
-
}],
|
|
2821
|
-
});
|
|
2822
|
-
|
|
2823
|
-
expect(sqlStatements.length).toBe(6);
|
|
2824
|
-
expect(sqlStatements[0]).toBe(
|
|
2825
|
-
`ALTER TABLE "public"."enum_table" ALTER COLUMN "id" SET DATA TYPE text;`,
|
|
2826
|
-
);
|
|
2827
|
-
expect(sqlStatements[1]).toBe(
|
|
2828
|
-
`ALTER TABLE "mySchema"."enum_table" ALTER COLUMN "id" SET DATA TYPE text;`,
|
|
2829
|
-
);
|
|
2830
|
-
expect(sqlStatements[2]).toBe(
|
|
2831
|
-
`DROP TYPE "public"."enum_users_customer_and_ship_to_settings_roles";`,
|
|
2832
|
-
);
|
|
2833
|
-
expect(sqlStatements[3]).toBe(
|
|
2834
|
-
`CREATE TYPE "public"."enum_users_customer_and_ship_to_settings_roles" AS ENUM('addedToTop', 'custAll', 'custAdmin', 'custClerk', 'custInvoiceManager', 'custApprover', 'custOrderWriter', 'custBuyer');`,
|
|
2835
|
-
);
|
|
2836
|
-
expect(sqlStatements[4]).toBe(
|
|
2837
|
-
`ALTER TABLE "public"."enum_table" ALTER COLUMN "id" SET DATA TYPE "public"."enum_users_customer_and_ship_to_settings_roles" USING "id"::"public"."enum_users_customer_and_ship_to_settings_roles";`,
|
|
2838
|
-
);
|
|
2839
|
-
expect(sqlStatements[5]).toBe(
|
|
2840
|
-
`ALTER TABLE "mySchema"."enum_table" ALTER COLUMN "id" SET DATA TYPE "public"."enum_users_customer_and_ship_to_settings_roles" USING "id"::"public"."enum_users_customer_and_ship_to_settings_roles";`,
|
|
2841
|
-
);
|
|
2842
|
-
});
|
|
2843
|
-
|
|
2844
|
-
// Policies and Roles push test
|
|
2845
|
-
test('full policy: no changes', async () => {
|
|
2846
|
-
const client = new PGlite();
|
|
2847
|
-
|
|
2848
|
-
const schema1 = {
|
|
2849
|
-
users: pgTable('users', {
|
|
2850
|
-
id: integer('id').primaryKey(),
|
|
2851
|
-
}, () => ({
|
|
2852
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
2853
|
-
})),
|
|
2854
|
-
};
|
|
2855
|
-
|
|
2856
|
-
const schema2 = {
|
|
2857
|
-
users: pgTable('users', {
|
|
2858
|
-
id: integer('id').primaryKey(),
|
|
2859
|
-
}, () => ({
|
|
2860
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
2861
|
-
})),
|
|
2862
|
-
};
|
|
2863
|
-
|
|
2864
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
2865
|
-
client,
|
|
2866
|
-
schema1,
|
|
2867
|
-
schema2,
|
|
2868
|
-
[],
|
|
2869
|
-
false,
|
|
2870
|
-
['public'],
|
|
2871
|
-
);
|
|
2872
|
-
|
|
2873
|
-
expect(statements.length).toBe(0);
|
|
2874
|
-
expect(sqlStatements.length).toBe(0);
|
|
2875
|
-
|
|
2876
|
-
for (const st of sqlStatements) {
|
|
2877
|
-
await client.query(st);
|
|
2878
|
-
}
|
|
2879
|
-
});
|
|
2880
|
-
|
|
2881
|
-
test('add policy', async () => {
|
|
2882
|
-
const client = new PGlite();
|
|
2883
|
-
|
|
2884
|
-
const schema1 = {
|
|
2885
|
-
users: pgTable('users', {
|
|
2886
|
-
id: integer('id').primaryKey(),
|
|
2887
|
-
}),
|
|
2888
|
-
};
|
|
2889
|
-
|
|
2890
|
-
const schema2 = {
|
|
2891
|
-
users: pgTable('users', {
|
|
2892
|
-
id: integer('id').primaryKey(),
|
|
2893
|
-
}, () => ({
|
|
2894
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
2895
|
-
})),
|
|
2896
|
-
};
|
|
2897
|
-
|
|
2898
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
2899
|
-
client,
|
|
2900
|
-
schema1,
|
|
2901
|
-
schema2,
|
|
2902
|
-
[],
|
|
2903
|
-
false,
|
|
2904
|
-
['public'],
|
|
2905
|
-
);
|
|
2906
|
-
|
|
2907
|
-
expect(statements).toStrictEqual([
|
|
2908
|
-
{ type: 'enable_rls', tableName: 'users', schema: '' },
|
|
2909
|
-
{
|
|
2910
|
-
type: 'create_policy',
|
|
2911
|
-
tableName: 'users',
|
|
2912
|
-
data: {
|
|
2913
|
-
name: 'test',
|
|
2914
|
-
as: 'PERMISSIVE',
|
|
2915
|
-
for: 'ALL',
|
|
2916
|
-
to: ['public'],
|
|
2917
|
-
on: undefined,
|
|
2918
|
-
},
|
|
2919
|
-
schema: '',
|
|
2920
|
-
},
|
|
2921
|
-
]);
|
|
2922
|
-
expect(sqlStatements).toStrictEqual([
|
|
2923
|
-
'ALTER TABLE "users" ENABLE ROW LEVEL SECURITY;',
|
|
2924
|
-
'CREATE POLICY "test" ON "users" AS PERMISSIVE FOR ALL TO public;',
|
|
2925
|
-
]);
|
|
2926
|
-
|
|
2927
|
-
for (const st of sqlStatements) {
|
|
2928
|
-
await client.query(st);
|
|
2929
|
-
}
|
|
2930
|
-
});
|
|
2931
|
-
|
|
2932
|
-
test('drop policy', async () => {
|
|
2933
|
-
const client = new PGlite();
|
|
2934
|
-
|
|
2935
|
-
const schema1 = {
|
|
2936
|
-
users: pgTable('users', {
|
|
2937
|
-
id: integer('id').primaryKey(),
|
|
2938
|
-
}, () => ({
|
|
2939
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
2940
|
-
})),
|
|
2941
|
-
};
|
|
2942
|
-
|
|
2943
|
-
const schema2 = {
|
|
2944
|
-
users: pgTable('users', {
|
|
2945
|
-
id: integer('id').primaryKey(),
|
|
2946
|
-
}),
|
|
2947
|
-
};
|
|
2948
|
-
|
|
2949
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
2950
|
-
client,
|
|
2951
|
-
schema1,
|
|
2952
|
-
schema2,
|
|
2953
|
-
[],
|
|
2954
|
-
false,
|
|
2955
|
-
['public'],
|
|
2956
|
-
);
|
|
2957
|
-
|
|
2958
|
-
expect(statements).toStrictEqual([
|
|
2959
|
-
{ type: 'disable_rls', tableName: 'users', schema: '' },
|
|
2960
|
-
{
|
|
2961
|
-
schema: '',
|
|
2962
|
-
tableName: 'users',
|
|
2963
|
-
type: 'disable_rls',
|
|
2964
|
-
},
|
|
2965
|
-
{
|
|
2966
|
-
type: 'drop_policy',
|
|
2967
|
-
tableName: 'users',
|
|
2968
|
-
data: {
|
|
2969
|
-
name: 'test',
|
|
2970
|
-
as: 'PERMISSIVE',
|
|
2971
|
-
for: 'ALL',
|
|
2972
|
-
to: ['public'],
|
|
2973
|
-
on: undefined,
|
|
2974
|
-
},
|
|
2975
|
-
schema: '',
|
|
2976
|
-
},
|
|
2977
|
-
]);
|
|
2978
|
-
expect(sqlStatements).toStrictEqual([
|
|
2979
|
-
'ALTER TABLE "users" DISABLE ROW LEVEL SECURITY;',
|
|
2980
|
-
'DROP POLICY "test" ON "users" CASCADE;',
|
|
2981
|
-
]);
|
|
2982
|
-
|
|
2983
|
-
for (const st of sqlStatements) {
|
|
2984
|
-
await client.query(st);
|
|
2985
|
-
}
|
|
2986
|
-
});
|
|
2987
|
-
|
|
2988
|
-
test('add policy without enable rls', async () => {
|
|
2989
|
-
const client = new PGlite();
|
|
2990
|
-
|
|
2991
|
-
const schema1 = {
|
|
2992
|
-
users: pgTable('users', {
|
|
2993
|
-
id: integer('id').primaryKey(),
|
|
2994
|
-
}, () => ({
|
|
2995
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
2996
|
-
})),
|
|
2997
|
-
};
|
|
2998
|
-
|
|
2999
|
-
const schema2 = {
|
|
3000
|
-
users: pgTable('users', {
|
|
3001
|
-
id: integer('id').primaryKey(),
|
|
3002
|
-
}, () => ({
|
|
3003
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3004
|
-
newrls: pgPolicy('newRls'),
|
|
3005
|
-
})),
|
|
3006
|
-
};
|
|
3007
|
-
|
|
3008
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3009
|
-
client,
|
|
3010
|
-
schema1,
|
|
3011
|
-
schema2,
|
|
3012
|
-
[],
|
|
3013
|
-
false,
|
|
3014
|
-
['public'],
|
|
3015
|
-
);
|
|
3016
|
-
|
|
3017
|
-
expect(statements).toStrictEqual([
|
|
3018
|
-
{
|
|
3019
|
-
type: 'create_policy',
|
|
3020
|
-
tableName: 'users',
|
|
3021
|
-
data: {
|
|
3022
|
-
name: 'newRls',
|
|
3023
|
-
as: 'PERMISSIVE',
|
|
3024
|
-
for: 'ALL',
|
|
3025
|
-
to: ['public'],
|
|
3026
|
-
on: undefined,
|
|
3027
|
-
},
|
|
3028
|
-
schema: '',
|
|
3029
|
-
},
|
|
3030
|
-
]);
|
|
3031
|
-
expect(sqlStatements).toStrictEqual([
|
|
3032
|
-
'CREATE POLICY "newRls" ON "users" AS PERMISSIVE FOR ALL TO public;',
|
|
3033
|
-
]);
|
|
3034
|
-
|
|
3035
|
-
for (const st of sqlStatements) {
|
|
3036
|
-
await client.query(st);
|
|
3037
|
-
}
|
|
3038
|
-
});
|
|
3039
|
-
|
|
3040
|
-
test('drop policy without disable rls', async () => {
|
|
3041
|
-
const client = new PGlite();
|
|
3042
|
-
|
|
3043
|
-
const schema1 = {
|
|
3044
|
-
users: pgTable('users', {
|
|
3045
|
-
id: integer('id').primaryKey(),
|
|
3046
|
-
}, () => ({
|
|
3047
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3048
|
-
oldRls: pgPolicy('oldRls'),
|
|
3049
|
-
})),
|
|
3050
|
-
};
|
|
3051
|
-
|
|
3052
|
-
const schema2 = {
|
|
3053
|
-
users: pgTable('users', {
|
|
3054
|
-
id: integer('id').primaryKey(),
|
|
3055
|
-
}, () => ({
|
|
3056
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3057
|
-
})),
|
|
3058
|
-
};
|
|
3059
|
-
|
|
3060
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3061
|
-
client,
|
|
3062
|
-
schema1,
|
|
3063
|
-
schema2,
|
|
3064
|
-
[],
|
|
3065
|
-
false,
|
|
3066
|
-
['public'],
|
|
3067
|
-
);
|
|
3068
|
-
|
|
3069
|
-
expect(statements).toStrictEqual([
|
|
3070
|
-
{
|
|
3071
|
-
type: 'drop_policy',
|
|
3072
|
-
tableName: 'users',
|
|
3073
|
-
data: {
|
|
3074
|
-
name: 'oldRls',
|
|
3075
|
-
as: 'PERMISSIVE',
|
|
3076
|
-
for: 'ALL',
|
|
3077
|
-
to: ['public'],
|
|
3078
|
-
on: undefined,
|
|
3079
|
-
},
|
|
3080
|
-
schema: '',
|
|
3081
|
-
},
|
|
3082
|
-
]);
|
|
3083
|
-
expect(sqlStatements).toStrictEqual([
|
|
3084
|
-
'DROP POLICY "oldRls" ON "users" CASCADE;',
|
|
3085
|
-
]);
|
|
3086
|
-
|
|
3087
|
-
for (const st of sqlStatements) {
|
|
3088
|
-
await client.query(st);
|
|
3089
|
-
}
|
|
3090
|
-
});
|
|
3091
|
-
|
|
3092
|
-
////
|
|
3093
|
-
|
|
3094
|
-
test('alter policy without recreation: changing roles', async (t) => {
|
|
3095
|
-
const client = new PGlite();
|
|
3096
|
-
|
|
3097
|
-
const schema1 = {
|
|
3098
|
-
users: pgTable('users', {
|
|
3099
|
-
id: integer('id').primaryKey(),
|
|
3100
|
-
}, () => ({
|
|
3101
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3102
|
-
})),
|
|
3103
|
-
};
|
|
3104
|
-
|
|
3105
|
-
const schema2 = {
|
|
3106
|
-
users: pgTable('users', {
|
|
3107
|
-
id: integer('id').primaryKey(),
|
|
3108
|
-
}, () => ({
|
|
3109
|
-
rls: pgPolicy('test', { as: 'permissive', to: 'current_role' }),
|
|
3110
|
-
})),
|
|
3111
|
-
};
|
|
3112
|
-
|
|
3113
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3114
|
-
client,
|
|
3115
|
-
schema1,
|
|
3116
|
-
schema2,
|
|
3117
|
-
[],
|
|
3118
|
-
false,
|
|
3119
|
-
['public'],
|
|
3120
|
-
);
|
|
3121
|
-
|
|
3122
|
-
expect(sqlStatements).toStrictEqual([
|
|
3123
|
-
'ALTER POLICY "test" ON "users" TO current_role;',
|
|
3124
|
-
]);
|
|
3125
|
-
expect(statements).toStrictEqual([
|
|
3126
|
-
{
|
|
3127
|
-
newData: 'test--PERMISSIVE--ALL--current_role--undefined',
|
|
3128
|
-
oldData: 'test--PERMISSIVE--ALL--public--undefined',
|
|
3129
|
-
schema: '',
|
|
3130
|
-
tableName: 'users',
|
|
3131
|
-
type: 'alter_policy',
|
|
3132
|
-
},
|
|
3133
|
-
]);
|
|
3134
|
-
|
|
3135
|
-
for (const st of sqlStatements) {
|
|
3136
|
-
await client.query(st);
|
|
3137
|
-
}
|
|
3138
|
-
});
|
|
3139
|
-
|
|
3140
|
-
test('alter policy without recreation: changing using', async (t) => {
|
|
3141
|
-
const client = new PGlite();
|
|
3142
|
-
|
|
3143
|
-
const schema1 = {
|
|
3144
|
-
users: pgTable('users', {
|
|
3145
|
-
id: integer('id').primaryKey(),
|
|
3146
|
-
}, () => ({
|
|
3147
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3148
|
-
})),
|
|
3149
|
-
};
|
|
3150
|
-
|
|
3151
|
-
const schema2 = {
|
|
3152
|
-
users: pgTable('users', {
|
|
3153
|
-
id: integer('id').primaryKey(),
|
|
3154
|
-
}, () => ({
|
|
3155
|
-
rls: pgPolicy('test', { as: 'permissive', using: sql`true` }),
|
|
3156
|
-
})),
|
|
3157
|
-
};
|
|
3158
|
-
|
|
3159
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3160
|
-
client,
|
|
3161
|
-
schema1,
|
|
3162
|
-
schema2,
|
|
3163
|
-
[],
|
|
3164
|
-
false,
|
|
3165
|
-
['public'],
|
|
3166
|
-
);
|
|
3167
|
-
|
|
3168
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
3169
|
-
expect(statements).toStrictEqual([]);
|
|
3170
|
-
|
|
3171
|
-
for (const st of sqlStatements) {
|
|
3172
|
-
await client.query(st);
|
|
3173
|
-
}
|
|
3174
|
-
});
|
|
3175
|
-
|
|
3176
|
-
test('alter policy without recreation: changing with check', async (t) => {
|
|
3177
|
-
const client = new PGlite();
|
|
3178
|
-
|
|
3179
|
-
const schema1 = {
|
|
3180
|
-
users: pgTable('users', {
|
|
3181
|
-
id: integer('id').primaryKey(),
|
|
3182
|
-
}, () => ({
|
|
3183
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3184
|
-
})),
|
|
3185
|
-
};
|
|
3186
|
-
|
|
3187
|
-
const schema2 = {
|
|
3188
|
-
users: pgTable('users', {
|
|
3189
|
-
id: integer('id').primaryKey(),
|
|
3190
|
-
}, () => ({
|
|
3191
|
-
rls: pgPolicy('test', { as: 'permissive', withCheck: sql`true` }),
|
|
3192
|
-
})),
|
|
3193
|
-
};
|
|
3194
|
-
|
|
3195
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3196
|
-
client,
|
|
3197
|
-
schema1,
|
|
3198
|
-
schema2,
|
|
3199
|
-
[],
|
|
3200
|
-
false,
|
|
3201
|
-
['public'],
|
|
3202
|
-
);
|
|
3203
|
-
|
|
3204
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
3205
|
-
expect(statements).toStrictEqual([]);
|
|
3206
|
-
|
|
3207
|
-
for (const st of sqlStatements) {
|
|
3208
|
-
await client.query(st);
|
|
3209
|
-
}
|
|
3210
|
-
});
|
|
3211
|
-
|
|
3212
|
-
test('alter policy with recreation: changing as', async (t) => {
|
|
3213
|
-
const client = new PGlite();
|
|
3214
|
-
|
|
3215
|
-
const schema1 = {
|
|
3216
|
-
users: pgTable('users', {
|
|
3217
|
-
id: integer('id').primaryKey(),
|
|
3218
|
-
}, () => ({
|
|
3219
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3220
|
-
})),
|
|
3221
|
-
};
|
|
3222
|
-
|
|
3223
|
-
const schema2 = {
|
|
3224
|
-
users: pgTable('users', {
|
|
3225
|
-
id: integer('id').primaryKey(),
|
|
3226
|
-
}, () => ({
|
|
3227
|
-
rls: pgPolicy('test', { as: 'restrictive' }),
|
|
3228
|
-
})),
|
|
3229
|
-
};
|
|
3230
|
-
|
|
3231
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3232
|
-
client,
|
|
3233
|
-
schema1,
|
|
3234
|
-
schema2,
|
|
3235
|
-
[],
|
|
3236
|
-
false,
|
|
3237
|
-
['public'],
|
|
3238
|
-
);
|
|
3239
|
-
|
|
3240
|
-
expect(sqlStatements).toStrictEqual([
|
|
3241
|
-
'DROP POLICY "test" ON "users" CASCADE;',
|
|
3242
|
-
'CREATE POLICY "test" ON "users" AS RESTRICTIVE FOR ALL TO public;',
|
|
3243
|
-
]);
|
|
3244
|
-
expect(statements).toStrictEqual([
|
|
3245
|
-
{
|
|
3246
|
-
data: {
|
|
3247
|
-
as: 'PERMISSIVE',
|
|
3248
|
-
for: 'ALL',
|
|
3249
|
-
name: 'test',
|
|
3250
|
-
to: ['public'],
|
|
3251
|
-
on: undefined,
|
|
3252
|
-
},
|
|
3253
|
-
schema: '',
|
|
3254
|
-
tableName: 'users',
|
|
3255
|
-
type: 'drop_policy',
|
|
3256
|
-
},
|
|
3257
|
-
{
|
|
3258
|
-
data: {
|
|
3259
|
-
as: 'RESTRICTIVE',
|
|
3260
|
-
for: 'ALL',
|
|
3261
|
-
name: 'test',
|
|
3262
|
-
to: ['public'],
|
|
3263
|
-
on: undefined,
|
|
3264
|
-
},
|
|
3265
|
-
schema: '',
|
|
3266
|
-
tableName: 'users',
|
|
3267
|
-
type: 'create_policy',
|
|
3268
|
-
},
|
|
3269
|
-
]);
|
|
3270
|
-
|
|
3271
|
-
for (const st of sqlStatements) {
|
|
3272
|
-
await client.query(st);
|
|
3273
|
-
}
|
|
3274
|
-
});
|
|
3275
|
-
|
|
3276
|
-
test('alter policy with recreation: changing for', async (t) => {
|
|
3277
|
-
const client = new PGlite();
|
|
3278
|
-
|
|
3279
|
-
const schema1 = {
|
|
3280
|
-
users: pgTable('users', {
|
|
3281
|
-
id: integer('id').primaryKey(),
|
|
3282
|
-
}, () => ({
|
|
3283
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3284
|
-
})),
|
|
3285
|
-
};
|
|
3286
|
-
|
|
3287
|
-
const schema2 = {
|
|
3288
|
-
users: pgTable('users', {
|
|
3289
|
-
id: integer('id').primaryKey(),
|
|
3290
|
-
}, () => ({
|
|
3291
|
-
rls: pgPolicy('test', { as: 'permissive', for: 'delete' }),
|
|
3292
|
-
})),
|
|
3293
|
-
};
|
|
3294
|
-
|
|
3295
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3296
|
-
client,
|
|
3297
|
-
schema1,
|
|
3298
|
-
schema2,
|
|
3299
|
-
[],
|
|
3300
|
-
false,
|
|
3301
|
-
['public'],
|
|
3302
|
-
);
|
|
3303
|
-
|
|
3304
|
-
expect(sqlStatements).toStrictEqual([
|
|
3305
|
-
'DROP POLICY "test" ON "users" CASCADE;',
|
|
3306
|
-
'CREATE POLICY "test" ON "users" AS PERMISSIVE FOR DELETE TO public;',
|
|
3307
|
-
]);
|
|
3308
|
-
expect(statements).toStrictEqual([
|
|
3309
|
-
{
|
|
3310
|
-
data: {
|
|
3311
|
-
as: 'PERMISSIVE',
|
|
3312
|
-
for: 'ALL',
|
|
3313
|
-
name: 'test',
|
|
3314
|
-
to: ['public'],
|
|
3315
|
-
on: undefined,
|
|
3316
|
-
},
|
|
3317
|
-
schema: '',
|
|
3318
|
-
tableName: 'users',
|
|
3319
|
-
type: 'drop_policy',
|
|
3320
|
-
},
|
|
3321
|
-
{
|
|
3322
|
-
data: {
|
|
3323
|
-
as: 'PERMISSIVE',
|
|
3324
|
-
for: 'DELETE',
|
|
3325
|
-
name: 'test',
|
|
3326
|
-
to: ['public'],
|
|
3327
|
-
on: undefined,
|
|
3328
|
-
},
|
|
3329
|
-
schema: '',
|
|
3330
|
-
tableName: 'users',
|
|
3331
|
-
type: 'create_policy',
|
|
3332
|
-
},
|
|
3333
|
-
]);
|
|
3334
|
-
|
|
3335
|
-
for (const st of sqlStatements) {
|
|
3336
|
-
await client.query(st);
|
|
3337
|
-
}
|
|
3338
|
-
});
|
|
3339
|
-
|
|
3340
|
-
test('alter policy with recreation: changing both "as" and "for"', async (t) => {
|
|
3341
|
-
const client = new PGlite();
|
|
3342
|
-
|
|
3343
|
-
const schema1 = {
|
|
3344
|
-
users: pgTable('users', {
|
|
3345
|
-
id: integer('id').primaryKey(),
|
|
3346
|
-
}, () => ({
|
|
3347
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3348
|
-
})),
|
|
3349
|
-
};
|
|
3350
|
-
|
|
3351
|
-
const schema2 = {
|
|
3352
|
-
users: pgTable('users', {
|
|
3353
|
-
id: integer('id').primaryKey(),
|
|
3354
|
-
}, () => ({
|
|
3355
|
-
rls: pgPolicy('test', { as: 'restrictive', for: 'insert' }),
|
|
3356
|
-
})),
|
|
3357
|
-
};
|
|
3358
|
-
|
|
3359
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3360
|
-
client,
|
|
3361
|
-
schema1,
|
|
3362
|
-
schema2,
|
|
3363
|
-
[],
|
|
3364
|
-
false,
|
|
3365
|
-
['public'],
|
|
3366
|
-
);
|
|
3367
|
-
|
|
3368
|
-
expect(sqlStatements).toStrictEqual([
|
|
3369
|
-
'DROP POLICY "test" ON "users" CASCADE;',
|
|
3370
|
-
'CREATE POLICY "test" ON "users" AS RESTRICTIVE FOR INSERT TO public;',
|
|
3371
|
-
]);
|
|
3372
|
-
expect(statements).toStrictEqual([
|
|
3373
|
-
{
|
|
3374
|
-
data: {
|
|
3375
|
-
as: 'PERMISSIVE',
|
|
3376
|
-
for: 'ALL',
|
|
3377
|
-
name: 'test',
|
|
3378
|
-
to: ['public'],
|
|
3379
|
-
on: undefined,
|
|
3380
|
-
},
|
|
3381
|
-
schema: '',
|
|
3382
|
-
tableName: 'users',
|
|
3383
|
-
type: 'drop_policy',
|
|
3384
|
-
},
|
|
3385
|
-
{
|
|
3386
|
-
data: {
|
|
3387
|
-
as: 'RESTRICTIVE',
|
|
3388
|
-
for: 'INSERT',
|
|
3389
|
-
name: 'test',
|
|
3390
|
-
to: ['public'],
|
|
3391
|
-
on: undefined,
|
|
3392
|
-
},
|
|
3393
|
-
schema: '',
|
|
3394
|
-
tableName: 'users',
|
|
3395
|
-
type: 'create_policy',
|
|
3396
|
-
},
|
|
3397
|
-
]);
|
|
3398
|
-
|
|
3399
|
-
for (const st of sqlStatements) {
|
|
3400
|
-
await client.query(st);
|
|
3401
|
-
}
|
|
3402
|
-
});
|
|
3403
|
-
|
|
3404
|
-
test('alter policy with recreation: changing all fields', async (t) => {
|
|
3405
|
-
const client = new PGlite();
|
|
3406
|
-
|
|
3407
|
-
const schema1 = {
|
|
3408
|
-
users: pgTable('users', {
|
|
3409
|
-
id: integer('id').primaryKey(),
|
|
3410
|
-
}, () => ({
|
|
3411
|
-
rls: pgPolicy('test', { as: 'permissive', for: 'select', using: sql`true` }),
|
|
3412
|
-
})),
|
|
3413
|
-
};
|
|
3414
|
-
|
|
3415
|
-
const schema2 = {
|
|
3416
|
-
users: pgTable('users', {
|
|
3417
|
-
id: integer('id').primaryKey(),
|
|
3418
|
-
}, () => ({
|
|
3419
|
-
rls: pgPolicy('test', { as: 'restrictive', to: 'current_role', withCheck: sql`true` }),
|
|
3420
|
-
})),
|
|
3421
|
-
};
|
|
3422
|
-
|
|
3423
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3424
|
-
client,
|
|
3425
|
-
schema1,
|
|
3426
|
-
schema2,
|
|
3427
|
-
[],
|
|
3428
|
-
false,
|
|
3429
|
-
['public'],
|
|
3430
|
-
);
|
|
3431
|
-
|
|
3432
|
-
expect(sqlStatements).toStrictEqual([
|
|
3433
|
-
'DROP POLICY "test" ON "users" CASCADE;',
|
|
3434
|
-
'CREATE POLICY "test" ON "users" AS RESTRICTIVE FOR ALL TO current_role;',
|
|
3435
|
-
]);
|
|
3436
|
-
expect(statements).toStrictEqual([
|
|
3437
|
-
{
|
|
3438
|
-
data: {
|
|
3439
|
-
as: 'PERMISSIVE',
|
|
3440
|
-
for: 'SELECT',
|
|
3441
|
-
name: 'test',
|
|
3442
|
-
to: ['public'],
|
|
3443
|
-
on: undefined,
|
|
3444
|
-
},
|
|
3445
|
-
schema: '',
|
|
3446
|
-
tableName: 'users',
|
|
3447
|
-
type: 'drop_policy',
|
|
3448
|
-
},
|
|
3449
|
-
{
|
|
3450
|
-
data: {
|
|
3451
|
-
as: 'RESTRICTIVE',
|
|
3452
|
-
for: 'ALL',
|
|
3453
|
-
name: 'test',
|
|
3454
|
-
to: ['current_role'],
|
|
3455
|
-
on: undefined,
|
|
3456
|
-
},
|
|
3457
|
-
schema: '',
|
|
3458
|
-
tableName: 'users',
|
|
3459
|
-
type: 'create_policy',
|
|
3460
|
-
},
|
|
3461
|
-
]);
|
|
3462
|
-
|
|
3463
|
-
for (const st of sqlStatements) {
|
|
3464
|
-
await client.query(st);
|
|
3465
|
-
}
|
|
3466
|
-
});
|
|
3467
|
-
|
|
3468
|
-
test('rename policy', async (t) => {
|
|
3469
|
-
const client = new PGlite();
|
|
3470
|
-
|
|
3471
|
-
const schema1 = {
|
|
3472
|
-
users: pgTable('users', {
|
|
3473
|
-
id: integer('id').primaryKey(),
|
|
3474
|
-
}, () => ({
|
|
3475
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3476
|
-
})),
|
|
3477
|
-
};
|
|
3478
|
-
|
|
3479
|
-
const schema2 = {
|
|
3480
|
-
users: pgTable('users', {
|
|
3481
|
-
id: integer('id').primaryKey(),
|
|
3482
|
-
}, () => ({
|
|
3483
|
-
rls: pgPolicy('newName', { as: 'permissive' }),
|
|
3484
|
-
})),
|
|
3485
|
-
};
|
|
3486
|
-
|
|
3487
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3488
|
-
client,
|
|
3489
|
-
schema1,
|
|
3490
|
-
schema2,
|
|
3491
|
-
['public.users.test->public.users.newName'],
|
|
3492
|
-
false,
|
|
3493
|
-
['public'],
|
|
3494
|
-
);
|
|
3495
|
-
|
|
3496
|
-
expect(sqlStatements).toStrictEqual([
|
|
3497
|
-
'ALTER POLICY "test" ON "users" RENAME TO "newName";',
|
|
3498
|
-
]);
|
|
3499
|
-
expect(statements).toStrictEqual([
|
|
3500
|
-
{
|
|
3501
|
-
newName: 'newName',
|
|
3502
|
-
oldName: 'test',
|
|
3503
|
-
schema: '',
|
|
3504
|
-
tableName: 'users',
|
|
3505
|
-
type: 'rename_policy',
|
|
3506
|
-
},
|
|
3507
|
-
]);
|
|
3508
|
-
|
|
3509
|
-
for (const st of sqlStatements) {
|
|
3510
|
-
await client.query(st);
|
|
3511
|
-
}
|
|
3512
|
-
});
|
|
3513
|
-
|
|
3514
|
-
test('rename policy in renamed table', async (t) => {
|
|
3515
|
-
const client = new PGlite();
|
|
3516
|
-
|
|
3517
|
-
const schema1 = {
|
|
3518
|
-
users: pgTable('users', {
|
|
3519
|
-
id: integer('id').primaryKey(),
|
|
3520
|
-
}, () => ({
|
|
3521
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3522
|
-
})),
|
|
3523
|
-
};
|
|
3524
|
-
|
|
3525
|
-
const schema2 = {
|
|
3526
|
-
users: pgTable('users2', {
|
|
3527
|
-
id: integer('id').primaryKey(),
|
|
3528
|
-
}, () => ({
|
|
3529
|
-
rls: pgPolicy('newName', { as: 'permissive' }),
|
|
3530
|
-
})),
|
|
3531
|
-
};
|
|
3532
|
-
|
|
3533
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3534
|
-
client,
|
|
3535
|
-
schema1,
|
|
3536
|
-
schema2,
|
|
3537
|
-
[
|
|
3538
|
-
'public.users->public.users2',
|
|
3539
|
-
'public.users2.test->public.users2.newName',
|
|
3540
|
-
],
|
|
3541
|
-
false,
|
|
3542
|
-
['public'],
|
|
3543
|
-
);
|
|
3544
|
-
|
|
3545
|
-
expect(sqlStatements).toStrictEqual([
|
|
3546
|
-
'ALTER TABLE "users" RENAME TO "users2";',
|
|
3547
|
-
'ALTER POLICY "test" ON "users2" RENAME TO "newName";',
|
|
3548
|
-
]);
|
|
3549
|
-
expect(statements).toStrictEqual([
|
|
3550
|
-
{
|
|
3551
|
-
fromSchema: '',
|
|
3552
|
-
tableNameFrom: 'users',
|
|
3553
|
-
tableNameTo: 'users2',
|
|
3554
|
-
toSchema: '',
|
|
3555
|
-
type: 'rename_table',
|
|
3556
|
-
},
|
|
3557
|
-
{
|
|
3558
|
-
newName: 'newName',
|
|
3559
|
-
oldName: 'test',
|
|
3560
|
-
schema: '',
|
|
3561
|
-
tableName: 'users2',
|
|
3562
|
-
type: 'rename_policy',
|
|
3563
|
-
},
|
|
3564
|
-
]);
|
|
3565
|
-
|
|
3566
|
-
for (const st of sqlStatements) {
|
|
3567
|
-
await client.query(st);
|
|
3568
|
-
}
|
|
3569
|
-
});
|
|
3570
|
-
|
|
3571
|
-
test('create table with a policy', async (t) => {
|
|
3572
|
-
const client = new PGlite();
|
|
3573
|
-
|
|
3574
|
-
const schema1 = {};
|
|
3575
|
-
|
|
3576
|
-
const schema2 = {
|
|
3577
|
-
users: pgTable('users2', {
|
|
3578
|
-
id: integer('id').primaryKey(),
|
|
3579
|
-
}, () => ({
|
|
3580
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3581
|
-
})),
|
|
3582
|
-
};
|
|
3583
|
-
|
|
3584
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3585
|
-
client,
|
|
3586
|
-
schema1,
|
|
3587
|
-
schema2,
|
|
3588
|
-
[],
|
|
3589
|
-
false,
|
|
3590
|
-
['public'],
|
|
3591
|
-
);
|
|
3592
|
-
|
|
3593
|
-
expect(sqlStatements).toStrictEqual([
|
|
3594
|
-
'CREATE TABLE "users2" (\n\t"id" integer PRIMARY KEY NOT NULL\n);\n',
|
|
3595
|
-
'ALTER TABLE "users2" ENABLE ROW LEVEL SECURITY;',
|
|
3596
|
-
'CREATE POLICY "test" ON "users2" AS PERMISSIVE FOR ALL TO public;',
|
|
3597
|
-
]);
|
|
3598
|
-
expect(statements).toStrictEqual([
|
|
3599
|
-
{
|
|
3600
|
-
columns: [
|
|
3601
|
-
{
|
|
3602
|
-
name: 'id',
|
|
3603
|
-
notNull: true,
|
|
3604
|
-
primaryKey: true,
|
|
3605
|
-
type: 'integer',
|
|
3606
|
-
},
|
|
3607
|
-
],
|
|
3608
|
-
checkConstraints: [],
|
|
3609
|
-
compositePKs: [],
|
|
3610
|
-
isRLSEnabled: false,
|
|
3611
|
-
compositePkName: '',
|
|
3612
|
-
policies: [
|
|
3613
|
-
'test--PERMISSIVE--ALL--public--undefined',
|
|
3614
|
-
],
|
|
3615
|
-
schema: '',
|
|
3616
|
-
tableName: 'users2',
|
|
3617
|
-
type: 'create_table',
|
|
3618
|
-
uniqueConstraints: [],
|
|
3619
|
-
},
|
|
3620
|
-
{
|
|
3621
|
-
data: {
|
|
3622
|
-
as: 'PERMISSIVE',
|
|
3623
|
-
for: 'ALL',
|
|
3624
|
-
name: 'test',
|
|
3625
|
-
to: [
|
|
3626
|
-
'public',
|
|
3627
|
-
],
|
|
3628
|
-
on: undefined,
|
|
3629
|
-
},
|
|
3630
|
-
schema: '',
|
|
3631
|
-
tableName: 'users2',
|
|
3632
|
-
type: 'create_policy',
|
|
3633
|
-
},
|
|
3634
|
-
]);
|
|
3635
|
-
|
|
3636
|
-
for (const st of sqlStatements) {
|
|
3637
|
-
await client.query(st);
|
|
3638
|
-
}
|
|
3639
|
-
});
|
|
3640
|
-
|
|
3641
|
-
test('drop table with a policy', async (t) => {
|
|
3642
|
-
const client = new PGlite();
|
|
3643
|
-
|
|
3644
|
-
const schema1 = {
|
|
3645
|
-
users: pgTable('users2', {
|
|
3646
|
-
id: integer('id').primaryKey(),
|
|
3647
|
-
}, () => ({
|
|
3648
|
-
rls: pgPolicy('test', { as: 'permissive' }),
|
|
3649
|
-
})),
|
|
3650
|
-
};
|
|
3651
|
-
|
|
3652
|
-
const schema2 = {};
|
|
3653
|
-
|
|
3654
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3655
|
-
client,
|
|
3656
|
-
schema1,
|
|
3657
|
-
schema2,
|
|
3658
|
-
[],
|
|
3659
|
-
false,
|
|
3660
|
-
['public'],
|
|
3661
|
-
);
|
|
3662
|
-
|
|
3663
|
-
expect(sqlStatements).toStrictEqual([
|
|
3664
|
-
'DROP POLICY "test" ON "users2" CASCADE;',
|
|
3665
|
-
'DROP TABLE "users2" CASCADE;',
|
|
3666
|
-
]);
|
|
3667
|
-
expect(statements).toStrictEqual([
|
|
3668
|
-
{
|
|
3669
|
-
policies: [
|
|
3670
|
-
'test--PERMISSIVE--ALL--public--undefined',
|
|
3671
|
-
],
|
|
3672
|
-
schema: '',
|
|
3673
|
-
tableName: 'users2',
|
|
3674
|
-
type: 'drop_table',
|
|
3675
|
-
},
|
|
3676
|
-
]);
|
|
3677
|
-
|
|
3678
|
-
for (const st of sqlStatements) {
|
|
3679
|
-
await client.query(st);
|
|
3680
|
-
}
|
|
3681
|
-
});
|
|
3682
|
-
|
|
3683
|
-
test('add policy with multiple "to" roles', async (t) => {
|
|
3684
|
-
const client = new PGlite();
|
|
3685
|
-
|
|
3686
|
-
client.query(`CREATE ROLE manager;`);
|
|
3687
|
-
|
|
3688
|
-
const schema1 = {
|
|
3689
|
-
users: pgTable('users', {
|
|
3690
|
-
id: integer('id').primaryKey(),
|
|
3691
|
-
}),
|
|
3692
|
-
};
|
|
3693
|
-
|
|
3694
|
-
const role = pgRole('manager').existing();
|
|
3695
|
-
|
|
3696
|
-
const schema2 = {
|
|
3697
|
-
role,
|
|
3698
|
-
users: pgTable('users', {
|
|
3699
|
-
id: integer('id').primaryKey(),
|
|
3700
|
-
}, () => ({
|
|
3701
|
-
rls: pgPolicy('test', { to: ['current_role', role] }),
|
|
3702
|
-
})),
|
|
3703
|
-
};
|
|
3704
|
-
|
|
3705
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3706
|
-
client,
|
|
3707
|
-
schema1,
|
|
3708
|
-
schema2,
|
|
3709
|
-
[],
|
|
3710
|
-
false,
|
|
3711
|
-
['public'],
|
|
3712
|
-
);
|
|
3713
|
-
|
|
3714
|
-
expect(sqlStatements).toStrictEqual([
|
|
3715
|
-
'ALTER TABLE "users" ENABLE ROW LEVEL SECURITY;',
|
|
3716
|
-
'CREATE POLICY "test" ON "users" AS PERMISSIVE FOR ALL TO current_role, "manager";',
|
|
3717
|
-
]);
|
|
3718
|
-
expect(statements).toStrictEqual([
|
|
3719
|
-
{
|
|
3720
|
-
schema: '',
|
|
3721
|
-
tableName: 'users',
|
|
3722
|
-
type: 'enable_rls',
|
|
3723
|
-
},
|
|
3724
|
-
{
|
|
3725
|
-
data: {
|
|
3726
|
-
as: 'PERMISSIVE',
|
|
3727
|
-
for: 'ALL',
|
|
3728
|
-
name: 'test',
|
|
3729
|
-
on: undefined,
|
|
3730
|
-
to: ['current_role', 'manager'],
|
|
3731
|
-
},
|
|
3732
|
-
schema: '',
|
|
3733
|
-
tableName: 'users',
|
|
3734
|
-
type: 'create_policy',
|
|
3735
|
-
},
|
|
3736
|
-
]);
|
|
3737
|
-
|
|
3738
|
-
for (const st of sqlStatements) {
|
|
3739
|
-
await client.query(st);
|
|
3740
|
-
}
|
|
3741
|
-
});
|
|
3742
|
-
|
|
3743
|
-
test('rename policy that is linked', async (t) => {
|
|
3744
|
-
const client = new PGlite();
|
|
3745
|
-
|
|
3746
|
-
const users = pgTable('users', {
|
|
3747
|
-
id: integer('id').primaryKey(),
|
|
3748
|
-
});
|
|
3749
|
-
|
|
3750
|
-
const { sqlStatements: createUsers } = await diffTestSchemas({}, { users }, []);
|
|
3751
|
-
|
|
3752
|
-
const schema1 = {
|
|
3753
|
-
rls: pgPolicy('test', { as: 'permissive' }).link(users),
|
|
3754
|
-
};
|
|
3755
|
-
|
|
3756
|
-
const schema2 = {
|
|
3757
|
-
users,
|
|
3758
|
-
rls: pgPolicy('newName', { as: 'permissive' }).link(users),
|
|
3759
|
-
};
|
|
3760
|
-
|
|
3761
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3762
|
-
client,
|
|
3763
|
-
schema1,
|
|
3764
|
-
schema2,
|
|
3765
|
-
['public.users.test->public.users.newName'],
|
|
3766
|
-
false,
|
|
3767
|
-
['public'],
|
|
3768
|
-
undefined,
|
|
3769
|
-
undefined,
|
|
3770
|
-
{ before: createUsers },
|
|
3771
|
-
);
|
|
3772
|
-
|
|
3773
|
-
expect(sqlStatements).toStrictEqual([
|
|
3774
|
-
'ALTER POLICY "test" ON "users" RENAME TO "newName";',
|
|
3775
|
-
]);
|
|
3776
|
-
expect(statements).toStrictEqual([
|
|
3777
|
-
{
|
|
3778
|
-
newName: 'newName',
|
|
3779
|
-
oldName: 'test',
|
|
3780
|
-
schema: '',
|
|
3781
|
-
tableName: 'users',
|
|
3782
|
-
type: 'rename_policy',
|
|
3783
|
-
},
|
|
3784
|
-
]);
|
|
3785
|
-
});
|
|
3786
|
-
|
|
3787
|
-
test('alter policy that is linked', async (t) => {
|
|
3788
|
-
const client = new PGlite();
|
|
3789
|
-
const users = pgTable('users', {
|
|
3790
|
-
id: integer('id').primaryKey(),
|
|
3791
|
-
});
|
|
3792
|
-
|
|
3793
|
-
const { sqlStatements: createUsers } = await diffTestSchemas({}, { users }, []);
|
|
3794
|
-
|
|
3795
|
-
const schema1 = {
|
|
3796
|
-
rls: pgPolicy('test', { as: 'permissive' }).link(users),
|
|
3797
|
-
};
|
|
3798
|
-
|
|
3799
|
-
const schema2 = {
|
|
3800
|
-
users,
|
|
3801
|
-
rls: pgPolicy('test', { as: 'permissive', to: 'current_role' }).link(users),
|
|
3802
|
-
};
|
|
3803
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3804
|
-
client,
|
|
3805
|
-
schema1,
|
|
3806
|
-
schema2,
|
|
3807
|
-
[],
|
|
3808
|
-
false,
|
|
3809
|
-
['public'],
|
|
3810
|
-
undefined,
|
|
3811
|
-
undefined,
|
|
3812
|
-
{ before: createUsers },
|
|
3813
|
-
);
|
|
3814
|
-
|
|
3815
|
-
expect(sqlStatements).toStrictEqual([
|
|
3816
|
-
'ALTER POLICY "test" ON "users" TO current_role;',
|
|
3817
|
-
]);
|
|
3818
|
-
expect(statements).toStrictEqual([{
|
|
3819
|
-
newData: 'test--PERMISSIVE--ALL--current_role--undefined',
|
|
3820
|
-
oldData: 'test--PERMISSIVE--ALL--public--undefined',
|
|
3821
|
-
schema: '',
|
|
3822
|
-
tableName: 'users',
|
|
3823
|
-
type: 'alter_policy',
|
|
3824
|
-
}]);
|
|
3825
|
-
});
|
|
3826
|
-
|
|
3827
|
-
test('alter policy that is linked: withCheck', async (t) => {
|
|
3828
|
-
const client = new PGlite();
|
|
3829
|
-
|
|
3830
|
-
const users = pgTable('users', {
|
|
3831
|
-
id: integer('id').primaryKey(),
|
|
3832
|
-
});
|
|
3833
|
-
|
|
3834
|
-
const { sqlStatements: createUsers } = await diffTestSchemas({}, { users }, []);
|
|
3835
|
-
|
|
3836
|
-
const schema1 = {
|
|
3837
|
-
rls: pgPolicy('test', { as: 'permissive', withCheck: sql`true` }).link(users),
|
|
3838
|
-
};
|
|
3839
|
-
|
|
3840
|
-
const schema2 = {
|
|
3841
|
-
users,
|
|
3842
|
-
rls: pgPolicy('test', { as: 'permissive', withCheck: sql`false` }).link(users),
|
|
3843
|
-
};
|
|
3844
|
-
|
|
3845
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3846
|
-
client,
|
|
3847
|
-
schema1,
|
|
3848
|
-
schema2,
|
|
3849
|
-
[],
|
|
3850
|
-
false,
|
|
3851
|
-
['public'],
|
|
3852
|
-
undefined,
|
|
3853
|
-
undefined,
|
|
3854
|
-
{ before: createUsers },
|
|
3855
|
-
);
|
|
3856
|
-
|
|
3857
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
3858
|
-
expect(statements).toStrictEqual([]);
|
|
3859
|
-
});
|
|
3860
|
-
|
|
3861
|
-
test('alter policy that is linked: using', async (t) => {
|
|
3862
|
-
const client = new PGlite();
|
|
3863
|
-
const users = pgTable('users', {
|
|
3864
|
-
id: integer('id').primaryKey(),
|
|
3865
|
-
});
|
|
3866
|
-
|
|
3867
|
-
const { sqlStatements: createUsers } = await diffTestSchemas({}, { users }, []);
|
|
3868
|
-
|
|
3869
|
-
const schema1 = {
|
|
3870
|
-
rls: pgPolicy('test', { as: 'permissive', using: sql`true` }).link(users),
|
|
3871
|
-
};
|
|
3872
|
-
|
|
3873
|
-
const schema2 = {
|
|
3874
|
-
users,
|
|
3875
|
-
rls: pgPolicy('test', { as: 'permissive', using: sql`false` }).link(users),
|
|
3876
|
-
};
|
|
3877
|
-
|
|
3878
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3879
|
-
client,
|
|
3880
|
-
schema1,
|
|
3881
|
-
schema2,
|
|
3882
|
-
[],
|
|
3883
|
-
false,
|
|
3884
|
-
['public'],
|
|
3885
|
-
undefined,
|
|
3886
|
-
undefined,
|
|
3887
|
-
{ before: createUsers },
|
|
3888
|
-
);
|
|
3889
|
-
|
|
3890
|
-
expect(sqlStatements).toStrictEqual([]);
|
|
3891
|
-
expect(statements).toStrictEqual([]);
|
|
3892
|
-
});
|
|
3893
|
-
|
|
3894
|
-
test('alter policy that is linked: using', async (t) => {
|
|
3895
|
-
const client = new PGlite();
|
|
3896
|
-
|
|
3897
|
-
const users = pgTable('users', {
|
|
3898
|
-
id: integer('id').primaryKey(),
|
|
3899
|
-
});
|
|
3900
|
-
|
|
3901
|
-
const { sqlStatements: createUsers } = await diffTestSchemas({}, { users }, []);
|
|
3902
|
-
|
|
3903
|
-
const schema1 = {
|
|
3904
|
-
rls: pgPolicy('test', { for: 'insert' }).link(users),
|
|
3905
|
-
};
|
|
3906
|
-
|
|
3907
|
-
const schema2 = {
|
|
3908
|
-
users,
|
|
3909
|
-
rls: pgPolicy('test', { for: 'delete' }).link(users),
|
|
3910
|
-
};
|
|
3911
|
-
|
|
3912
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3913
|
-
client,
|
|
3914
|
-
schema1,
|
|
3915
|
-
schema2,
|
|
3916
|
-
[],
|
|
3917
|
-
false,
|
|
3918
|
-
['public'],
|
|
3919
|
-
undefined,
|
|
3920
|
-
undefined,
|
|
3921
|
-
{ before: createUsers },
|
|
3922
|
-
);
|
|
3923
|
-
|
|
3924
|
-
expect(sqlStatements).toStrictEqual([
|
|
3925
|
-
'DROP POLICY "test" ON "users" CASCADE;',
|
|
3926
|
-
'CREATE POLICY "test" ON "users" AS PERMISSIVE FOR DELETE TO public;',
|
|
3927
|
-
]);
|
|
3928
|
-
expect(statements).toStrictEqual([
|
|
3929
|
-
{
|
|
3930
|
-
data: {
|
|
3931
|
-
as: 'PERMISSIVE',
|
|
3932
|
-
for: 'INSERT',
|
|
3933
|
-
name: 'test',
|
|
3934
|
-
on: undefined,
|
|
3935
|
-
to: [
|
|
3936
|
-
'public',
|
|
3937
|
-
],
|
|
3938
|
-
},
|
|
3939
|
-
schema: '',
|
|
3940
|
-
tableName: 'users',
|
|
3941
|
-
type: 'drop_policy',
|
|
3942
|
-
},
|
|
3943
|
-
{
|
|
3944
|
-
data: {
|
|
3945
|
-
as: 'PERMISSIVE',
|
|
3946
|
-
for: 'DELETE',
|
|
3947
|
-
name: 'test',
|
|
3948
|
-
on: undefined,
|
|
3949
|
-
to: [
|
|
3950
|
-
'public',
|
|
3951
|
-
],
|
|
3952
|
-
},
|
|
3953
|
-
schema: '',
|
|
3954
|
-
tableName: 'users',
|
|
3955
|
-
type: 'create_policy',
|
|
3956
|
-
},
|
|
3957
|
-
]);
|
|
3958
|
-
});
|
|
3959
|
-
|
|
3960
|
-
////
|
|
3961
|
-
|
|
3962
|
-
test('create role', async (t) => {
|
|
3963
|
-
const client = new PGlite();
|
|
3964
|
-
|
|
3965
|
-
const schema1 = {};
|
|
3966
|
-
|
|
3967
|
-
const schema2 = {
|
|
3968
|
-
manager: pgRole('manager'),
|
|
3969
|
-
};
|
|
3970
|
-
|
|
3971
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
3972
|
-
client,
|
|
3973
|
-
schema1,
|
|
3974
|
-
schema2,
|
|
3975
|
-
[],
|
|
3976
|
-
false,
|
|
3977
|
-
['public'],
|
|
3978
|
-
undefined,
|
|
3979
|
-
{ roles: { include: ['manager'] } },
|
|
3980
|
-
);
|
|
3981
|
-
|
|
3982
|
-
expect(sqlStatements).toStrictEqual(['CREATE ROLE "manager";']);
|
|
3983
|
-
expect(statements).toStrictEqual([
|
|
3984
|
-
{
|
|
3985
|
-
name: 'manager',
|
|
3986
|
-
type: 'create_role',
|
|
3987
|
-
values: {
|
|
3988
|
-
createDb: false,
|
|
3989
|
-
createRole: false,
|
|
3990
|
-
inherit: true,
|
|
3991
|
-
},
|
|
3992
|
-
},
|
|
3993
|
-
]);
|
|
3994
|
-
|
|
3995
|
-
for (const st of sqlStatements) {
|
|
3996
|
-
await client.query(st);
|
|
3997
|
-
}
|
|
3998
|
-
});
|
|
3999
|
-
|
|
4000
|
-
test('create role with properties', async (t) => {
|
|
4001
|
-
const client = new PGlite();
|
|
4002
|
-
|
|
4003
|
-
const schema1 = {};
|
|
4004
|
-
|
|
4005
|
-
const schema2 = {
|
|
4006
|
-
manager: pgRole('manager', { createDb: true, inherit: false, createRole: true }),
|
|
4007
|
-
};
|
|
4008
|
-
|
|
4009
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4010
|
-
client,
|
|
4011
|
-
schema1,
|
|
4012
|
-
schema2,
|
|
4013
|
-
[],
|
|
4014
|
-
false,
|
|
4015
|
-
['public'],
|
|
4016
|
-
undefined,
|
|
4017
|
-
{ roles: { include: ['manager'] } },
|
|
4018
|
-
);
|
|
4019
|
-
|
|
4020
|
-
expect(sqlStatements).toStrictEqual(['CREATE ROLE "manager" WITH CREATEDB CREATEROLE NOINHERIT;']);
|
|
4021
|
-
expect(statements).toStrictEqual([
|
|
4022
|
-
{
|
|
4023
|
-
name: 'manager',
|
|
4024
|
-
type: 'create_role',
|
|
4025
|
-
values: {
|
|
4026
|
-
createDb: true,
|
|
4027
|
-
createRole: true,
|
|
4028
|
-
inherit: false,
|
|
4029
|
-
},
|
|
4030
|
-
},
|
|
4031
|
-
]);
|
|
4032
|
-
|
|
4033
|
-
for (const st of sqlStatements) {
|
|
4034
|
-
await client.query(st);
|
|
4035
|
-
}
|
|
4036
|
-
});
|
|
4037
|
-
|
|
4038
|
-
test('create role with some properties', async (t) => {
|
|
4039
|
-
const client = new PGlite();
|
|
4040
|
-
|
|
4041
|
-
const schema1 = {};
|
|
4042
|
-
|
|
4043
|
-
const schema2 = {
|
|
4044
|
-
manager: pgRole('manager', { createDb: true, inherit: false }),
|
|
4045
|
-
};
|
|
4046
|
-
|
|
4047
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4048
|
-
client,
|
|
4049
|
-
schema1,
|
|
4050
|
-
schema2,
|
|
4051
|
-
[],
|
|
4052
|
-
false,
|
|
4053
|
-
['public'],
|
|
4054
|
-
undefined,
|
|
4055
|
-
{ roles: { include: ['manager'] } },
|
|
4056
|
-
);
|
|
4057
|
-
|
|
4058
|
-
expect(sqlStatements).toStrictEqual(['CREATE ROLE "manager" WITH CREATEDB NOINHERIT;']);
|
|
4059
|
-
expect(statements).toStrictEqual([
|
|
4060
|
-
{
|
|
4061
|
-
name: 'manager',
|
|
4062
|
-
type: 'create_role',
|
|
4063
|
-
values: {
|
|
4064
|
-
createDb: true,
|
|
4065
|
-
createRole: false,
|
|
4066
|
-
inherit: false,
|
|
4067
|
-
},
|
|
4068
|
-
},
|
|
4069
|
-
]);
|
|
4070
|
-
|
|
4071
|
-
for (const st of sqlStatements) {
|
|
4072
|
-
await client.query(st);
|
|
4073
|
-
}
|
|
4074
|
-
});
|
|
4075
|
-
|
|
4076
|
-
test('drop role', async (t) => {
|
|
4077
|
-
const client = new PGlite();
|
|
4078
|
-
|
|
4079
|
-
const schema1 = { manager: pgRole('manager') };
|
|
4080
|
-
|
|
4081
|
-
const schema2 = {};
|
|
4082
|
-
|
|
4083
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4084
|
-
client,
|
|
4085
|
-
schema1,
|
|
4086
|
-
schema2,
|
|
4087
|
-
[],
|
|
4088
|
-
false,
|
|
4089
|
-
['public'],
|
|
4090
|
-
undefined,
|
|
4091
|
-
{ roles: { include: ['manager'] } },
|
|
4092
|
-
);
|
|
4093
|
-
|
|
4094
|
-
expect(sqlStatements).toStrictEqual(['DROP ROLE "manager";']);
|
|
4095
|
-
expect(statements).toStrictEqual([
|
|
4096
|
-
{
|
|
4097
|
-
name: 'manager',
|
|
4098
|
-
type: 'drop_role',
|
|
4099
|
-
},
|
|
4100
|
-
]);
|
|
4101
|
-
|
|
4102
|
-
for (const st of sqlStatements) {
|
|
4103
|
-
await client.query(st);
|
|
4104
|
-
}
|
|
4105
|
-
});
|
|
4106
|
-
|
|
4107
|
-
test('create and drop role', async (t) => {
|
|
4108
|
-
const client = new PGlite();
|
|
4109
|
-
|
|
4110
|
-
const schema1 = {
|
|
4111
|
-
manager: pgRole('manager'),
|
|
4112
|
-
};
|
|
4113
|
-
|
|
4114
|
-
const schema2 = {
|
|
4115
|
-
admin: pgRole('admin'),
|
|
4116
|
-
};
|
|
4117
|
-
|
|
4118
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4119
|
-
client,
|
|
4120
|
-
schema1,
|
|
4121
|
-
schema2,
|
|
4122
|
-
[],
|
|
4123
|
-
false,
|
|
4124
|
-
['public'],
|
|
4125
|
-
undefined,
|
|
4126
|
-
{ roles: { include: ['manager', 'admin'] } },
|
|
4127
|
-
);
|
|
4128
|
-
|
|
4129
|
-
expect(sqlStatements).toStrictEqual(['DROP ROLE "manager";', 'CREATE ROLE "admin";']);
|
|
4130
|
-
expect(statements).toStrictEqual([
|
|
4131
|
-
{
|
|
4132
|
-
name: 'manager',
|
|
4133
|
-
type: 'drop_role',
|
|
4134
|
-
},
|
|
4135
|
-
{
|
|
4136
|
-
name: 'admin',
|
|
4137
|
-
type: 'create_role',
|
|
4138
|
-
values: {
|
|
4139
|
-
createDb: false,
|
|
4140
|
-
createRole: false,
|
|
4141
|
-
inherit: true,
|
|
4142
|
-
},
|
|
4143
|
-
},
|
|
4144
|
-
]);
|
|
4145
|
-
|
|
4146
|
-
for (const st of sqlStatements) {
|
|
4147
|
-
await client.query(st);
|
|
4148
|
-
}
|
|
4149
|
-
});
|
|
4150
|
-
|
|
4151
|
-
test('rename role', async (t) => {
|
|
4152
|
-
const client = new PGlite();
|
|
4153
|
-
|
|
4154
|
-
const schema1 = {
|
|
4155
|
-
manager: pgRole('manager'),
|
|
4156
|
-
};
|
|
4157
|
-
|
|
4158
|
-
const schema2 = {
|
|
4159
|
-
admin: pgRole('admin'),
|
|
4160
|
-
};
|
|
4161
|
-
|
|
4162
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4163
|
-
client,
|
|
4164
|
-
schema1,
|
|
4165
|
-
schema2,
|
|
4166
|
-
['manager->admin'],
|
|
4167
|
-
false,
|
|
4168
|
-
['public'],
|
|
4169
|
-
undefined,
|
|
4170
|
-
{ roles: { include: ['manager', 'admin'] } },
|
|
4171
|
-
);
|
|
4172
|
-
|
|
4173
|
-
expect(sqlStatements).toStrictEqual(['ALTER ROLE "manager" RENAME TO "admin";']);
|
|
4174
|
-
expect(statements).toStrictEqual([
|
|
4175
|
-
{ nameFrom: 'manager', nameTo: 'admin', type: 'rename_role' },
|
|
4176
|
-
]);
|
|
4177
|
-
|
|
4178
|
-
for (const st of sqlStatements) {
|
|
4179
|
-
await client.query(st);
|
|
4180
|
-
}
|
|
4181
|
-
});
|
|
4182
|
-
|
|
4183
|
-
test('alter all role field', async (t) => {
|
|
4184
|
-
const client = new PGlite();
|
|
4185
|
-
|
|
4186
|
-
const schema1 = {
|
|
4187
|
-
manager: pgRole('manager'),
|
|
4188
|
-
};
|
|
4189
|
-
|
|
4190
|
-
const schema2 = {
|
|
4191
|
-
manager: pgRole('manager', { createDb: true, createRole: true, inherit: false }),
|
|
4192
|
-
};
|
|
4193
|
-
|
|
4194
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4195
|
-
client,
|
|
4196
|
-
schema1,
|
|
4197
|
-
schema2,
|
|
4198
|
-
[],
|
|
4199
|
-
false,
|
|
4200
|
-
['public'],
|
|
4201
|
-
undefined,
|
|
4202
|
-
{ roles: { include: ['manager'] } },
|
|
4203
|
-
);
|
|
4204
|
-
|
|
4205
|
-
expect(sqlStatements).toStrictEqual(['ALTER ROLE "manager" WITH CREATEDB CREATEROLE NOINHERIT;']);
|
|
4206
|
-
expect(statements).toStrictEqual([
|
|
4207
|
-
{
|
|
4208
|
-
name: 'manager',
|
|
4209
|
-
type: 'alter_role',
|
|
4210
|
-
values: {
|
|
4211
|
-
createDb: true,
|
|
4212
|
-
createRole: true,
|
|
4213
|
-
inherit: false,
|
|
4214
|
-
},
|
|
4215
|
-
},
|
|
4216
|
-
]);
|
|
4217
|
-
|
|
4218
|
-
for (const st of sqlStatements) {
|
|
4219
|
-
await client.query(st);
|
|
4220
|
-
}
|
|
4221
|
-
});
|
|
4222
|
-
|
|
4223
|
-
test('alter createdb in role', async (t) => {
|
|
4224
|
-
const client = new PGlite();
|
|
4225
|
-
|
|
4226
|
-
const schema1 = {
|
|
4227
|
-
manager: pgRole('manager'),
|
|
4228
|
-
};
|
|
4229
|
-
|
|
4230
|
-
const schema2 = {
|
|
4231
|
-
manager: pgRole('manager', { createDb: true }),
|
|
4232
|
-
};
|
|
4233
|
-
|
|
4234
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4235
|
-
client,
|
|
4236
|
-
schema1,
|
|
4237
|
-
schema2,
|
|
4238
|
-
[],
|
|
4239
|
-
false,
|
|
4240
|
-
['public'],
|
|
4241
|
-
undefined,
|
|
4242
|
-
{ roles: { include: ['manager'] } },
|
|
4243
|
-
);
|
|
4244
|
-
|
|
4245
|
-
expect(sqlStatements).toStrictEqual(['ALTER ROLE "manager" WITH CREATEDB NOCREATEROLE INHERIT;']);
|
|
4246
|
-
expect(statements).toStrictEqual([
|
|
4247
|
-
{
|
|
4248
|
-
name: 'manager',
|
|
4249
|
-
type: 'alter_role',
|
|
4250
|
-
values: {
|
|
4251
|
-
createDb: true,
|
|
4252
|
-
createRole: false,
|
|
4253
|
-
inherit: true,
|
|
4254
|
-
},
|
|
4255
|
-
},
|
|
4256
|
-
]);
|
|
4257
|
-
|
|
4258
|
-
for (const st of sqlStatements) {
|
|
4259
|
-
await client.query(st);
|
|
4260
|
-
}
|
|
4261
|
-
});
|
|
4262
|
-
|
|
4263
|
-
test('alter createrole in role', async (t) => {
|
|
4264
|
-
const client = new PGlite();
|
|
4265
|
-
|
|
4266
|
-
const schema1 = {
|
|
4267
|
-
manager: pgRole('manager'),
|
|
4268
|
-
};
|
|
4269
|
-
|
|
4270
|
-
const schema2 = {
|
|
4271
|
-
manager: pgRole('manager', { createRole: true }),
|
|
4272
|
-
};
|
|
4273
|
-
|
|
4274
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4275
|
-
client,
|
|
4276
|
-
schema1,
|
|
4277
|
-
schema2,
|
|
4278
|
-
[],
|
|
4279
|
-
false,
|
|
4280
|
-
['public'],
|
|
4281
|
-
undefined,
|
|
4282
|
-
{ roles: { include: ['manager'] } },
|
|
4283
|
-
);
|
|
4284
|
-
|
|
4285
|
-
expect(sqlStatements).toStrictEqual(['ALTER ROLE "manager" WITH NOCREATEDB CREATEROLE INHERIT;']);
|
|
4286
|
-
expect(statements).toStrictEqual([
|
|
4287
|
-
{
|
|
4288
|
-
name: 'manager',
|
|
4289
|
-
type: 'alter_role',
|
|
4290
|
-
values: {
|
|
4291
|
-
createDb: false,
|
|
4292
|
-
createRole: true,
|
|
4293
|
-
inherit: true,
|
|
4294
|
-
},
|
|
4295
|
-
},
|
|
4296
|
-
]);
|
|
4297
|
-
|
|
4298
|
-
for (const st of sqlStatements) {
|
|
4299
|
-
await client.query(st);
|
|
4300
|
-
}
|
|
4301
|
-
});
|
|
4302
|
-
|
|
4303
|
-
test('alter inherit in role', async (t) => {
|
|
4304
|
-
const client = new PGlite();
|
|
4305
|
-
|
|
4306
|
-
const schema1 = {
|
|
4307
|
-
manager: pgRole('manager'),
|
|
4308
|
-
};
|
|
4309
|
-
|
|
4310
|
-
const schema2 = {
|
|
4311
|
-
manager: pgRole('manager', { inherit: false }),
|
|
4312
|
-
};
|
|
4313
|
-
|
|
4314
|
-
const { statements, sqlStatements } = await diffTestSchemasPush(
|
|
4315
|
-
client,
|
|
4316
|
-
schema1,
|
|
4317
|
-
schema2,
|
|
4318
|
-
[],
|
|
4319
|
-
false,
|
|
4320
|
-
['public'],
|
|
4321
|
-
undefined,
|
|
4322
|
-
{ roles: { include: ['manager'] } },
|
|
4323
|
-
);
|
|
4324
|
-
|
|
4325
|
-
expect(sqlStatements).toStrictEqual(['ALTER ROLE "manager" WITH NOCREATEDB NOCREATEROLE NOINHERIT;']);
|
|
4326
|
-
expect(statements).toStrictEqual([
|
|
4327
|
-
{
|
|
4328
|
-
name: 'manager',
|
|
4329
|
-
type: 'alter_role',
|
|
4330
|
-
values: {
|
|
4331
|
-
createDb: false,
|
|
4332
|
-
createRole: false,
|
|
4333
|
-
inherit: false,
|
|
4334
|
-
},
|
|
4335
|
-
},
|
|
4336
|
-
]);
|
|
4337
|
-
|
|
4338
|
-
for (const st of sqlStatements) {
|
|
4339
|
-
await client.query(st);
|
|
4340
|
-
}
|
|
4341
|
-
});
|