@anthonyalayo/drizzle-kit 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.d.mts +2891 -0
- package/api.d.ts +2891 -0
- package/api.js +47534 -0
- package/api.mjs +47521 -0
- package/bin.cjs +80043 -0
- package/{src/index.ts → index-Z-1TKnbX.d.mts} +123 -141
- package/index-Z-1TKnbX.d.ts +308 -0
- package/index.d.mts +2 -0
- package/index.d.ts +2 -0
- package/index.js +32 -0
- package/index.mjs +7 -0
- package/package.json +1 -1
- package/utils.js +6056 -0
- package/utils.mjs +6032 -0
- package/.gitignore +0 -25
- package/build.dev.ts +0 -51
- package/build.ts +0 -112
- package/imports-checker/analyze.ts +0 -78
- package/imports-checker/checker.ts +0 -296
- package/imports-checker/grammar/grammar.ohm +0 -121
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +0 -164
- package/imports-checker/grammar/grammar.ohm-bundle.js +0 -753
- package/imports-checker/index.ts +0 -48
- package/patches/difflib@0.2.4.patch +0 -31
- package/src/@types/utils.ts +0 -58
- package/src/api.ts +0 -504
- package/src/cli/commands/_es5.ts +0 -2
- package/src/cli/commands/check.ts +0 -52
- package/src/cli/commands/drop.ts +0 -60
- package/src/cli/commands/introspect.ts +0 -772
- package/src/cli/commands/libSqlPushUtils.ts +0 -355
- package/src/cli/commands/migrate.ts +0 -1526
- package/src/cli/commands/mysqlIntrospect.ts +0 -53
- package/src/cli/commands/mysqlPushUtils.ts +0 -352
- package/src/cli/commands/mysqlUp.ts +0 -102
- package/src/cli/commands/pgIntrospect.ts +0 -55
- package/src/cli/commands/pgPushUtils.ts +0 -269
- package/src/cli/commands/pgUp.ts +0 -217
- package/src/cli/commands/push.ts +0 -637
- package/src/cli/commands/singlestoreIntrospect.ts +0 -53
- package/src/cli/commands/singlestorePushUtils.ts +0 -456
- package/src/cli/commands/singlestoreUp.ts +0 -1
- package/src/cli/commands/sqliteIntrospect.ts +0 -96
- package/src/cli/commands/sqlitePushUtils.ts +0 -322
- package/src/cli/commands/sqliteUp.ts +0 -52
- package/src/cli/commands/utils.ts +0 -858
- package/src/cli/connections.ts +0 -914
- package/src/cli/index.ts +0 -48
- package/src/cli/schema.ts +0 -794
- package/src/cli/selector-ui.ts +0 -38
- package/src/cli/utils.ts +0 -112
- package/src/cli/validations/cli.ts +0 -79
- package/src/cli/validations/common.ts +0 -193
- package/src/cli/validations/libsql.ts +0 -27
- package/src/cli/validations/mysql.ts +0 -61
- package/src/cli/validations/outputs.ts +0 -91
- package/src/cli/validations/postgres.ts +0 -83
- package/src/cli/validations/singlestore.ts +0 -61
- package/src/cli/validations/sqlite.ts +0 -104
- package/src/cli/validations/studio.ts +0 -24
- package/src/cli/views.ts +0 -658
- package/src/extensions/getTablesFilterByExtensions.ts +0 -16
- package/src/extensions/vector.ts +0 -10
- package/src/global.ts +0 -61
- package/src/introspect-mysql.ts +0 -1030
- package/src/introspect-pg.ts +0 -1435
- package/src/introspect-singlestore.ts +0 -913
- package/src/introspect-sqlite.ts +0 -533
- package/src/jsonDiffer.js +0 -882
- package/src/jsonStatements.ts +0 -3590
- package/src/loader.mjs +0 -57
- package/src/migrationPreparator.ts +0 -217
- package/src/schemaValidator.ts +0 -23
- package/src/serializer/index.ts +0 -142
- package/src/serializer/mysqlImports.ts +0 -38
- package/src/serializer/mysqlSchema.ts +0 -423
- package/src/serializer/mysqlSerializer.ts +0 -999
- package/src/serializer/pgImports.ts +0 -106
- package/src/serializer/pgSchema.ts +0 -937
- package/src/serializer/pgSerializer.ts +0 -2223
- package/src/serializer/singlestoreImports.ts +0 -38
- package/src/serializer/singlestoreSchema.ts +0 -257
- package/src/serializer/singlestoreSerializer.ts +0 -767
- package/src/serializer/sqliteImports.ts +0 -41
- package/src/serializer/sqliteSchema.ts +0 -352
- package/src/serializer/sqliteSerializer.ts +0 -931
- package/src/serializer/studio.ts +0 -694
- package/src/serializer/utils.ts +0 -45
- package/src/simulator.ts +0 -157
- package/src/snapshotsDiffer.ts +0 -4398
- package/src/sqlgenerator.ts +0 -4212
- package/src/statementCombiner.ts +0 -597
- package/src/utils/certs.ts +0 -44
- package/src/utils/words.ts +0 -1333
- package/src/utils.ts +0 -370
- package/tests/bin.test.ts +0 -58
- package/tests/cli/d1http.config.ts +0 -12
- package/tests/cli/drizzle.config.ts +0 -9
- package/tests/cli/durable-sqlite.config.ts +0 -7
- package/tests/cli/expo.config.ts +0 -7
- package/tests/cli/postgres.config.ts +0 -13
- package/tests/cli/postgres2.config.ts +0 -17
- package/tests/cli/schema.ts +0 -1
- package/tests/cli/turso.config.ts +0 -10
- package/tests/cli-export.test.ts +0 -79
- package/tests/cli-generate.test.ts +0 -259
- package/tests/cli-migrate.test.ts +0 -103
- package/tests/cli-push.test.ts +0 -126
- package/tests/common.ts +0 -15
- package/tests/indexes/common.ts +0 -21
- package/tests/indexes/pg.test.ts +0 -245
- package/tests/introspect/libsql.test.ts +0 -35
- package/tests/introspect/mysql.test.ts +0 -319
- package/tests/introspect/pg.test.ts +0 -894
- package/tests/introspect/singlestore.test.ts +0 -275
- package/tests/introspect/sqlite.test.ts +0 -122
- package/tests/libsql-checks.test.ts +0 -308
- package/tests/libsql-statements.test.ts +0 -989
- package/tests/libsql-views.test.ts +0 -218
- package/tests/migrate/libsq-schema.ts +0 -6
- package/tests/migrate/libsql-migrate.test.ts +0 -58
- package/tests/migrate/migrations/0000_little_blizzard.sql +0 -4
- package/tests/migrate/migrations/0001_nebulous_storm.sql +0 -10
- package/tests/migrate/migrations/meta/0000_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/0001_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/_journal.json +0 -20
- package/tests/mysql-checks.test.ts +0 -291
- package/tests/mysql-generated.test.ts +0 -1290
- package/tests/mysql-schemas.test.ts +0 -156
- package/tests/mysql-views.test.ts +0 -553
- package/tests/mysql.test.ts +0 -863
- package/tests/pg-array.test.ts +0 -368
- package/tests/pg-checks.test.ts +0 -329
- package/tests/pg-columns.test.ts +0 -484
- package/tests/pg-domains.test.ts +0 -306
- package/tests/pg-enums.test.ts +0 -751
- package/tests/pg-generated.test.ts +0 -529
- package/tests/pg-identity.test.ts +0 -447
- package/tests/pg-schemas.test.ts +0 -105
- package/tests/pg-sequences.test.ts +0 -298
- package/tests/pg-tables.test.ts +0 -1021
- package/tests/pg-views.test.ts +0 -1929
- package/tests/push/common.ts +0 -58
- package/tests/push/libsql.test.ts +0 -1400
- package/tests/push/mysql-push.test.ts +0 -345
- package/tests/push/mysql.test.ts +0 -788
- package/tests/push/pg.test.ts +0 -4341
- package/tests/push/singlestore-push.test.ts +0 -894
- package/tests/push/singlestore.test.ts +0 -444
- package/tests/push/sqlite.test.ts +0 -1613
- package/tests/rls/pg-policy.test.ts +0 -1656
- package/tests/rls/pg-role.test.ts +0 -234
- package/tests/schemaDiffer.ts +0 -2852
- package/tests/singlestore-generated.test.ts +0 -1290
- package/tests/singlestore-schemas.test.ts +0 -155
- package/tests/singlestore.test.ts +0 -978
- package/tests/sqlite-checks.test.ts +0 -308
- package/tests/sqlite-columns.test.ts +0 -1049
- package/tests/sqlite-generated.test.ts +0 -1726
- package/tests/sqlite-tables.test.ts +0 -637
- package/tests/sqlite-views.test.ts +0 -218
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +0 -1812
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +0 -882
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +0 -1211
- package/tests/test/sqlite.test.ts +0 -39
- package/tests/testsinglestore.ts +0 -29
- package/tests/validations.test.ts +0 -869
- package/tests/wrap-param.test.ts +0 -16
- package/tsconfig.build.json +0 -4
- package/tsconfig.cli-types.json +0 -9
- package/tsconfig.json +0 -28
- package/vitest.config.ts +0 -34
package/src/statementCombiner.ts
DELETED
|
@@ -1,597 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
JsonCreateIndexStatement,
|
|
3
|
-
JsonRecreateTableStatement,
|
|
4
|
-
JsonStatement,
|
|
5
|
-
prepareCreateIndexesJson,
|
|
6
|
-
} from './jsonStatements';
|
|
7
|
-
import { SingleStoreSchemaSquashed } from './serializer/singlestoreSchema';
|
|
8
|
-
import { SQLiteSchemaSquashed, SQLiteSquasher } from './serializer/sqliteSchema';
|
|
9
|
-
|
|
10
|
-
export const prepareLibSQLRecreateTable = (
|
|
11
|
-
table: SQLiteSchemaSquashed['tables'][keyof SQLiteSchemaSquashed['tables']],
|
|
12
|
-
action?: 'push',
|
|
13
|
-
): (JsonRecreateTableStatement | JsonCreateIndexStatement)[] => {
|
|
14
|
-
const { name, columns, uniqueConstraints, indexes, checkConstraints } = table;
|
|
15
|
-
|
|
16
|
-
const composites: string[][] = Object.values(table.compositePrimaryKeys).map(
|
|
17
|
-
(it) => SQLiteSquasher.unsquashPK(it),
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
const references: string[] = Object.values(table.foreignKeys);
|
|
21
|
-
const fks = references.map((it) =>
|
|
22
|
-
action === 'push' ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
const statements: (JsonRecreateTableStatement | JsonCreateIndexStatement)[] = [
|
|
26
|
-
{
|
|
27
|
-
type: 'recreate_table',
|
|
28
|
-
tableName: name,
|
|
29
|
-
columns: Object.values(columns),
|
|
30
|
-
compositePKs: composites,
|
|
31
|
-
referenceData: fks,
|
|
32
|
-
uniqueConstraints: Object.values(uniqueConstraints),
|
|
33
|
-
checkConstraints: Object.values(checkConstraints),
|
|
34
|
-
},
|
|
35
|
-
];
|
|
36
|
-
|
|
37
|
-
if (Object.keys(indexes).length) {
|
|
38
|
-
statements.push(...prepareCreateIndexesJson(name, '', indexes));
|
|
39
|
-
}
|
|
40
|
-
return statements;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const prepareSQLiteRecreateTable = (
|
|
44
|
-
table: SQLiteSchemaSquashed['tables'][keyof SQLiteSchemaSquashed['tables']],
|
|
45
|
-
action?: 'push',
|
|
46
|
-
): JsonStatement[] => {
|
|
47
|
-
const { name, columns, uniqueConstraints, indexes, checkConstraints } = table;
|
|
48
|
-
|
|
49
|
-
const composites: string[][] = Object.values(table.compositePrimaryKeys).map(
|
|
50
|
-
(it) => SQLiteSquasher.unsquashPK(it),
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
const references: string[] = Object.values(table.foreignKeys);
|
|
54
|
-
const fks = references.map((it) =>
|
|
55
|
-
action === 'push' ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
const statements: JsonStatement[] = [
|
|
59
|
-
{
|
|
60
|
-
type: 'recreate_table',
|
|
61
|
-
tableName: name,
|
|
62
|
-
columns: Object.values(columns),
|
|
63
|
-
compositePKs: composites,
|
|
64
|
-
referenceData: fks,
|
|
65
|
-
uniqueConstraints: Object.values(uniqueConstraints),
|
|
66
|
-
checkConstraints: Object.values(checkConstraints),
|
|
67
|
-
},
|
|
68
|
-
];
|
|
69
|
-
|
|
70
|
-
if (Object.keys(indexes).length) {
|
|
71
|
-
statements.push(...prepareCreateIndexesJson(name, '', indexes));
|
|
72
|
-
}
|
|
73
|
-
return statements;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
export const libSQLCombineStatements = (
|
|
77
|
-
statements: JsonStatement[],
|
|
78
|
-
json2: SQLiteSchemaSquashed,
|
|
79
|
-
action?: 'push',
|
|
80
|
-
) => {
|
|
81
|
-
// const tablesContext: Record<string, string[]> = {};
|
|
82
|
-
const newStatements: Record<string, JsonStatement[]> = {};
|
|
83
|
-
for (const statement of statements) {
|
|
84
|
-
if (
|
|
85
|
-
statement.type === 'alter_table_alter_column_drop_autoincrement'
|
|
86
|
-
|| statement.type === 'alter_table_alter_column_set_autoincrement'
|
|
87
|
-
|| statement.type === 'alter_table_alter_column_drop_pk'
|
|
88
|
-
|| statement.type === 'alter_table_alter_column_set_pk'
|
|
89
|
-
|| statement.type === 'create_composite_pk'
|
|
90
|
-
|| statement.type === 'alter_composite_pk'
|
|
91
|
-
|| statement.type === 'delete_composite_pk'
|
|
92
|
-
|| statement.type === 'create_check_constraint'
|
|
93
|
-
|| statement.type === 'delete_check_constraint'
|
|
94
|
-
) {
|
|
95
|
-
const tableName = statement.tableName;
|
|
96
|
-
|
|
97
|
-
const statementsForTable = newStatements[tableName];
|
|
98
|
-
|
|
99
|
-
if (!statementsForTable) {
|
|
100
|
-
newStatements[tableName] = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
101
|
-
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
106
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
107
|
-
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
108
|
-
|
|
109
|
-
if (wasRename) {
|
|
110
|
-
newStatements[tableName].push(...preparedStatements);
|
|
111
|
-
} else {
|
|
112
|
-
newStatements[tableName] = preparedStatements;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (
|
|
122
|
-
statement.type === 'alter_table_alter_column_set_type'
|
|
123
|
-
|| statement.type === 'alter_table_alter_column_drop_notnull'
|
|
124
|
-
|| statement.type === 'alter_table_alter_column_set_notnull'
|
|
125
|
-
|| statement.type === 'alter_table_alter_column_set_default'
|
|
126
|
-
|| statement.type === 'alter_table_alter_column_drop_default'
|
|
127
|
-
) {
|
|
128
|
-
const { tableName, columnName, columnPk } = statement;
|
|
129
|
-
|
|
130
|
-
const columnIsPartOfForeignKey = Object.values(
|
|
131
|
-
json2.tables[tableName].foreignKeys,
|
|
132
|
-
).some((it) => {
|
|
133
|
-
const unsquashFk = action === 'push' ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it);
|
|
134
|
-
|
|
135
|
-
return (
|
|
136
|
-
unsquashFk.columnsFrom.includes(columnName)
|
|
137
|
-
);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const statementsForTable = newStatements[tableName];
|
|
141
|
-
|
|
142
|
-
if (
|
|
143
|
-
!statementsForTable && (columnIsPartOfForeignKey || columnPk)
|
|
144
|
-
) {
|
|
145
|
-
newStatements[tableName] = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (
|
|
150
|
-
statementsForTable && (columnIsPartOfForeignKey || columnPk)
|
|
151
|
-
) {
|
|
152
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
153
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
154
|
-
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
155
|
-
|
|
156
|
-
if (wasRename) {
|
|
157
|
-
newStatements[tableName].push(...preparedStatements);
|
|
158
|
-
} else {
|
|
159
|
-
newStatements[tableName] = preparedStatements;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
if (
|
|
165
|
-
statementsForTable && !(columnIsPartOfForeignKey || columnPk)
|
|
166
|
-
) {
|
|
167
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
168
|
-
newStatements[tableName].push(statement);
|
|
169
|
-
}
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
newStatements[tableName] = [statement];
|
|
174
|
-
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (statement.type === 'create_reference') {
|
|
179
|
-
const tableName = statement.tableName;
|
|
180
|
-
|
|
181
|
-
const data = action === 'push'
|
|
182
|
-
? SQLiteSquasher.unsquashPushFK(statement.data)
|
|
183
|
-
: SQLiteSquasher.unsquashFK(statement.data);
|
|
184
|
-
|
|
185
|
-
const statementsForTable = newStatements[tableName];
|
|
186
|
-
|
|
187
|
-
if (!statementsForTable) {
|
|
188
|
-
newStatements[tableName] = statement.isMulticolumn
|
|
189
|
-
? prepareLibSQLRecreateTable(json2.tables[tableName], action)
|
|
190
|
-
: [statement];
|
|
191
|
-
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// if add column with reference -> skip create_reference statement
|
|
196
|
-
if (
|
|
197
|
-
!statement.isMulticolumn
|
|
198
|
-
&& statementsForTable.some((st) =>
|
|
199
|
-
st.type === 'sqlite_alter_table_add_column' && st.column.name === data.columnsFrom[0]
|
|
200
|
-
)
|
|
201
|
-
) {
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (statement.isMulticolumn) {
|
|
206
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
207
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
208
|
-
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
209
|
-
|
|
210
|
-
if (wasRename) {
|
|
211
|
-
newStatements[tableName].push(...preparedStatements);
|
|
212
|
-
} else {
|
|
213
|
-
newStatements[tableName] = preparedStatements;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
continue;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
223
|
-
newStatements[tableName].push(statement);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
continue;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (statement.type === 'delete_reference') {
|
|
230
|
-
const tableName = statement.tableName;
|
|
231
|
-
|
|
232
|
-
const statementsForTable = newStatements[tableName];
|
|
233
|
-
|
|
234
|
-
if (!statementsForTable) {
|
|
235
|
-
newStatements[tableName] = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
236
|
-
continue;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
240
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
241
|
-
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
242
|
-
|
|
243
|
-
if (wasRename) {
|
|
244
|
-
newStatements[tableName].push(...preparedStatements);
|
|
245
|
-
} else {
|
|
246
|
-
newStatements[tableName] = preparedStatements;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (statement.type === 'sqlite_alter_table_add_column' && statement.column.primaryKey) {
|
|
256
|
-
const tableName = statement.tableName;
|
|
257
|
-
|
|
258
|
-
const statementsForTable = newStatements[tableName];
|
|
259
|
-
|
|
260
|
-
if (!statementsForTable) {
|
|
261
|
-
newStatements[tableName] = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
266
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
267
|
-
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
268
|
-
|
|
269
|
-
if (wasRename) {
|
|
270
|
-
newStatements[tableName].push(...preparedStatements);
|
|
271
|
-
} else {
|
|
272
|
-
newStatements[tableName] = preparedStatements;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const tableName = statement.type === 'rename_table'
|
|
282
|
-
? statement.tableNameTo
|
|
283
|
-
: (statement as { tableName: string }).tableName;
|
|
284
|
-
const statementsForTable = newStatements[tableName];
|
|
285
|
-
|
|
286
|
-
if (!statementsForTable) {
|
|
287
|
-
newStatements[tableName] = [statement];
|
|
288
|
-
continue;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
292
|
-
newStatements[tableName].push(statement);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const combinedStatements = Object.values(newStatements).flat();
|
|
297
|
-
const renamedTables = combinedStatements.filter((it) => it.type === 'rename_table');
|
|
298
|
-
const renamedColumns = combinedStatements.filter((it) => it.type === 'alter_table_rename_column');
|
|
299
|
-
|
|
300
|
-
const rest = combinedStatements.filter((it) => it.type !== 'rename_table' && it.type !== 'alter_table_rename_column');
|
|
301
|
-
|
|
302
|
-
return [...renamedTables, ...renamedColumns, ...rest];
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
export const sqliteCombineStatements = (
|
|
306
|
-
statements: JsonStatement[],
|
|
307
|
-
json2: SQLiteSchemaSquashed,
|
|
308
|
-
action?: 'push',
|
|
309
|
-
) => {
|
|
310
|
-
// const tablesContext: Record<string, string[]> = {};
|
|
311
|
-
const newStatements: Record<string, JsonStatement[]> = {};
|
|
312
|
-
for (const statement of statements) {
|
|
313
|
-
if (
|
|
314
|
-
statement.type === 'alter_table_alter_column_set_type'
|
|
315
|
-
|| statement.type === 'alter_table_alter_column_set_default'
|
|
316
|
-
|| statement.type === 'alter_table_alter_column_drop_default'
|
|
317
|
-
|| statement.type === 'alter_table_alter_column_set_notnull'
|
|
318
|
-
|| statement.type === 'alter_table_alter_column_drop_notnull'
|
|
319
|
-
|| statement.type === 'alter_table_alter_column_drop_autoincrement'
|
|
320
|
-
|| statement.type === 'alter_table_alter_column_set_autoincrement'
|
|
321
|
-
|| statement.type === 'alter_table_alter_column_drop_pk'
|
|
322
|
-
|| statement.type === 'alter_table_alter_column_set_pk'
|
|
323
|
-
|| statement.type === 'delete_reference'
|
|
324
|
-
|| statement.type === 'alter_reference'
|
|
325
|
-
|| statement.type === 'create_composite_pk'
|
|
326
|
-
|| statement.type === 'alter_composite_pk'
|
|
327
|
-
|| statement.type === 'delete_composite_pk'
|
|
328
|
-
|| statement.type === 'create_unique_constraint'
|
|
329
|
-
|| statement.type === 'delete_unique_constraint'
|
|
330
|
-
|| statement.type === 'create_check_constraint'
|
|
331
|
-
|| statement.type === 'delete_check_constraint'
|
|
332
|
-
) {
|
|
333
|
-
const tableName = statement.tableName;
|
|
334
|
-
|
|
335
|
-
const statementsForTable = newStatements[tableName];
|
|
336
|
-
|
|
337
|
-
if (!statementsForTable) {
|
|
338
|
-
newStatements[tableName] = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
339
|
-
continue;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
343
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
344
|
-
const preparedStatements = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
345
|
-
|
|
346
|
-
if (wasRename) {
|
|
347
|
-
newStatements[tableName].push(...preparedStatements);
|
|
348
|
-
} else {
|
|
349
|
-
newStatements[tableName] = preparedStatements;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
continue;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
continue;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
if (statement.type === 'sqlite_alter_table_add_column' && statement.column.primaryKey) {
|
|
359
|
-
const tableName = statement.tableName;
|
|
360
|
-
|
|
361
|
-
const statementsForTable = newStatements[tableName];
|
|
362
|
-
|
|
363
|
-
if (!statementsForTable) {
|
|
364
|
-
newStatements[tableName] = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
365
|
-
continue;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
369
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
370
|
-
const preparedStatements = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
371
|
-
|
|
372
|
-
if (wasRename) {
|
|
373
|
-
newStatements[tableName].push(...preparedStatements);
|
|
374
|
-
} else {
|
|
375
|
-
newStatements[tableName] = preparedStatements;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
continue;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
if (statement.type === 'create_reference') {
|
|
385
|
-
const tableName = statement.tableName;
|
|
386
|
-
|
|
387
|
-
const data = action === 'push'
|
|
388
|
-
? SQLiteSquasher.unsquashPushFK(statement.data)
|
|
389
|
-
: SQLiteSquasher.unsquashFK(statement.data);
|
|
390
|
-
const statementsForTable = newStatements[tableName];
|
|
391
|
-
|
|
392
|
-
if (!statementsForTable) {
|
|
393
|
-
newStatements[tableName] = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
394
|
-
continue;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
// if add column with reference -> skip create_reference statement
|
|
398
|
-
if (
|
|
399
|
-
data.columnsFrom.length === 1
|
|
400
|
-
&& statementsForTable.some((st) =>
|
|
401
|
-
st.type === 'sqlite_alter_table_add_column' && st.column.name === data.columnsFrom[0]
|
|
402
|
-
)
|
|
403
|
-
) {
|
|
404
|
-
continue;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
408
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
409
|
-
const preparedStatements = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
410
|
-
|
|
411
|
-
if (wasRename) {
|
|
412
|
-
newStatements[tableName].push(...preparedStatements);
|
|
413
|
-
} else {
|
|
414
|
-
newStatements[tableName] = preparedStatements;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
continue;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
continue;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
const tableName = statement.type === 'rename_table'
|
|
424
|
-
? statement.tableNameTo
|
|
425
|
-
: (statement as { tableName: string }).tableName;
|
|
426
|
-
|
|
427
|
-
const statementsForTable = newStatements[tableName];
|
|
428
|
-
|
|
429
|
-
if (!statementsForTable) {
|
|
430
|
-
newStatements[tableName] = [statement];
|
|
431
|
-
continue;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
435
|
-
newStatements[tableName].push(statement);
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
const combinedStatements = Object.values(newStatements).flat();
|
|
440
|
-
|
|
441
|
-
const renamedTables = combinedStatements.filter((it) => it.type === 'rename_table');
|
|
442
|
-
const renamedColumns = combinedStatements.filter((it) => it.type === 'alter_table_rename_column');
|
|
443
|
-
|
|
444
|
-
const rest = combinedStatements.filter((it) => it.type !== 'rename_table' && it.type !== 'alter_table_rename_column');
|
|
445
|
-
|
|
446
|
-
return [...renamedTables, ...renamedColumns, ...rest];
|
|
447
|
-
};
|
|
448
|
-
|
|
449
|
-
export const prepareSingleStoreRecreateTable = (
|
|
450
|
-
table: SingleStoreSchemaSquashed['tables'][keyof SingleStoreSchemaSquashed['tables']],
|
|
451
|
-
): JsonStatement[] => {
|
|
452
|
-
const { name, columns, uniqueConstraints, indexes, compositePrimaryKeys } = table;
|
|
453
|
-
|
|
454
|
-
const composites: string[] = Object.values(compositePrimaryKeys);
|
|
455
|
-
|
|
456
|
-
const statements: JsonStatement[] = [
|
|
457
|
-
{
|
|
458
|
-
type: 'singlestore_recreate_table',
|
|
459
|
-
tableName: name,
|
|
460
|
-
columns: Object.values(columns),
|
|
461
|
-
compositePKs: composites,
|
|
462
|
-
uniqueConstraints: Object.values(uniqueConstraints),
|
|
463
|
-
},
|
|
464
|
-
];
|
|
465
|
-
|
|
466
|
-
if (Object.keys(indexes).length) {
|
|
467
|
-
statements.push(...prepareCreateIndexesJson(name, '', indexes));
|
|
468
|
-
}
|
|
469
|
-
return statements;
|
|
470
|
-
};
|
|
471
|
-
|
|
472
|
-
export const singleStoreCombineStatements = (
|
|
473
|
-
statements: JsonStatement[],
|
|
474
|
-
json2: SingleStoreSchemaSquashed,
|
|
475
|
-
) => {
|
|
476
|
-
const newStatements: Record<string, JsonStatement[]> = {};
|
|
477
|
-
|
|
478
|
-
for (const statement of statements) {
|
|
479
|
-
if (
|
|
480
|
-
statement.type === 'alter_table_alter_column_set_type'
|
|
481
|
-
|| statement.type === 'alter_table_alter_column_set_notnull'
|
|
482
|
-
|| statement.type === 'alter_table_alter_column_drop_notnull'
|
|
483
|
-
|| statement.type === 'alter_table_alter_column_drop_autoincrement'
|
|
484
|
-
|| statement.type === 'alter_table_alter_column_set_autoincrement'
|
|
485
|
-
|| statement.type === 'alter_table_alter_column_drop_pk'
|
|
486
|
-
|| statement.type === 'alter_table_alter_column_set_pk'
|
|
487
|
-
|| statement.type === 'create_composite_pk'
|
|
488
|
-
|| statement.type === 'alter_composite_pk'
|
|
489
|
-
|| statement.type === 'delete_composite_pk'
|
|
490
|
-
) {
|
|
491
|
-
const tableName = statement.tableName;
|
|
492
|
-
|
|
493
|
-
const statementsForTable = newStatements[tableName];
|
|
494
|
-
|
|
495
|
-
if (!statementsForTable) {
|
|
496
|
-
newStatements[tableName] = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
497
|
-
continue;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
501
|
-
const wasRename = statementsForTable.some(({ type }) =>
|
|
502
|
-
type === 'rename_table' || type === 'alter_table_rename_column'
|
|
503
|
-
);
|
|
504
|
-
const preparedStatements = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
505
|
-
|
|
506
|
-
if (wasRename) {
|
|
507
|
-
newStatements[tableName].push(...preparedStatements);
|
|
508
|
-
} else {
|
|
509
|
-
newStatements[tableName] = preparedStatements;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
continue;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
continue;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
if (
|
|
519
|
-
(statement.type === 'alter_table_alter_column_drop_default'
|
|
520
|
-
|| statement.type === 'alter_table_alter_column_set_default') && statement.columnNotNull
|
|
521
|
-
) {
|
|
522
|
-
const tableName = statement.tableName;
|
|
523
|
-
|
|
524
|
-
const statementsForTable = newStatements[tableName];
|
|
525
|
-
|
|
526
|
-
if (!statementsForTable) {
|
|
527
|
-
newStatements[tableName] = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
528
|
-
continue;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
532
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
533
|
-
const preparedStatements = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
534
|
-
|
|
535
|
-
if (wasRename) {
|
|
536
|
-
newStatements[tableName].push(...preparedStatements);
|
|
537
|
-
} else {
|
|
538
|
-
newStatements[tableName] = preparedStatements;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
continue;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
continue;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
if (statement.type === 'alter_table_add_column' && statement.column.primaryKey) {
|
|
548
|
-
const tableName = statement.tableName;
|
|
549
|
-
|
|
550
|
-
const statementsForTable = newStatements[tableName];
|
|
551
|
-
|
|
552
|
-
if (!statementsForTable) {
|
|
553
|
-
newStatements[tableName] = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
554
|
-
continue;
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
558
|
-
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
559
|
-
const preparedStatements = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
560
|
-
|
|
561
|
-
if (wasRename) {
|
|
562
|
-
newStatements[tableName].push(...preparedStatements);
|
|
563
|
-
} else {
|
|
564
|
-
newStatements[tableName] = preparedStatements;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
continue;
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
const tableName = statement.type === 'rename_table'
|
|
574
|
-
? statement.tableNameTo
|
|
575
|
-
: (statement as { tableName: string }).tableName;
|
|
576
|
-
|
|
577
|
-
const statementsForTable = newStatements[tableName];
|
|
578
|
-
|
|
579
|
-
if (!statementsForTable) {
|
|
580
|
-
newStatements[tableName] = [statement];
|
|
581
|
-
continue;
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
if (!statementsForTable.some(({ type }) => type === 'singlestore_recreate_table')) {
|
|
585
|
-
newStatements[tableName].push(statement);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
const combinedStatements = Object.values(newStatements).flat();
|
|
590
|
-
|
|
591
|
-
const renamedTables = combinedStatements.filter((it) => it.type === 'rename_table');
|
|
592
|
-
const renamedColumns = combinedStatements.filter((it) => it.type === 'alter_table_rename_column');
|
|
593
|
-
|
|
594
|
-
const rest = combinedStatements.filter((it) => it.type !== 'rename_table' && it.type !== 'alter_table_rename_column');
|
|
595
|
-
|
|
596
|
-
return [...renamedTables, ...renamedColumns, ...rest];
|
|
597
|
-
};
|
package/src/utils/certs.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import envPaths from 'env-paths';
|
|
2
|
-
import { mkdirSync } from 'fs';
|
|
3
|
-
import { access, readFile } from 'fs/promises';
|
|
4
|
-
import { exec, ExecOptions } from 'node:child_process';
|
|
5
|
-
import { join } from 'path';
|
|
6
|
-
|
|
7
|
-
export function runCommand(command: string, options: ExecOptions = {}) {
|
|
8
|
-
return new Promise<{ exitCode: number }>((resolve) => {
|
|
9
|
-
exec(command, options, (error) => {
|
|
10
|
-
return resolve({ exitCode: error?.code ?? 0 });
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const certs = async () => {
|
|
16
|
-
const res = await runCommand('mkcert --help');
|
|
17
|
-
|
|
18
|
-
if (res.exitCode === 0) {
|
|
19
|
-
const p = envPaths('drizzle-studio', {
|
|
20
|
-
suffix: '',
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// create ~/.local/share/drizzle-studio
|
|
24
|
-
mkdirSync(p.data, { recursive: true });
|
|
25
|
-
|
|
26
|
-
// ~/.local/share/drizzle-studio
|
|
27
|
-
const keyPath = join(p.data, 'localhost-key.pem');
|
|
28
|
-
const certPath = join(p.data, 'localhost.pem');
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
// check if the files exist
|
|
32
|
-
await Promise.all([access(keyPath), access(certPath)]);
|
|
33
|
-
} catch (e) {
|
|
34
|
-
// if not create them
|
|
35
|
-
await runCommand(`mkcert localhost`, { cwd: p.data });
|
|
36
|
-
}
|
|
37
|
-
const [key, cert] = await Promise.all([
|
|
38
|
-
readFile(keyPath, { encoding: 'utf-8' }),
|
|
39
|
-
readFile(certPath, { encoding: 'utf-8' }),
|
|
40
|
-
]);
|
|
41
|
-
return key && cert ? { key, cert } : null;
|
|
42
|
-
}
|
|
43
|
-
return null;
|
|
44
|
-
};
|