@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,269 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
};
|