@anthonyalayo/drizzle-kit 1.0.1
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/.gitignore +25 -0
- package/README.md +79 -0
- package/build.dev.ts +51 -0
- package/build.ts +112 -0
- package/imports-checker/analyze.ts +78 -0
- package/imports-checker/checker.ts +296 -0
- package/imports-checker/grammar/grammar.ohm +121 -0
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +164 -0
- package/imports-checker/grammar/grammar.ohm-bundle.js +753 -0
- package/imports-checker/index.ts +48 -0
- package/package.json +142 -0
- package/patches/difflib@0.2.4.patch +31 -0
- package/src/@types/utils.ts +58 -0
- package/src/api.ts +504 -0
- package/src/cli/commands/_es5.ts +2 -0
- package/src/cli/commands/check.ts +52 -0
- package/src/cli/commands/drop.ts +60 -0
- package/src/cli/commands/introspect.ts +772 -0
- package/src/cli/commands/libSqlPushUtils.ts +355 -0
- package/src/cli/commands/migrate.ts +1526 -0
- package/src/cli/commands/mysqlIntrospect.ts +53 -0
- package/src/cli/commands/mysqlPushUtils.ts +352 -0
- package/src/cli/commands/mysqlUp.ts +102 -0
- package/src/cli/commands/pgIntrospect.ts +55 -0
- package/src/cli/commands/pgPushUtils.ts +269 -0
- package/src/cli/commands/pgUp.ts +217 -0
- package/src/cli/commands/push.ts +637 -0
- package/src/cli/commands/singlestoreIntrospect.ts +53 -0
- package/src/cli/commands/singlestorePushUtils.ts +456 -0
- package/src/cli/commands/singlestoreUp.ts +1 -0
- package/src/cli/commands/sqliteIntrospect.ts +96 -0
- package/src/cli/commands/sqlitePushUtils.ts +322 -0
- package/src/cli/commands/sqliteUp.ts +52 -0
- package/src/cli/commands/utils.ts +858 -0
- package/src/cli/connections.ts +914 -0
- package/src/cli/index.ts +48 -0
- package/src/cli/schema.ts +794 -0
- package/src/cli/selector-ui.ts +38 -0
- package/src/cli/utils.ts +112 -0
- package/src/cli/validations/cli.ts +79 -0
- package/src/cli/validations/common.ts +193 -0
- package/src/cli/validations/libsql.ts +27 -0
- package/src/cli/validations/mysql.ts +61 -0
- package/src/cli/validations/outputs.ts +91 -0
- package/src/cli/validations/postgres.ts +83 -0
- package/src/cli/validations/singlestore.ts +61 -0
- package/src/cli/validations/sqlite.ts +104 -0
- package/src/cli/validations/studio.ts +24 -0
- package/src/cli/views.ts +658 -0
- package/src/extensions/getTablesFilterByExtensions.ts +16 -0
- package/src/extensions/vector.ts +10 -0
- package/src/global.ts +61 -0
- package/src/index.ts +326 -0
- package/src/introspect-mysql.ts +1030 -0
- package/src/introspect-pg.ts +1435 -0
- package/src/introspect-singlestore.ts +913 -0
- package/src/introspect-sqlite.ts +533 -0
- package/src/jsonDiffer.js +882 -0
- package/src/jsonStatements.ts +3590 -0
- package/src/loader.mjs +57 -0
- package/src/migrationPreparator.ts +217 -0
- package/src/schemaValidator.ts +23 -0
- package/src/serializer/index.ts +142 -0
- package/src/serializer/mysqlImports.ts +38 -0
- package/src/serializer/mysqlSchema.ts +423 -0
- package/src/serializer/mysqlSerializer.ts +999 -0
- package/src/serializer/pgImports.ts +106 -0
- package/src/serializer/pgSchema.ts +937 -0
- package/src/serializer/pgSerializer.ts +2223 -0
- package/src/serializer/singlestoreImports.ts +38 -0
- package/src/serializer/singlestoreSchema.ts +257 -0
- package/src/serializer/singlestoreSerializer.ts +767 -0
- package/src/serializer/sqliteImports.ts +41 -0
- package/src/serializer/sqliteSchema.ts +352 -0
- package/src/serializer/sqliteSerializer.ts +931 -0
- package/src/serializer/studio.ts +694 -0
- package/src/serializer/utils.ts +45 -0
- package/src/simulator.ts +157 -0
- package/src/snapshotsDiffer.ts +4398 -0
- package/src/sqlgenerator.ts +4212 -0
- package/src/statementCombiner.ts +597 -0
- package/src/utils/certs.ts +44 -0
- package/src/utils/words.ts +1333 -0
- package/src/utils.ts +370 -0
- package/tests/bin.test.ts +58 -0
- package/tests/cli/d1http.config.ts +12 -0
- package/tests/cli/drizzle.config.ts +9 -0
- package/tests/cli/durable-sqlite.config.ts +7 -0
- package/tests/cli/expo.config.ts +7 -0
- package/tests/cli/postgres.config.ts +13 -0
- package/tests/cli/postgres2.config.ts +17 -0
- package/tests/cli/schema.ts +1 -0
- package/tests/cli/turso.config.ts +10 -0
- package/tests/cli-export.test.ts +79 -0
- package/tests/cli-generate.test.ts +259 -0
- package/tests/cli-migrate.test.ts +103 -0
- package/tests/cli-push.test.ts +126 -0
- package/tests/common.ts +15 -0
- package/tests/indexes/common.ts +21 -0
- package/tests/indexes/pg.test.ts +245 -0
- package/tests/introspect/libsql.test.ts +35 -0
- package/tests/introspect/mysql.test.ts +319 -0
- package/tests/introspect/pg.test.ts +894 -0
- package/tests/introspect/singlestore.test.ts +275 -0
- package/tests/introspect/sqlite.test.ts +122 -0
- package/tests/libsql-checks.test.ts +308 -0
- package/tests/libsql-statements.test.ts +989 -0
- package/tests/libsql-views.test.ts +218 -0
- package/tests/migrate/libsq-schema.ts +6 -0
- package/tests/migrate/libsql-migrate.test.ts +58 -0
- package/tests/migrate/migrations/0000_little_blizzard.sql +4 -0
- package/tests/migrate/migrations/0001_nebulous_storm.sql +10 -0
- package/tests/migrate/migrations/meta/0000_snapshot.json +40 -0
- package/tests/migrate/migrations/meta/0001_snapshot.json +40 -0
- package/tests/migrate/migrations/meta/_journal.json +20 -0
- package/tests/mysql-checks.test.ts +291 -0
- package/tests/mysql-generated.test.ts +1290 -0
- package/tests/mysql-schemas.test.ts +156 -0
- package/tests/mysql-views.test.ts +553 -0
- package/tests/mysql.test.ts +863 -0
- package/tests/pg-array.test.ts +368 -0
- package/tests/pg-checks.test.ts +329 -0
- package/tests/pg-columns.test.ts +484 -0
- package/tests/pg-domains.test.ts +306 -0
- package/tests/pg-enums.test.ts +751 -0
- package/tests/pg-generated.test.ts +529 -0
- package/tests/pg-identity.test.ts +447 -0
- package/tests/pg-schemas.test.ts +105 -0
- package/tests/pg-sequences.test.ts +298 -0
- package/tests/pg-tables.test.ts +1021 -0
- package/tests/pg-views.test.ts +1929 -0
- package/tests/push/common.ts +58 -0
- package/tests/push/libsql.test.ts +1400 -0
- package/tests/push/mysql-push.test.ts +345 -0
- package/tests/push/mysql.test.ts +788 -0
- package/tests/push/pg.test.ts +4341 -0
- package/tests/push/singlestore-push.test.ts +894 -0
- package/tests/push/singlestore.test.ts +444 -0
- package/tests/push/sqlite.test.ts +1613 -0
- package/tests/rls/pg-policy.test.ts +1656 -0
- package/tests/rls/pg-role.test.ts +234 -0
- package/tests/schemaDiffer.ts +2852 -0
- package/tests/singlestore-generated.test.ts +1290 -0
- package/tests/singlestore-schemas.test.ts +155 -0
- package/tests/singlestore.test.ts +978 -0
- package/tests/sqlite-checks.test.ts +308 -0
- package/tests/sqlite-columns.test.ts +1049 -0
- package/tests/sqlite-generated.test.ts +1726 -0
- package/tests/sqlite-tables.test.ts +637 -0
- package/tests/sqlite-views.test.ts +218 -0
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +1812 -0
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +882 -0
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +1211 -0
- package/tests/test/sqlite.test.ts +39 -0
- package/tests/testsinglestore.ts +29 -0
- package/tests/validations.test.ts +869 -0
- package/tests/wrap-param.test.ts +16 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.cli-types.json +9 -0
- package/tsconfig.json +28 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
import { sql } from 'drizzle-orm';
|
|
2
|
+
import {
|
|
3
|
+
AnySQLiteColumn,
|
|
4
|
+
foreignKey,
|
|
5
|
+
index,
|
|
6
|
+
int,
|
|
7
|
+
primaryKey,
|
|
8
|
+
sqliteTable,
|
|
9
|
+
text,
|
|
10
|
+
unique,
|
|
11
|
+
uniqueIndex,
|
|
12
|
+
} from 'drizzle-orm/sqlite-core';
|
|
13
|
+
import { expect, test } from 'vitest';
|
|
14
|
+
import { diffTestSchemasSqlite } from './schemaDiffer';
|
|
15
|
+
|
|
16
|
+
test('add table #1', async () => {
|
|
17
|
+
const to = {
|
|
18
|
+
users: sqliteTable('users', {}),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const { statements } = await diffTestSchemasSqlite({}, to, []);
|
|
22
|
+
|
|
23
|
+
expect(statements.length).toBe(1);
|
|
24
|
+
expect(statements[0]).toStrictEqual({
|
|
25
|
+
type: 'sqlite_create_table',
|
|
26
|
+
tableName: 'users',
|
|
27
|
+
columns: [],
|
|
28
|
+
compositePKs: [],
|
|
29
|
+
uniqueConstraints: [],
|
|
30
|
+
referenceData: [],
|
|
31
|
+
checkConstraints: [],
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('add table #2', async () => {
|
|
36
|
+
const to = {
|
|
37
|
+
users: sqliteTable('users', {
|
|
38
|
+
id: int('id').primaryKey({ autoIncrement: true }),
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const { statements } = await diffTestSchemasSqlite({}, to, []);
|
|
43
|
+
|
|
44
|
+
expect(statements.length).toBe(1);
|
|
45
|
+
expect(statements[0]).toStrictEqual({
|
|
46
|
+
type: 'sqlite_create_table',
|
|
47
|
+
tableName: 'users',
|
|
48
|
+
columns: [
|
|
49
|
+
{
|
|
50
|
+
name: 'id',
|
|
51
|
+
notNull: true,
|
|
52
|
+
primaryKey: true,
|
|
53
|
+
type: 'integer',
|
|
54
|
+
autoincrement: true,
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
compositePKs: [],
|
|
58
|
+
referenceData: [],
|
|
59
|
+
uniqueConstraints: [],
|
|
60
|
+
checkConstraints: [],
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('add table #3', async () => {
|
|
65
|
+
const to = {
|
|
66
|
+
users: sqliteTable(
|
|
67
|
+
'users',
|
|
68
|
+
{
|
|
69
|
+
id: int('id'),
|
|
70
|
+
},
|
|
71
|
+
(t) => {
|
|
72
|
+
return {
|
|
73
|
+
pk: primaryKey({
|
|
74
|
+
name: 'users_pk',
|
|
75
|
+
columns: [t.id],
|
|
76
|
+
}),
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
),
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const { statements } = await diffTestSchemasSqlite({}, to, []);
|
|
83
|
+
|
|
84
|
+
expect(statements.length).toBe(1);
|
|
85
|
+
expect(statements[0]).toStrictEqual({
|
|
86
|
+
type: 'sqlite_create_table',
|
|
87
|
+
tableName: 'users',
|
|
88
|
+
columns: [
|
|
89
|
+
{
|
|
90
|
+
name: 'id',
|
|
91
|
+
notNull: false,
|
|
92
|
+
primaryKey: true,
|
|
93
|
+
type: 'integer',
|
|
94
|
+
autoincrement: false,
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
compositePKs: [],
|
|
98
|
+
uniqueConstraints: [],
|
|
99
|
+
referenceData: [],
|
|
100
|
+
checkConstraints: [],
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('add table #4', async () => {
|
|
105
|
+
const to = {
|
|
106
|
+
users: sqliteTable('users', {}),
|
|
107
|
+
posts: sqliteTable('posts', {}),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const { statements } = await diffTestSchemasSqlite({}, to, []);
|
|
111
|
+
|
|
112
|
+
expect(statements.length).toBe(2);
|
|
113
|
+
expect(statements[0]).toStrictEqual({
|
|
114
|
+
type: 'sqlite_create_table',
|
|
115
|
+
tableName: 'users',
|
|
116
|
+
columns: [],
|
|
117
|
+
compositePKs: [],
|
|
118
|
+
uniqueConstraints: [],
|
|
119
|
+
referenceData: [],
|
|
120
|
+
checkConstraints: [],
|
|
121
|
+
});
|
|
122
|
+
expect(statements[1]).toStrictEqual({
|
|
123
|
+
type: 'sqlite_create_table',
|
|
124
|
+
tableName: 'posts',
|
|
125
|
+
columns: [],
|
|
126
|
+
compositePKs: [],
|
|
127
|
+
uniqueConstraints: [],
|
|
128
|
+
referenceData: [],
|
|
129
|
+
checkConstraints: [],
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('add table #5', async () => {
|
|
134
|
+
// no schemas in sqlite
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('add table #6', async () => {
|
|
138
|
+
const from = {
|
|
139
|
+
users1: sqliteTable('users1', {}),
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const to = {
|
|
143
|
+
users2: sqliteTable('users2', {}),
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const { statements } = await diffTestSchemasSqlite(from, to, []);
|
|
147
|
+
|
|
148
|
+
expect(statements.length).toBe(2);
|
|
149
|
+
expect(statements[0]).toStrictEqual({
|
|
150
|
+
type: 'sqlite_create_table',
|
|
151
|
+
tableName: 'users2',
|
|
152
|
+
columns: [],
|
|
153
|
+
compositePKs: [],
|
|
154
|
+
uniqueConstraints: [],
|
|
155
|
+
referenceData: [],
|
|
156
|
+
checkConstraints: [],
|
|
157
|
+
});
|
|
158
|
+
expect(statements[1]).toStrictEqual({
|
|
159
|
+
type: 'drop_table',
|
|
160
|
+
tableName: 'users1',
|
|
161
|
+
schema: undefined,
|
|
162
|
+
policies: [],
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test('add table #7', async () => {
|
|
167
|
+
const from = {
|
|
168
|
+
users1: sqliteTable('users1', {}),
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const to = {
|
|
172
|
+
users: sqliteTable('users', {}),
|
|
173
|
+
users2: sqliteTable('users2', {}),
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const { statements } = await diffTestSchemasSqlite(from, to, [
|
|
177
|
+
'public.users1->public.users2',
|
|
178
|
+
]);
|
|
179
|
+
|
|
180
|
+
expect(statements.length).toBe(2);
|
|
181
|
+
expect(statements[0]).toStrictEqual({
|
|
182
|
+
type: 'rename_table',
|
|
183
|
+
tableNameFrom: 'users1',
|
|
184
|
+
tableNameTo: 'users2',
|
|
185
|
+
fromSchema: undefined,
|
|
186
|
+
toSchema: undefined,
|
|
187
|
+
});
|
|
188
|
+
expect(statements[1]).toStrictEqual({
|
|
189
|
+
type: 'sqlite_create_table',
|
|
190
|
+
tableName: 'users',
|
|
191
|
+
columns: [],
|
|
192
|
+
compositePKs: [],
|
|
193
|
+
uniqueConstraints: [],
|
|
194
|
+
referenceData: [],
|
|
195
|
+
checkConstraints: [],
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test('add table #8', async () => {
|
|
200
|
+
const users = sqliteTable('users', {
|
|
201
|
+
id: int('id').primaryKey({ autoIncrement: true }),
|
|
202
|
+
reporteeId: int('reportee_id').references((): AnySQLiteColumn => users.id),
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const to = {
|
|
206
|
+
users,
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const { statements } = await diffTestSchemasSqlite({}, to, []);
|
|
210
|
+
|
|
211
|
+
expect(statements.length).toBe(1);
|
|
212
|
+
expect(statements[0]).toStrictEqual({
|
|
213
|
+
type: 'sqlite_create_table',
|
|
214
|
+
tableName: 'users',
|
|
215
|
+
columns: [
|
|
216
|
+
{
|
|
217
|
+
autoincrement: true,
|
|
218
|
+
name: 'id',
|
|
219
|
+
notNull: true,
|
|
220
|
+
primaryKey: true,
|
|
221
|
+
type: 'integer',
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
autoincrement: false,
|
|
225
|
+
name: 'reportee_id',
|
|
226
|
+
notNull: false,
|
|
227
|
+
primaryKey: false,
|
|
228
|
+
type: 'integer',
|
|
229
|
+
},
|
|
230
|
+
],
|
|
231
|
+
compositePKs: [],
|
|
232
|
+
uniqueConstraints: [],
|
|
233
|
+
checkConstraints: [],
|
|
234
|
+
referenceData: [
|
|
235
|
+
{
|
|
236
|
+
columnsFrom: ['reportee_id'],
|
|
237
|
+
columnsTo: ['id'],
|
|
238
|
+
name: 'users_reportee_id_users_id_fk',
|
|
239
|
+
onDelete: 'no action',
|
|
240
|
+
onUpdate: 'no action',
|
|
241
|
+
tableFrom: 'users',
|
|
242
|
+
tableTo: 'users',
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
test('add table #9', async () => {
|
|
249
|
+
const to = {
|
|
250
|
+
users: sqliteTable(
|
|
251
|
+
'users',
|
|
252
|
+
{
|
|
253
|
+
id: int('id').primaryKey({ autoIncrement: true }),
|
|
254
|
+
reporteeId: int('reportee_id'),
|
|
255
|
+
},
|
|
256
|
+
(t) => {
|
|
257
|
+
return {
|
|
258
|
+
reporteeIdx: index('reportee_idx').on(t.reporteeId),
|
|
259
|
+
};
|
|
260
|
+
},
|
|
261
|
+
),
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const { statements } = await diffTestSchemasSqlite({}, to, []);
|
|
265
|
+
|
|
266
|
+
expect(statements.length).toBe(2);
|
|
267
|
+
expect(statements[0]).toStrictEqual({
|
|
268
|
+
type: 'sqlite_create_table',
|
|
269
|
+
tableName: 'users',
|
|
270
|
+
columns: [
|
|
271
|
+
{
|
|
272
|
+
autoincrement: true,
|
|
273
|
+
name: 'id',
|
|
274
|
+
notNull: true,
|
|
275
|
+
primaryKey: true,
|
|
276
|
+
type: 'integer',
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
autoincrement: false,
|
|
280
|
+
name: 'reportee_id',
|
|
281
|
+
notNull: false,
|
|
282
|
+
primaryKey: false,
|
|
283
|
+
type: 'integer',
|
|
284
|
+
},
|
|
285
|
+
],
|
|
286
|
+
compositePKs: [],
|
|
287
|
+
uniqueConstraints: [],
|
|
288
|
+
referenceData: [],
|
|
289
|
+
checkConstraints: [],
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
expect(statements[1]).toStrictEqual({
|
|
293
|
+
type: 'create_index',
|
|
294
|
+
tableName: 'users',
|
|
295
|
+
internal: {
|
|
296
|
+
indexes: {},
|
|
297
|
+
},
|
|
298
|
+
schema: undefined,
|
|
299
|
+
data: 'reportee_idx;reportee_id;false;',
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
test('add table #10', async () => {
|
|
304
|
+
const to = {
|
|
305
|
+
users: sqliteTable('table', {
|
|
306
|
+
json: text('json', { mode: 'json' }).default({}),
|
|
307
|
+
}),
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const { sqlStatements } = await diffTestSchemasSqlite({}, to, []);
|
|
311
|
+
expect(sqlStatements.length).toBe(1);
|
|
312
|
+
expect(sqlStatements[0]).toBe(
|
|
313
|
+
"CREATE TABLE `table` (\n\t`json` text DEFAULT '{}'\n);\n",
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test('add table #11', async () => {
|
|
318
|
+
const to = {
|
|
319
|
+
users: sqliteTable('table', {
|
|
320
|
+
json: text('json', { mode: 'json' }).default([]),
|
|
321
|
+
}),
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
const { sqlStatements } = await diffTestSchemasSqlite({}, to, []);
|
|
325
|
+
expect(sqlStatements.length).toBe(1);
|
|
326
|
+
expect(sqlStatements[0]).toBe(
|
|
327
|
+
"CREATE TABLE `table` (\n\t`json` text DEFAULT '[]'\n);\n",
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('add table #12', async () => {
|
|
332
|
+
const to = {
|
|
333
|
+
users: sqliteTable('table', {
|
|
334
|
+
json: text('json', { mode: 'json' }).default([1, 2, 3]),
|
|
335
|
+
}),
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const { sqlStatements } = await diffTestSchemasSqlite({}, to, []);
|
|
339
|
+
expect(sqlStatements.length).toBe(1);
|
|
340
|
+
expect(sqlStatements[0]).toBe(
|
|
341
|
+
"CREATE TABLE `table` (\n\t`json` text DEFAULT '[1,2,3]'\n);\n",
|
|
342
|
+
);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test('add table #13', async () => {
|
|
346
|
+
const to = {
|
|
347
|
+
users: sqliteTable('table', {
|
|
348
|
+
json: text('json', { mode: 'json' }).default({ key: 'value' }),
|
|
349
|
+
}),
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const { sqlStatements } = await diffTestSchemasSqlite({}, to, []);
|
|
353
|
+
expect(sqlStatements.length).toBe(1);
|
|
354
|
+
expect(sqlStatements[0]).toBe(
|
|
355
|
+
'CREATE TABLE `table` (\n\t`json` text DEFAULT \'{"key":"value"}\'\n);\n',
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
test('add table #14', async () => {
|
|
360
|
+
const to = {
|
|
361
|
+
users: sqliteTable('table', {
|
|
362
|
+
json: text('json', { mode: 'json' }).default({
|
|
363
|
+
key: 'value',
|
|
364
|
+
arr: [1, 2, 3],
|
|
365
|
+
}),
|
|
366
|
+
}),
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const { sqlStatements } = await diffTestSchemasSqlite({}, to, []);
|
|
370
|
+
expect(sqlStatements.length).toBe(1);
|
|
371
|
+
expect(sqlStatements[0]).toBe(
|
|
372
|
+
'CREATE TABLE `table` (\n\t`json` text DEFAULT \'{"key":"value","arr":[1,2,3]}\'\n);\n',
|
|
373
|
+
);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
test('add table with indexes', async () => {
|
|
377
|
+
const from = {};
|
|
378
|
+
|
|
379
|
+
const to = {
|
|
380
|
+
users: sqliteTable(
|
|
381
|
+
'users',
|
|
382
|
+
{
|
|
383
|
+
id: int('id').primaryKey(),
|
|
384
|
+
name: text('name'),
|
|
385
|
+
email: text('email'),
|
|
386
|
+
},
|
|
387
|
+
(t) => ({
|
|
388
|
+
uniqueExpr: uniqueIndex('uniqueExpr').on(sql`(lower(${t.email}))`),
|
|
389
|
+
indexExpr: index('indexExpr').on(sql`(lower(${t.email}))`),
|
|
390
|
+
indexExprMultiple: index('indexExprMultiple').on(
|
|
391
|
+
sql`(lower(${t.email}))`,
|
|
392
|
+
sql`(lower(${t.email}))`,
|
|
393
|
+
),
|
|
394
|
+
|
|
395
|
+
uniqueCol: uniqueIndex('uniqueCol').on(t.email),
|
|
396
|
+
indexCol: index('indexCol').on(t.email),
|
|
397
|
+
indexColMultiple: index('indexColMultiple').on(t.email, t.email),
|
|
398
|
+
|
|
399
|
+
indexColExpr: index('indexColExpr').on(
|
|
400
|
+
sql`(lower(${t.email}))`,
|
|
401
|
+
t.email,
|
|
402
|
+
),
|
|
403
|
+
}),
|
|
404
|
+
),
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const { sqlStatements } = await diffTestSchemasSqlite(from, to, []);
|
|
408
|
+
expect(sqlStatements.length).toBe(8);
|
|
409
|
+
expect(sqlStatements).toStrictEqual([
|
|
410
|
+
'CREATE TABLE `users` (\n\t`id` integer PRIMARY KEY NOT NULL,\n\t`name` text,\n\t`email` text\n);\n',
|
|
411
|
+
'CREATE UNIQUE INDEX `uniqueExpr` ON `users` ((lower("email")));',
|
|
412
|
+
'CREATE INDEX `indexExpr` ON `users` ((lower("email")));',
|
|
413
|
+
'CREATE INDEX `indexExprMultiple` ON `users` ((lower("email")),(lower("email")));',
|
|
414
|
+
'CREATE UNIQUE INDEX `uniqueCol` ON `users` (`email`);',
|
|
415
|
+
'CREATE INDEX `indexCol` ON `users` (`email`);',
|
|
416
|
+
'CREATE INDEX `indexColMultiple` ON `users` (`email`,`email`);',
|
|
417
|
+
'CREATE INDEX `indexColExpr` ON `users` ((lower("email")),`email`);',
|
|
418
|
+
]);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('composite primary key', async () => {
|
|
422
|
+
const from = {};
|
|
423
|
+
const to = {
|
|
424
|
+
table: sqliteTable('works_to_creators', {
|
|
425
|
+
workId: int('work_id').notNull(),
|
|
426
|
+
creatorId: int('creator_id').notNull(),
|
|
427
|
+
classification: text('classification').notNull(),
|
|
428
|
+
}, (t) => ({
|
|
429
|
+
pk: primaryKey({
|
|
430
|
+
columns: [t.workId, t.creatorId, t.classification],
|
|
431
|
+
}),
|
|
432
|
+
})),
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
const { sqlStatements } = await diffTestSchemasSqlite(from, to, []);
|
|
436
|
+
|
|
437
|
+
expect(sqlStatements).toStrictEqual([
|
|
438
|
+
'CREATE TABLE `works_to_creators` (\n\t`work_id` integer NOT NULL,\n\t`creator_id` integer NOT NULL,\n\t`classification` text NOT NULL,\n\tPRIMARY KEY(`work_id`, `creator_id`, `classification`)\n);\n',
|
|
439
|
+
]);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test('add column before creating unique constraint', async () => {
|
|
443
|
+
const from = {
|
|
444
|
+
table: sqliteTable('table', {
|
|
445
|
+
id: int('id').primaryKey(),
|
|
446
|
+
}),
|
|
447
|
+
};
|
|
448
|
+
const to = {
|
|
449
|
+
table: sqliteTable('table', {
|
|
450
|
+
id: int('id').primaryKey(),
|
|
451
|
+
name: text('name').notNull(),
|
|
452
|
+
}, (t) => ({
|
|
453
|
+
uq: unique('uq').on(t.name),
|
|
454
|
+
})),
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
const { sqlStatements } = await diffTestSchemasSqlite(from, to, []);
|
|
458
|
+
|
|
459
|
+
expect(sqlStatements).toStrictEqual([
|
|
460
|
+
'ALTER TABLE `table` ADD `name` text NOT NULL;',
|
|
461
|
+
'CREATE UNIQUE INDEX `uq` ON `table` (`name`);',
|
|
462
|
+
]);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
test('optional db aliases (snake case)', async () => {
|
|
466
|
+
const from = {};
|
|
467
|
+
|
|
468
|
+
const t1 = sqliteTable(
|
|
469
|
+
't1',
|
|
470
|
+
{
|
|
471
|
+
t1Id1: int().notNull().primaryKey(),
|
|
472
|
+
t1Col2: int().notNull(),
|
|
473
|
+
t1Col3: int().notNull(),
|
|
474
|
+
t2Ref: int().notNull().references(() => t2.t2Id),
|
|
475
|
+
t1Uni: int().notNull(),
|
|
476
|
+
t1UniIdx: int().notNull(),
|
|
477
|
+
t1Idx: int().notNull(),
|
|
478
|
+
},
|
|
479
|
+
(table) => ({
|
|
480
|
+
uni: unique('t1_uni').on(table.t1Uni),
|
|
481
|
+
uniIdx: uniqueIndex('t1_uni_idx').on(table.t1UniIdx),
|
|
482
|
+
idx: index('t1_idx').on(table.t1Idx),
|
|
483
|
+
fk: foreignKey({
|
|
484
|
+
columns: [table.t1Col2, table.t1Col3],
|
|
485
|
+
foreignColumns: [t3.t3Id1, t3.t3Id2],
|
|
486
|
+
}),
|
|
487
|
+
}),
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
const t2 = sqliteTable(
|
|
491
|
+
't2',
|
|
492
|
+
{
|
|
493
|
+
t2Id: int().primaryKey({ autoIncrement: true }),
|
|
494
|
+
},
|
|
495
|
+
);
|
|
496
|
+
|
|
497
|
+
const t3 = sqliteTable(
|
|
498
|
+
't3',
|
|
499
|
+
{
|
|
500
|
+
t3Id1: int(),
|
|
501
|
+
t3Id2: int(),
|
|
502
|
+
},
|
|
503
|
+
(table) => ({
|
|
504
|
+
pk: primaryKey({
|
|
505
|
+
columns: [table.t3Id1, table.t3Id2],
|
|
506
|
+
}),
|
|
507
|
+
}),
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
const to = {
|
|
511
|
+
t1,
|
|
512
|
+
t2,
|
|
513
|
+
t3,
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
const { sqlStatements } = await diffTestSchemasSqlite(from, to, [], false, 'snake_case');
|
|
517
|
+
|
|
518
|
+
const st1 = `CREATE TABLE \`t1\` (
|
|
519
|
+
\`t1_id1\` integer PRIMARY KEY NOT NULL,
|
|
520
|
+
\`t1_col2\` integer NOT NULL,
|
|
521
|
+
\`t1_col3\` integer NOT NULL,
|
|
522
|
+
\`t2_ref\` integer NOT NULL,
|
|
523
|
+
\`t1_uni\` integer NOT NULL,
|
|
524
|
+
\`t1_uni_idx\` integer NOT NULL,
|
|
525
|
+
\`t1_idx\` integer NOT NULL,
|
|
526
|
+
FOREIGN KEY (\`t2_ref\`) REFERENCES \`t2\`(\`t2_id\`) ON UPDATE no action ON DELETE no action,
|
|
527
|
+
FOREIGN KEY (\`t1_col2\`,\`t1_col3\`) REFERENCES \`t3\`(\`t3_id1\`,\`t3_id2\`) ON UPDATE no action ON DELETE no action
|
|
528
|
+
);
|
|
529
|
+
`;
|
|
530
|
+
|
|
531
|
+
const st2 = `CREATE UNIQUE INDEX \`t1_uni_idx\` ON \`t1\` (\`t1_uni_idx\`);`;
|
|
532
|
+
|
|
533
|
+
const st3 = `CREATE INDEX \`t1_idx\` ON \`t1\` (\`t1_idx\`);`;
|
|
534
|
+
|
|
535
|
+
const st4 = `CREATE UNIQUE INDEX \`t1_uni\` ON \`t1\` (\`t1_uni\`);`;
|
|
536
|
+
|
|
537
|
+
const st5 = `CREATE TABLE \`t2\` (
|
|
538
|
+
\`t2_id\` integer PRIMARY KEY AUTOINCREMENT NOT NULL
|
|
539
|
+
);
|
|
540
|
+
`;
|
|
541
|
+
|
|
542
|
+
const st6 = `CREATE TABLE \`t3\` (
|
|
543
|
+
\`t3_id1\` integer,
|
|
544
|
+
\`t3_id2\` integer,
|
|
545
|
+
PRIMARY KEY(\`t3_id1\`, \`t3_id2\`)
|
|
546
|
+
);
|
|
547
|
+
`;
|
|
548
|
+
|
|
549
|
+
expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6]);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
test('optional db aliases (camel case)', async () => {
|
|
553
|
+
const from = {};
|
|
554
|
+
|
|
555
|
+
const t1 = sqliteTable(
|
|
556
|
+
't1',
|
|
557
|
+
{
|
|
558
|
+
t1_id1: int().notNull().primaryKey(),
|
|
559
|
+
t1_col2: int().notNull(),
|
|
560
|
+
t1_col3: int().notNull(),
|
|
561
|
+
t2_ref: int().notNull().references(() => t2.t2_id),
|
|
562
|
+
t1_uni: int().notNull(),
|
|
563
|
+
t1_uni_idx: int().notNull(),
|
|
564
|
+
t1_idx: int().notNull(),
|
|
565
|
+
},
|
|
566
|
+
(table) => ({
|
|
567
|
+
uni: unique('t1Uni').on(table.t1_uni),
|
|
568
|
+
uni_idx: uniqueIndex('t1UniIdx').on(table.t1_uni_idx),
|
|
569
|
+
idx: index('t1Idx').on(table.t1_idx),
|
|
570
|
+
fk: foreignKey({
|
|
571
|
+
columns: [table.t1_col2, table.t1_col3],
|
|
572
|
+
foreignColumns: [t3.t3_id1, t3.t3_id2],
|
|
573
|
+
}),
|
|
574
|
+
}),
|
|
575
|
+
);
|
|
576
|
+
|
|
577
|
+
const t2 = sqliteTable(
|
|
578
|
+
't2',
|
|
579
|
+
{
|
|
580
|
+
t2_id: int().primaryKey({ autoIncrement: true }),
|
|
581
|
+
},
|
|
582
|
+
);
|
|
583
|
+
|
|
584
|
+
const t3 = sqliteTable(
|
|
585
|
+
't3',
|
|
586
|
+
{
|
|
587
|
+
t3_id1: int(),
|
|
588
|
+
t3_id2: int(),
|
|
589
|
+
},
|
|
590
|
+
(table) => ({
|
|
591
|
+
pk: primaryKey({
|
|
592
|
+
columns: [table.t3_id1, table.t3_id2],
|
|
593
|
+
}),
|
|
594
|
+
}),
|
|
595
|
+
);
|
|
596
|
+
|
|
597
|
+
const to = {
|
|
598
|
+
t1,
|
|
599
|
+
t2,
|
|
600
|
+
t3,
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
const { sqlStatements } = await diffTestSchemasSqlite(from, to, [], false, 'camelCase');
|
|
604
|
+
|
|
605
|
+
const st1 = `CREATE TABLE \`t1\` (
|
|
606
|
+
\`t1Id1\` integer PRIMARY KEY NOT NULL,
|
|
607
|
+
\`t1Col2\` integer NOT NULL,
|
|
608
|
+
\`t1Col3\` integer NOT NULL,
|
|
609
|
+
\`t2Ref\` integer NOT NULL,
|
|
610
|
+
\`t1Uni\` integer NOT NULL,
|
|
611
|
+
\`t1UniIdx\` integer NOT NULL,
|
|
612
|
+
\`t1Idx\` integer NOT NULL,
|
|
613
|
+
FOREIGN KEY (\`t2Ref\`) REFERENCES \`t2\`(\`t2Id\`) ON UPDATE no action ON DELETE no action,
|
|
614
|
+
FOREIGN KEY (\`t1Col2\`,\`t1Col3\`) REFERENCES \`t3\`(\`t3Id1\`,\`t3Id2\`) ON UPDATE no action ON DELETE no action
|
|
615
|
+
);
|
|
616
|
+
`;
|
|
617
|
+
|
|
618
|
+
const st2 = `CREATE UNIQUE INDEX \`t1UniIdx\` ON \`t1\` (\`t1UniIdx\`);`;
|
|
619
|
+
|
|
620
|
+
const st3 = `CREATE INDEX \`t1Idx\` ON \`t1\` (\`t1Idx\`);`;
|
|
621
|
+
|
|
622
|
+
const st4 = `CREATE UNIQUE INDEX \`t1Uni\` ON \`t1\` (\`t1Uni\`);`;
|
|
623
|
+
|
|
624
|
+
const st5 = `CREATE TABLE \`t2\` (
|
|
625
|
+
\`t2Id\` integer PRIMARY KEY AUTOINCREMENT NOT NULL
|
|
626
|
+
);
|
|
627
|
+
`;
|
|
628
|
+
|
|
629
|
+
const st6 = `CREATE TABLE \`t3\` (
|
|
630
|
+
\`t3Id1\` integer,
|
|
631
|
+
\`t3Id2\` integer,
|
|
632
|
+
PRIMARY KEY(\`t3Id1\`, \`t3Id2\`)
|
|
633
|
+
);
|
|
634
|
+
`;
|
|
635
|
+
|
|
636
|
+
expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6]);
|
|
637
|
+
});
|