@anthonyalayo/drizzle-kit 1.0.1 → 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.
Files changed (172) hide show
  1. package/api.d.mts +2891 -0
  2. package/api.d.ts +2891 -0
  3. package/api.js +47534 -0
  4. package/api.mjs +47521 -0
  5. package/bin.cjs +80043 -0
  6. package/{src/index.ts → index-Z-1TKnbX.d.mts} +123 -141
  7. package/index-Z-1TKnbX.d.ts +308 -0
  8. package/index.d.mts +2 -0
  9. package/index.d.ts +2 -0
  10. package/index.js +32 -0
  11. package/index.mjs +7 -0
  12. package/package.json +2 -2
  13. package/utils.js +6056 -0
  14. package/utils.mjs +6032 -0
  15. package/.gitignore +0 -25
  16. package/build.dev.ts +0 -51
  17. package/build.ts +0 -112
  18. package/imports-checker/analyze.ts +0 -78
  19. package/imports-checker/checker.ts +0 -296
  20. package/imports-checker/grammar/grammar.ohm +0 -121
  21. package/imports-checker/grammar/grammar.ohm-bundle.d.ts +0 -164
  22. package/imports-checker/grammar/grammar.ohm-bundle.js +0 -753
  23. package/imports-checker/index.ts +0 -48
  24. package/patches/difflib@0.2.4.patch +0 -31
  25. package/src/@types/utils.ts +0 -58
  26. package/src/api.ts +0 -504
  27. package/src/cli/commands/_es5.ts +0 -2
  28. package/src/cli/commands/check.ts +0 -52
  29. package/src/cli/commands/drop.ts +0 -60
  30. package/src/cli/commands/introspect.ts +0 -772
  31. package/src/cli/commands/libSqlPushUtils.ts +0 -355
  32. package/src/cli/commands/migrate.ts +0 -1526
  33. package/src/cli/commands/mysqlIntrospect.ts +0 -53
  34. package/src/cli/commands/mysqlPushUtils.ts +0 -352
  35. package/src/cli/commands/mysqlUp.ts +0 -102
  36. package/src/cli/commands/pgIntrospect.ts +0 -55
  37. package/src/cli/commands/pgPushUtils.ts +0 -269
  38. package/src/cli/commands/pgUp.ts +0 -217
  39. package/src/cli/commands/push.ts +0 -637
  40. package/src/cli/commands/singlestoreIntrospect.ts +0 -53
  41. package/src/cli/commands/singlestorePushUtils.ts +0 -456
  42. package/src/cli/commands/singlestoreUp.ts +0 -1
  43. package/src/cli/commands/sqliteIntrospect.ts +0 -96
  44. package/src/cli/commands/sqlitePushUtils.ts +0 -322
  45. package/src/cli/commands/sqliteUp.ts +0 -52
  46. package/src/cli/commands/utils.ts +0 -858
  47. package/src/cli/connections.ts +0 -914
  48. package/src/cli/index.ts +0 -48
  49. package/src/cli/schema.ts +0 -794
  50. package/src/cli/selector-ui.ts +0 -38
  51. package/src/cli/utils.ts +0 -112
  52. package/src/cli/validations/cli.ts +0 -79
  53. package/src/cli/validations/common.ts +0 -193
  54. package/src/cli/validations/libsql.ts +0 -27
  55. package/src/cli/validations/mysql.ts +0 -61
  56. package/src/cli/validations/outputs.ts +0 -91
  57. package/src/cli/validations/postgres.ts +0 -83
  58. package/src/cli/validations/singlestore.ts +0 -61
  59. package/src/cli/validations/sqlite.ts +0 -104
  60. package/src/cli/validations/studio.ts +0 -24
  61. package/src/cli/views.ts +0 -658
  62. package/src/extensions/getTablesFilterByExtensions.ts +0 -16
  63. package/src/extensions/vector.ts +0 -10
  64. package/src/global.ts +0 -61
  65. package/src/introspect-mysql.ts +0 -1030
  66. package/src/introspect-pg.ts +0 -1435
  67. package/src/introspect-singlestore.ts +0 -913
  68. package/src/introspect-sqlite.ts +0 -533
  69. package/src/jsonDiffer.js +0 -882
  70. package/src/jsonStatements.ts +0 -3590
  71. package/src/loader.mjs +0 -57
  72. package/src/migrationPreparator.ts +0 -217
  73. package/src/schemaValidator.ts +0 -23
  74. package/src/serializer/index.ts +0 -142
  75. package/src/serializer/mysqlImports.ts +0 -38
  76. package/src/serializer/mysqlSchema.ts +0 -423
  77. package/src/serializer/mysqlSerializer.ts +0 -999
  78. package/src/serializer/pgImports.ts +0 -106
  79. package/src/serializer/pgSchema.ts +0 -937
  80. package/src/serializer/pgSerializer.ts +0 -2223
  81. package/src/serializer/singlestoreImports.ts +0 -38
  82. package/src/serializer/singlestoreSchema.ts +0 -257
  83. package/src/serializer/singlestoreSerializer.ts +0 -767
  84. package/src/serializer/sqliteImports.ts +0 -41
  85. package/src/serializer/sqliteSchema.ts +0 -352
  86. package/src/serializer/sqliteSerializer.ts +0 -931
  87. package/src/serializer/studio.ts +0 -694
  88. package/src/serializer/utils.ts +0 -45
  89. package/src/simulator.ts +0 -157
  90. package/src/snapshotsDiffer.ts +0 -4398
  91. package/src/sqlgenerator.ts +0 -4212
  92. package/src/statementCombiner.ts +0 -597
  93. package/src/utils/certs.ts +0 -44
  94. package/src/utils/words.ts +0 -1333
  95. package/src/utils.ts +0 -370
  96. package/tests/bin.test.ts +0 -58
  97. package/tests/cli/d1http.config.ts +0 -12
  98. package/tests/cli/drizzle.config.ts +0 -9
  99. package/tests/cli/durable-sqlite.config.ts +0 -7
  100. package/tests/cli/expo.config.ts +0 -7
  101. package/tests/cli/postgres.config.ts +0 -13
  102. package/tests/cli/postgres2.config.ts +0 -17
  103. package/tests/cli/schema.ts +0 -1
  104. package/tests/cli/turso.config.ts +0 -10
  105. package/tests/cli-export.test.ts +0 -79
  106. package/tests/cli-generate.test.ts +0 -259
  107. package/tests/cli-migrate.test.ts +0 -103
  108. package/tests/cli-push.test.ts +0 -126
  109. package/tests/common.ts +0 -15
  110. package/tests/indexes/common.ts +0 -21
  111. package/tests/indexes/pg.test.ts +0 -245
  112. package/tests/introspect/libsql.test.ts +0 -35
  113. package/tests/introspect/mysql.test.ts +0 -319
  114. package/tests/introspect/pg.test.ts +0 -894
  115. package/tests/introspect/singlestore.test.ts +0 -275
  116. package/tests/introspect/sqlite.test.ts +0 -122
  117. package/tests/libsql-checks.test.ts +0 -308
  118. package/tests/libsql-statements.test.ts +0 -989
  119. package/tests/libsql-views.test.ts +0 -218
  120. package/tests/migrate/libsq-schema.ts +0 -6
  121. package/tests/migrate/libsql-migrate.test.ts +0 -58
  122. package/tests/migrate/migrations/0000_little_blizzard.sql +0 -4
  123. package/tests/migrate/migrations/0001_nebulous_storm.sql +0 -10
  124. package/tests/migrate/migrations/meta/0000_snapshot.json +0 -40
  125. package/tests/migrate/migrations/meta/0001_snapshot.json +0 -40
  126. package/tests/migrate/migrations/meta/_journal.json +0 -20
  127. package/tests/mysql-checks.test.ts +0 -291
  128. package/tests/mysql-generated.test.ts +0 -1290
  129. package/tests/mysql-schemas.test.ts +0 -156
  130. package/tests/mysql-views.test.ts +0 -553
  131. package/tests/mysql.test.ts +0 -863
  132. package/tests/pg-array.test.ts +0 -368
  133. package/tests/pg-checks.test.ts +0 -329
  134. package/tests/pg-columns.test.ts +0 -484
  135. package/tests/pg-domains.test.ts +0 -306
  136. package/tests/pg-enums.test.ts +0 -751
  137. package/tests/pg-generated.test.ts +0 -529
  138. package/tests/pg-identity.test.ts +0 -447
  139. package/tests/pg-schemas.test.ts +0 -105
  140. package/tests/pg-sequences.test.ts +0 -298
  141. package/tests/pg-tables.test.ts +0 -1021
  142. package/tests/pg-views.test.ts +0 -1929
  143. package/tests/push/common.ts +0 -58
  144. package/tests/push/libsql.test.ts +0 -1400
  145. package/tests/push/mysql-push.test.ts +0 -345
  146. package/tests/push/mysql.test.ts +0 -788
  147. package/tests/push/pg.test.ts +0 -4341
  148. package/tests/push/singlestore-push.test.ts +0 -894
  149. package/tests/push/singlestore.test.ts +0 -444
  150. package/tests/push/sqlite.test.ts +0 -1613
  151. package/tests/rls/pg-policy.test.ts +0 -1656
  152. package/tests/rls/pg-role.test.ts +0 -234
  153. package/tests/schemaDiffer.ts +0 -2852
  154. package/tests/singlestore-generated.test.ts +0 -1290
  155. package/tests/singlestore-schemas.test.ts +0 -155
  156. package/tests/singlestore.test.ts +0 -978
  157. package/tests/sqlite-checks.test.ts +0 -308
  158. package/tests/sqlite-columns.test.ts +0 -1049
  159. package/tests/sqlite-generated.test.ts +0 -1726
  160. package/tests/sqlite-tables.test.ts +0 -637
  161. package/tests/sqlite-views.test.ts +0 -218
  162. package/tests/statements-combiner/libsql-statements-combiner.test.ts +0 -1812
  163. package/tests/statements-combiner/singlestore-statements-combiner.test.ts +0 -882
  164. package/tests/statements-combiner/sqlite-statements-combiner.test.ts +0 -1211
  165. package/tests/test/sqlite.test.ts +0 -39
  166. package/tests/testsinglestore.ts +0 -29
  167. package/tests/validations.test.ts +0 -869
  168. package/tests/wrap-param.test.ts +0 -16
  169. package/tsconfig.build.json +0 -4
  170. package/tsconfig.cli-types.json +0 -9
  171. package/tsconfig.json +0 -28
  172. package/vitest.config.ts +0 -34
