@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.
Files changed (161) hide show
  1. package/.gitignore +25 -0
  2. package/README.md +79 -0
  3. package/build.dev.ts +51 -0
  4. package/build.ts +112 -0
  5. package/imports-checker/analyze.ts +78 -0
  6. package/imports-checker/checker.ts +296 -0
  7. package/imports-checker/grammar/grammar.ohm +121 -0
  8. package/imports-checker/grammar/grammar.ohm-bundle.d.ts +164 -0
  9. package/imports-checker/grammar/grammar.ohm-bundle.js +753 -0
  10. package/imports-checker/index.ts +48 -0
  11. package/package.json +142 -0
  12. package/patches/difflib@0.2.4.patch +31 -0
  13. package/src/@types/utils.ts +58 -0
  14. package/src/api.ts +504 -0
  15. package/src/cli/commands/_es5.ts +2 -0
  16. package/src/cli/commands/check.ts +52 -0
  17. package/src/cli/commands/drop.ts +60 -0
  18. package/src/cli/commands/introspect.ts +772 -0
  19. package/src/cli/commands/libSqlPushUtils.ts +355 -0
  20. package/src/cli/commands/migrate.ts +1526 -0
  21. package/src/cli/commands/mysqlIntrospect.ts +53 -0
  22. package/src/cli/commands/mysqlPushUtils.ts +352 -0
  23. package/src/cli/commands/mysqlUp.ts +102 -0
  24. package/src/cli/commands/pgIntrospect.ts +55 -0
  25. package/src/cli/commands/pgPushUtils.ts +269 -0
  26. package/src/cli/commands/pgUp.ts +217 -0
  27. package/src/cli/commands/push.ts +637 -0
  28. package/src/cli/commands/singlestoreIntrospect.ts +53 -0
  29. package/src/cli/commands/singlestorePushUtils.ts +456 -0
  30. package/src/cli/commands/singlestoreUp.ts +1 -0
  31. package/src/cli/commands/sqliteIntrospect.ts +96 -0
  32. package/src/cli/commands/sqlitePushUtils.ts +322 -0
  33. package/src/cli/commands/sqliteUp.ts +52 -0
  34. package/src/cli/commands/utils.ts +858 -0
  35. package/src/cli/connections.ts +914 -0
  36. package/src/cli/index.ts +48 -0
  37. package/src/cli/schema.ts +794 -0
  38. package/src/cli/selector-ui.ts +38 -0
  39. package/src/cli/utils.ts +112 -0
  40. package/src/cli/validations/cli.ts +79 -0
  41. package/src/cli/validations/common.ts +193 -0
  42. package/src/cli/validations/libsql.ts +27 -0
  43. package/src/cli/validations/mysql.ts +61 -0
  44. package/src/cli/validations/outputs.ts +91 -0
  45. package/src/cli/validations/postgres.ts +83 -0
  46. package/src/cli/validations/singlestore.ts +61 -0
  47. package/src/cli/validations/sqlite.ts +104 -0
  48. package/src/cli/validations/studio.ts +24 -0
  49. package/src/cli/views.ts +658 -0
  50. package/src/extensions/getTablesFilterByExtensions.ts +16 -0
  51. package/src/extensions/vector.ts +10 -0
  52. package/src/global.ts +61 -0
  53. package/src/index.ts +326 -0
  54. package/src/introspect-mysql.ts +1030 -0
  55. package/src/introspect-pg.ts +1435 -0
  56. package/src/introspect-singlestore.ts +913 -0
  57. package/src/introspect-sqlite.ts +533 -0
  58. package/src/jsonDiffer.js +882 -0
  59. package/src/jsonStatements.ts +3590 -0
  60. package/src/loader.mjs +57 -0
  61. package/src/migrationPreparator.ts +217 -0
  62. package/src/schemaValidator.ts +23 -0
  63. package/src/serializer/index.ts +142 -0
  64. package/src/serializer/mysqlImports.ts +38 -0
  65. package/src/serializer/mysqlSchema.ts +423 -0
  66. package/src/serializer/mysqlSerializer.ts +999 -0
  67. package/src/serializer/pgImports.ts +106 -0
  68. package/src/serializer/pgSchema.ts +937 -0
  69. package/src/serializer/pgSerializer.ts +2223 -0
  70. package/src/serializer/singlestoreImports.ts +38 -0
  71. package/src/serializer/singlestoreSchema.ts +257 -0
  72. package/src/serializer/singlestoreSerializer.ts +767 -0
  73. package/src/serializer/sqliteImports.ts +41 -0
  74. package/src/serializer/sqliteSchema.ts +352 -0
  75. package/src/serializer/sqliteSerializer.ts +931 -0
  76. package/src/serializer/studio.ts +694 -0
  77. package/src/serializer/utils.ts +45 -0
  78. package/src/simulator.ts +157 -0
  79. package/src/snapshotsDiffer.ts +4398 -0
  80. package/src/sqlgenerator.ts +4212 -0
  81. package/src/statementCombiner.ts +597 -0
  82. package/src/utils/certs.ts +44 -0
  83. package/src/utils/words.ts +1333 -0
  84. package/src/utils.ts +370 -0
  85. package/tests/bin.test.ts +58 -0
  86. package/tests/cli/d1http.config.ts +12 -0
  87. package/tests/cli/drizzle.config.ts +9 -0
  88. package/tests/cli/durable-sqlite.config.ts +7 -0
  89. package/tests/cli/expo.config.ts +7 -0
  90. package/tests/cli/postgres.config.ts +13 -0
  91. package/tests/cli/postgres2.config.ts +17 -0
  92. package/tests/cli/schema.ts +1 -0
  93. package/tests/cli/turso.config.ts +10 -0
  94. package/tests/cli-export.test.ts +79 -0
  95. package/tests/cli-generate.test.ts +259 -0
  96. package/tests/cli-migrate.test.ts +103 -0
  97. package/tests/cli-push.test.ts +126 -0
  98. package/tests/common.ts +15 -0
  99. package/tests/indexes/common.ts +21 -0
  100. package/tests/indexes/pg.test.ts +245 -0
  101. package/tests/introspect/libsql.test.ts +35 -0
  102. package/tests/introspect/mysql.test.ts +319 -0
  103. package/tests/introspect/pg.test.ts +894 -0
  104. package/tests/introspect/singlestore.test.ts +275 -0
  105. package/tests/introspect/sqlite.test.ts +122 -0
  106. package/tests/libsql-checks.test.ts +308 -0
  107. package/tests/libsql-statements.test.ts +989 -0
  108. package/tests/libsql-views.test.ts +218 -0
  109. package/tests/migrate/libsq-schema.ts +6 -0
  110. package/tests/migrate/libsql-migrate.test.ts +58 -0
  111. package/tests/migrate/migrations/0000_little_blizzard.sql +4 -0
  112. package/tests/migrate/migrations/0001_nebulous_storm.sql +10 -0
  113. package/tests/migrate/migrations/meta/0000_snapshot.json +40 -0
  114. package/tests/migrate/migrations/meta/0001_snapshot.json +40 -0
  115. package/tests/migrate/migrations/meta/_journal.json +20 -0
  116. package/tests/mysql-checks.test.ts +291 -0
  117. package/tests/mysql-generated.test.ts +1290 -0
  118. package/tests/mysql-schemas.test.ts +156 -0
  119. package/tests/mysql-views.test.ts +553 -0
  120. package/tests/mysql.test.ts +863 -0
  121. package/tests/pg-array.test.ts +368 -0
  122. package/tests/pg-checks.test.ts +329 -0
  123. package/tests/pg-columns.test.ts +484 -0
  124. package/tests/pg-domains.test.ts +306 -0
  125. package/tests/pg-enums.test.ts +751 -0
  126. package/tests/pg-generated.test.ts +529 -0
  127. package/tests/pg-identity.test.ts +447 -0
  128. package/tests/pg-schemas.test.ts +105 -0
  129. package/tests/pg-sequences.test.ts +298 -0
  130. package/tests/pg-tables.test.ts +1021 -0
  131. package/tests/pg-views.test.ts +1929 -0
  132. package/tests/push/common.ts +58 -0
  133. package/tests/push/libsql.test.ts +1400 -0
  134. package/tests/push/mysql-push.test.ts +345 -0
  135. package/tests/push/mysql.test.ts +788 -0
  136. package/tests/push/pg.test.ts +4341 -0
  137. package/tests/push/singlestore-push.test.ts +894 -0
  138. package/tests/push/singlestore.test.ts +444 -0
  139. package/tests/push/sqlite.test.ts +1613 -0
  140. package/tests/rls/pg-policy.test.ts +1656 -0
  141. package/tests/rls/pg-role.test.ts +234 -0
  142. package/tests/schemaDiffer.ts +2852 -0
  143. package/tests/singlestore-generated.test.ts +1290 -0
  144. package/tests/singlestore-schemas.test.ts +155 -0
  145. package/tests/singlestore.test.ts +978 -0
  146. package/tests/sqlite-checks.test.ts +308 -0
  147. package/tests/sqlite-columns.test.ts +1049 -0
  148. package/tests/sqlite-generated.test.ts +1726 -0
  149. package/tests/sqlite-tables.test.ts +637 -0
  150. package/tests/sqlite-views.test.ts +218 -0
  151. package/tests/statements-combiner/libsql-statements-combiner.test.ts +1812 -0
  152. package/tests/statements-combiner/singlestore-statements-combiner.test.ts +882 -0
  153. package/tests/statements-combiner/sqlite-statements-combiner.test.ts +1211 -0
  154. package/tests/test/sqlite.test.ts +39 -0
  155. package/tests/testsinglestore.ts +29 -0
  156. package/tests/validations.test.ts +869 -0
  157. package/tests/wrap-param.test.ts +16 -0
  158. package/tsconfig.build.json +4 -0
  159. package/tsconfig.cli-types.json +9 -0
  160. package/tsconfig.json +28 -0
  161. package/vitest.config.ts +34 -0
@@ -0,0 +1,38 @@
1
+ import { is } from 'drizzle-orm';
2
+ import { AnySingleStoreTable, SingleStoreTable } from 'drizzle-orm/singlestore-core';
3
+ import { safeRegister } from '../cli/commands/utils';
4
+
5
+ export const prepareFromExports = (exports: Record<string, unknown>) => {
6
+ const tables: AnySingleStoreTable[] = [];
7
+ /* const views: SingleStoreView[] = []; */
8
+
9
+ const i0values = Object.values(exports);
10
+ i0values.forEach((t) => {
11
+ if (is(t, SingleStoreTable)) {
12
+ tables.push(t);
13
+ }
14
+
15
+ /* if (is(t, SingleStoreView)) {
16
+ views.push(t);
17
+ } */
18
+ });
19
+
20
+ return { tables /* views */ };
21
+ };
22
+
23
+ export const prepareFromSingleStoreImports = async (imports: string[]) => {
24
+ const tables: AnySingleStoreTable[] = [];
25
+ /* const views: SingleStoreView[] = []; */
26
+
27
+ const { unregister } = await safeRegister();
28
+ for (let i = 0; i < imports.length; i++) {
29
+ const it = imports[i];
30
+ const i0: Record<string, unknown> = require(`${it}`);
31
+ const prepared = prepareFromExports(i0);
32
+
33
+ tables.push(...prepared.tables);
34
+ /* views.push(...prepared.views); */
35
+ }
36
+ unregister();
37
+ return { tables: Array.from(new Set(tables)) /* , views */ };
38
+ };
@@ -0,0 +1,257 @@
1
+ import { any, boolean, enum as enumType, literal, object, record, string, TypeOf, union } from 'zod';
2
+ import { mapValues, originUUID, snapshotVersion } from '../global';
3
+
4
+ // ------- V3 --------
5
+ const index = object({
6
+ name: string(),
7
+ columns: string().array(),
8
+ isUnique: boolean(),
9
+ using: enumType(['btree', 'hash']).optional(),
10
+ algorithm: enumType(['default', 'inplace', 'copy']).optional(),
11
+ lock: enumType(['default', 'none', 'shared', 'exclusive']).optional(),
12
+ }).strict();
13
+
14
+ const column = object({
15
+ name: string(),
16
+ type: string(),
17
+ primaryKey: boolean(),
18
+ notNull: boolean(),
19
+ autoincrement: boolean().optional(),
20
+ default: any().optional(),
21
+ onUpdate: any().optional(),
22
+ generated: object({
23
+ type: enumType(['stored', 'virtual']),
24
+ as: string(),
25
+ }).optional(),
26
+ }).strict();
27
+
28
+ const compositePK = object({
29
+ name: string(),
30
+ columns: string().array(),
31
+ }).strict();
32
+
33
+ const uniqueConstraint = object({
34
+ name: string(),
35
+ columns: string().array(),
36
+ }).strict();
37
+
38
+ const table = object({
39
+ name: string(),
40
+ columns: record(string(), column),
41
+ indexes: record(string(), index),
42
+ compositePrimaryKeys: record(string(), compositePK),
43
+ uniqueConstraints: record(string(), uniqueConstraint).default({}),
44
+ }).strict();
45
+
46
+ const viewMeta = object({
47
+ algorithm: enumType(['undefined', 'merge', 'temptable']),
48
+ sqlSecurity: enumType(['definer', 'invoker']),
49
+ withCheckOption: enumType(['local', 'cascaded']).optional(),
50
+ }).strict();
51
+
52
+ /* export const view = object({
53
+ name: string(),
54
+ columns: record(string(), column),
55
+ definition: string().optional(),
56
+ isExisting: boolean(),
57
+ }).strict().merge(viewMeta);
58
+ type SquasherViewMeta = Omit<TypeOf<typeof viewMeta>, 'definer'>; */
59
+
60
+ export const kitInternals = object({
61
+ tables: record(
62
+ string(),
63
+ object({
64
+ columns: record(
65
+ string(),
66
+ object({ isDefaultAnExpression: boolean().optional() }).optional(),
67
+ ),
68
+ }).optional(),
69
+ ).optional(),
70
+ indexes: record(
71
+ string(),
72
+ object({
73
+ columns: record(
74
+ string(),
75
+ object({ isExpression: boolean().optional() }).optional(),
76
+ ),
77
+ }).optional(),
78
+ ).optional(),
79
+ }).optional();
80
+
81
+ // use main dialect
82
+ const dialect = literal('singlestore');
83
+
84
+ const schemaHash = object({
85
+ id: string(),
86
+ prevId: string(),
87
+ });
88
+
89
+ export const schemaInternal = object({
90
+ version: literal('1'),
91
+ dialect: dialect,
92
+ tables: record(string(), table),
93
+ /* views: record(string(), view).default({}), */
94
+ _meta: object({
95
+ tables: record(string(), string()),
96
+ columns: record(string(), string()),
97
+ }),
98
+ internal: kitInternals,
99
+ }).strict();
100
+
101
+ export const schema = schemaInternal.merge(schemaHash);
102
+
103
+ const tableSquashed = object({
104
+ name: string(),
105
+ columns: record(string(), column),
106
+ indexes: record(string(), string()),
107
+ compositePrimaryKeys: record(string(), string()),
108
+ uniqueConstraints: record(string(), string()).default({}),
109
+ }).strict();
110
+
111
+ /* const viewSquashed = view.omit({
112
+ algorithm: true,
113
+ sqlSecurity: true,
114
+ withCheckOption: true,
115
+ }).extend({ meta: string() }); */
116
+
117
+ export const schemaSquashed = object({
118
+ version: literal('1'),
119
+ dialect: dialect,
120
+ tables: record(string(), tableSquashed),
121
+ /* views: record(string(), viewSquashed), */
122
+ }).strict();
123
+
124
+ export type Dialect = TypeOf<typeof dialect>;
125
+ export type Column = TypeOf<typeof column>;
126
+ export type Table = TypeOf<typeof table>;
127
+ export type SingleStoreSchema = TypeOf<typeof schema>;
128
+ export type SingleStoreSchemaInternal = TypeOf<typeof schemaInternal>;
129
+ export type SingleStoreKitInternals = TypeOf<typeof kitInternals>;
130
+ export type SingleStoreSchemaSquashed = TypeOf<typeof schemaSquashed>;
131
+ export type Index = TypeOf<typeof index>;
132
+ export type PrimaryKey = TypeOf<typeof compositePK>;
133
+ export type UniqueConstraint = TypeOf<typeof uniqueConstraint>;
134
+ /* export type View = TypeOf<typeof view>; */
135
+ /* export type ViewSquashed = TypeOf<typeof viewSquashed>; */
136
+
137
+ export const SingleStoreSquasher = {
138
+ squashIdx: (idx: Index) => {
139
+ index.parse(idx);
140
+ return `${idx.name};${idx.columns.join(',')};${idx.isUnique};${idx.using ?? ''};${idx.algorithm ?? ''};${
141
+ idx.lock ?? ''
142
+ }`;
143
+ },
144
+ unsquashIdx: (input: string): Index => {
145
+ const [name, columnsString, isUnique, using, algorithm, lock] = input.split(';');
146
+ const destructed = {
147
+ name,
148
+ columns: columnsString.split(','),
149
+ isUnique: isUnique === 'true',
150
+ using: using ? using : undefined,
151
+ algorithm: algorithm ? algorithm : undefined,
152
+ lock: lock ? lock : undefined,
153
+ };
154
+ return index.parse(destructed);
155
+ },
156
+ squashPK: (pk: PrimaryKey) => {
157
+ return `${pk.name};${pk.columns.join(',')}`;
158
+ },
159
+ unsquashPK: (pk: string): PrimaryKey => {
160
+ const splitted = pk.split(';');
161
+ return { name: splitted[0], columns: splitted[1].split(',') };
162
+ },
163
+ squashUnique: (unq: UniqueConstraint) => {
164
+ return `${unq.name};${unq.columns.join(',')}`;
165
+ },
166
+ unsquashUnique: (unq: string): UniqueConstraint => {
167
+ const [name, columns] = unq.split(';');
168
+ return { name, columns: columns.split(',') };
169
+ },
170
+ /* squashView: (view: View): string => {
171
+ return `${view.algorithm};${view.sqlSecurity};${view.withCheckOption}`;
172
+ },
173
+ unsquashView: (meta: string): SquasherViewMeta => {
174
+ const [algorithm, sqlSecurity, withCheckOption] = meta.split(';');
175
+ const toReturn = {
176
+ algorithm: algorithm,
177
+ sqlSecurity: sqlSecurity,
178
+ withCheckOption: withCheckOption !== 'undefined' ? withCheckOption : undefined,
179
+ };
180
+
181
+ return viewMeta.parse(toReturn);
182
+ }, */
183
+ };
184
+
185
+ export const squashSingleStoreScheme = (json: SingleStoreSchema): SingleStoreSchemaSquashed => {
186
+ const mappedTables = Object.fromEntries(
187
+ Object.entries(json.tables).map((it) => {
188
+ const squashedIndexes = mapValues(it[1].indexes, (index) => {
189
+ return SingleStoreSquasher.squashIdx(index);
190
+ });
191
+
192
+ const squashedPKs = mapValues(it[1].compositePrimaryKeys, (pk) => {
193
+ return SingleStoreSquasher.squashPK(pk);
194
+ });
195
+
196
+ const squashedUniqueConstraints = mapValues(
197
+ it[1].uniqueConstraints,
198
+ (unq) => {
199
+ return SingleStoreSquasher.squashUnique(unq);
200
+ },
201
+ );
202
+
203
+ return [
204
+ it[0],
205
+ {
206
+ name: it[1].name,
207
+ columns: it[1].columns,
208
+ indexes: squashedIndexes,
209
+ compositePrimaryKeys: squashedPKs,
210
+ uniqueConstraints: squashedUniqueConstraints,
211
+ },
212
+ ];
213
+ }),
214
+ );
215
+
216
+ /* const mappedViews = Object.fromEntries(
217
+ Object.entries(json.views).map(([key, value]) => {
218
+ const meta = SingleStoreSquasher.squashView(value);
219
+
220
+ return [key, {
221
+ name: value.name,
222
+ isExisting: value.isExisting,
223
+ columns: value.columns,
224
+ definition: value.definition,
225
+ meta,
226
+ }];
227
+ }),
228
+ ); */
229
+
230
+ return {
231
+ version: '1',
232
+ dialect: json.dialect,
233
+ tables: mappedTables,
234
+ /* views: mappedViews, */
235
+ };
236
+ };
237
+
238
+ export const singlestoreSchema = schema;
239
+ export const singlestoreSchemaSquashed = schemaSquashed;
240
+
241
+ // no prev version
242
+ export const backwardCompatibleSingleStoreSchema = union([singlestoreSchema, schema]);
243
+
244
+ export const drySingleStore = singlestoreSchema.parse({
245
+ version: '1',
246
+ dialect: 'singlestore',
247
+ id: originUUID,
248
+ prevId: '',
249
+ tables: {},
250
+ schemas: {},
251
+ /* views: {}, */
252
+ _meta: {
253
+ schemas: {},
254
+ tables: {},
255
+ columns: {},
256
+ },
257
+ });