@@ -1,269 +0,0 @@
1
- import chalk from 'chalk';
2
- import { render } from 'hanji';
3
- import type { JsonStatement } from '../../jsonStatements';
4
- import { PgSquasher } from '../../serializer/pgSchema';
5
- import { fromJson } from '../../sqlgenerator';
6
- import type { DB } from '../../utils';
7
- import { Select } from '../selector-ui';
8
-
9
- // export const filterStatements = (statements: JsonStatement[]) => {
10
- // return statements.filter((statement) => {
11
- // if (statement.type === "alter_table_alter_column_set_type") {
12
- // // Don't need to handle it on migrations step and introspection
13
- // // but for both it should be skipped
14
- // if (
15
- // statement.oldDataType.startsWith("tinyint") &&
16
- // statement.newDataType.startsWith("boolean")
17
- // ) {
18
- // return false;
19
- // }
20
- // } else if (statement.type === "alter_table_alter_column_set_default") {
21
- // if (
22
- // statement.newDefaultValue === false &&
23
- // statement.oldDefaultValue === 0 &&
24
- // statement.newDataType === "boolean"
25
- // ) {
26
- // return false;
27
- // }
28
- // if (
29
- // statement.newDefaultValue === true &&
30
- // statement.oldDefaultValue === 1 &&
31
- // statement.newDataType === "boolean"
32
- // ) {
33
- // return false;
34
- // }
35
- // }
36
- // return true;
37
- // });
38
- // };
39
-
40
- function concatSchemaAndTableName(schema: string | undefined, table: string) {
41
- return schema ? `"${schema}"."${table}"` : `"${table}"`;
42
- }
43
-
44
- function tableNameWithSchemaFrom(
45
- schema: string | undefined,
46
- tableName: string,
47
- renamedSchemas: Record<string, string>,
48
- renamedTables: Record<string, string>,
49
- ) {
50
- const newSchemaName = schema ? (renamedSchemas[schema] ? renamedSchemas[schema] : schema) : undefined;
51
-
52
- const newTableName = renamedTables[concatSchemaAndTableName(newSchemaName, tableName)]
53
- ? renamedTables[concatSchemaAndTableName(newSchemaName, tableName)]
54
- : tableName;
55
-
56
- return concatSchemaAndTableName(newSchemaName, newTableName);
57
- }
58
-
59
- export const pgSuggestions = async (db: DB, statements: JsonStatement[]) => {
60
- let shouldAskForApprove = false;
61
- const statementsToExecute: string[] = [];
62
- const infoToPrint: string[] = [];
63
-
64
- const tablesToRemove: string[] = [];
65
- const columnsToRemove: string[] = [];
66
- const schemasToRemove: string[] = [];
67
- const tablesToTruncate: string[] = [];
68
- const matViewsToRemove: string[] = [];
69
-
70
- let renamedSchemas: Record<string, string> = {};
71
- let renamedTables: Record<string, string> = {};
72
-
73
- for (const statement of statements) {
74
- if (statement.type === 'rename_schema') {
75
- renamedSchemas[statement.to] = statement.from;
76
- } else if (statement.type === 'rename_table') {
77
- renamedTables[concatSchemaAndTableName(statement.toSchema, statement.tableNameTo)] = statement.tableNameFrom;
78
- } else if (statement.type === 'drop_table') {
79
- const res = await db.query(
80
- `select count(*) as count from ${
81
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
82
- }`,
83
- );
84
- const count = Number(res[0].count);
85
- if (count > 0) {
86
- infoToPrint.push(`· You're about to delete ${chalk.underline(statement.tableName)} table with ${count} items`);
87
- // statementsToExecute.push(
88
- // `truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
89
- // );
90
- tablesToRemove.push(statement.tableName);
91
- shouldAskForApprove = true;
92
- }
93
- } else if (statement.type === 'drop_view' && statement.materialized) {
94
- const res = await db.query(`select count(*) as count from "${statement.schema ?? 'public'}"."${statement.name}"`);
95
- const count = Number(res[0].count);
96
- if (count > 0) {
97
- infoToPrint.push(
98
- `· You're about to delete "${chalk.underline(statement.name)}" materialized view with ${count} items`,
99
- );
100
-
101
- matViewsToRemove.push(statement.name);
102
- shouldAskForApprove = true;
103
- }
104
- } else if (statement.type === 'alter_table_drop_column') {
105
- const res = await db.query(
106
- `select count(*) as count from ${
107
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
108
- }`,
109
- );
110
- const count = Number(res[0].count);
111
- if (count > 0) {
112
- infoToPrint.push(
113
- `· You're about to delete ${
114
- chalk.underline(statement.columnName)
115
- } column in ${statement.tableName} table with ${count} items`,
116
- );
117
- columnsToRemove.push(`${statement.tableName}_${statement.columnName}`);
118
- shouldAskForApprove = true;
119
- }
120
- } else if (statement.type === 'drop_schema') {
121
- const res = await db.query(
122
- `select count(*) as count from information_schema.tables where table_schema = '${statement.name}';`,
123
- );
124
- const count = Number(res[0].count);
125
- if (count > 0) {
126
- infoToPrint.push(`· You're about to delete ${chalk.underline(statement.name)} schema with ${count} tables`);
127
- schemasToRemove.push(statement.name);
128
- shouldAskForApprove = true;
129
- }
130
- } else if (statement.type === 'alter_table_alter_column_set_type') {
131
- const res = await db.query(
132
- `select count(*) as count from ${
133
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
134
- }`,
135
- );
136
- const count = Number(res[0].count);
137
- if (count > 0) {
138
- infoToPrint.push(
139
- `· You're about to change ${chalk.underline(statement.columnName)} column type from ${
140
- chalk.underline(statement.oldDataType)
141
- } to ${
142
- chalk.underline(
143
- statement.newDataType,
144
- )
145
- } with ${count} items`,
146
- );
147
- statementsToExecute.push(
148
- `truncate table ${
149
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
150
- } cascade;`,
151
- );
152
- tablesToTruncate.push(statement.tableName);
153
- shouldAskForApprove = true;
154
- }
155
- } else if (statement.type === 'alter_table_alter_column_drop_pk') {
156
- const res = await db.query(
157
- `select count(*) as count from ${
158
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
159
- }`,
160
- );
161
- const count = Number(res[0].count);
162
- if (count > 0) {
163
- infoToPrint.push(
164
- `· You're about to change ${
165
- chalk.underline(statement.tableName)
166
- } primary key. This statements may fail and you table may left without primary key`,
167
- );
168
-
169
- tablesToTruncate.push(statement.tableName);
170
- shouldAskForApprove = true;
171
- }
172
-
173
- const tableNameWithSchema = tableNameWithSchemaFrom(
174
- statement.schema,
175
- statement.tableName,
176
- renamedSchemas,
177
- renamedTables,
178
- );
179
-
180
- const pkNameResponse = await db.query(
181
- `SELECT constraint_name FROM information_schema.table_constraints
182
- WHERE table_schema = '${
183
- typeof statement.schema === 'undefined' || statement.schema === '' ? 'public' : statement.schema
184
- }'
185
- AND table_name = '${statement.tableName}'
186
- AND constraint_type = 'PRIMARY KEY';`,
187
- );
188
-
189
- statementsToExecute.push(
190
- `ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT "${pkNameResponse[0].constraint_name}"`,
191
- );
192
- // we will generate statement for drop pk here and not after all if-else statements
193
- continue;
194
- } else if (statement.type === 'alter_table_add_column') {
195
- if (statement.column.notNull && typeof statement.column.default === 'undefined') {
196
- const res = await db.query(
197
- `select count(*) as count from ${
198
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
199
- }`,
200
- );
201
- const count = Number(res[0].count);
202
- if (count > 0) {
203
- infoToPrint.push(
204
- `· You're about to add not-null ${
205
- chalk.underline(statement.column.name)
206
- } column without default value, which contains ${count} items`,
207
- );
208
-
209
- tablesToTruncate.push(statement.tableName);
210
- statementsToExecute.push(
211
- `truncate table ${
212
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
213
- } cascade;`,
214
- );
215
-
216
- shouldAskForApprove = true;
217
- }
218
- }
219
- } else if (statement.type === 'create_unique_constraint') {
220
- const res = await db.query(
221
- `select count(*) as count from ${
222
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
223
- }`,
224
- );
225
- const count = Number(res[0].count);
226
- if (count > 0) {
227
- const unsquashedUnique = PgSquasher.unsquashUnique(statement.data);
228
- console.log(
229
- `· You're about to add ${
230
- chalk.underline(
231
- unsquashedUnique.name,
232
- )
233
- } unique constraint to the table, which contains ${count} items. If this statement fails, you will receive an error from the database. Do you want to truncate ${
234
- chalk.underline(
235
- statement.tableName,
236
- )
237
- } table?\n`,
238
- );
239
- const { status, data } = await render(
240
- new Select(['No, add the constraint without truncating the table', `Yes, truncate the table`]),
241
- );
242
- if (data?.index === 1) {
243
- tablesToTruncate.push(statement.tableName);
244
- statementsToExecute.push(
245
- `truncate table ${
246
- tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
247
- } cascade;`,
248
- );
249
- shouldAskForApprove = true;
250
- }
251
- }
252
- }
253
- const stmnt = fromJson([statement], 'postgresql', 'push');
254
- if (typeof stmnt !== 'undefined') {
255
- statementsToExecute.push(...stmnt);
256
- }
257
- }
258
-
259
- return {
260
- statementsToExecute: [...new Set(statementsToExecute)],
261
- shouldAskForApprove,
262
- infoToPrint,
263
- matViewsToRemove: [...new Set(matViewsToRemove)],
264
- columnsToRemove: [...new Set(columnsToRemove)],
265
- schemasToRemove: [...new Set(schemasToRemove)],
266
- tablesToTruncate: [...new Set(tablesToTruncate)],
267
- tablesToRemove: [...new Set(tablesToRemove)],
268
- };
269
- };
@@ -1,217 +0,0 @@
1
- import chalk from 'chalk';
2
- import { writeFileSync } from 'fs';
3
- import {
4
- Column,
5
- Index,
6
- PgSchema,
7
- PgSchemaV4,
8
- PgSchemaV5,
9
- pgSchemaV5,
10
- PgSchemaV6,
11
- pgSchemaV6,
12
- PgSchemaV7,
13
- Table,
14
- TableV5,
15
- } from '../../serializer/pgSchema';
16
- import { prepareOutFolder, validateWithReport } from '../../utils';
17
-
18
- export const upPgHandler = (out: string) => {
19
- const { snapshots } = prepareOutFolder(out, 'postgresql');
20
- const report = validateWithReport(snapshots, 'postgresql');
21
-
22
- report.nonLatest
23
- .map((it) => ({
24
- path: it,
25
- raw: report.rawMap[it]!! as Record<string, any>,
26
- }))
27
- .forEach((it) => {
28
- const path = it.path;
29
-
30
- let resultV6 = it.raw;
31
- if (it.raw.version === '5') {
32
- resultV6 = updateUpToV6(it.raw);
33
- }
34
-
35
- const result = updateUpToV7(resultV6);
36
-
37
- console.log(`[${chalk.green('✓')}] ${path}`);
38
-
39
- writeFileSync(path, JSON.stringify(result, null, 2));
40
- });
41
-
42
- console.log("Everything's fine 🐶🔥");
43
- };
44
-
45
- export const updateUpToV6 = (json: Record<string, any>): PgSchemaV6 => {
46
- const schema = pgSchemaV5.parse(json);
47
- const tables = Object.fromEntries(
48
- Object.entries(schema.tables).map((it) => {
49
- const table = it[1];
50
- const schema = table.schema || 'public';
51
- return [`${schema}.${table.name}`, table];
52
- }),
53
- );
54
- const enums = Object.fromEntries(
55
- Object.entries(schema.enums).map((it) => {
56
- const en = it[1];
57
- return [
58
- `public.${en.name}`,
59
- {
60
- name: en.name,
61
- schema: 'public',
62
- values: Object.values(en.values),
63
- },
64
- ];
65
- }),
66
- );
67
- return {
68
- ...schema,
69
- version: '6',
70
- dialect: 'postgresql',
71
- tables: tables,
72
- enums,
73
- };
74
- };
75
-
76
- export const updateUpToV7 = (json: Record<string, any>): PgSchemaV7 => {
77
- const schema = pgSchemaV6.parse(json);
78
- const tables = Object.fromEntries(
79
- Object.entries(schema.tables).map((it) => {
80
- const table = it[1];
81
- const mappedIndexes = Object.fromEntries(
82
- Object.entries(table.indexes).map((idx) => {
83
- const { columns, ...rest } = idx[1];
84
- const mappedColumns = columns.map<Index['columns'][number]>((it) => {
85
- return {
86
- expression: it,
87
- isExpression: false,
88
- asc: true,
89
- nulls: 'last',
90
- opClass: undefined,
91
- };
92
- });
93
- return [idx[0], { columns: mappedColumns, with: {}, ...rest }];
94
- }),
95
- );
96
- return [it[0], { ...table, indexes: mappedIndexes, policies: {}, isRLSEnabled: false, checkConstraints: {} }];
97
- }),
98
- );
99
-
100
- return {
101
- ...schema,
102
- version: '7',
103
- dialect: 'postgresql',
104
- sequences: {},
105
- tables: tables,
106
- policies: {},
107
- views: {},
108
- roles: {},
109
- };
110
- };
111
-
112
- // Changed index format stored in snapshot for PostgreSQL in 0.22.0
113
- export const updateUpToV8 = (json: Record<string, any>): PgSchema => {
114
- const schema = pgSchemaV6.parse(json);
115
- const tables = Object.fromEntries(
116
- Object.entries(schema.tables).map((it) => {
117
- const table = it[1];
118
- const mappedIndexes = Object.fromEntries(
119
- Object.entries(table.indexes).map((idx) => {
120
- const { columns, ...rest } = idx[1];
121
- const mappedColumns = columns.map<Index['columns'][number]>((it) => {
122
- return {
123
- expression: it,
124
- isExpression: false,
125
- asc: true,
126
- nulls: 'last',
127
- opClass: undefined,
128
- };
129
- });
130
- return [idx[0], { columns: mappedColumns, with: {}, ...rest }];
131
- }),
132
- );
133
- return [it[0], { ...table, indexes: mappedIndexes, policies: {}, isRLSEnabled: false, checkConstraints: {} }];
134
- }),
135
- );
136
-
137
- return {
138
- ...schema,
139
- version: '8',
140
- dialect: 'postgresql',
141
- domains: {},
142
- sequences: {},
143
- tables: tables,
144
- policies: {},
145
- views: {},
146
- roles: {},
147
- };
148
- };
149
-
150
- // major migration with of folder structure, etc...
151
- export const upPgHandlerV4toV5 = (obj: PgSchemaV4): PgSchemaV5 => {
152
- const mappedTables: Record<string, TableV5> = {};
153
-
154
- for (const [key, table] of Object.entries(obj.tables)) {
155
- const mappedColumns: Record<string, Column> = {};
156
- for (const [ckey, column] of Object.entries(table.columns)) {
157
- let newDefault: any = column.default;
158
- let newType: string = column.type;
159
- if (column.type.toLowerCase() === 'date') {
160
- if (typeof column.default !== 'undefined') {
161
- if (column.default.startsWith("'") && column.default.endsWith("'")) {
162
- newDefault = `'${
163
- column.default
164
- .substring(1, column.default.length - 1)
165
- .split('T')[0]
166
- }'`;
167
- } else {
168
- newDefault = column.default.split('T')[0];
169
- }
170
- }
171
- } else if (column.type.toLowerCase().startsWith('timestamp')) {
172
- if (typeof column.default !== 'undefined') {
173
- if (column.default.startsWith("'") && column.default.endsWith("'")) {
174
- newDefault = `'${
175
- column.default
176
- .substring(1, column.default.length - 1)
177
- .replace('T', ' ')
178
- .slice(0, 23)
179
- }'`;
180
- } else {
181
- newDefault = column.default.replace('T', ' ').slice(0, 23);
182
- }
183
- }
184
- newType = column.type
185
- .toLowerCase()
186
- .replace('timestamp (', 'timestamp(');
187
- } else if (column.type.toLowerCase().startsWith('time')) {
188
- newType = column.type.toLowerCase().replace('time (', 'time(');
189
- } else if (column.type.toLowerCase().startsWith('interval')) {
190
- newType = column.type.toLowerCase().replace(' (', '(');
191
- }
192
- mappedColumns[ckey] = { ...column, default: newDefault, type: newType };
193
- }
194
-
195
- mappedTables[key] = {
196
- ...table,
197
- columns: mappedColumns,
198
- compositePrimaryKeys: {},
199
- uniqueConstraints: {},
200
- };
201
- }
202
-
203
- return {
204
- version: '5',
205
- dialect: obj.dialect,
206
- id: obj.id,
207
- prevId: obj.prevId,
208
- tables: mappedTables,
209
- enums: obj.enums,
210
- schemas: obj.schemas,
211
- _meta: {
212
- schemas: {} as Record<string, string>,
213
- tables: {} as Record<string, string>,
214
- columns: {} as Record<string, string>,
215
- },
216
- };
217
- };